46 Error(
"TPgSQLServer",
"malformed db argument %s", db);
52 Error(
"TPgSQLServer",
"protocol in db argument should be pgsql it is %s",
58 const char *dbase = url.
GetFile();
63 fPgSQL = PQsetdbLogin(url.
GetHost(), port, 0, 0, dbase, uid, pw);
65 fPgSQL = PQsetdbLogin(url.
GetHost(), 0, 0, 0, dbase, uid, pw);
68 if (PQstatus(
fPgSQL) != CONNECTION_BAD) {
76 static const char *sql =
"select setting from pg_settings where name='server_version'";
77 PGresult *res = PQexec(
fPgSQL, sql);
78 int stat = PQresultStatus(res);
79 if (stat == PGRES_TUPLES_OK && PQntuples(res)) {
80 char *vers = PQgetvalue(res,0,0);
84 fSrvInfo +=
"unknown version number";
87 Error(
"TPgSQLServer",
"connection to %s failed", url.
GetHost());
121 Error(
"Query",
"not connected");
125 PGresult *res = PQexec(
fPgSQL, sql);
128 if ((PQresultStatus(res) != PGRES_COMMAND_OK) &&
129 (PQresultStatus(res) != PGRES_TUPLES_OK)) {
130 Error(
"Query",
"%s",PQresultErrorMessage(res));
149 Error(
"SelectDataBase",
"not connected");
163 opts.
Data(), 0, dbname,
166 if (PQstatus(
fPgSQL) == CONNECTION_OK) {
170 Error(
"SelectDataBase",
"%s",PQerrorMessage(
fPgSQL));
186 Error(
"GetDataBases",
"not connected");
190 TString sql =
"SELECT pg_database.datname FROM pg_database";
192 sql +=
Form(
" WHERE pg_database.datname LIKE '%s'", wild);
206 Error(
"GetTables",
"not connected");
211 Error(
"GetTables",
"no such database %s", dbname);
215 TString sql =
"SELECT relname FROM pg_class where relkind='r'";
217 sql +=
Form(
" AND relname LIKE '%s'", wild);
232 Error(
"GetColumns",
"not connected");
237 Error(
"GetColumns",
"no such database %s", dbname);
243 sql =
Form(
"select a.attname,t.typname,a.attnotnull \ 244 from pg_attribute a, pg_class c, pg_type t \ 245 where c.oid=a.attrelid and c.relname='%s' and \ 246 a.atttypid=t.oid and a.attnum>0 \ 247 and a.attname like '%s' order by a.attnum ", table,wild);
249 sql =
Form(
"select a.attname,t.typname,a.attnotnull \ 250 from pg_attribute a, pg_class c, pg_type t \ 251 where c.oid=a.attrelid and c.relname='%s' and \ 252 a.atttypid=t.oid and a.attnum>0 order by a.attnum",table);
263 Error(
"CreateDataBase",
"not connected");
267 sql =
Form(
"CREATE DATABASE %s", dbname);
268 PGresult *res = PQexec(
fPgSQL, sql);
280 Error(
"DropDataBase",
"not connected");
284 sql =
Form(
"DROP DATABASE %s", dbname);
285 PGresult *res = PQexec(
fPgSQL, sql);
297 Error(
"Reload",
"not connected");
301 Error(
"Reload",
"not implemented");
312 Error(
"Shutdown",
"not connected");
316 Error(
"Shutdown",
"not implemented");
326 Error(
"ServerInfo",
"not connected");
340 #ifdef PG_VERSION_NUM 350 #ifdef PG_VERSION_NUM 356 #ifdef PG_VERSION_NUM 358 SetError(-1,
"no query string specified",
"Statement");
364 SetError(-1,
"cannot allocate PgSQL_Stmt_t",
"Statement");
368 stmt->
fRes = PQprepare(
fPgSQL,
"preparedstmt", sql, 0, (
const Oid*)0);
370 ExecStatusType stat = PQresultStatus(stmt->
fRes);
375 SetError(stat, PQresultErrorMessage(stmt->
fRes),
"Statement");
381 Error(
"Statement",
"not implemented for pgsql < 8.2");
392 Error(
"GetColumns",
"not connected");
397 if ((tablename==0) || (*tablename==0))
return 0;
401 if ((PQresultStatus(res) != PGRES_COMMAND_OK) &&
402 (PQresultStatus(res) != PGRES_TUPLES_OK)) {
403 Error(
"Query",
"%s",PQresultErrorMessage(res));
411 PGresult *res_type = PQexec(
fPgSQL,
"SELECT OID, TYPNAME FROM PG_TYPE;");
413 if ((PQresultStatus(res_type) != PGRES_COMMAND_OK) &&
414 (PQresultStatus(res_type) != PGRES_TUPLES_OK)) {
415 Error(
"Query",
"%s", PQresultErrorMessage(res_type));
421 Int_t nOids = PQntuples(res_type);
422 for (
Int_t oid=0; oid<nOids; oid++) {
424 char* oidString = PQgetvalue(res_type, oid, 0);
425 char* typeString = PQgetvalue(res_type, oid, 1);
426 if (sscanf(oidString,
"%10d", &tOid) != 1) {
427 Error(
"GetTableInfo",
"Bad non-numeric oid '%s' for type '%s'", oidString, typeString);
436 Int_t nfields = PQnfields(res);
438 for (
Int_t col=0;col<nfields;col++){
440 Int_t data_size = -1;
441 Int_t data_length = -1;
442 Int_t data_scale = -1;
443 Int_t data_sign = -1;
446 const char* column_name = PQfname(res,col);
447 const char* type_name;
448 int imod = PQfmod(res,col);
451 int oid_code = PQftype(res,col);
454 std::map<Int_t,std::string>::iterator lookupOid =
fOidTypNameMap.find(oid_code);
459 type_name =
"NUMERIC";
461 }
else if (lookupOid->second ==
"int2"){
465 }
else if (lookupOid->second ==
"int4"){
469 }
else if (lookupOid->second ==
"int8"){
473 }
else if (lookupOid->second ==
"float4"){
477 }
else if (lookupOid->second ==
"float8"){
479 type_name =
"DOUBLE";
481 }
else if (lookupOid->second ==
"bool"){
485 }
else if (lookupOid->second ==
"char"){
489 }
else if (lookupOid->second ==
"varchar"){
491 type_name =
"VARCHAR";
493 }
else if (lookupOid->second ==
"text"){
495 type_name =
"VARCHAR";
497 }
else if (lookupOid->second ==
"name"){
499 type_name =
"VARCHAR";
501 }
else if (lookupOid->second ==
"date"){
503 type_name =
"TIMESTAMP";
505 }
else if (lookupOid->second ==
"time"){
507 type_name =
"TIMESTAMP";
509 }
else if (lookupOid->second ==
"timetz"){
511 type_name =
"TIMESTAMP";
513 }
else if (lookupOid->second ==
"timestamp"){
515 type_name =
"TIMESTAMP";
517 }
else if (lookupOid->second ==
"timestamptz"){
519 type_name =
"TIMESTAMP";
521 }
else if (lookupOid->second ==
"interval"){
523 type_name =
"TIMESTAMP";
525 }
else if (lookupOid->second ==
"bytea"){
527 type_name =
"BINARY";
529 }
else if (lookupOid->second ==
""){
531 type_name =
"UNKNOWN";
536 type_name =
"NUMERIC";
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.
TPgSQLServer(const char *db, const char *uid, const char *pw)
Open a connection to a 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 * GetFile() const
Int_t DropDataBase(const char *dbname)
Drop (i.e.
const char * GetHost() const
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
TSQLServer * ROOT_Plugin_TPgSQLServer(const char *db, const char *uid, const char *pw)
PluginManager generator function.
TSQLResult * GetDataBases(const char *wild=0)
List all available databases.
TSQLStatement * Statement(const char *sql, Int_t=100)
Produce TPgSQLStatement.
TSQLResult * GetTables(const char *dbname, const char *wild=0)
List all tables in the specified database.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
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.
void SetError(Int_t code, const char *msg, const char *method=0)
set new values for error fields if method is specified, displays error message
Int_t CreateDataBase(const char *dbname)
Create a database. Returns 0 if successful, non-zero otherwise.
virtual Bool_t IsConnected() const
std::map< Int_t, std::string > fOidTypNameMap
virtual void Add(TObject *obj)
Int_t Atoi() const
Return integer value of string.
Int_t Reload()
Reload permission tables.
Bool_t HasStatement() const
PG_VERSION_NUM conveniently only started being #defined at 8.2.3 which is the first version of libpq ...
const char * Data() const