Isis 3 Developer Reference
|
Construct and execute a query on a database and manage result. More...
#include <SqlQuery.h>
Public Member Functions | |
SqlQuery () | |
Default constructor. More... | |
SqlQuery (Database &db) | |
Construtor using a specified database. More... | |
SqlQuery (const std::string &query, Database db=Database(Database::Connect)) | |
Construct a query object and execute the provided query string. More... | |
SqlQuery (const SqlQuery &other) | |
Constructor using an existing SqlQuery object. More... | |
virtual | ~SqlQuery () |
bool | isThrowing () const |
Report error status when executing queries. More... | |
void | setThrowOnFailure () |
Sets throwing of exceptions on errors to true. More... | |
void | setNoThrowOnFailure () |
Turns throwing of iExceptions off on errors. More... | |
bool | exec (const std::string &query) |
Execute an SQL query provided in the query string. More... | |
bool | exec () |
std::string | getQuery () const |
Returns the executed query string. More... | |
int | nFields () const |
Returns the number of fields (columns) from query. More... | |
std::string | fieldName (int index) const |
Returns the column name of the resulting query at the given index. More... | |
int | fieldIndex (const std::string &name) const |
Returns index of column for given name. More... | |
std::vector< std::string > | fieldNameList () const |
Returns the names of all fields in the resulting query. More... | |
std::vector< std::string > | fieldTypeList () const |
Returns the types of each field/column in a resutling query. More... | |
int | nRows () const |
Returns the count of rows resulting from the query. More... | |
SqlRecord | getRecord () const |
Returns a SqlRecord for the current query row. More... | |
Construct and execute a query on a database and manage result.
This class is derived from the Qt QSqlQuery class. It is designed to be used in conjunction with the functionality of that class.
The major features are to to make it easier to specify what happens when errors occurs (setThrowOnFailure()) and return and accept query strings and results using the standard C++ library and classes (vector, string). It exists mainly as a convenience class interface to the Qt QSqlQuery class, providing standard C++ elements as opposed to Qt elements.
NOTE all constructors initially set exception throwing as the default.
Some examples follow:
This example uses some of Qt's features, which you can use at will throughout this and other classes. This one uses the named/positional binding features"
Thats all well and good but Qt's binding is restricted to using it in the INSERT/VALUES constructs. They have no support for binding used outside this SQL operation. Below is an example with a more general approach provided with this API, although a bit less robust.
Isis::SqlQuery::SqlQuery | ( | ) |
Isis::SqlQuery::SqlQuery | ( | Database & | db | ) |
Construtor using a specified database.
This constructor should be used for preparing for queries using a specified database. This would be used when using a database other than the default.
Turns exception throwing on.
db | Database to use for subsequent database queries |
Isis::SqlQuery::SqlQuery | ( | const std::string & | query, |
Database | db = Database(Database::Connect) |
||
) |
Construct a query object and execute the provided query string.
This constructor will take a query string and an optional database specification and execute the query all after the initial construction. If the caller does not provide a Database, the default one is used and a connection is automatically attempted.
It can be used to execute an initial query and is perhaps the most powerful example of the Isis database design. It could be used as the starting point for any database access and an initial query in one line of code. For example, below is a line of code that executes a line of code showing all the tables in a PostgreSQL database using the default database:
This will use the Database() default constructor since one is not explicitly provide. That constructor attempts a connection using the default database access profile as read from your IsisPreferences file. Its default behaviour is to initiate the connection to the database. If that suceeds, the query is issued. The resulting pgtables SqlQuery object can now be contiually used for other queries until it goes out of scope.
query | IString containing a valid SQL query to execute |
db | An optional database object to issue the query to. This database now becomes the one used in all subsequent queries using this SqlQuery object. |
References exec().
Isis::SqlQuery::SqlQuery | ( | const SqlQuery & | other | ) |
|
inlinevirtual |
bool Isis::SqlQuery::exec | ( | const std::string & | query | ) |
Execute an SQL query provided in the query string.
This method executes the given query in the string. This method assumes this query object has a valid and open Database connection associated with it. It will also check the result for valid completion and toss an iException if the caller has established this course of action when it fails.
Results are ready for processing on completion.
query | An SQL query string to issue to a database |
References _FILEINFO_, isThrowing(), and Isis::IString::ToQt().
|
inline |
Referenced by SqlQuery().
int Isis::SqlQuery::fieldIndex | ( | const std::string & | name | ) | const |
Returns index of column for given name.
This method returns the index of the given column name.
NOTE this is not valid until after the first next() is issued.
name | Name of column to get index for |
References Isis::IString::ToQt().
std::string Isis::SqlQuery::fieldName | ( | int | index | ) | const |
Returns the column name of the resulting query at the given index.
This method returns the name of the column heading as a result of the query at the given index.
NOTE this is not valid until after the first next() is issued.
index | Zero-based starting index of column name ot retreive |
References Isis::IString::ToStd().
std::vector< std::string > Isis::SqlQuery::fieldNameList | ( | ) | const |
Returns the names of all fields in the resulting query.
After a query has been issued, this method will return the names of all fields/columns in the resulting query.
NOTE this is not valid until after the first next() is issued.
References Isis::IString::ToStd().
std::vector< std::string > Isis::SqlQuery::fieldTypeList | ( | ) | const |
Returns the types of each field/column in a resutling query.
After a query has been issued, this method will return the types of all fields/columns. These types are defined by the SqlRecord::getType() method.
References getRecord(), and Isis::SqlRecord::getType().
std::string Isis::SqlQuery::getQuery | ( | ) | const |
Returns the executed query string.
This method returns the last executed query string as it was issued to the database. Note that some database systems do not support this option directly. This routine will attempt to return the last executed query first from the Qt QSqlQuery class. If this is empty/undefined, then the last current query will be returned.
References Isis::IString::ToStd().
SqlRecord Isis::SqlQuery::getRecord | ( | ) | const |
Returns a SqlRecord for the current query row.
While traversing through the resulting query row set, this method returns a lower level interface to individual rows. The returned object is provided by the SqlRecord class.
NOTE this is not valid until after the first next() is issued.
Referenced by fieldTypeList().
|
inline |
Report error status when executing queries.
Referenced by exec().
int Isis::SqlQuery::nFields | ( | ) | const |
Returns the number of fields (columns) from query.
The method returns the number of fields or columns returned by the last issued query string. Note that if the query has not been issued, it will return 0 or an undefined value (-1?).
NOTE this is not valid until after the first next() is issued.
int Isis::SqlQuery::nRows | ( | ) | const |
Returns the count of rows resulting from the query.
This returns the number of rows returned/accessable as a result of the issued query. Its value is governed by the Qt QsqlQuery::size() method. Namely, a -1 can be returned under many different conditions of the queray and the database (driver) support. Check the documentation for details.
|
inline |
Turns throwing of iExceptions off on errors.
|
inline |
Sets throwing of exceptions on errors to true.