38 Error(
"TPgSQLServer",
"malformed db argument %s", db);
44 Error(
"TPgSQLServer",
"protocol in db argument should be pgsql it is %s",
50 const char *dbase = url.
GetFile();
55 fPgSQL = PQsetdbLogin(url.
GetHost(), port, 0, 0, dbase, uid, pw);
57 fPgSQL = PQsetdbLogin(url.
GetHost(), 0, 0, 0, dbase, uid, pw);
60 if (PQstatus(fPgSQL) != CONNECTION_BAD) {
67 fSrvInfo =
"postgres ";
68 static const char *sql =
"select setting from pg_settings where name='server_version'";
69 PGresult *res = PQexec(fPgSQL, sql);
70 int stat = PQresultStatus(res);
71 if (stat == PGRES_TUPLES_OK && PQntuples(res)) {
72 char *vers = PQgetvalue(res,0,0);
76 fSrvInfo +=
"unknown version number";
79 Error(
"TPgSQLServer",
"connection to %s failed", url.
GetHost());
113 Error(
"Query",
"not connected");
117 PGresult *res = PQexec(
fPgSQL, sql);
120 if ((PQresultStatus(res) != PGRES_COMMAND_OK) &&
121 (PQresultStatus(res) != PGRES_TUPLES_OK)) {
122 Error(
"Query",
"%s",PQresultErrorMessage(res));
141 Error(
"SelectDataBase",
"not connected");
158 if (PQstatus(
fPgSQL) == CONNECTION_OK) {
162 Error(
"SelectDataBase",
"%s",PQerrorMessage(
fPgSQL));
178 Error(
"GetDataBases",
"not connected");
182 TString sql =
"SELECT pg_database.datname FROM pg_database";
184 sql +=
Form(
" WHERE pg_database.datname LIKE '%s'", wild);
198 Error(
"GetTables",
"not connected");
203 Error(
"GetTables",
"no such database %s", dbname);
207 TString sql =
"SELECT relname FROM pg_class where relkind='r'";
209 sql +=
Form(
" AND relname LIKE '%s'", wild);
224 Error(
"GetColumns",
"not connected");
229 Error(
"GetColumns",
"no such database %s", dbname);
235 sql =
Form(
"select a.attname,t.typname,a.attnotnull \
236 from pg_attribute a, pg_class c, pg_type t \
237 where c.oid=a.attrelid and c.relname='%s' and \
238 a.atttypid=t.oid and a.attnum>0 \
239 and a.attname like '%s' order by a.attnum ", table,wild);
241 sql =
Form(
"select a.attname,t.typname,a.attnotnull \
242 from pg_attribute a, pg_class c, pg_type t \
243 where c.oid=a.attrelid and c.relname='%s' and \
244 a.atttypid=t.oid and a.attnum>0 order by a.attnum",table);
255 Error(
"CreateDataBase",
"not connected");
259 sql =
Form(
"CREATE DATABASE %s", dbname);
260 PGresult *res = PQexec(
fPgSQL, sql);
272 Error(
"DropDataBase",
"not connected");
276 sql =
Form(
"DROP DATABASE %s", dbname);
277 PGresult *res = PQexec(
fPgSQL, sql);
289 Error(
"Reload",
"not connected");
293 Error(
"Reload",
"not implemented");
304 Error(
"Shutdown",
"not connected");
308 Error(
"Shutdown",
"not implemented");
318 Error(
"ServerInfo",
"not connected");
332 #ifdef PG_VERSION_NUM
342 #ifdef PG_VERSION_NUM
348 #ifdef PG_VERSION_NUM
350 SetError(-1,
"no query string specified",
"Statement");
356 SetError(-1,
"cannot allocate PgSQL_Stmt_t",
"Statement");
359 stmt->
fConn = fPgSQL;
360 stmt->
fRes = PQprepare(fPgSQL,
"preparedstmt", sql, 0, (
const Oid*)0);
362 ExecStatusType stat = PQresultStatus(stmt->
fRes);
367 SetError(stat, PQresultErrorMessage(stmt->
fRes),
"Statement");
373 Error(
"Statement",
"not implemented for pgsql < 8.2");
384 Error(
"GetColumns",
"not connected");
389 if ((tablename==0) || (*tablename==0))
return 0;
393 if ((PQresultStatus(res) != PGRES_COMMAND_OK) &&
394 (PQresultStatus(res) != PGRES_TUPLES_OK)) {
395 Error(
"Query",
"%s",PQresultErrorMessage(res));
403 PGresult *res_type = PQexec(
fPgSQL,
"SELECT OID, TYPNAME FROM PG_TYPE;");
405 if ((PQresultStatus(res_type) != PGRES_COMMAND_OK) &&
406 (PQresultStatus(res_type) != PGRES_TUPLES_OK)) {
407 Error(
"Query",
"%s", PQresultErrorMessage(res_type));
413 Int_t nOids = PQntuples(res_type);
414 for (
Int_t oid=0; oid<nOids; oid++) {
416 char* oidString = PQgetvalue(res_type, oid, 0);
417 char* typeString = PQgetvalue(res_type, oid, 1);
418 if (sscanf(oidString,
"%10d", &tOid) != 1) {
419 Error(
"GetTableInfo",
"Bad non-numeric oid '%s' for type '%s'", oidString, typeString);
428 Int_t nfields = PQnfields(res);
430 for (
Int_t col=0;col<nfields;col++){
432 Int_t data_size = -1;
433 Int_t data_length = -1;
434 Int_t data_scale = -1;
435 Int_t data_sign = -1;
438 const char* column_name = PQfname(res,col);
439 const char* type_name;
440 int imod = PQfmod(res,col);
443 int oid_code = PQftype(res,col);
446 std::map<Int_t,std::string>::iterator lookupOid =
fOidTypNameMap.find(oid_code);
451 type_name =
"NUMERIC";
453 }
else if (lookupOid->second ==
"int2"){
457 }
else if (lookupOid->second ==
"int4"){
461 }
else if (lookupOid->second ==
"int8"){
465 }
else if (lookupOid->second ==
"float4"){
469 }
else if (lookupOid->second ==
"float8"){
471 type_name =
"DOUBLE";
473 }
else if (lookupOid->second ==
"bool"){
477 }
else if (lookupOid->second ==
"char"){
481 }
else if (lookupOid->second ==
"varchar"){
483 type_name =
"VARCHAR";
485 }
else if (lookupOid->second ==
"text"){
487 type_name =
"VARCHAR";
489 }
else if (lookupOid->second ==
"name"){
491 type_name =
"VARCHAR";
493 }
else if (lookupOid->second ==
"date"){
495 type_name =
"TIMESTAMP";
497 }
else if (lookupOid->second ==
"time"){
499 type_name =
"TIMESTAMP";
501 }
else if (lookupOid->second ==
"timetz"){
503 type_name =
"TIMESTAMP";
505 }
else if (lookupOid->second ==
"timestamp"){
507 type_name =
"TIMESTAMP";
509 }
else if (lookupOid->second ==
"timestamptz"){
511 type_name =
"TIMESTAMP";
513 }
else if (lookupOid->second ==
"interval"){
515 type_name =
"TIMESTAMP";
517 }
else if (lookupOid->second ==
"bytea"){
519 type_name =
"BINARY";
521 }
else if (lookupOid->second ==
""){
523 type_name =
"UNKNOWN";
528 type_name =
"NUMERIC";
const char * GetHost() const
Int_t Shutdown()
Shutdown the database server.
This class represents a WWW compatible URL.
const char * GetProtocol() const
~TPgSQLServer()
Close connection to PgSQL DB server.
TSQLResult * Query(const char *sql)
Execute SQL command.
TSQLResult * GetColumns(const char *dbname, const char *table, const char *wild=0)
List all columns in specified table in the specified database.
TSQLTableInfo * GetTableInfo(const char *tablename)
Produce TSQLTableInfo.
const char * Data() const
Int_t DropDataBase(const char *dbname)
Drop (i.e.
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
TSQLResult * GetDataBases(const char *wild=0)
List all available databases.
Int_t Atoi() const
Return integer value of string.
TSQLStatement * Statement(const char *sql, Int_t=100)
Produce TPgSQLStatement.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
TSQLResult * GetTables(const char *dbname, const char *wild=0)
List all tables in the specified database.
void Error(const char *location, const char *msgfmt,...)
virtual Bool_t IsConnected() const
ClassImp(TPgSQLServer) TPgSQLServer
Open a connection to a PgSQL DB server.
Bool_t HasStatement() const
PG_VERSION_NUM conveniently only started being #defined at 8.2.3 which is the first version of libpq ...
char * Form(const char *fmt,...)
void Close(Option_t *opt="")
Close connection to PgSQL DB server.
const char * ServerInfo()
Return server info.
Int_t SelectDataBase(const char *dbname)
Select a database. Returns 0 if successful, non-zero otherwise.
Int_t CreateDataBase(const char *dbname)
Create a database. Returns 0 if successful, non-zero otherwise.
std::map< Int_t, std::string > fOidTypNameMap
virtual void Add(TObject *obj)
Int_t Reload()
Reload permission tables.
const char * GetFile() const