fatalmind::SQL::SQLStatement Class Reference

#include <SQLStatement.hh>

Inherited by fatalmind::oracle::oracleStatement.

List of all members.


Detailed Description

A representation of a SQL string.

The SQLStatement class has to be used whenever a SQL statement (string) has to be passed to a command.

Implicit conversion from const char* make expressions like this valid:

 SQLSelect sel("select * from table"); 
In fact, a SQLStatement temporary instance is created and passed to SQLSelect's constructor.

SQLStatement is driver independent, the same instance can be passed to a oracle or mysql command. The class implements some additional features needed by more then one driver to avoid code duplication. In fact it's current main task is to parse the SQL for placeholders (?) and make meta data available to the drivers. Please note the following additional meta-data which can be provided for each placeholder:

 ?{<name>}
 ?{:<type>}
 ?{<name>:<type>}

That means, a question mark can be followed by curly brackets including a name for the placeholder ([A-Za-z0-9_]) and/or type information (CHAR, INT, FLOAT).

Please not that the actual parsing of the SQL string is done upon construction. It's therefore a good practice to have static SQLStatement instances for the frequently executed statements to avoid parsing overhead.

This information may or may not be used by the actual command, please refer the the documentation of the command in question.

As of v0.99.27 the information is not used by any command. Future plans are to allow bind-parameter re-ordering via numbered placeholders (e.g. ?{3}) and named binds. As of v0.99.27, it's not clear if the list of types will remain as it is now, it might also change.

example statements:

 select * from a where a = ? and b = ?
 select * from a where a = ?{a} and b = ?{b}
 select * from a where a = ?{a:CHAR} and b = ?{b:INT}
The first is the classical standard for with a plain question mark as placeholder. The second adds names to each placeholder while the third also supplies type information to each placeholder.

Since:
0.99.27

Public Member Functions

 SQLStatement ()
 Default constructor, same as SQLStatement("").
 SQLStatement (const std::string &sql)
 Constructs a new instance from a c++ string.
 SQLStatement (const char *sql)
 Constructs a new instance from a c string.
 SQLStatement (const SQLStatement &)
unsigned int getParamCount () const
 Returns the number of placeholders in the sql string.
std::string getNameFor (const int pos) const
 Returns the name of a placeholder, or the empty string if no name was assigned.
int getTypeFor (const int pos) const
 Returns the type of a placeholder, or the UNKNOWN constant if no type was assigned.
const std::string & getSQL () const
 Returns a reference to the original SQL supplied on construction.
const std::string & getSQLFragment (const int pos) const
 Returns the n-th fragment of the original SQL when split at each placeholder.
bool operator== (const SQLStatement &rho)
bool operator!= (const SQLStatement &rho)

Friends

class SQLParser

Classes

struct  Parameter
 Holds all available information about a placeholder (?) within an SQL string. More...

Constructor & Destructor Documentation

fatalmind::SQL::SQLStatement::SQLStatement ( const std::string &  sql  ) 

Constructs a new instance from a c++ string.

Exceptions:
SQLException in case parsing isn't successful. e.g.:
 SQLStatement(std::string("select * from x where a = ?{:INVALID}")); // will throw SQLException
The problem is that ?{:INVALID} does not name a defined type (INVALID instead of INT, CHAR or FLOAT). Type names are always uppercase, e.g. ?{:int} would also cause an exception.

fatalmind::SQL::SQLStatement::SQLStatement ( const char *  sql  ) 

Constructs a new instance from a c string.

This constructor's main purpose is to not break pre 0.99.27 code and allow easy in-line SQL's.

However, you should prefer to construct the SQLStatement explicitly and keep it to void parsing overhead.

Otherwise the same as

See also:
SQLStatement::SQLStatement(const std::string&)


Member Function Documentation

unsigned int fatalmind::SQL::SQLStatement::getParamCount (  )  const

Returns the number of placeholders in the sql string.

e.g.

 SQLStatement("select * from dual").getParamCount(); // returns 0
 SQLStatement("select ? from dual").getParamCount(); // returns 1
 SQLStatement("select ? from x where a = ?{a}").getParamCount(); // returns 2

std::string fatalmind::SQL::SQLStatement::getNameFor ( const int  pos  )  const

Returns the name of a placeholder, or the empty string if no name was assigned.

e.g.

 SQLStatement("select * from dual").getNameFor(0); // throws std::out_of_range
 SQLStatement("select ? from dual").getNameFor(0); // returns an empty string ("")
 SQLStatement("select ? from x where a = ?{a}").getNameFor(1); // returns "a"

Exceptions:
std::out_of_range if an invalid position is requested.

int fatalmind::SQL::SQLStatement::getTypeFor ( const int  pos  )  const

Returns the type of a placeholder, or the UNKNOWN constant if no type was assigned.

e.g.

 SQLStatement("select * from dual").getTypeFor(0); // throws std::out_of_range
 SQLStatement("select ? from x where a = ?{a:INT}").getNameFor(0); // returns SQLStatement::Parameter::UNKNOWN
 SQLStatement("select ? from x where a = ?{a:INT}").getNameFor(1); // returns SQLStatement::Parameter::INT

Exceptions:
std::out_of_range if an invalid position is requested.

const std::string & fatalmind::SQL::SQLStatement::getSQL (  )  const

Returns a reference to the original SQL supplied on construction.

Returns:
a reference to the member of the SQLStatement instance.

const std::string & fatalmind::SQL::SQLStatement::getSQLFragment ( const int  pos  )  const

Returns the n-th fragment of the original SQL when split at each placeholder.

e.g.

 select * from x where a = ? and b = ?{b:INT} order by c
 \___________ 0 __________/ \__ 1 __/        \___ 2 ___/
Please note that the placeholders themselves are not included. In case the last character in the SQL is a placeholder, the last fragment will be returned as empty string.
Exceptions:
std::out_of_range if pos > (getParamCount() + 1)
Returns:
a reference to the member of the SQLStatement instance.


The documentation for this class was generated from the following files:
Generated on Mon Nov 9 16:21:24 2009 for ResourcePool by  doxygen 1.5.3