Database Broker Interface Definition

Database broker API Guide

 

Database Broker Interface Definition

A Database Broker API Guide

Introduction

The Database Broker is a C++ class built on top of ODBC Layer. It encapsulates much of the complexity of the ODBC API and provides a uniform interface to application developers.

The Database broker class is a higher abstraction of a group of classes developed via the raw ODBC APIs. It is capable of handling dynamic query, batch SQL commands, and database caching, and runtime binding.

In the application developments using ODBC, developers usually define C/C++ variables that match what’s in the database table. The approach assumes the database table is defined before the corresponding database classes. There are several problems with the approach:

      1. New classes have to be designed if new tables are introduced into the system. There is a lot of redundant development of database classes and lack of higher abstraction. The system grows as the number of database table increase.
      2. Any changes in the database schema (i.e., change the data type or size of a column) leads to modifications to the database classes. It usually not only involves recompiling and re-linking, but also lengthy testing. The system becomes virtually non-maintainable. It is very costly to make any changes in the database schema.

The Database Broker is designed to solve such problems. It can process any ANSI SQL supported by an ODBC driver at runtime, and perform runtime binding for result sets. Users of the database broker do not need to know ODBC at all.

Key Features

    • Dynamic Binding: Columns in database table are bound at runtime.  The class maintains the allocated buffer internally, so there is no allocation and deallocation at the client.
    • Build-in Database Connection Manager: The connection manager can caches database connections and reuse them when appropriate.
    • Batch SQL Processing: Multiple SQL statements can be passed to the ODBC Broker and they are processed as a single transaction.
    • Dynamic SQL Processor: The Broker can process any ANSI SQL commands at runtime as long as the underlying driver supports.
    • Batch Fetching: The ODBC Broker fetches multiple rows at a time.
    • High Performance: The ODBC Broker uses only ODBC API,  It is the most efficient library in the market. Web servers that use the library have undergone millions of hits per day.
    • Easy of Use: The library takes care of many complicated details in ODBC so that user can just concentrate on build the business logic. User can build a dynamic SQL processor in just a few minutes.

 

The Definition of Database Broker

In order to use the Database Broker, we first needs to look at the class definition. The detailed description of each function will be given later.

class BROKERCLASS Broker

