22#define pgsql_success(x) (((x) == PGRES_EMPTY_QUERY) \
23 || ((x) == PGRES_COMMAND_OK) \
24 || ((x) == PGRES_TUPLES_OK))
51 Error(
"TPgSQLServer",
"malformed db argument %s", db);
57 Error(
"TPgSQLServer",
"protocol in db argument should be pgsql it is %s",
63 const char *dbase = url.
GetFile();
68 fPgSQL = PQsetdbLogin(url.
GetHost(), port, 0, 0, dbase, uid, pw);
70 fPgSQL = PQsetdbLogin(url.
GetHost(), 0, 0, 0, dbase, uid, pw);
73 if (PQstatus(
fPgSQL) != CONNECTION_BAD) {
81 static const char *sql =
"select setting from pg_settings where name='server_version'";
82 PGresult *res = PQexec(
fPgSQL, sql);
83 int stat = PQresultStatus(res);
84 if (stat == PGRES_TUPLES_OK && PQntuples(res)) {
85 char *vers = PQgetvalue(res,0,0);
89 fSrvInfo +=
"unknown version number";
92 Error(
"TPgSQLServer",
"connection to %s failed", url.
GetHost());
126 Error(
"Query",
"not connected");
130 PGresult *res = PQexec(
fPgSQL, sql);
133 if ((PQresultStatus(res) != PGRES_COMMAND_OK) &&
134 (PQresultStatus(res) != PGRES_TUPLES_OK)) {
135 Error(
"Query",
"%s",PQresultErrorMessage(res));
154 Error(
"SelectDataBase",
"not connected");
168 opts.
Data(), 0, dbname,
171 if (PQstatus(
fPgSQL) == CONNECTION_OK) {
175 Error(
"SelectDataBase",
"%s",PQerrorMessage(
fPgSQL));
191 Error(
"GetDataBases",
"not connected");
195 TString sql =
"SELECT pg_database.datname FROM pg_database";
197 sql +=
Form(
" WHERE pg_database.datname LIKE '%s'", wild);
211 Error(
"GetTables",
"not connected");
216 Error(
"GetTables",
"no such database %s", dbname);
220 TString sql =
"SELECT relname FROM pg_class where relkind='r'";
222 sql +=
Form(
" AND relname LIKE '%s'", wild);
237 Error(
"GetColumns",
"not connected");
242 Error(
"GetColumns",
"no such database %s", dbname);
248 sql =
Form(
"select a.attname,t.typname,a.attnotnull \
249 from pg_attribute a, pg_class c, pg_type t \
250 where c.oid=a.attrelid and c.relname='%s' and \
251 a.atttypid=t.oid and a.attnum>0 \
252 and a.attname like '%s' order by a.attnum ", table,wild);
254 sql =
Form(
"select a.attname,t.typname,a.attnotnull \
255 from pg_attribute a, pg_class c, pg_type t \
256 where c.oid=a.attrelid and c.relname='%s' and \
257 a.atttypid=t.oid and a.attnum>0 order by a.attnum",table);
268 Error(
"CreateDataBase",
"not connected");
272 sql =
Form(
"CREATE DATABASE %s", dbname);
273 PGresult *res = PQexec(
fPgSQL, sql);
285 Error(
"DropDataBase",
"not connected");
289 sql =
Form(
"DROP DATABASE %s", dbname);
290 PGresult *res = PQexec(
fPgSQL, sql);
302 Error(
"Reload",
"not connected");
306 Error(
"Reload",
"not implemented");
317 Error(
"Shutdown",
"not connected");
321 Error(
"Shutdown",
"not implemented");
331 Error(
"ServerInfo",
"not connected");
363 SetError(-1,
"no query string specified",
"Statement");
369 SetError(-1,
"cannot allocate PgSQL_Stmt_t",
"Statement");
372 stmt->
fConn = fPgSQL;
373 stmt->
fRes = PQprepare(fPgSQL,
"preparedstmt", sql, 0, (
const Oid*)0);
375 ExecStatusType stat = PQresultStatus(stmt->
fRes);
380 SetError(stat, PQresultErrorMessage(stmt->
fRes),
"Statement");
386 Error(
"Statement",
"not implemented for pgsql < 8.2");
397 Error(
"GetColumns",
"not connected");
402 if ((tablename==0) || (*tablename==0))
return 0;
406 if ((PQresultStatus(res) != PGRES_COMMAND_OK) &&
407 (PQresultStatus(res) != PGRES_TUPLES_OK)) {
408 Error(
"Query",
"%s",PQresultErrorMessage(res));
416 PGresult *res_type = PQexec(
fPgSQL,
"SELECT OID, TYPNAME FROM PG_TYPE;");
418 if ((PQresultStatus(res_type) != PGRES_COMMAND_OK) &&
419 (PQresultStatus(res_type) != PGRES_TUPLES_OK)) {
420 Error(
"Query",
"%s", PQresultErrorMessage(res_type));
426 Int_t nOids = PQntuples(res_type);
427 for (
Int_t oid=0; oid<nOids; oid++) {
429 char* oidString = PQgetvalue(res_type, oid, 0);
430 char* typeString = PQgetvalue(res_type, oid, 1);
431 if (sscanf(oidString,
"%10d", &tOid) != 1) {
432 Error(
"GetTableInfo",
"Bad non-numeric oid '%s' for type '%s'", oidString, typeString);
441 Int_t nfields = PQnfields(res);
443 for (
Int_t col=0;col<nfields;col++){
445 Int_t data_size = -1;
446 Int_t data_length = -1;
447 Int_t data_scale = -1;
448 Int_t data_sign = -1;
451 const char* column_name = PQfname(res,col);
452 const char* type_name;
453 int imod = PQfmod(res,col);
456 int oid_code = PQftype(res,col);
459 std::map<Int_t,std::string>::iterator lookupOid =
fOidTypNameMap.find(oid_code);
464 type_name =
"NUMERIC";
466 }
else if (lookupOid->second ==
"int2"){
470 }
else if (lookupOid->second ==
"int4"){
474 }
else if (lookupOid->second ==
"int8"){
478 }
else if (lookupOid->second ==
"float4"){
482 }
else if (lookupOid->second ==
"float8"){
484 type_name =
"DOUBLE";
486 }
else if (lookupOid->second ==
"bool"){
490 }
else if (lookupOid->second ==
"char"){
494 }
else if (lookupOid->second ==
"varchar"){
496 type_name =
"VARCHAR";
498 }
else if (lookupOid->second ==
"text"){
500 type_name =
"VARCHAR";
502 }
else if (lookupOid->second ==
"name"){
504 type_name =
"VARCHAR";
506 }
else if (lookupOid->second ==
"date"){
508 type_name =
"TIMESTAMP";
510 }
else if (lookupOid->second ==
"time"){
512 type_name =
"TIMESTAMP";
514 }
else if (lookupOid->second ==
"timetz"){
516 type_name =
"TIMESTAMP";
518 }
else if (lookupOid->second ==
"timestamp"){
520 type_name =
"TIMESTAMP";
522 }
else if (lookupOid->second ==
"timestamptz"){
524 type_name =
"TIMESTAMP";
526 }
else if (lookupOid->second ==
"interval"){
528 type_name =
"TIMESTAMP";
530 }
else if (lookupOid->second ==
"bytea"){
532 type_name =
"BINARY";
534 }
else if (lookupOid->second ==
""){
536 type_name =
"UNKNOWN";
541 type_name =
"NUMERIC";
void Error(const char *location, const char *msgfmt,...)
TSQLServer * ROOT_Plugin_TPgSQLServer(const char *db, const char *uid, const char *pw)
PluginManager generator function.
char * Form(const char *fmt,...)
virtual void Add(TObject *obj)
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
TSQLResult * GetTables(const char *dbname, const char *wild=nullptr) final
List all tables in the specified database.
Int_t CreateDataBase(const char *dbname) final
Create a database. Returns 0 if successful, non-zero otherwise.
TSQLResult * Query(const char *sql) final
Execute SQL command.
const char * ServerInfo() final
Return server info.
Int_t SelectDataBase(const char *dbname) final
Select a database. Returns 0 if successful, non-zero otherwise.
Bool_t HasStatement() const final
PG_VERSION_NUM conveniently only started being #defined at 8.2.3 which is the first version of libpq ...
TSQLTableInfo * GetTableInfo(const char *tablename) final
Produce TSQLTableInfo.
std::map< Int_t, std::string > fOidTypNameMap
Int_t Reload() final
Reload permission tables.
Int_t Shutdown() final
Shutdown the database server.
Int_t DropDataBase(const char *dbname) final
Drop (i.e.
TSQLResult * GetDataBases(const char *wild=nullptr) final
List all available databases.
void Close(Option_t *opt="") final
Close connection to PgSQL DB server.
TPgSQLServer(const char *db, const char *uid, const char *pw)
Open a connection to a PgSQL DB server.
TSQLStatement * Statement(const char *sql, Int_t=100) final
Produce TPgSQLStatement.
~TPgSQLServer()
Close connection to PgSQL DB server.
TSQLResult * GetColumns(const char *dbname, const char *table, const char *wild=nullptr) final
List all columns in specified table in the specified database.
virtual Bool_t IsConnected() const
Int_t Atoi() const
Return integer value of string.
const char * Data() const
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
This class represents a WWW compatible URL.
const char * GetFile() const
const char * GetHost() const
const char * GetProtocol() const