32#include "libpq/libpq-fs.h"
34static const Int_t kBindStringSize = 30;
48 fParamLengths(nullptr),
49 fParamFormats(nullptr),
54 if (fStmt->fRes !=
nullptr) {
58 fStmt->fRes = PQdescribePrepared(fStmt->fConn,
"preparedstmt");
59 unsigned long paramcount = PQnparams(fStmt->fRes);
60 fNumResultCols = PQnfields(fStmt->fRes);
65 SetBuffersNumber(paramcount);
68 SetBuffersNumber(fNumResultCols);
90 PGresult *res=PQexec(
fStmt->
fConn,
"DEALLOCATE preparedstmt;");
101#define CheckStmt(method, res) \
105 SetError(-1,"Statement handle is 0",method); \
110#define CheckErrNo(method, force, wtf) \
112 int stmterrno = PQresultStatus(fStmt->fRes); \
113 if ((stmterrno!=0) || force) { \
114 const char* stmterrmsg = PQresultErrorMessage(fStmt->fRes); \
115 if (stmterrno==0) { stmterrno = -1; stmterrmsg = "PgSQL statement error"; } \
116 SetError(stmterrno, stmterrmsg, method); \
121#define CheckErrResult(method, pqresult, retVal) \
123 ExecStatusType stmterrno=PQresultStatus(pqresult); \
124 if (!pgsql_success(stmterrno)) { \
125 const char* stmterrmsg = PQresultErrorMessage(fStmt->fRes); \
126 SetError(stmterrno, stmterrmsg, method); \
132#define RollBackTransaction(method) \
134 PGresult *resnum=PQexec(fStmt->fConn,"COMMIT"); \
135 CheckErrResult("RollBackTransaction", resnum, kFALSE); \
140#define CheckGetField(method, res) \
143 if (!IsResultSetMode()) { \
144 SetError(-1,"Cannot get statement parameters",method); \
147 if ((npar<0) || (npar>=fNumBuffers)) { \
148 SetError(-1,Form("Invalid parameter number %d", npar),method); \
168 (
const char*
const*)
fBind,
174 fStmt->
fRes= PQexecPrepared(
fStmt->
fConn,
"preparedstmt",0,(
const char*
const*)
nullptr,
nullptr,
nullptr,0);
176 ExecStatusType stat = PQresultStatus(
fStmt->
fRes);
220 ExecStatusType stat = PQresultStatus(
fStmt->
fRes);
274 SetError(-1,
"Cannot call for that statement",
"NextIteration");
283 (
const char*
const*)
fBind,
287 ExecStatusType stat = PQresultStatus(
fStmt->
fRes);
329 if (numpars<=0)
return;
335 fBind[i] =
new char[kBindStringSize];
361 return (
long double)0;
465 unsigned char * mptr = PQunescapeBytea((
const unsigned char*)cptr,&sz);
467 delete [] (
unsigned char*) mem;
468 mem = (
void*)
new unsigned char[sz];
486 CheckErrResult(
"GetLargeObject", res,
kFALSE);
492 Error(
"GetLargeObject",
"SQL Error on lo_open: %s", PQerrorMessage(
fStmt->
fConn));
493 RollBackTransaction(
"GetLargeObject");
511 delete [] (
unsigned char*) mem;
512 mem = (
void*)
new unsigned char[sz];
518 if (readBytes != sz) {
519 Error(
"GetLargeObject",
"SQL Error on lo_read: %s", PQerrorMessage(
fStmt->
fConn));
520 RollBackTransaction(
"GetLargeObject");
525 Error(
"GetLargeObject",
"SQL Error on lo_close: %s", PQerrorMessage(
fStmt->
fConn));
526 RollBackTransaction(
"GetLargeObject");
532 ExecStatusType stat = PQresultStatus(res);
534 Error(
"GetLargeObject",
"SQL Error on COMMIT: %s", PQerrorMessage(
fStmt->
fConn));
535 RollBackTransaction(
"GetLargeObject");
551 month =
d.GetMonth();
554 Int_t min =
d.GetMinute();
555 Int_t sec =
d.GetSecond();
571 Int_t month =
d.GetMonth();
585 month =
d.GetMonth();
604 if ((tzP !=
kNPOS) && (tzP > p) ) {
609 if ((tzM !=
kNPOS) && (tzM > p) ) {
614 if (hasZone ==
kTRUE) {
616 Int_t hourOffset, minuteOffset = 0;
617 Int_t conversions=sscanf(s_zone->
Data(),
"%2d:%2d", &hourOffset, &minuteOffset);
618 Int_t secondOffset = hourOffset*3600;
621 secondOffset += (
TMath::Sign(minuteOffset, hourOffset))*60;
624 TTimeStamp ts(year, month, day, hour, min, sec, 0,
kTRUE, -secondOffset);
625 UInt_t uyear, umonth, uday, uhour, umin, usec;
626 ts.GetDate(
kTRUE, 0, &uyear, &umonth, &uday);
627 ts.GetTime(
kTRUE, 0, &uhour, &umin, &usec);
650 month =
d.GetMonth();
676 Int_t year, month, day, hour, min, sec, microsec;
677 GetTimestamp(npar, year, month, day, hour, min, sec, microsec);
680 SetError(-1,
"Date before year 1970 does not supported by TTimeStamp type",
"GetTimestamp");
684 tm.
Set(year, month, day, hour, min, sec, microsec*1000,
kTRUE, 0);
699 fBind[npar] =
nullptr;
700 }
else if (maxsize > kBindStringSize) {
702 fBind[npar] =
new char[maxsize];
703 }
else if (!
fBind[npar]) {
704 fBind[npar] =
new char[kBindStringSize];
801 strlcpy(
fBind[npar], value, maxsize);
811 if (size > maxsize) maxsize = size;
815 memcpy(
fBind[npar], mem, size);
828 CheckErrResult(
"GetLargeObject", res,
kFALSE);
833 Error(
"SetLargeObject",
"Error in SetLargeObject: %s", PQerrorMessage(
fStmt->
fConn));
834 RollBackTransaction(
"GetLargeObject");
840 Error(
"SetLargeObject",
"Error in SetLargeObject: %s", PQerrorMessage(
fStmt->
fConn));
841 RollBackTransaction(
"GetLargeObject");
847 if (writtenBytes != size) {
848 Error(
"SetLargeObject",
"SQL Error on lo_write: %s", PQerrorMessage(
fStmt->
fConn));
849 RollBackTransaction(
"GetLargeObject");
854 Error(
"SetLargeObject",
"SQL Error on lo_close: %s", PQerrorMessage(
fStmt->
fConn));
855 RollBackTransaction(
"GetLargeObject");
860 ExecStatusType stat = PQresultStatus(res);
862 Error(
"SetLargeObject",
"SQL Error on COMMIT: %s", PQerrorMessage(
fStmt->
fConn));
881 snprintf(
fBind[npar],kBindStringSize,
"%s",(
char*)
d.AsSQLString());
894 snprintf(
fBind[npar],kBindStringSize,
"%s",(
char*)
d.AsSQLString());
905 TDatime d(year,month,day,hour,min,sec);
906 snprintf(
fBind[npar],kBindStringSize,
"%s+00",(
char*)
d.AsSQLString());
920 TDatime d(year,month,day,hour,min,sec);
921 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.
virtual Bool_t SetDatime(Int_t npar, Int_t year, Int_t month, Int_t day, Int_t hour, Int_t min, Int_t sec)
Set parameter value as date & time.
virtual UInt_t GetUInt(Int_t npar)
Return field value as unsigned integer.
virtual Int_t GetNumParameters()
Return number of statement parameters.
virtual ULong64_t GetULong64(Int_t npar)
Return field value as unsigned 64-bit integer.
virtual 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 &)
Return field value as time stamp.
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.
virtual Bool_t GetDatime(Int_t npar, Int_t &year, Int_t &month, Int_t &day, Int_t &hour, Int_t &min, Int_t &sec)
Return field value as date & time.
Int_t fNumResultRows
data type (OID)
virtual Bool_t SetBinary(Int_t npar, void *mem, Long_t size, Long_t maxsize=0x1000)
Set parameter value as binary data.
virtual Bool_t SetLargeObject(Int_t npar, void *mem, Long_t size, Long_t maxsize=0x1000)
Set parameter value to large object and immediately insert the large object into DB.
virtual Bool_t SetDate(Int_t npar, Int_t year, Int_t month, Int_t day)
Set parameter value as date.
virtual Bool_t NextIteration()
Increment iteration counter for statement, where parameter can be set.
virtual Bool_t GetLargeObject(Int_t npar, void *&mem, Long_t &size)
Return large object whose oid is in the given field.
long double ConvertToNumeric(Int_t npar)
Convert field to numeric value.
Bool_t IsResultSetMode() const
TPgSQLStatement(PgSQL_Stmt_t *stmt, Bool_t errout=kTRUE)
Normal constructor.
int * fParamLengths
number of iteration
virtual Int_t GetNumFields()
Return number of fields in result set.
virtual Bool_t SetInt(Int_t npar, Int_t value)
Set parameter value as integer.
virtual Bool_t StoreResult()
Store result of statement processing to access them via GetInt(), GetDouble() and so on methods.
virtual Bool_t NextResultRow()
Shift cursor to nect row in result set.
virtual Bool_t SetString(Int_t npar, const char *value, Int_t maxsize=256)
Set parameter value as string.
virtual Bool_t SetLong(Int_t npar, Long_t value)
Set parameter value as long integer.
virtual Bool_t SetTime(Int_t npar, Int_t hour, Int_t min, Int_t sec)
Set parameter value as time.
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
virtual Int_t GetNumAffectedRows()
Return number of affected rows after statement is processed.
virtual Bool_t SetUInt(Int_t npar, UInt_t value)
Set parameter value as unsigned integer.
virtual Bool_t GetDate(Int_t npar, Int_t &year, Int_t &month, Int_t &day)
Return field value as date.
virtual Int_t GetInt(Int_t npar)
Return field value as integer.
virtual Bool_t SetLong64(Int_t npar, Long64_t value)
Set parameter value as 64-bit integer.
Int_t fNumBuffers
executed statement
Int_t fWorkingMode
array of column names
Bool_t IsSetParsMode() const
virtual Bool_t GetTime(Int_t npar, Int_t &hour, Int_t &min, Int_t &sec)
Return field value as time.
virtual Double_t GetDouble(Int_t npar)
Return field value as double.
virtual Long64_t GetLong64(Int_t npar)
Return field value as 64-bit integer.
virtual const char * GetString(Int_t npar)
Return field value as string.
virtual Bool_t SetULong64(Int_t npar, ULong64_t value)
Set parameter value as unsigned 64-bit integer.
virtual Bool_t Process()
Process statement.
virtual void Close(Option_t *="")
Close statement.
virtual Bool_t GetBinary(Int_t npar, void *&mem, Long_t &size)
Return field value as binary array.
virtual const char * GetFieldName(Int_t nfield)
Returns field name in result set.
virtual 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)
Set parameter value as timestamp.
virtual Bool_t SetNull(Int_t npar)
Set NULL as parameter value.
const char * ConvertToString(Int_t npar)
Convert field value to string.
char ** fBind
number of statement parameters
void FreeBuffers()
Release all buffers, used by statement.
virtual ~TPgSQLStatement()
Destructor.
virtual Bool_t SetDouble(Int_t npar, Double_t value)
Set parameter value as double.
int * fParamFormats
length of column
char ** fFieldName
array of data for input
virtual Bool_t IsNull(Int_t npar)
Checks if field value is null.
virtual Long_t GetLong(Int_t npar)
Return field value as long integer.
void SetError(Int_t code, const char *msg, const char *method=0)
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.