{

public:

enum EnumType {ET_TABLE, ET_COLUMN, ET_DATASOURCE,

ET_INDEX, ET_TYPEINFO};

enum BrokerType {DATA_BROKER, RPC_BROKER, HTTP_BROKER,

OLE_BROKER, MULTIPLEX_BROKER};

enum InterpretType{IT_C, IT_SQL};

public:

Broker(const char* szURL=NULL);

Broker(const char* szDSN, const char* szUser,

const char* szPasswd);

virtual ~Broker();

public: // Database connection

virtual BOOL Connect(const char* szDSN, const char* szUser=NULL,

const char* szPasswd=NULL, BOOL bCache=FALSE)=0;

virtual void Disconnect()=0;

public: // direct SQL

virtual BOOL ExecuteSQL(const char* szSQL=NULL)=0;

public: // interpreter

virtual BOOL Interpret(const char* szCmd, void *pIO,

int nType)=0;

public: // Query operations

virtual int SetParameter(int nIndex, short nInOut,

short fCType, short fSqlType, int nPrecision,

short nScale, const char* value,

int nMaxLen, long * pcbValue)=0;

virtual long Move(long nRow)=0;

virtual long Fetch(long nStartPos= ROWID_CURRENT)=0;

virtual BOOL OpenSQL(const char* szQuery,

unsigned int nBind= -1)=0;

virtual void CloseSQL()=0;

virtual BOOL IsOpen()=0;

public : // record access

virtual char * GetHeader()=0;

virtual char * GetRecord(long nRow)=0;

virtual long GetRecordLength()=0;

virtual long GetRowCount()=0 ;

virtual long GetCursorPos()=0;

virtual char* GetTypeInfo(const int fSqlType)=0;

public: // field access

virtual unsigned int GetFieldCount()=0;

virtual char * GetFieldValue(long nRow, int nCol)=0;

virtual char * GetFieldName(int nCol)=0;

virtual int GetFieldNameLen(int nCol)=0;

virtual long GetFieldLength(int nCol)=0;

virtual long GetFieldSpan(int nCol)=0;

virtual int GetFieldType(int nCol)=0;

virtual int GetFieldScale(int nCol)=0;

virtual int GetFieldPrecision(int nCol)=0;

virtual int GetFieldAttribute(int nCol,

int fDescType)=0;

public: // DML

virtual BOOL Delete(long nRow)=0;

virtual BOOL Update(long nRow, char**ppNames, char** ppValues)=0;

virtual BOOL Insert(char* *ppNames, char** ppValues)=0;

virtual char* Enumerate(EnumType nType, char* szParam)=0;

public:

// transactions

virtual BOOL BeginTransaction()=0;

virtual BOOL EndTransaction(BOOL bCommit)=0;

public: // error handling

virtual char* GetErrorString(BOOL bGet=TRUE)=0;

public: // not dispatched .

virtual BOOL IsFieldNumeric(int nCol)=0;

virtual int MaxFieldNameLen()=0;

virtual int IsFieldUpdatable(int nCol)=0;

virtual int GetFieldPos(const char* szFieldName)=0;

virtual const char* GetSQLStatement()=0;

virtual const char* GetDSN()=0;

public:

virtual BOOL FindObject(const char* szDSN, const char* szTable,

const char* szField=NULL, const char* szUser=NULL,

const char* szPasswd=NULL)=0;

char* GetServerURL(){return m_szURL;}

 

public:

char * m_szURL;

};

The Database Broker is a pure virtual class as can be seen in the definition. Different broker can be implemented without disturbing the user of the class. The BrokerType shows that the Database Broker, the HTTP Broker, the RPC Broker, the OLE Broker, and MULTIPLEX_BROKER are implemented. Each type of broker accesses database through different network protocols but the result should be the same. We focus to the DATA_BROKER in the document.

The interface can be divided into three major categories:

      1. Database Connection Handling: The functions are Connect and Disconnect. These are used to establish database connections to ODBC data source and remove the connection when done.
      2. SQL Processing: This group is responsible for preparing and submitting SQL command to a given data source. The OpenSQL function is used for preparing a SQL before executing or preparing a query. ExecuteSQL can be used to directly executing SQL Commands. Data fetching and cursor movement are handled by the Fetch and Move. The class fetches multiple rows in each operation.
      3. Information Retrieval: This group allows user to retrieve record by record or field by field data after opening a query. Database Schema (detailed definition of fields) can also be determined through function calls in the category.

Using Database Broker

Using Database Broker in Your Application:

The distribution package of the Broker contains four subdirectories:

      1. The BIN directory: It contains all the DLLs that is needed for your application. You should either include the directory in your PATH environment variable or copy the files into a directory that is in the PATH variable.
      2. The INCLUDE directory: It contains two include files that define the Database Broker. You should add the directory into the INCLUDE environment variable of your IDE.
      3. The LIB directory: It contains the library file that needs to be linked into your application. You can move the file into a directory where the linker can find the file.
      4. The SAMPLES directory: It contains some simple examples that demonstrate how the library can be used.

Processing SQL Statements

Using the Database Broker to process a SQL statements other than query is very simple, just pass the SQL statement to the ExecuteSQL function after a successful database connection. The function returns TRUE if the statement was executed successfully. Here is a general procedure for processing such SQL statement.

      1. Create a Database Broker.
      2. Make a database connection.
      3. Call the ExecuteSQL function and pass the SQL statement.
      4. You can use OpenSQL function if there are runtime parameters in the SQL Statement, and then use the SetParameter function to assign values to the parameters. Call the ExecuteSQL to execute the SQL command.

      5. Use CloseSQL to close the statement. The call is optional because the database broker will close the statement automatically when it is deleted or a new SQL statement is open.
      6. Disconnect from the database using the Disconnect function. The database broker closes any open connection automatically when a new connection is made or the broker is destroyed. However, it is always a good idea to close a database connection when it is no longer in use.

