WORM 0.2
A C++ DAL/ORM code generation framework
Public Member Functions

WSql::WMysqlDriver Class Reference

A driver for MySQL databases. More...

#include <wmysqldriver.h>

Inheritance diagram for WSql::WMysqlDriver:

List of all members.

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.
WSqlResultresult (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 ()

Detailed Description

A driver for MySQL databases.

This class provides a driver for MySQL databases. It supports:

Todo:
uncached results, transactions, prepared statements .. and fix string escaping...

Definition at line 31 of file wmysqldriver.h.


Constructor & Destructor Documentation

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.


Member Function Documentation

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.

Note:
If this is called after a previous call and the results were not cached any results in the previous WSqlResult will be INVALID.
Returns:
bool - true on success

Todo:
set connection options here, esp. utf* and friends

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().

Returns:
bool on success

Todo:
RESOLVE - this also escapes LIKE quotes .. need a way to handle this, ie. a general escape/sanitizer method somewhere ..

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).

Note:
Only use this after a call to query()! Do not use twice in a row as it will delete the previous result and return a newly created object. Example:
 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.
Parameters:
booliscached - if true (the default) fetches entire result set at once.
Returns:
WSqlResult* contains the result set

Todo:

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.

Note:
It is recommended that this not be used directly but rather from a WSqlDatabase object. Note also the warning below - unless you really only need metadata for a single table it is better to initialize all the tables at once with WSqlDatabase::initMetaData() and avoid the possibility of invalidating a result set.

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;
    }
Warning:
If the table metadata has not been initialized yet this method will invalidate any previous WSqlResult pointer returned - in this case nesting calls to this method inside of a loop iterating over WSqlResults WILL NOT WORK. Obtain the WSqlTable first and then query() a query and fetch the result set using getResult() or use WSqlDatabase::initMetaData() to initialize the metadata for all tables at once.
Parameters:
stringthe name of the table to use
Returns:
WSqlTable an object containing metadata
See also:
WSqlTable WSqlColumn

Todo:
change to use strings, defined? and test clm.setColumnName(record.field("Field").data<std::string>()); initColumnType(clm, record.field("Type").data<std::string>()); bool nullable = ! (record.field("Null").data<std::string>().compare("NO") == 0); bool primarykey = (record.field("Key").data<std::string>().compare("PRI") == 0); clm.setDefaultValue( record.field("Default").data<std::string>()); bool isauto = (record.field("Extra").data<std::string>().find("auto_increment") != std::string::npos);

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.

Note:
WARNING: This method will invalidate previous WSqlResults returned - nesting calls to this method inside of a loop iterating over WSqlResults will not work. Obtain the list first and aftwards execute a query and fetch the result set using getResult().
Return values:
vector<string>a list of table names

Implements WSql::WSqlDriver.

Definition at line 275 of file wmysqldriver.cpp.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Defines