31 #include "libpq/libpq-fs.h" 33 static const Int_t kBindStringSize = 25;
53 if (fStmt->fRes !=
NULL) {
57 fStmt->fRes = PQdescribePrepared(fStmt->fConn,
"preparedstmt");
58 unsigned long paramcount = PQnparams(fStmt->fRes);
59 fNumResultCols = PQnfields(fStmt->fRes);
64 SetBuffersNumber(paramcount);
67 SetBuffersNumber(fNumResultCols);
89 PGresult *res=PQexec(fStmt->fConn,
"DEALLOCATE preparedstmt;");
100 #define CheckStmt(method, res) \ 104 SetError(-1,"Statement handle is 0",method); \ 109 #define CheckErrNo(method, force, wtf) \ 111 int stmterrno = PQresultStatus(fStmt->fRes); \ 112 if ((stmterrno!=0) || force) { \ 113 const char* stmterrmsg = PQresultErrorMessage(fStmt->fRes); \ 114 if (stmterrno==0) { stmterrno = -1; stmterrmsg = "PgSQL statement error"; } \ 115 SetError(stmterrno, stmterrmsg, method); \ 120 #define CheckErrResult(method, pqresult, retVal) \ 122 ExecStatusType stmterrno=PQresultStatus(pqresult); \ 123 if (!pgsql_success(stmterrno)) { \ 124 const char* stmterrmsg = PQresultErrorMessage(fStmt->fRes); \ 125 SetError(stmterrno, stmterrmsg, method); \ 131 #define RollBackTransaction(method) \ 133 PGresult *resnum=PQexec(fStmt->fConn,"COMMIT"); \ 134 CheckErrResult("RollBackTransaction", resnum, kFALSE); \ 139 #define CheckGetField(method, res) \ 142 if (!IsResultSetMode()) { \ 143 SetError(-1,"Cannot get statement parameters",method); \ 146 if ((npar<0) || (npar>=fNumBuffers)) { \ 147 SetError(-1,Form("Invalid parameter number %d", npar),method); \ 161 if (fStmt->fRes !=
NULL) {
162 PQclear(fStmt->fRes);
165 if (IsSetParsMode()) {
166 fStmt->fRes= PQexecPrepared(fStmt->fConn,
"preparedstmt",fNumBuffers,
167 (
const char*
const*)fBind,
172 fStmt->fRes= PQexecPrepared(fStmt->fConn,
"preparedstmt",0,(
const char*
const*) 0,0,0,0);
174 ExecStatusType stat = PQresultStatus(fStmt->fRes);
187 return (
Int_t) atoi(PQcmdTuples(fStmt->fRes));
197 if (IsSetParsMode()) {
211 for (i=0;i<fNumResultCols;i++){
212 fFieldName[i] = PQfname(fStmt->fRes,i);
213 fParamFormats[i]=PQftype(fStmt->fRes,i);
214 fParamLengths[i]=PQfsize(fStmt->fRes,i);
217 fNumResultRows=PQntuples(fStmt->fRes);
218 ExecStatusType stat = PQresultStatus(fStmt->fRes);
233 return fNumResultCols;
242 if (!IsResultSetMode() || (nfield<0) || (nfield>=fNumBuffers))
return 0;
244 return fFieldName[nfield];
252 if ((fStmt==0) || !IsResultSetMode())
return kFALSE;
257 if (fIterationCount>=fNumResultRows)
271 if (!IsSetParsMode() || (fBind==0)) {
272 SetError(-1,
"Cannot call for that statement",
"NextIteration");
278 if (fIterationCount==0)
return kTRUE;
280 fStmt->fRes= PQexecPrepared(fStmt->fConn,
"preparedstmt",fNumBuffers,
281 (
const char*
const*)fBind,
285 ExecStatusType stat = PQresultStatus(fStmt->fRes);
303 for (
Int_t i=0;i<fNumBuffers;i++)
309 delete [] fParamLengths;
312 delete [] fParamFormats;
327 if (numpars<=0)
return;
329 fNumBuffers = numpars;
331 fBind =
new char*[fNumBuffers];
332 for(
int i=0; i<fNumBuffers; ++i){
333 fBind[i] =
new char[kBindStringSize];
335 fFieldName =
new char*[fNumBuffers];
337 fParamLengths =
new int[fNumBuffers];
338 memset(fParamLengths, 0,
sizeof(
int)*fNumBuffers);
340 fParamFormats =
new int[fNumBuffers];
341 memset(fParamFormats, 0,
sizeof(
int)*fNumBuffers);
349 const char *buf = PQgetvalue(fStmt->fRes, fIterationCount, npar);
358 if (PQgetisnull(fStmt->fRes,fIterationCount,npar))
359 return (
long double)0;
361 return (
long double) atof(PQgetvalue(fStmt->fRes,fIterationCount,npar));
371 return PQgetisnull(fStmt->fRes,fIterationCount,npar);
379 if (PQgetisnull(fStmt->fRes,fIterationCount,npar))
382 return (
Int_t) atoi(PQgetvalue(fStmt->fRes,fIterationCount,npar));
390 if (PQgetisnull(fStmt->fRes,fIterationCount,npar))
393 return (
UInt_t) atoi(PQgetvalue(fStmt->fRes,fIterationCount,npar));
401 if (PQgetisnull(fStmt->fRes,fIterationCount,npar))
404 return (
Long_t) atol(PQgetvalue(fStmt->fRes,fIterationCount,npar));
412 if (PQgetisnull(fStmt->fRes,fIterationCount,npar))
416 return (
Long64_t) atoll(PQgetvalue(fStmt->fRes,fIterationCount,npar));
418 return (
Long64_t) _atoi64(PQgetvalue(fStmt->fRes,fIterationCount,npar));
427 if (PQgetisnull(fStmt->fRes,fIterationCount,npar))
431 return (
ULong64_t) atoll(PQgetvalue(fStmt->fRes,fIterationCount,npar));
433 return (
ULong64_t) _atoi64(PQgetvalue(fStmt->fRes,fIterationCount,npar));
442 if (PQgetisnull(fStmt->fRes,fIterationCount,npar))
444 return (
Double_t) atof(PQgetvalue(fStmt->fRes,fIterationCount,npar));
452 return PQgetvalue(fStmt->fRes,fIterationCount,npar);
462 char *cptr = PQgetvalue(fStmt->fRes,fIterationCount,npar);
463 unsigned char * mptr = PQunescapeBytea((
const unsigned char*)cptr,&sz);
465 delete [] (
unsigned char*) mem;
466 mem = (
void*)
new unsigned char[sz];
479 Int_t objID = atoi(PQgetvalue(fStmt->fRes,fIterationCount,npar));
482 PGresult *res=PQexec(fStmt->fConn,
"BEGIN");
484 CheckErrResult(
"GetLargeObject", res,
kFALSE);
487 Int_t lObjFD = lo_open(fStmt->fConn, objID, INV_READ);
490 Error(
"GetLargeObject",
"SQL Error on lo_open: %s", PQerrorMessage(fStmt->fConn));
491 RollBackTransaction(
"GetLargeObject");
504 lo_lseek(fStmt->fConn, lObjFD, 0, SEEK_END);
505 Long_t sz = lo_tell(fStmt->fConn, lObjFD);
506 lo_lseek(fStmt->fConn, lObjFD, 0, SEEK_SET);
509 delete [] (
unsigned char*) mem;
510 mem = (
void*)
new unsigned char[sz];
514 Int_t readBytes = lo_read(fStmt->fConn, lObjFD, (
char*)mem, size);
516 if (readBytes != sz) {
517 Error(
"GetLargeObject",
"SQL Error on lo_read: %s", PQerrorMessage(fStmt->fConn));
518 RollBackTransaction(
"GetLargeObject");
522 if (lo_close(fStmt->fConn, lObjFD) != 0) {
523 Error(
"GetLargeObject",
"SQL Error on lo_close: %s", PQerrorMessage(fStmt->fConn));
524 RollBackTransaction(
"GetLargeObject");
528 res=PQexec(fStmt->fConn,
"COMMIT");
530 ExecStatusType stat = PQresultStatus(res);
532 Error(
"GetLargeObject",
"SQL Error on COMMIT: %s", PQerrorMessage(fStmt->fConn));
533 RollBackTransaction(
"GetLargeObject");
546 TString val=PQgetvalue(fStmt->fRes,fIterationCount,npar);
554 ConvertTimeToUTC(val, year, month, day, hour, min, sec);
563 TString val=PQgetvalue(fStmt->fRes,fIterationCount,npar);
571 ConvertTimeToUTC(val, year, month, day, hour, min, sec);
580 TString val=PQgetvalue(fStmt->fRes,fIterationCount,npar);
588 ConvertTimeToUTC(val, year, month, day, hour, min, sec);
602 if ((tzP !=
kNPOS) && (tzP > p) ) {
607 if ((tzM !=
kNPOS) && (tzM > p) ) {
612 if (hasZone ==
kTRUE) {
614 Int_t hourOffset, minuteOffset = 0;
615 Int_t conversions=sscanf(s_zone->
Data(),
"%2d:%2d", &hourOffset, &minuteOffset);
616 Int_t secondOffset = hourOffset*3600;
619 secondOffset += (
TMath::Sign(minuteOffset, hourOffset))*60;
622 TTimeStamp ts(year, month, day, hour, min, sec, 0,
kTRUE, -secondOffset);
623 UInt_t uyear, umonth, uday, uhour, umin, usec;
624 ts.GetDate(
kTRUE, 0, &uyear, &umonth, &uday);
625 ts.GetTime(
kTRUE, 0, &uhour, &umin, &usec);
645 TString val=PQgetvalue(fStmt->fRes,fIterationCount,npar);
654 ConvertTimeToUTC(val, year, month, day, hour, min, sec);
689 snprintf(fBind[npar],kBindStringSize,
"%d",value);
699 snprintf(fBind[npar],kBindStringSize,
"%u",value);
709 snprintf(fBind[npar],kBindStringSize,
"%ld",value);
739 snprintf(fBind[npar],kBindStringSize,
"%lf",value);
749 if(
sizeof(fBind[npar])<(
unsigned)maxsize){
750 delete [] fBind[npar];
751 fBind[npar] =
new char[
maxsize];
753 strlcpy(fBind[npar],value,maxsize);
764 size_t sz = size, mxsz =
maxsize;
765 unsigned char* escape_ptr = PQescapeBytea((
const unsigned char*)mem, sz, &mxsz);
766 unsigned char* binary_ptr = PQunescapeBytea((
const unsigned char*)escape_ptr, &mxsz);
767 PQfreemem(escape_ptr);
769 delete [] fBind[npar];
770 fBind[npar] =
new char[mxsz+1];
771 fBind[npar][mxsz] =
'\0';
772 memcpy(fBind[npar], binary_ptr, mxsz);
774 PQfreemem(binary_ptr);
784 PGresult *res=PQexec(fStmt->fConn,
"BEGIN");
786 CheckErrResult(
"GetLargeObject", res,
kFALSE);
789 Int_t lObjID = lo_creat(fStmt->fConn, INV_READ | INV_WRITE);
791 Error(
"SetLargeObject",
"Error in SetLargeObject: %s", PQerrorMessage(fStmt->fConn));
792 RollBackTransaction(
"GetLargeObject");
796 Int_t lObjFD = lo_open(fStmt->fConn, lObjID, INV_READ | INV_WRITE);
798 Error(
"SetLargeObject",
"Error in SetLargeObject: %s", PQerrorMessage(fStmt->fConn));
799 RollBackTransaction(
"GetLargeObject");
803 Int_t writtenBytes = lo_write(fStmt->fConn, lObjFD, (
char*)mem, size);
805 if (writtenBytes != size) {
806 Error(
"SetLargeObject",
"SQL Error on lo_write: %s", PQerrorMessage(fStmt->fConn));
807 RollBackTransaction(
"GetLargeObject");
811 if (lo_close(fStmt->fConn, lObjFD) != 0) {
812 Error(
"SetLargeObject",
"SQL Error on lo_close: %s", PQerrorMessage(fStmt->fConn));
813 RollBackTransaction(
"GetLargeObject");
817 res=PQexec(fStmt->fConn,
"COMMIT");
818 ExecStatusType stat = PQresultStatus(res);
820 Error(
"SetLargeObject",
"SQL Error on COMMIT: %s", PQerrorMessage(fStmt->fConn));
826 snprintf(fBind[npar],kBindStringSize,
"%d",lObjID);
870 TDatime d(year,month,day,hour,min,sec);
1130 if ((npar >= 0) && (npar < fNumBuffers))
1239 #endif //PG_VERSION_NUM A zero length substring is legal.
Bool_t SetSQLParamType(Int_t npar, int sqltype, bool sig, int sqlsize=0)
Set parameter type to be used as buffer.
virtual Bool_t StoreResult()
Store result of statement processing to access them via GetInt(), GetDouble() and so on methods...
const char * ConvertToString(Int_t npar)
Convert field value to string.
virtual Int_t GetNumAffectedRows()
Return number of affected rows after statement is processed.
virtual const char * GetString(Int_t npar)
Return field value as string.
virtual Bool_t SetDouble(Int_t npar, Double_t value)
Set parameter value as double.
#define CheckStmt(method, res)
virtual Int_t GetInt(Int_t npar)
Return field value as integer.
virtual Bool_t SetNull(Int_t npar)
Set NULL as parameter value.
virtual Bool_t NextResultRow()
Shift cursor to nect row in result set.
virtual Bool_t GetTime(Int_t npar, Int_t &hour, Int_t &min, Int_t &sec)
Return field value as time.
virtual UInt_t GetUInt(Int_t npar)
Return field value as unsigned integer.
virtual Bool_t GetLargeObject(Int_t npar, void *&mem, Long_t &size)
Return large object whose oid is in the given field.
void ConvertTimeToUTC(const TString &PQvalue, Int_t &year, Int_t &month, Int_t &day, Int_t &hour, Int_t &min, Int_t &sec)
const char * Data() const
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 SetString(Int_t npar, const char *value, Int_t maxsize=256)
Set parameter value as string.
virtual Bool_t GetBinary(Int_t npar, void *&mem, Long_t &size)
Return field value as binary array.
virtual Bool_t IsNull(Int_t npar)
Checks if field value is null.
virtual void Close(Option_t *="")
Close statement.
void ClearError()
reset error fields
long double ConvertToNumeric(Int_t npar)
Convert field to numeric value.
virtual Int_t GetNumFields()
Return number of fields in result set.
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 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 ULong64_t GetULong64(Int_t npar)
Return field value as unsigned 64-bit integer.
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.
#define CheckErrNo(method, force, res)
virtual Double_t GetDouble(Int_t npar)
Return field value as double.
virtual Bool_t SetInt(Int_t npar, Int_t value)
Set parameter value as integer.
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 SetLong(Int_t npar, Long_t value)
Set parameter value as long integer.
const char * AsSQLString() const
Return the date & time in SQL compatible string format, like: 1997-01-15 20:16:28.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
void Close(Option_t *opt="")
Close connection to PgSQL DB server.
virtual const char * GetFieldName(Int_t nfield)
Returns field name in result set.
void FreeBuffers()
Release all buffers, used by statement.
Ssiz_t Last(char c) const
Find last occurrence of a character c.
virtual Long_t GetLong(Int_t npar)
Return field value as long integer.
unsigned long long ULong64_t
virtual Long64_t GetLong64(Int_t npar)
Return field value as 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.
The TTimeStamp encapsulates seconds and ns since EPOCH.
virtual Int_t GetNumParameters()
Return number of statement parameters.
virtual Bool_t SetUInt(Int_t npar, UInt_t value)
Set parameter value as unsigned integer.
void SetError(Int_t code, const char *msg, const char *method=0)
set new values for error fields if method is specified, displays error message
#define CheckGetField(method, defres)
virtual Bool_t NextIteration()
Increment iteration counter for statement, where parameter can be set.
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 SetTime(Int_t npar, Int_t hour, Int_t min, Int_t sec)
Set parameter value as time.
void SetBuffersNumber(Int_t n)
Allocate buffers for statement parameters/ result fields.
virtual ~TPgSQLStatement()
Destructor.
TPgSQLStatement(PgSQL_Stmt_t *stmt, Bool_t errout=kTRUE)
Normal constructor.
virtual Bool_t GetDate(Int_t npar, Int_t &year, Int_t &month, Int_t &day)
Return field value as date.
virtual Bool_t SetDate(Int_t npar, Int_t year, Int_t month, Int_t day)
Set parameter value as date.
virtual Bool_t SetLong64(Int_t npar, Long64_t value)
Set parameter value as 64-bit integer.
This class stores the date and time with a precision of one second in an unsigned 32 bit word (950130...
const char * Data() const