| |
12 Using the C++ APIBack to TOC |
For an application programmer, it may be useful to access the cego database with a more low level programming interface.
For those guys, cego provides a C++ programming interface.
To use the C++ API, you should have compiled and installed the base library, the xml library and the cego client library.
All packages are available as opensource at www.lemke-it.com. Please refer to the README information of each package,
how to compile and install it.
The first thing, you have to do in you application program is to connect to the cego database daemon. For this, you can use the following code.
/* from base */
#include <Chain.h>
#include <Logger.h>
#include <Exception.h>
#include <Net.h>
#include <NetHandler.h>
#define NETBUFLEN 1024
main(int argc, char **argv)
{
Chain serverName("myServer");
int portNo = 2200;
Chain tableSet("TS1");
Chain user("lemke");
Chain password("lemke");
Chain logFile("cg.log");
Chain logMode("notice");
CegoNet *pCegoNet = new CegoNet( logFile, logMode );
pCegoNet->connect(serverName, portNo, tableSet, user, password);
...
|
If a connection has been set up and a database session bas been started, any kind of database queries can be executed.
// define any needful sql query here
Chain stmt("insert into ....");
try
{
long res = _pCegoNet->doQuery(stmt);
}
catch ( Exception e )
{
Chain msg;
e.pop(msg);
cout << "Error : " << msg << endl;
}
|
For queries, returning any kind of user data, please see the following sections.
...
ListT<CegoField> schema;
pCegoNet->getSchema(schema);
ListT<CegoField> fvl;
while ( pCegoNet->fetchData(schema, fvl) )
{
CegoField *pF = fvl.First();
while ( pF )
{
cout << pF->getValue() << "\t";
pF = fvl.Next();
}
cout << endl;
fvl.Empty();
}
...
|
The amount of affected rows for modifying queries ( e.g. insert, update, delete ) can be retrieved with the return value of the doQuery method
...
Chain stmt("update t1 set b = 24 where a = 11;");
long res = _pCegoNet->doQuery(stmt);
...
|
...
Chain stmt(":p1 = call myProc( 23, :p2 )");
int res = _pCegoNet->doQuery(stmt);
ListT
|
In the following a description is given for all cego classes, which might be used for a client application program.
The following methods of CegoNet are recommended to be used in client programs
| CegoNet | |
|---|---|
| Method | Description |
| CegoNet( const Chain& logFile ) | |
| ~CegoNet() | |
| void connect(const Chain& serverName, int port, const Chain& tableSet, const Chain& user, const Chain& pwd) | |
| long doQuery(const Chain& query) | |
| bool fetchData(const ListT<CegoField>& schema, ListT<CegoField>& fvl) | |
| void disconnect() | |
| bool isFetchablet() | |
| bool abortQuery() | |
| bool resetQuery() | |
| long getAffected() | |
| void getSchema(ListT<CegoField>& schema) | |
| void getProcResult(ListT<CegoProcVar>& outParamList, CegoFieldValue& retValue) | |
The following methods of CegoField are recommended to be used in client programs
| CegoField | |
|---|---|
| Method | Description |
| bool isNullable() const | |
| const Chain& getTableName() const | |
| const Chain& getTableAlias() const | |
| const Chain& getAttrName() const | |
| CegoFieldValue& getFieldValue() | |
| Chain toChain() const | |
| friend ostream& operator << (ostream& s, const CegoField& f) | |
The following methods of CegoField are recommended to be used in client programs
| CegoFieldValue | |
|---|---|
| Method | Description |
| const CegoDataType getType() const | |
| void* getValue() const | |
| int getLength() const | |
| Chain toChain() const | |
| Chain valAsChain() const | |
The following methods of CegoProcVar are recommended to be used in client programs
| CegoFieldValue | |
|---|---|
| Method | Description |
| const Chain& getName() const | |
| const CegoFieldValue& getValue() const | |