28#define pgsql_success(x) (((x) == PGRES_EMPTY_QUERY) \
29 || ((x) == PGRES_COMMAND_OK) \
30 || ((x) == PGRES_TUPLES_OK))
37#include "libpq/libpq-fs.h"
39static const Int_t kBindStringSize = 30;
53 fParamLengths(nullptr),
54 fParamFormats(nullptr),
59 if (fStmt->fRes !=
nullptr) {
63 fStmt->fRes = PQdescribePrepared(fStmt->fConn,
"preparedstmt");
64 unsigned long paramcount = PQnparams(fStmt->fRes);
65 fNumResultCols = PQnfields(fStmt->fRes);
70 SetBuffersNumber(paramcount);
73 SetBuffersNumber(fNumResultCols);
95 PGresult *res=PQexec(
fStmt->
fConn,
"DEALLOCATE preparedstmt;");
106#define CheckStmt(method, res) \
110 SetError(-1,"Statement handle is 0",method); \
115#define CheckErrNo(method, force, wtf) \
117 int stmterrno = PQresultStatus(fStmt->fRes); \
118 if ((stmterrno!=0) || force) { \
119 const char* stmterrmsg = PQresultErrorMessage(fStmt->fRes); \
120 if (stmterrno==0) { stmterrno = -1; stmterrmsg = "PgSQL statement error"; } \
121 SetError(stmterrno, stmterrmsg, method); \
126#define CheckErrResult(method, pqresult, retVal) \
128 ExecStatusType stmterrno=PQresultStatus(pqresult); \
129 if (!pgsql_success(stmterrno)) { \
130 const char* stmterrmsg = PQresultErrorMessage(fStmt->fRes); \
131 SetError(stmterrno, stmterrmsg, method); \
137#define RollBackTransaction(method) \
139 PGresult *resnum=PQexec(fStmt->fConn,"COMMIT"); \
140 CheckErrResult("RollBackTransaction", resnum, kFALSE); \
145#define CheckGetField(method, res) \
148 if (!IsResultSetMode()) { \
149 SetError(-1,"Cannot get statement parameters",method); \
152 if ((npar<0) || (npar>=fNumBuffers)) { \
153 SetError(-1,Form("Invalid parameter number %d", npar),method); \
173 (
const char*
const*)
fBind,
179 fStmt->
fRes= PQexecPrepared(
fStmt->
fConn,
"preparedstmt",0,(
const char*
const*)
nullptr,
nullptr,
nullptr,0);
181 ExecStatusType stat = PQresultStatus(
fStmt->
fRes);
225 ExecStatusType stat = PQresultStatus(
fStmt->
fRes);
279 SetError(-1,
"Cannot call for that statement",
"NextIteration");
288 (
const char*
const*)
fBind,
292 ExecStatusType stat = PQresultStatus(
fStmt->
fRes);
334 if (numpars<=0)
return;
340 fBind[i] =
new char[kBindStringSize];
366 return (
long double)0;
470 unsigned char * mptr = PQunescapeBytea((
const unsigned char*)cptr,&sz);
472 delete [] (
unsigned char*) mem;
473 mem = (
void*)
new unsigned char[sz];
491 CheckErrResult(
"GetLargeObject", res,
kFALSE);
497 Error(
"GetLargeObject",
"SQL Error on lo_open: %s", PQerrorMessage(
fStmt->
fConn));
498 RollBackTransaction(
"GetLargeObject");
516 delete [] (
unsigned char*) mem;
517 mem = (
void*)
new unsigned char[sz];
523 if (readBytes != sz) {
524 Error(
"GetLargeObject",
"SQL Error on lo_read: %s", PQerrorMessage(
fStmt->
fConn));
525 RollBackTransaction(
"GetLargeObject");
530 Error(
"GetLargeObject",
"SQL Error on lo_close: %s", PQerrorMessage(
fStmt->
fConn));
531 RollBackTransaction(
"GetLargeObject");
537 ExecStatusType stat = PQresultStatus(res);
539 Error(
"GetLargeObject",
"SQL Error on COMMIT: %s", PQerrorMessage(
fStmt->
fConn));
540 RollBackTransaction(
"GetLargeObject");
556 month =
d.GetMonth();
559 Int_t min =
d.GetMinute();
560 Int_t sec =
d.GetSecond();
576 Int_t month =
d.GetMonth();
590 month =
d.GetMonth();
609 if ((tzP !=
kNPOS) && (tzP > p) ) {
614 if ((tzM !=
kNPOS) && (tzM > p) ) {
619 if (hasZone ==
kTRUE) {
621 Int_t hourOffset, minuteOffset = 0;
622 Int_t conversions=sscanf(s_zone->
Data(),
"%2d:%2d", &hourOffset, &minuteOffset);
623 Int_t secondOffset = hourOffset*3600;
626 secondOffset += (
TMath::Sign(minuteOffset, hourOffset))*60;
629 TTimeStamp ts(year, month, day, hour, min, sec, 0,
kTRUE, -secondOffset);
630 UInt_t uyear, umonth, uday, uhour, umin, usec;
631 ts.GetDate(
kTRUE, 0, &uyear, &umonth, &uday);
632 ts.GetTime(
kTRUE, 0, &uhour, &umin, &usec);
655 month =
d.GetMonth();
681 Int_t year, month, day, hour, min, sec, microsec;
682 GetTimestamp(npar, year, month, day, hour, min, sec, microsec);
685 SetError(-1,
"Date before year 1970 does not supported by TTimeStamp type",
"GetTimestamp");
689 tm.
Set(year, month, day, hour, min, sec, microsec*1000,
kTRUE, 0);
704 fBind[npar] =
nullptr;
705 }
else if (maxsize > kBindStringSize) {
707 fBind[npar] =
new char[maxsize];
708 }
else if (!
fBind[npar]) {
709 fBind[npar] =
new char[kBindStringSize];
806 strlcpy(
fBind[npar], value, maxsize);
816 if (size > maxsize) maxsize = size;
820 memcpy(
fBind[npar], mem, size);
833 CheckErrResult(
"GetLargeObject", res,
kFALSE);
838 Error(
"SetLargeObject",
"Error in SetLargeObject: %s", PQerrorMessage(
fStmt->
fConn));
839 RollBackTransaction(
"GetLargeObject");
845 Error(
"SetLargeObject",
"Error in SetLargeObject: %s", PQerrorMessage(
fStmt->
fConn));
846 RollBackTransaction(
"GetLargeObject");
852 if (writtenBytes != size) {
853 Error(
"SetLargeObject",
"SQL Error on lo_write: %s", PQerrorMessage(
fStmt->
fConn));
854 RollBackTransaction(
"GetLargeObject");
859 Error(
"SetLargeObject",
"SQL Error on lo_close: %s", PQerrorMessage(
fStmt->
fConn));
860 RollBackTransaction(
"GetLargeObject");
865 ExecStatusType stat = PQresultStatus(res);
867 Error(
"SetLargeObject",
"SQL Error on COMMIT: %s", PQerrorMessage(
fStmt->
fConn));
886 snprintf(
fBind[npar],kBindStringSize,
"%s",(
char*)
d.AsSQLString());
899 snprintf(
fBind[npar],kBindStringSize,
"%s",(
char*)
d.AsSQLString());
910 TDatime d(year,month,day,hour,min,sec);
911 snprintf(
fBind[npar],kBindStringSize,
"%s+00",(
char*)
d.AsSQLString());
925 TDatime d(year,month,day,hour,min,sec);
926 snprintf(
fBind[npar],kBindStringSize,
"%s.%06d+00",(
char*)
d.AsSQLString(),frac);
unsigned long long ULong64_t
#define CheckErrNo(method, force, res)
#define CheckGetField(method, defres)
#define CheckStmt(method, res)
This class stores the date and time with a precision of one second in an unsigned 32 bit word (950130...
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Bool_t SetDatime(Int_t npar, Int_t year, Int_t month, Int_t day, Int_t hour, Int_t min, Int_t sec) final
Set parameter value as date & time.
void ConvertTimeToUTC(const TString &PQvalue, Int_t &year, Int_t &month, Int_t &day, Int_t &hour, Int_t &min, Int_t &sec)
void SetBuffersNumber(Int_t n)
Allocate buffers for statement parameters/ result fields.
Int_t fNumResultRows
data type (OID)
UInt_t GetUInt(Int_t npar) final
Return field value as unsigned integer.
Bool_t SetTime(Int_t npar, Int_t hour, Int_t min, Int_t sec) final
Set parameter value as time.
long double ConvertToNumeric(Int_t npar)
Convert field to numeric value.
Bool_t GetTime(Int_t npar, Int_t &hour, Int_t &min, Int_t &sec) final
Return field value as time.
Bool_t SetLargeObject(Int_t npar, void *mem, Long_t size, Long_t maxsize=0x1000) final
Set parameter value to large object and immediately insert the large object into DB.
Bool_t IsResultSetMode() const
TPgSQLStatement(PgSQL_Stmt_t *stmt, Bool_t errout=kTRUE)
Normal constructor.
int * fParamLengths
number of iteration
Bool_t GetDate(Int_t npar, Int_t &year, Int_t &month, Int_t &day) final
Return field value as date.
Int_t GetInt(Int_t npar) final
Return field value as integer.
Int_t GetNumParameters() final
Return number of statement parameters.
const char * GetString(Int_t npar) final
Return field value as string.
Bool_t SetDate(Int_t npar, Int_t year, Int_t month, Int_t day) final
Set parameter value as date.
Bool_t GetLargeObject(Int_t npar, void *&mem, Long_t &size) final
Return large object whose oid is in the given field.
Bool_t SetInt(Int_t npar, Int_t value) final
Set parameter value as integer.
ULong64_t GetULong64(Int_t npar) final
Return field value as unsigned 64-bit integer.
Long_t GetLong(Int_t npar) final
Return field value as long integer.
Bool_t SetSQLParamType(Int_t npar, Bool_t isbinary=kFALSE, Int_t param_len=0, Int_t maxsize=0)
Set parameter type to be used as buffer.
Int_t fIterationCount
1 - setting parameters, 2 - retrieving results
Int_t fNumBuffers
executed statement
Int_t fWorkingMode
array of column names
Bool_t GetBinary(Int_t npar, void *&mem, Long_t &size) final
Return field value as binary array.
Bool_t IsSetParsMode() const
Bool_t IsNull(Int_t npar) final
Checks if field value is null.
Int_t GetNumFields() final
Return number of fields in result set.
Double_t GetDouble(Int_t npar) final
Return field value as double.
Bool_t SetDouble(Int_t npar, Double_t value) final
Set parameter value as double.
Bool_t NextResultRow() final
Shift cursor to nect row in result set.
Long64_t GetLong64(Int_t npar) final
Return field value as 64-bit integer.
Bool_t GetDatime(Int_t npar, Int_t &year, Int_t &month, Int_t &day, Int_t &hour, Int_t &min, Int_t &sec) final
Return field value as date & time.
Bool_t SetTimestamp(Int_t npar, Int_t year, Int_t month, Int_t day, Int_t hour, Int_t min, Int_t sec, Int_t frac=0) final
Set parameter value as timestamp.
Int_t GetNumAffectedRows() final
Return number of affected rows after statement is processed.
Bool_t SetLong(Int_t npar, Long_t value) final
Set parameter value as long integer.
Bool_t StoreResult() final
Store result of statement processing to access them via GetInt(), GetDouble() and so on methods.
Bool_t SetUInt(Int_t npar, UInt_t value) final
Set parameter value as unsigned integer.
const char * GetFieldName(Int_t nfield) final
Returns field name in result set.
Bool_t SetNull(Int_t npar) final
Set NULL as parameter value.
const char * ConvertToString(Int_t npar)
Convert field value to string.
Bool_t SetULong64(Int_t npar, ULong64_t value) final
Set parameter value as unsigned 64-bit integer.
char ** fBind
number of statement parameters
void FreeBuffers()
Release all buffers, used by statement.
Bool_t SetLong64(Int_t npar, Long64_t value) final
Set parameter value as 64-bit integer.
virtual ~TPgSQLStatement()
Destructor.
Bool_t SetString(Int_t npar, const char *value, Int_t maxsize=256) final
Set parameter value as string.
Bool_t SetBinary(Int_t npar, void *mem, Long_t size, Long_t maxsize=0x1000) final
Set parameter value as binary data.
Bool_t Process() final
Process statement.
int * fParamFormats
length of column
char ** fFieldName
array of data for input
Bool_t GetTimestamp(Int_t npar, Int_t &year, Int_t &month, Int_t &day, Int_t &hour, Int_t &min, Int_t &sec, Int_t &) final
Return field value as time stamp.
Bool_t NextIteration() final
Increment iteration counter for statement, where parameter can be set.
void Close(Option_t *="") final
Close statement.
void SetError(Int_t code, const char *msg, const char *method=nullptr)
set new values for error fields if method specified, displays error message
void ClearError()
reset error fields
const char * Data() const
Ssiz_t Last(char c) const
Find last occurrence of a character c.
A zero length substring is legal.
const char * Data() const
The TTimeStamp encapsulates seconds and ns since EPOCH.
void Set()
Set Date/Time to current time as reported by the system.
const char * AsString(const Option_t *option="") const
Return the date & time as a string.
Int_t Nint(T x)
Round to nearest integer. Rounds half integers to the nearest even integer.