RFC C++ Interface

From: Randy Jonasz <rjonasz(at)click2net(dot)com>
To: Adam Haberlach <adam(at)newsnipple(dot)com>, "Clark, Joel" <jclark(at)lendingtree(dot)com>, Postgres Interfaces <pgsql-interfaces(at)postgresql(dot)org>
Subject: RFC C++ Interface
Date: 2000-12-06 21:48:36
Message-ID: Pine.BSF.4.30.0012061646490.32123-100000@nietzsche.jaded.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-interfaces


I know it's been a while since we last discussed a possible rewrite of
the C++ API but I now have some time to devote to it.

The following are my ideas for implementing the C++ API:

I need suggestions, additions, comments etc!

All classes will be defined in postgres namespace.

The desired usage would be as follows

//create a database object
postgres::db = database("host = somewhere.com user=someone
password=something dbname=mydb");

//synchronous connection returns TRUE upon success FALSE upon error
if( db.connect() ) {
string sql("SELECT * FROM foo");
postgres::result res = db.exec(sql);

if( !res.error() ) {
int numrows = res.numrows(); //used to obtain number of rows returned
//by previous sql statement
int numcols = res.numcols(); //used to obtain number of columns
//returned by previous sql statement
int field1;
char field2[size],field3[size];
long field4;

//results can be obtained within a for loop using numrows, numcols or as
//below
while( res.getrow() ) { //increment row

//result object has insert operator and array operator overloaded
res >> field1 >> field2; //result object will return datatypes not
//just chars
field3 = res[0];
field4 = res["fieldname"];
// .. do something with values ..
}

}
else {
cerr << res.display_error();
}

}
else {
cerr << db.display_error();
}

Alternatively one could access db asynchronously

//create a database object
postgres::db = database("host = somewhere.com user=someone
password=something dbname=mydb");

db.setasync(); //set asyncrhonous conection with back-end
//setsync does the opposite
while( !db.connect() && !db.error() ) {

//..do something

}
if( db.error() ) {
cerr << db.display_error();
exit(1);
}

string sql("SELECT * FROM foo");
postgres::result res = db.exec(sql);

while( !res.ready() && !res.error() ) {

//..do something

}

One could also set exceptions with

//create a database object
postgres::db = database("host = somewhere.com user=someone
password=something dbname=mydb");

db.setexception();

try {

db.connect();
string sql("SELECT * FROM foo");
postgres::result res = db.exec(sql);

}
catch( postgres::error& err ) {

//..do something
cerr << err.display_error();
}

The above examples make use of embedded sql being passed to the db object
via a string object. ( exec will be overloaded to accept a const char * as well).
I also envision a higher level which might prove usefull.

//create a database object
postgres::db = database("host = somewhere.com user=someone
password=something dbname=mydb");

postgres::table mytable = db.gettable("tablename");
//table can now be queried about characteristics of table
uint64_t numcols = mytable.numcols(); //need to find the max values and return an appropriate type
uint64_t numrows = mytable.numrows();
size_t colsize = mytable.colsize("column");

//obtain an inserter

postgres::inserter myinsert mytable.getinsert();
inserter.setcolumn("colname");

ifstream infile;
infile.open("myfile");
char data[32];

while (infile.getline(line,sizeof(data),'\t')) {
inserter << data;
}

the above can be extended to include update and delete functions as well

postgres::updater myupdate mytable.getupdate();
myupdate.setcolumn("colname");
myupdate.setcond("WHERE something = something");

ifstream infile;
infile.open("myfile");
char data[32];

while (infile.getline(line,sizeof(data),'\t')) {
myupdate << data;
}

Randy Jonasz
Software Engineer
Click2net Inc.
Web: http://www.click2net.com
Phone: (905) 271-3550

"You cannot possibly pay a philosopher what he's worth,
but try your best" -- Aristotle

Browse pgsql-interfaces by date

  From Date Subject
Next Message Michael Fork 2000-12-06 23:42:03 ODBC Driver patch
Previous Message Frodo Larik 2000-12-06 15:30:06 PostgreSQL -> ODBC -> MS ACCESS