Processing Query Statements

The Database Broker automatically allocates required memory space for each record and it’s fields when a query statement is opened. The selected field values are converted to character strings.

      1. Create a Database Broker.
      2. Make a database connection.
      3. Call the OpenSQL function and pass the SQL statement.
      4. You can use OpenSQL function if there are runtime parameters in the SQL Statement, and then use the SetParameter function to assign values to the parameters. Call the ExecuteSQL to execute the SQL command.

      5. Use the Fetch function to get a bunch of records.
      6. Call the GetRecord or GetField function to get the record values. Go back to step 4 until the Fetch function returns
      7. Use CloseSQL to close the statement. The call is optional because the database broker will close the statement automatically when it is deleted or a new SQL statement is open.
      8. Disconnect from the database using the Disconnect function. The database broker closes any open connection automatically when a new connection is made or the broker is destroyed. However, it is always a good idea to close a database connection when it is no longer in use.

The following code demonstrates how to process a SQL dynamically. The SQL statement is passed to the function at run-time. The function prints the returned records on the console screen. For simplicity, error handling is omitted from the sample.

// this sample shows how to process a SQL query.

long Query(const char * szDataSource, const char* szUser, const char * szPassword, const char * szSQL)

{

int nTotalRows=0;

Broker *pBroker =CreateBroker(Broker::DATA_BROKER, NULL);

// make a database connection , without user name and pwd

if(!pBroker->Connect(szDataSource, szUser, szPassword))

{

// we have trouble connecting to the database

printf("Unable to connect to the database %s\n",

szDataSource);

return 0;

}

 

if(pBroker->OpenSQL(szSQL))

{

printf("%s\n", pBroker->GetHeader());

long rows=0;

while((rows=pBroker->Fetch())>0)

{

// we fetched n rows

for(int i=0; i<rows; i++)

{

printf("%s\n", pBroker->GetRecord(i));

}

nTotalRows +=rows;

}

}

else

{

// some thing wrong, get the error string

printf("Error open query, %s\n",

pBroker->GetErrorString());

}

// we are done

DestroyBroker(pBroker);

return nTotalRows;

}

Processing Stored Procedures

Stored procedures differ widely from one DBMS to another. How it is implemented and supported depends very much on the ODBC driver. In this section, we will demonstrate how to process a simple stored procedure and how to pass parameters to stored procedures.

The steps for processing stored procedure is very much like processing a SQL statement, except that stored procedure may require input or output parameters that must be set:

    1. Open the procedure call statement using the OpenSQL function:
      OpenSQL("{call ProcedureName(?, ?)}")
      where ProcedureName is the name of the stored procedure to be called and '?' is the parameter marker of the stored procedure. The number of '?' must match the number of parameters required by the stored procedure.
    2. Bind parameters using the SetParameter function. The SetParameter function is a wrapper of the ODBC SQLBindParameter function. The difference is that the parameter number is zero based. i.e., the parameter number for the first parameter is 0 instead of 1.
    3. Execute the stored procedure using the ExecuteSQL function. The function will return TRUE if the stored procedure is executed successfully.

The following example demonstrates how to pass input and output parameter to stored procedures. Our simple stored procedure has one input parameter and one output parameter. Both are numerical. The procedure does nothing but assign the input parameter to the output parameter. In other words, whatever number we pass to the store procedure as input will be returned as output.

Store procedure for SQL Server:

CREATE PROC assignment @in_param decimal(15, 0), @out_param decimal(15, 0) output AS @out_param = @in_param

Store procedure for ORACLE server.

create procedure assignment(in_param IN NUMBER, out_param OUT NUMBER) AS

BEGIN

