24#define pgsql_success(x) (((x) == PGRES_EMPTY_QUERY) \
25 || ((x) == PGRES_COMMAND_OK) \
26 || ((x) == PGRES_TUPLES_OK))
53 Error(
"TPgSQLServer",
"malformed db argument %s", db);
59 Error(
"TPgSQLServer",
"protocol in db argument should be pgsql it is %s",
65 const char *dbase = url.
GetFile();
70 fPgSQL = PQsetdbLogin(url.
GetHost(), port, 0, 0, dbase, uid, pw);
72 fPgSQL = PQsetdbLogin(url.
GetHost(), 0, 0, 0, dbase, uid, pw);
75 if (PQstatus(
fPgSQL) != CONNECTION_BAD) {
83 static const char *sql =
"select setting from pg_settings where name='server_version'";
84 PGresult *res = PQexec(
fPgSQL, sql);
85 int stat = PQresultStatus(res);
86 if (stat == PGRES_TUPLES_OK && PQntuples(res)) {
87 char *vers = PQgetvalue(res,0,0);
91 fSrvInfo +=
"unknown version number";
94 Error(
"TPgSQLServer",
"connection to %s failed", url.
GetHost());
128 Error(
"Query",
"not connected");
132 PGresult *res = PQexec(
fPgSQL, sql);
135 if ((PQresultStatus(res) != PGRES_COMMAND_OK) &&
136 (PQresultStatus(res) != PGRES_TUPLES_OK)) {
137 Error(
"Query",
"%s",PQresultErrorMessage(res));
156 Error(
"SelectDataBase",
"not connected");
170 opts.
Data(), 0, dbname,
173 if (PQstatus(
fPgSQL) == CONNECTION_OK) {
177 Error(
"SelectDataBase",
"%s",PQerrorMessage(
fPgSQL));
193 Error(
"GetDataBases",
"not connected");
197 TString sql =
"SELECT pg_database.datname FROM pg_database";
199 sql +=
Form(
" WHERE pg_database.datname LIKE '%s'", wild);
213 Error(
"GetTables",
"not connected");
218 Error(
"GetTables",
"no such database %s", dbname);
222 TString sql =
"SELECT relname FROM pg_class where relkind='r'";
224 sql +=
Form(
" AND relname LIKE '%s'", wild);
239 Error(
"GetColumns",
"not connected");
244 Error(
"GetColumns",
"no such database %s", dbname);
250 sql =
Form(
"select a.attname,t.typname,a.attnotnull \
251 from pg_attribute a, pg_class c, pg_type t \
252 where c.oid=a.attrelid and c.relname='%s' and \
253 a.atttypid=t.oid and a.attnum>0 \
254 and a.attname like '%s' order by a.attnum ", table,wild);
256 sql =
Form(
"select a.attname,t.typname,a.attnotnull \
257 from pg_attribute a, pg_class c, pg_type t \
258 where c.oid=a.attrelid and c.relname='%s' and \
259 a.atttypid=t.oid and a.attnum>0 order by a.attnum",table);
270 Error(
"CreateDataBase",
"not connected");
274 sql =
Form(
"CREATE DATABASE %s", dbname);
275 PGresult *res = PQexec(
fPgSQL, sql);
287 Error(
"DropDataBase",
"not connected");
291 sql =
Form(
"DROP DATABASE %s", dbname);
292 PGresult *res = PQexec(
fPgSQL, sql);
304 Error(
"Reload",
"not connected");
308 Error(
"Reload",
"not implemented");
319 Error(
"Shutdown",
"not connected");
323 Error(
"Shutdown",
"not implemented");
333 Error(
"ServerInfo",
"not connected");
365 SetError(-1,
"no query string specified",
"Statement");
371 SetError(-1,
"cannot allocate PgSQL_Stmt_t",
"Statement");
374 stmt->
fConn = fPgSQL;
375 stmt->
fRes = PQprepare(fPgSQL,
"preparedstmt", sql, 0, (
const Oid*)0);
377 ExecStatusType stat = PQresultStatus(stmt->
fRes);
382 SetError(stat, PQresultErrorMessage(stmt->
fRes),
"Statement");
388 Error(
"Statement",
"not implemented for pgsql < 8.2");
399 Error(
"GetColumns",
"not connected");
404 if ((tablename==0) || (*tablename==0))
return 0;
408 if ((PQresultStatus(res) != PGRES_COMMAND_OK) &&
409 (PQresultStatus(res) != PGRES_TUPLES_OK)) {
410 Error(
"Query",
"%s",PQresultErrorMessage(res));
418 PGresult *res_type = PQexec(
fPgSQL,
"SELECT OID, TYPNAME FROM PG_TYPE;");
420 if ((PQresultStatus(res_type) != PGRES_COMMAND_OK) &&
421 (PQresultStatus(res_type) != PGRES_TUPLES_OK)) {
422 Error(
"Query",
"%s", PQresultErrorMessage(res_type));
428 Int_t nOids = PQntuples(res_type);
429 for (
Int_t oid=0; oid<nOids; oid++) {
431 char* oidString = PQgetvalue(res_type, oid, 0);
432 char* typeString = PQgetvalue(res_type, oid, 1);
433 if (sscanf(oidString,
"%10d", &tOid) != 1) {
434 Error(
"GetTableInfo",
"Bad non-numeric oid '%s' for type '%s'", oidString, typeString);
443 Int_t nfields = PQnfields(res);
445 for (
Int_t col=0;col<nfields;col++){
447 Int_t data_size = -1;
448 Int_t data_length = -1;
449 Int_t data_scale = -1;
450 Int_t data_sign = -1;
453 const char* column_name = PQfname(res,col);
454 const char* type_name;
455 int imod = PQfmod(res,col);
458 int oid_code = PQftype(res,col);
461 std::map<Int_t,std::string>::iterator lookupOid =
fOidTypNameMap.find(oid_code);
466 type_name =
"NUMERIC";
468 }
else if (lookupOid->second ==
"int2"){
472 }
else if (lookupOid->second ==
"int4"){
476 }
else if (lookupOid->second ==
"int8"){
480 }
else if (lookupOid->second ==
"float4"){
484 }
else if (lookupOid->second ==
"float8"){
486 type_name =
"DOUBLE";
488 }
else if (lookupOid->second ==
"bool"){
492 }
else if (lookupOid->second ==
"char"){
496 }
else if (lookupOid->second ==
"varchar"){
498 type_name =
"VARCHAR";
500 }
else if (lookupOid->second ==
"text"){
502 type_name =
"VARCHAR";
504 }
else if (lookupOid->second ==
"name"){
506 type_name =
"VARCHAR";
508 }
else if (lookupOid->second ==
"date"){
510 type_name =
"TIMESTAMP";
512 }
else if (lookupOid->second ==
"time"){
514 type_name =
"TIMESTAMP";
516 }
else if (lookupOid->second ==
"timetz"){
518 type_name =
"TIMESTAMP";
520 }
else if (lookupOid->second ==
"timestamp"){
522 type_name =
"TIMESTAMP";
524 }
else if (lookupOid->second ==
"timestamptz"){
526 type_name =
"TIMESTAMP";
528 }
else if (lookupOid->second ==
"interval"){
530 type_name =
"TIMESTAMP";
532 }
else if (lookupOid->second ==
"bytea"){
534 type_name =
"BINARY";
536 }
else if (lookupOid->second ==
""){
538 type_name =
"UNKNOWN";
543 type_name =
"NUMERIC";
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
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