32#define pgsql_success(x) (((x) == PGRES_EMPTY_QUERY) \
33 || ((x) == PGRES_COMMAND_OK) \
34 || ((x) == PGRES_TUPLES_OK))
42#include "libpq/libpq-fs.h"
44static const Int_t kBindStringSize = 30;
58 fParamLengths(nullptr),
59 fParamFormats(nullptr),
64 if (fStmt->fRes !=
nullptr) {
68 fStmt->fRes = PQdescribePrepared(fStmt->fConn,
"preparedstmt");
69 unsigned long paramcount = PQnparams(fStmt->fRes);
70 fNumResultCols = PQnfields(fStmt->fRes);
75 SetBuffersNumber(paramcount);
78 SetBuffersNumber(fNumResultCols);
98 fStmt->fRes =
nullptr;
100 PGresult *res=PQexec(
fStmt->fConn,
"DEALLOCATE preparedstmt;");
105 fStmt->fConn =
nullptr;
111#define CheckStmt(method, res) \
115 SetError(-1,"Statement handle is 0",method); \
120#define CheckErrNo(method, force, wtf) \
122 int stmterrno = PQresultStatus(fStmt->fRes); \
123 if ((stmterrno!=0) || force) { \
124 const char* stmterrmsg = PQresultErrorMessage(fStmt->fRes); \
125 if (stmterrno==0) { stmterrno = -1; stmterrmsg = "PgSQL statement error"; } \
126 SetError(stmterrno, stmterrmsg, method); \
131#define CheckErrResult(method, pqresult, retVal) \
133 ExecStatusType stmterrno=PQresultStatus(pqresult); \
134 if (!pgsql_success(stmterrno)) { \
135 const char* stmterrmsg = PQresultErrorMessage(fStmt->fRes); \
136 SetError(stmterrno, stmterrmsg, method); \
142#define RollBackTransaction(method) \
144 PGresult *resnum=PQexec(fStmt->fConn,"COMMIT"); \
145 CheckErrResult("RollBackTransaction", resnum, kFALSE); \
150#define CheckGetField(method, res) \
153 if (!IsResultSetMode()) { \
154 SetError(-1,"Cannot get statement parameters",method); \
157 if ((npar<0) || (npar>=fNumBuffers)) { \
158 SetError(-1,Form("Invalid parameter number %d", npar),method); \
172 if (
fStmt->fRes != NULL) {
173 PQclear(
fStmt->fRes);
178 (
const char*
const*)
fBind,
184 fStmt->fRes= PQexecPrepared(
fStmt->fConn,
"preparedstmt",0,(
const char*
const*)
nullptr,
nullptr,
nullptr,0);
186 ExecStatusType stat = PQresultStatus(
fStmt->fRes);
199 return (
Int_t) atoi(PQcmdTuples(
fStmt->fRes));
230 ExecStatusType stat = PQresultStatus(
fStmt->fRes);
284 SetError(-1,
"Cannot call for that statement",
"NextIteration");
293 (
const char*
const*)
fBind,
297 ExecStatusType stat = PQresultStatus(
fStmt->fRes);
339 if (numpars<=0)
return;
345 fBind[
i] =
new char[kBindStringSize];
371 return (
long double)0;
475 unsigned char * mptr = PQunescapeBytea((
const unsigned char*)cptr,&sz);
477 delete [] (
unsigned char*) mem;
478 mem = (
void*)
new unsigned char[sz];
496 CheckErrResult(
"GetLargeObject", res,
kFALSE);
499 Int_t lObjFD = lo_open(
fStmt->fConn, objID, INV_READ);
502 Error(
"GetLargeObject",
"SQL Error on lo_open: %s", PQerrorMessage(
fStmt->fConn));
503 RollBackTransaction(
"GetLargeObject");
516 lo_lseek(
fStmt->fConn, lObjFD, 0, SEEK_END);
518 lo_lseek(
fStmt->fConn, lObjFD, 0, SEEK_SET);
521 delete [] (
unsigned char*) mem;
522 mem = (
void*)
new unsigned char[sz];
526 Int_t readBytes = lo_read(
fStmt->fConn, lObjFD, (
char*)mem,
size);
528 if (readBytes != sz) {
529 Error(
"GetLargeObject",
"SQL Error on lo_read: %s", PQerrorMessage(
fStmt->fConn));
530 RollBackTransaction(
"GetLargeObject");
534 if (lo_close(
fStmt->fConn, lObjFD) != 0) {
535 Error(
"GetLargeObject",
"SQL Error on lo_close: %s", PQerrorMessage(
fStmt->fConn));
536 RollBackTransaction(
"GetLargeObject");
540 res=PQexec(
fStmt->fConn,
"COMMIT");
542 ExecStatusType stat = PQresultStatus(res);
544 Error(
"GetLargeObject",
"SQL Error on COMMIT: %s", PQerrorMessage(
fStmt->fConn));
545 RollBackTransaction(
"GetLargeObject");
559 TDatime
d = TDatime(val.
Data());
561 month =
d.GetMonth();
564 Int_t min =
d.GetMinute();
565 Int_t sec =
d.GetSecond();
576 TDatime
d = TDatime(val.
Data());
581 Int_t month =
d.GetMonth();
593 TDatime
d = TDatime(val.
Data());
595 month =
d.GetMonth();
611 TSubString *s_zone =
nullptr;
614 if ((tzP !=
kNPOS) && (tzP > p) ) {
615 s_zone =
new TSubString(PQvalue(tzP+1,PQvalue.
Length()-tzP));
619 if ((tzM !=
kNPOS) && (tzM > p) ) {
620 s_zone =
new TSubString(PQvalue(tzM+1,PQvalue.
Length()-tzM));
624 if (hasZone ==
kTRUE) {
626 Int_t hourOffset, minuteOffset = 0;
627 Int_t conversions=sscanf(s_zone->
Data(),
"%2d:%2d", &hourOffset, &minuteOffset);
628 Int_t secondOffset = hourOffset*3600;
631 secondOffset += (
TMath::Sign(minuteOffset, hourOffset))*60;
634 TTimeStamp ts(year, month, day, hour, min, sec, 0,
kTRUE, -secondOffset);
635 UInt_t uyear, umonth, uday, uhour, umin, usec;
636 ts.GetDate(
kTRUE, 0, &uyear, &umonth, &uday);
637 ts.GetTime(
kTRUE, 0, &uhour, &umin, &usec);
658 TDatime
d(val.
Data());
660 month =
d.GetMonth();
669 TSubString s_frac = val(p,val.
Length()-p+1);
686 Int_t year, month, day, hour, min, sec, microsec;
687 GetTimestamp(npar, year, month, day, hour, min, sec, microsec);
690 SetError(-1,
"Date before year 1970 does not supported by TTimeStamp type",
"GetTimestamp");
694 tm.
Set(year, month, day, hour, min, sec, microsec*1000,
kTRUE, 0);
709 fBind[npar] =
nullptr;
710 }
else if (maxsize > kBindStringSize) {
712 fBind[npar] =
new char[maxsize];
713 }
else if (!
fBind[npar]) {
714 fBind[npar] =
new char[kBindStringSize];
812 strlcpy(
fBind[npar],
value, (maxsize > kBindStringSize) ? maxsize : kBindStringSize);
826 if (
fBind[npar] && mem)
840 CheckErrResult(
"GetLargeObject", res,
kFALSE);
843 Int_t lObjID = lo_creat(
fStmt->fConn, INV_READ | INV_WRITE);
845 Error(
"SetLargeObject",
"Error in SetLargeObject: %s", PQerrorMessage(
fStmt->fConn));
846 RollBackTransaction(
"GetLargeObject");
850 Int_t lObjFD = lo_open(
fStmt->fConn, lObjID, INV_READ | INV_WRITE);
852 Error(
"SetLargeObject",
"Error in SetLargeObject: %s", PQerrorMessage(
fStmt->fConn));
853 RollBackTransaction(
"GetLargeObject");
857 Int_t writtenBytes = lo_write(
fStmt->fConn, lObjFD, (
char*)mem,
size);
859 if (writtenBytes !=
size) {
860 Error(
"SetLargeObject",
"SQL Error on lo_write: %s", PQerrorMessage(
fStmt->fConn));
861 RollBackTransaction(
"GetLargeObject");
865 if (lo_close(
fStmt->fConn, lObjFD) != 0) {
866 Error(
"SetLargeObject",
"SQL Error on lo_close: %s", PQerrorMessage(
fStmt->fConn));
867 RollBackTransaction(
"GetLargeObject");
871 res=PQexec(
fStmt->fConn,
"COMMIT");
872 ExecStatusType stat = PQresultStatus(res);
874 Error(
"SetLargeObject",
"SQL Error on COMMIT: %s", PQerrorMessage(
fStmt->fConn));
892 TDatime
d(year,month,day,0,0,0);
893 snprintf(
fBind[npar],kBindStringSize,
"%s",(
char*)
d.AsSQLString());
905 TDatime
d(2000,1,1,hour,min,sec);
906 snprintf(
fBind[npar],kBindStringSize,
"%s",(
char*)
d.AsSQLString());
917 TDatime
d(year,month,day,hour,min,sec);
918 snprintf(
fBind[npar],kBindStringSize,
"%s+00",(
char*)
d.AsSQLString());
932 TDatime
d(year,month,day,hour,min,sec);
933 snprintf(
fBind[npar],kBindStringSize,
"%s.%06d+00",(
char*)
d.AsSQLString(),frac);
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
unsigned long long ULong64_t
if(isa< VarDecl >(D)||isa< FieldDecl >(D)||isa< EnumConstantDecl >(D))
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
#define CheckErrNo(method, force, res)
struct pg_result PGresult
#define CheckGetField(method, res)
#define CheckStmt(method, res)
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.
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.
T1 Sign(T1 a, T2 b)
Returns a value with the magnitude of a and the sign of b.