CTBCAFDb
This class is used to connect to a MySql database.
The CAF library contains DAOs (Database Access Objects) for each table found in the Toolpack configuration database. These DAOs include a class to retrieve the table rows, and a class that represents the content of one database row.
To initialize the DAOs, it's mandatory that the application uses the class CTBCAFDb to access the MySql database.
For more information on using the DAOs, please refer to the following page: Accessing_Toolpack_DB
Contents |
Initializing CTBCAFDb
The constructor of CTBCAFDb (as well as function SetConnectString) receive a "connection string" as an argument. This connection string is of the form:
ODBC:SERVER=10.3.2.240/10.3.6.240;PORT=3306;UID=root;PWD=root;DATABASE=toolpack_0
The application should use the connection string provided by Toolpack OAM system through class CTBCAFServiceCmMgmtClient (see CTBCAFServiceCmMgmtClient for more information).
Connecting to the database
To connect to the database using the previously provided connection string, use the Open function.
Querying the database
To query the database:
- Create a new query using function CreateQuery
- Use the query object's Exec function to start the query
- Get query result using functions NextRow, ColCount, ColName, and ColValue, etc.
- Delete the query object
Warning: Opening the database and executing queries are synchronous operations, and will block if the database is not available. It is thus recommended to call that function using a background thread in the application that will not prevent the application from properly running while the database is attempted to be accessed. Among other things, the application shall continue to respond to Toolpack OAM Heart-beat (see CTBCAFServiceAlmMgmt) even while waiting for database operation to complete, to avoid the application from being killed during that time.
Example code
Here is some example code that show how to perform a simple query.
Pre-requisites for the code below:
- The application must have initialized an object of class CTBCAFServiceCmMgmtClient
- The application must have received the callback OnReloadConfig inherited from ITBCAFServiceCmMgmtClient
- The application must have connected to the database:
- using an object of type CTBCAFDb (mpConfigDb in the example code below)
- connected to the DB using the connection string received by OnReloadConfig
- The application must use the "configuration id" as received by OnReloadConfig (mun32ConfigurationId in the example code below)
TBX_RESULT MyApplication::ExampleDbQuery() { TBX_RESULT Result; CTBCAFString strQuery; PITBCAFDbQuery pQuery = NULL; /*--------------------------------------------------------------------------------------------------------------------------- | Code section *--------------------------------------------------------------------------------------------------------------------------*/ CAFCODE( MyApplication::ExampleDbQuery ) { // Build the SQL query string that we want to perform strQuery.Format ( "select * from virtual_adapters where configuration_id = %u", mun32ConfigurationId ); pQuery = mpConfigDb->CreateQuery( strQuery ); TBCAF_EXIT_ON_NULL( pQuery, TBX_RESULT_FAIL, "Failed to query query object" ); // Execute the query Result = pQuery->Exec( mpConfigDb ); TBCAF_EXIT_ON_ERROR( Result, "Failed to execute query" ); // Collect the results for each returned row while( pQuery->NextRow() ) { // Get and print value of column "name" CTBCAFVariant Value = pQuery->ColValue( "name" ); LogTrace( TBCAF_TRACE_LEVEL_ALWAYS, FMAGEN "Found adapter %s\n", Value.ToString().c_str() ); } TBX_EXIT_SUCCESS( TBX_RESULT_OK ); } /*--------------------------------------------------------------------------------------------------------------------------- | Exception handling section *--------------------------------------------------------------------------------------------------------------------------*/ CAF_EXCEPTION_HANDLING /*--------------------------------------------------------------------------------------------------------------------------- | Error handling section *--------------------------------------------------------------------------------------------------------------------------*/ CAF_ERROR_HANDLING( CAF_VERBOSE ) { } /*--------------------------------------------------------------------------------------------------------------------------- | Cleanup section *--------------------------------------------------------------------------------------------------------------------------*/ CAF_CLEANUP { delete pQuery; pQuery = NULL; } RETURN; }