out_param := in_param

END;

The following function invokes the stored procedure and returns the value passed back from the stored procedure.

long foo(long nInput)

{

int nOutput= -1;

Broker * pMyBroker = CreateBroker(0, NULL);

if(!pMyBroker->Connect("YourDSN", "sa", ""))

return rc;

if(pMyBroker->OpenSQL("{call assignment(?, ?)}"))

{

long cbOut = sizeof(nInput);

// bind the input parameter.

rc=pMyBroker->SetParameter(0, SQL_PARAM_INPUT, SQL_C_SLONG,

SQL_DECIMAL, 15, 0, (char*)&nInput, 0, &cbOut);

// bind the output parameter.

rc=pMyBroker->SetParameter(0, SQL_PARAM_OUTPUT, SQL_C_LONG,

SQL_DECIMAL, 0, 0, (char*)&nOutput, sizeof(nOutput), &cbOut);

if(!pMyBroker->ExecuteSQL())

{

printf("Huston, we got a problem.\n");

}

}

DestroyBroker(pMyBroker);

// nOutput contains the returned values from the stored

// procedure if successful.

return nOutput;

}

 

Broker API Reference

This section contains detailed description of the functions in the Database Broker class.

Broker(const char* szURL=NULL)

The constructor of the Broker object.

Syntax

Broker(const char* szURL)

Where szURL is the URL of the database server.

Parameters

szURL

The URL of the sever that are running on another host. For instances, the szURL may point to a host name MyMachine with an instance of Database Broker running.

The URL should also contain necessary information for logon to the DBMS.

This constructor is used for distributed processing where the database operations are spread on different machines. The parameter should be NULL if the broker is local.

 

Return

There is no returned value for constructors.

Example

