> For the complete documentation index, see [llms.txt](https://prosbcdocs.telcobridges.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://prosbcdocs.telcobridges.com/telecom-references/apis-and-integration/ctbcafdb.md).

# 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](https://github.com/telcobridges-main/tmedia-wiki/tree/main/reference/api/accessing-toolpack-db/README.md)

### 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](https://github.com/telcobridges-main/tmedia-wiki/tree/main/reference/api/ctbcafservicecmmgmtclient/README.md) 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](https://github.com/telcobridges-main/tmedia-wiki/tree/main/tmedia/operations/ctbcafservicealmmgmt/README.md)) 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; }


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://prosbcdocs.telcobridges.com/telecom-references/apis-and-integration/ctbcafdb.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
