WORM 0.2
A C++ DAL/ORM code generation framework
|
A driver for MySQL databases. More...
#include <wmysqldriver.h>
Public Member Functions | |
WMysqlDriver (WSqlDatabase *db) | |
Constructs a WMysqlDriver with the database db. | |
~WMysqlDriver () | |
bool | open () |
Opens the connection to the database. | |
void | close () |
Closes the connection and deallocates MYSQL objects. | |
bool | query (std::string sql) |
Sends the SQL in sqlstring to the server for execution. | |
WSqlResult * | result (bool iscached=true) |
Return the result from the most recent query. | |
std::vector< std::string > | tableNames () |
Returns a list of the table names in the current database. | |
WSqlTable | tableMetaData (const std::string &tableName) |
Fetch metadata for table tableName. | |
bool | isOpen () |
A driver for MySQL databases.
This class provides a driver for MySQL databases. It supports:
Definition at line 31 of file wmysqldriver.h.
WSql::WMysqlDriver::WMysqlDriver | ( | WSqlDatabase * | db | ) |
Constructs a WMysqlDriver with the database db.
This constructor initializes the mysql libraries and prepares for establishing a connection.
Definition at line 45 of file wmysqldriver.cpp.
WSql::WMysqlDriver::~WMysqlDriver | ( | ) |
Definition at line 61 of file wmysqldriver.cpp.
void WSql::WMysqlDriver::close | ( | ) | [virtual] |
Closes the connection and deallocates MYSQL objects.
This method closes the connection and frees the MYSQL (_mysql) memory. WARNING: Uncached result sets in a WSqlResult will be INVALID after calling this! It is recommended that one leave this to the destructor - it is called automatically when the WMysqlDriver object is destroyed.
Implements WSql::WSqlDriver.
Definition at line 96 of file wmysqldriver.cpp.
bool WSql::WMysqlDriver::isOpen | ( | ) |
Definition at line 410 of file wmysqldriver.cpp.
bool WSql::WMysqlDriver::open | ( | ) | [virtual] |
Opens the connection to the database.
This method opens the connection to the database - it must be called before any queries are sent. Note that it may be called more than once - calling this if the connection is still open from a previous call will close and reopen the connection, equivilant to a reset() function. This returns true if the connection was successful otherwise returns false, sets isValid() and hasError() to return false and sets an error if possible.
Implements WSql::WSqlDriver.
Definition at line 119 of file wmysqldriver.cpp.
bool WSql::WMysqlDriver::query | ( | std::string | sqlstring | ) | [virtual] |
Sends the SQL in sqlstring to the server for execution.
This method sends the SQL given in sqlstring to the server to be executed. If the query was successful it returns true, if not it sets an error and returns false. If the query should return results (eg. a SELECT) they will be available from getResult().
Implements WSql::WSqlDriver.
Definition at line 159 of file wmysqldriver.cpp.
WSqlResult * WSql::WMysqlDriver::result | ( | bool | iscached = true | ) | [virtual] |
Return the result from the most recent query.
This method returns a pointer to a WSqlResult object containing the most recent result set from a query - it may be empty if no results were returned. You must not delete this pointer yourself - it is owned by the driver.
The parameter iscached may be set to true to indicate a non-cached result set that will be fetched row by row - the default is true and results are cached. (currently uncached results are not yet supported).
WSqlDatabase db; if (!db.open()) dosomeerror(); if (!db.query(std::string("select foo from bar"))) dosomeerror(); WSqlResult *result = db.getResult(); //WSqlResult *result2 = db.getResult(); <- wrong //...iterate over results ..._then repeat: if (!db.query(std::string("select baz from bar"))) dosomeerror(); WSqlResult *result = db.getResult(); ..etc.
bool | iscached - if true (the default) fetches entire result set at once. |
Reimplemented from WSql::WSqlDriver.
Definition at line 215 of file wmysqldriver.cpp.
WSqlTable WSql::WMysqlDriver::tableMetaData | ( | const std::string & | tableName | ) | [virtual] |
Fetch metadata for table tableName.
This method returns a WSqlTable object populated with WSqlColumns that contain metadata for the columns in the given table tableName.
Example:
meta_table = db.tableMetaData(*it); numflds = meta_table.count(); for (int i=0; i < numflds; ++i) { WSql::WSqlColumn column = meta_table.column(i); std::cout << "Column " << i << " = " << column.columnName() << std::endl; std::cout << " * Data type: " << WSql::WSqlDataType::toString(column.dataType()) << std::endl; std::cout << " * Max Length: " << column.maxLength() << std::endl; std::cout << " * Unsigned: " << (column.isUnsigned() ? "true" : "false") << std::endl; std::cout << " * Can be null: " << (column.canBeNull() ? "true" : "false") << std::endl; std::cout << " * Primary key: " << (column.isPrimaryKey() ? "true" : "false") << std::endl; std::cout << " * Autoincrement: " << (column.isAutoIncremented()?"true" : "false") << std::endl; std::cout << " * default value: " << column.defaultValue<std::string>() << std::endl; }
string | the name of the table to use |
Implements WSql::WSqlDriver.
Definition at line 335 of file wmysqldriver.cpp.
std::vector< std::string > WSql::WMysqlDriver::tableNames | ( | ) | [virtual] |
Returns a list of the table names in the current database.
This method will return a vector<string> containing the names of the tables in the current database - the database name must be set first.
vector<string> | a list of table names |
Implements WSql::WSqlDriver.
Definition at line 275 of file wmysqldriver.cpp.