RFC C++ Interface - Mailing list pgsql-hackers

From Randy Jonasz
Subject RFC C++ Interface
Date
Msg-id Pine.BSF.4.30.0012061708500.32123-100000@nietzsche.jaded.net
Whole thread Raw
Responses Re: RFC C++ Interface  (ncm@zembu.com (Nathan Myers))
Re: RFC C++ Interface  (Peter Eisentraut <peter_e@gmx.net>)
List pgsql-hackers
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
returndatatypes 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




pgsql-hackers by date:

Previous
From: ncm@zembu.com (Nathan Myers)
Date:
Subject: CRCs (was: beta testing version)
Next
From: Oleg Bartunov
Date:
Subject: Re: CVS: miscadmin.h is missing