Broker myBroker = new Broker("rpc://www.myhost.com?DataSource=TestDbase&UserName="tester");

 

Remarks

Use this constructor to create a broker that is capable of accessing other Brokers in the operating environment. This constructor is also the default constructor, which create a broker without any database connection. Database connection can be established using the Connect function later.


Broker(const char* szDSN, const char * szUserName, const char *password)

The constructor of the Broker object with database connection information

Syntax

Broker(const char* szDSN, const char * szUserName, const char *szPassword)

Parameters

    • szDSN: The ODBC data source name.
    • szUsername: The user name for logon to the database server.
    • szPassWord: The Password for authentication.

 

The szUserName and szPassword parameter can be empty if no authentication is required on the specified database server.

 

Return

There is no returned value for constructors.

Example

Broker myBroker = new Broker("TestDB", "scott", "tiger");

 

Remarks

Use this constructor to create a broker that establishes a database connection immediately.


BOOL Connect(const char* szDSN, const char * szUserName, const char *password, BOOL bCacheConnection)

The function makes an ODBC connection to the specified database.

Syntax

BOOL Connect(const char* szDSN, const char * szUserName, const char *password, BOOL bCacheConnection);

Parameters

    • szDSN: The ODBC data source name.
    • szUsername: The user name for logon to the database server.
    • szPassWord: The Password for authentication.
    • BCacheConnection: The database connection is cached on the server if the parameter is TRUE. A cached connection can be reused later. The default value is FALSE.

 

The szUserName and szPassword parameter can be empty if no authentication is required on the specified database server.

 

Return

The function returns true if successful, otherwise FALSE.

Example

If(!myBroker->Connect("TestDB", "scott", "tiger"))

Printf("Database connection failed.\n");

 

Remarks

Use this function to establish a database connection. Any open connection from previous calls will be closed. Any open SQL statement associated with previous connection will also be closed automatically.


 

BOOL OpenSQL(const char* szSQL, int nBindType = -1)

The function open a SQL statement.

Syntax

BOOL OpenSQL(const char* szSQL, int nBindType)

Parameters

    • szSQL: The SQL Statement to be processed.
    • nBindType: Specify how to perform dynamic binding. 1 means binding to character strings, 2 means binding to C structure. The default is binding to character strings.

 

Return

The function returns true if the SQL statement is opened successfully. FALSE if there is an error in the supplied statement.

Example

If(!myBroker->OpenSQL("select * from customer"))

Printf("could not open the sql statement.\n");

 

Remarks

Use this function to open a SQL statement. Any previous opened statement will be closed. The broker does automatic binding in most of the cases. It allocates necessary buffer for the result set if there is any.

The default binding for a query statement is character strings. This is the preferred bind type for the object as all columns are bound uniformly and almost all data fields could be converted to strings.

For a query statement, the function executes the SQL statement immediately if there is no parameter in the statement.

You should always test if the function is successful or not before executing the statement or fetching the result set.


 

long Fetch(long nStartPosition=ROWID_CURRENT)

The function fetches result set associated with a query statement.

Syntax

long Fetch(long nStartPosition)

Parameters

    • nStartPosition: The row number for the first row in the buffer. The default is ROWID_CURRENT, which is the row the cursor is positioned on.

 

Return

The function returns the number of rows fetched from the database. The returned value is 0 if there is no data available and SQL_ERROR if the cursor is positioned at end of the result set.

Example

while((rows=myBroker->Fetch())>0)

{

// we fetched n rows

for(int i=0; i<rows; i++)

{

printf("%s\n", myBroker->GetRecord(i));

}

}

 

Remarks

The broker performs batch fetching when the Fetch function is called. So there are multiple rows in the buffer after the operation. The cursor is positioned to the start position plus the number of rows fetched after each successful fetch.

The broker calls the Move function to position the cursor to the specified row before fetching any data into the buffer. There are three special row position defined in the object:

    • ROWID_CURRENT : Fetch from the current row.
    • ROWID_FIRSTROW: Fetch from the first row in the result set.
    • ROWID_LASTROW: Fetch from the last row.

 

long ExecuteSQL(const char* szSQL=NULL)

The function executes the specified SQL statement or an opened SQL statement (Prepared statement).

Syntax

long ExecuteSQL(const char* szSQL);

Parameters

    • szSQL:. The SQL statement to be executed, the parameter can be NULL if the statement is already opened using OpenSQL function.

 

Return

The function returns TRUE if the statement is executed successfully.

Example

if((rows=myBroker->ExecuteSQL("delete from junktable"))>0)

{

printf("Number of rows deleted : %d\n", myBroker->GetRowCount());

}

 

Remarks

The function can be used to execute any SQL statement other than query directly. Query statements without parameters are executed when OpenSQL is called. However, the ExecuteSQL function should be called if there is any runtime parameter for query statements.

The number of rows affected can be obtained by calling GetRowCount function.

 


 

long Move(long nRow)

Move the cursor to the specified location.

Syntax

long Move(long nRow);

Parameters

    • nRow:. The absolute row identifier or one of the special id : ROWID_CURRENT, ROWID_FIRSTROW, ROWID_LASTROW.

 

Return

The number of rows fetched after the move. A 0 or negative value indicates the move has failed.

Example

if((rows=myBroker->Move(0))>0)

{

printf("Number of rows fetched : %d\n", rows);

}

 

Remarks

The function not only moves the cursor to the specified location, but also fetches record into buffers from the location. The function can be used to randomly access the result set.

Depends on your DBMS, Moving to the last row in a result set the first time may cause ODBC to fetch all data in the result set and may take a little longer.


BOOL Delete(long nRow)

Delete a record in the result set.

Syntax

BOOL Delete(long nRow);

Parameters

    • nRow:. The absolute row number.

 

Return

The function returns TRUE if the record is deleted successfully.

Example

// delete the first row.

if(myBroker->Delete(0)>0)

{

printf("The first row is deleted");

}

 

Remarks

The function deletes the specified record in the current result set. The result set must be opened using the OpenSQL function and there should be no multiple table join.


BOOL Update(long nRow, const char**ppNames, const char* ppValues)

Update a record the new values.

Syntax

BOOL Update(long nRow, const char**ppNames, const char* ppValues);

Parameters

    • nRow:. The absolute row number.
    • ppNames: An array of character pointers point to the names of the fields to be updated. The array must be ended with a NULL pointer.
    • ppValues: An array of character pointers point to the values of the fields to be updated. The position of the value in the array must match that of ppNames. The array must be ended with a NULL pointer.

 

Return

The function returns TRUE if the record is updated successfully.

Example

// update the first row.

char * pNames[]={"EmployeeId", "EmployeeName", "EmployeePhone", NULL};

char * pValues[]={"873245", "SQLData", "(301)-515-0477", NULL);

if(myBroker->Update(0, &pNames, &pValues)>0)

{

printf("The first row is updated");

}

 

Remarks

The function updates the specified record in the current result set with a set of new values. The result set must be opened using the OpenSQL function which select data from only a single table.

Note, there may be more than one row updated if the field selected by the query statement could not uniquely identify a record. These can be avoided by put the key field in the SELECT clause of the query statement.


BOOL Insert(const char**ppNames, const char* ppValues)

Insert a record into the current table.

Syntax

BOOL Insert(const char**ppNames, const char* ppValues);

Parameters

    • ppNames: An array of character pointers point to the names of the fields in the table to be inserted. The array must be ended with a NULL pointer.
    • ppValues: An array of character pointers point to the values of the fields to be added. The position of the value in the array must match that of ppNames. The array must be ended with a NULL pointer.

 

Return

The function returns TRUE if the record is updated successfully.

Example

// insert the first row.

char * pNames[]={"EmployeeId", "EmployeeName", "EmployeePhone", NULL};

char * pValues[]={"873245", "SQLData", "(301)-515-0477", NULL);

if(myBroker->Insert(&pNames, &pValues)>0)

{

printf("The record is added into the table successfully.");

}

 

Remarks

The function adds the specified record to the current table. The result set must be opened using the OpenSQL function.

Note: The newly added record may not be reflected in the current result set immediately. Reopen the result set to load the new record when needed.


 

void CloseSQL()

Close an opened SQL Statement.

Syntax

void CloseSQL();

Parameters

None.

 

Return

none

Remarks

Call the function to close a SQL statement immediately. A SQL Statement is closed automatically under the following situations:

    • A new SQL statement is opened.
    • The database connection is closed.
    • The Broker is destroyed.

 

BOOL BeginTransaction()

Start a new transaction.

Syntax

BOOL BeginTransaction()

Parameters

None.

 

Return

TRUE if the operation is successful.

Remarks

The default behavior of the server is to commit each SQL statement if successful. Use the BeginTransaction to control transactions in your own way. You must commit your transaction manually after the function call.

Note that some DBMS do not support transaction, the function will have no effect under such situation. Consult your ODBC driver document for further information.


 

BOOL EndTransaction(BOOL bCommit)

Terminate a transaction.

Syntax

BOOL EndTransaction(BOOL bCommit)

Parameters

bCommit: The transaction is committed if bCommit is TRUE. The transaction is rolled back otherwise.

 

Return

TRUE if the operation is successful.

Remarks

Use this function to commit or roll back your transactions.

 

 


 

Char* GetErrorString()

Retrieve an error string.

Syntax

Char* GetErrorString();

Parameters

None.

 

Return

The formatted error string from the ODBC layer.

Remarks

Call the function if an operation fails. The error string contains the details about the nature of the error, the ODBC error code, the SQL state and the native error.

 


 

long GetRowCount()

Get the total number of rows affected by the SQL statement.

Syntax

long GetRowCount();

Parameters

None.

 

Return

The number of rows affected by the SQL statement.

Example

long nRows = pBroker->GetRowCount();

 

Remarks

The function returns the total number of rows in the result set if the SQL statement is a query. The returned value is the number of rows affected by the statement in all other cases.

Depends on the DBMS, the ODBC driver may have to fetch all records in order to determine the total number of rows selected by a query statement. So, it may be a very expensive operation in such a situation.

The function should be used after OpenSQL function for queries and after ExecuteSQL for all other SQL statements.


 

long GetFieldCount()

Get the number of fields in a SQL statement.

Syntax

long GetFieldCount();

Parameters

None.

 

Return

The number of fields selected by a query statement.

Example

long nFields = pBroker->GetFieldCount();

 

Remarks

The function should be used after a query statement is opened successfully.

 

 


 

Char * GetRecord(long nRow)

Get a record in the current buffer.

Syntax

Char * GetRecord(long nRow);

Parameters

    • nRow:. The row number in the current buffer. Note that this is not the absolute row id.

 

Return

A character string that contains all formatted field values in the record. NULL is returned if the specified row doesn’t exist in the buffer.

Example

while((rows=pBroker->Fetch())>0)

{

// we fetched n rows

for(int i=0; i<rows; i++)

{

printf("%s\n", pBroker->GetRecord(i));

}

nTotalRows +=rows;

}

 

Remarks

The function should be used after either a success move or fetch operation.

The returned value is formatted based on the width of each column and the data type of the column. The formatted string align perfectly with the record header (contains a string of all field names) returned by the GetHeader function if fixed pitch font is used.


 

Char * GetHeader(void)

Get header of the result set.

Syntax

Char * GetHeader(void)

Parameters

none.

 

Return

A character string that contains all formatted field names in the result set. NULL is returned if there is an error.

Example

printf("%s\n", pBroker->GetHeader());

 

Remarks

The function should be used after a query statement is opened successfully..

The returned value is formatted based on the width of each column and the data type of the column. The formatted string aligns perfectly with the data returned by the GetRecord function, if fixed pitch font is used.


 

char * GetFieldValue(long nRow, int nCol)

Get a single field value in a record.

Syntax

char * GetFieldValue(long nRow, int nCol)

Parameters

    • nRow: The row number in the current buffer. Note that this is not the absolute row id.
    • nCol: The Field index, starting from 0.

 

Return

A character string that contains the value of the field, NULL is returned if either nRow or nCol is out of bound.

Example

int nFieldCount= pBroker->GetFieldCount();

while((rows=pBroker->Fetch())>0)

{

for(int i=0; i<rows; i++)

{

for(int j=0; j<nFieldCount; j++)

printf("%s\n", pBroker->GetFieldValue(i, j));

}

}

 

Remarks

The function should be used after result is fetched into the internal buffer.

 


 

char * GetFieldName(int nCol)

Get name of a field.

Syntax

char * GetFieldName(int nCol)

Parameters

    • nCol: The Field index, starting from 0.

 

Return

A character string that contains the name of the field, NULL is returned if nCol is out of bound.

Example

// Let’s print out all field names

int nFieldCount= pBroker->GetFieldCount();

for(int j=0; j<nFieldCount; j++)

printf("%s\n", pBroker->GetFieldName(j));

 

Remarks

The function should be used after a query is opened successfully.


 

long GetCursorPos()

Get the current cursor positron.

Syntax

long GetCursorPos()

Parameters

None.

 

Return

The current cursor position..

Example

long nRows = pBroker->GetCursorPos();

 

Remarks

The current cursor position is actually the row id in the result set. You can use the function to remember the current cursor position, moves the cursor to another row and perform some operation, and then restore the cursor position.

The returned value can be used as the parameter for the Move function.


 

long GetFieldLength(int nCol)

Get the length of a field.

Syntax

long GetFieldLength(int nCol)

Parameters

    • nCol: The Field index, starting from 0.

 

Return

The size of the column as defined by the database table.

Example

int nFieldCount= pBroker->GetFieldLength();

char * szFieldValue = new char[nFieldCount+1];

strcpy(szFieldValue, pBroker->GetFieldValue(0, 0));

 

Remarks

The length of a field can be understand as the maximum number of characters in the column when it is converted to a string.


 

Int GetFieldType(int nCol)

Get the ODBC data type of a field.

Syntax

Int GetFieldType (int nCol)

Parameters

    • nCol: The Field index, starting from 0.

 

Return

The ODBC data type of the field.

Example

// here is some of the common ODBC data types:

int nFieldType= pBroker->GetFieldType();

switch(nFieldType)

{

case SQL_SMALLINT:

case SQL_INTEGER:

case SQL_TINYINT:

case SQL_BIGINT:

case SQL_REAL:

case SQL_FLOAT:

case SQL_DOUBLE:

case SQL_TIME:

case SQL_DATE:

case SQL_TIMESTAMP:

break;

case SQL_DECIMAL:

case SQL_NUMERIC:

break;

case SQL_BINARY:

case SQL_VARBINARY:

case SQL_CHAR:

case SQL_VARCHAR:

break;

case SQL_LONGVARCHAR:

case SQL_LONGVARBINARY:

break;

}

 

Remarks

The data type returned may not directly map to the native DBMS data types.


int GetFieldAttribute(int nCol, int nDescType)

Get the one of the attributes of a field.

Syntax

Int GetFieldAttribute(int nCol, int nDescType)

Parameters

    • nCol: The Field index, starting from 0.
    • nDescType:The type of the attributes. It can be any type as defined in the ODBC function SQLGetColAttributes.

 

Return

The value of the attribute.

Example

//find out if the first field is searchable.

int nSearchable= pBroker->GetFieldAttribute(0, SQL_DESC_SEARCHABLE);

 

Remarks

The function returns only the numeric attribute associated with the field. A


char * Enumerate(EnumType nType, char * szParam)

Enumerate database tables, data sources and field names.

Syntax

char * Enumerate(EnumType nType, char * szParam);

Parameters

    • nType: Type of the enumeration: It can be :
      1. ET_TABLE: Enumerate all tables in the current data source.
      2. ET_DATASOURCE: Enumerate all data sources.
      3. ET_COLUMN: Enumerate all columns in a table
    • szParam:Used in ET_COLUMN, szParam should point to the name of the table.

 

Return

The function returns a value each time when it’s called. It returns NULL to indicate the end of the enumeration.

Example

//make a database connection

pBroker->Connect("MyDbase", "admin", "");

// enumerate all tables in MyDbase

while((szTableName= pBroker->Enumerate(ET_TABLES, NULL)))

{

printf("%s\n", szTableName);

}

 

Remarks

The function returns one value at a time until all the values are enumerated. Database connection is required for ET_TABLE and ET_COLUMN.

 

SetPrameter

SetParameter binds a buffer to a parameter marker in an SQL statement.

Syntax

BOOL SetParameter(int nIndex, short nInOut,

short fCType, short fSqlType, int nPrecision,

short nScale, const char* value,

int nMaxLen, long * pcbValue);

Parameters

    • nIndex: Parameter number, starts from 0.
    • nInOut: The type of the parameter, either SQL_PARAM_INPUT, SQL_PARAM_OUTPUT or SQL_PARAM_INPUTOUTPUT.
    • fCType: C data type of the parameter.
    • fSqlType:SQL data type of the parameter.
    • nPrecision: The size of the column expression of the parameter.
    • nScale:The decimal digits of the column or expression.
    • value:A pointer to a buffer for the parameter.
    • nMaxLen:The max length of the buffer.
    • pcbValue:A pointer to a buffer for the parameter's length.

Return

The function returns TRUE if the parameter is set successfully, FALSE otherwise.

Example

 

Remarks

The function is just a wrapper of the ODBC SQLBindParameter function. \Please refer to your ODBC document if you need further information.

 

 

 

Download | Purchase | ContactFeedback


Send mail to  info2-at-sqldata-dot-com with questions or comments about this web site.
Copyright 2008-2010 SQLData Systems, Inc.
Last modified: July 10, 2010