Isis 3 Programmer 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... | |
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... | |
Private Member Functions | |
void | tossQueryError (const std::string &message, const char *f, int l) const |
Issues an IException from various sources of error states in this class. More... | |
Private Attributes | |
bool | _throwIfFailed |
User can select action on query results. 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.
2007-04-19 Kris Becker - Reordered return of getQuery() result to first try lastQuery(), then executedQuery().
2007-06-05 Brendan George - Modified to work with QString/StringTools merge
2008-10-30 Steven Lambright - tossQueryError now accepts a const char* for a filename, issue pointed out by "novus0x2a" (Support Board Member)
Definition at line 152 of file SqlQuery.h.
Isis::SqlQuery::SqlQuery | ( | ) |
Default constructor.
This constructor prepares a query using the default database as established through the Database class. It will also ensure that on any error, an exception is thrown, in line with the Isis way of doing things.
Definition at line 43 of file SqlQuery.cpp.
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 |
Definition at line 55 of file SqlQuery.cpp.
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. |
Definition at line 88 of file SqlQuery.cpp.
References exec().
Isis::SqlQuery::SqlQuery | ( | const SqlQuery & | other | ) |
Constructor using an existing SqlQuery object.
This constructor creates a new SqlQuery object from an existing one, inheriting all its characteristics - including a Database if one is associated to the object.
other | A SqlQuery object used to create this one from |
Definition at line 103 of file SqlQuery.cpp.
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 |
Definition at line 125 of file SqlQuery.cpp.
References _FILEINFO_, isThrowing(), Isis::IString::ToQt(), and tossQueryError().
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 |
Definition at line 194 of file SqlQuery.cpp.
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 |
Definition at line 179 of file SqlQuery.cpp.
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.
Definition at line 211 of file SqlQuery.cpp.
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.
Definition at line 228 of file SqlQuery.cpp.
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.
Definition at line 146 of file SqlQuery.cpp.
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.
Definition at line 267 of file SqlQuery.cpp.
Referenced by fieldTypeList().
|
inline |
Report error status when executing queries.
Definition at line 167 of file SqlQuery.h.
References _throwIfFailed.
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.
Definition at line 163 of file SqlQuery.cpp.
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.
Definition at line 250 of file SqlQuery.cpp.
|
inline |
Turns throwing of iExceptions off on errors.
Definition at line 180 of file SqlQuery.h.
References _throwIfFailed.
|
inline |
Sets throwing of exceptions on errors to true.
Definition at line 174 of file SqlQuery.h.
References _throwIfFailed.
|
private |
Issues an IException from various sources of error states in this class.
This method is provided to issue a consistant error message format from this class. The user of this class can decide at runtime to issue Isis::IException exceptions when error condition or query failures are detected or handle the errors themselves. All exceptions go through this method for deployment to simplify the process.
Note callers of this method within this class provide the context of the error, such as name and line of code, to preserve accuracy of the error context.
message | Context of the error to report |
f | Routine name issuing the error |
l | Line number of the offending error |
Definition at line 291 of file SqlQuery.cpp.
References Isis::IString::ToStd(), and Isis::IException::User.
Referenced by exec().
|
private |
User can select action on query results.
Definition at line 200 of file SqlQuery.h.
Referenced by isThrowing(), setNoThrowOnFailure(), and setThrowOnFailure().