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); \
167 (
const char*
const*)
fBind,
174 ExecStatusType stat = PQresultStatus(
fStmt->
fRes);
218 ExecStatusType stat = PQresultStatus(
fStmt->
fRes);
242 if (!
IsResultSetMode() || (nfield<0) || (nfield>=fNumBuffers))
return 0;
272 SetError(-1,
"Cannot call for that statement",
"NextIteration");
281 (
const char*
const*)
fBind,
285 ExecStatusType stat = PQresultStatus(
fStmt->
fRes);
327 if (numpars<=0)
return;
329 fNumBuffers = numpars;
333 fBind[i] =
new char[kBindStringSize];
359 return (
long double)0;
463 unsigned char * mptr = PQunescapeBytea((
const unsigned char*)cptr,&sz);
465 delete [] (
unsigned char*) mem;
466 mem = (
void*)
new unsigned char[sz];
484 CheckErrResult(
"GetLargeObject", res,
kFALSE);
490 Error(
"GetLargeObject",
"SQL Error on lo_open: %s", PQerrorMessage(
fStmt->
fConn));
491 RollBackTransaction(
"GetLargeObject");
509 delete [] (
unsigned char*) mem;
510 mem = (
void*)
new unsigned char[sz];
516 if (readBytes != sz) {
517 Error(
"GetLargeObject",
"SQL Error on lo_read: %s", PQerrorMessage(
fStmt->
fConn));
518 RollBackTransaction(
"GetLargeObject");
523 Error(
"GetLargeObject",
"SQL Error on lo_close: %s", PQerrorMessage(
fStmt->
fConn));
524 RollBackTransaction(
"GetLargeObject");
530 ExecStatusType stat = PQresultStatus(res);
532 Error(
"GetLargeObject",
"SQL Error on COMMIT: %s", PQerrorMessage(
fStmt->
fConn));
533 RollBackTransaction(
"GetLargeObject");
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);
689 snprintf(
fBind[npar],kBindStringSize,
"%d",value);
699 snprintf(
fBind[npar],kBindStringSize,
"%u",value);
709 snprintf(
fBind[npar],kBindStringSize,
"%ld",value);
719 snprintf(
fBind[npar],kBindStringSize,
"%lld",(
Long64_t)value);
739 snprintf(
fBind[npar],kBindStringSize,
"%lf",value);
749 if(
sizeof(
fBind[npar])<(
unsigned)maxsize){
750 delete []
fBind[npar];
753 strlcpy(
fBind[npar],value,maxsize);
764 char* mptr = (
char*)
malloc(2*sz+1);
765 mxsz=PQescapeString (mptr,(
char*)mem,sz);
767 delete []
fBind[npar];
768 fBind[npar]=
new char[mxsz+1];
769 memcpy(
fBind[npar],mptr,mxsz);
782 CheckErrResult(
"GetLargeObject", res,
kFALSE);
787 Error(
"SetLargeObject",
"Error in SetLargeObject: %s", PQerrorMessage(
fStmt->
fConn));
788 RollBackTransaction(
"GetLargeObject");
794 Error(
"SetLargeObject",
"Error in SetLargeObject: %s", PQerrorMessage(
fStmt->
fConn));
795 RollBackTransaction(
"GetLargeObject");
801 if (writtenBytes != size) {
802 Error(
"SetLargeObject",
"SQL Error on lo_write: %s", PQerrorMessage(
fStmt->
fConn));
803 RollBackTransaction(
"GetLargeObject");
808 Error(
"SetLargeObject",
"SQL Error on lo_close: %s", PQerrorMessage(
fStmt->
fConn));
809 RollBackTransaction(
"GetLargeObject");
814 ExecStatusType stat = PQresultStatus(res);
816 Error(
"SetLargeObject",
"SQL Error on COMMIT: %s", PQerrorMessage(
fStmt->
fConn));
822 snprintf(
fBind[npar],kBindStringSize,
"%d",lObjID);
866 TDatime d(year,month,day,hour,min,sec);
867 snprintf(
fBind[npar],kBindStringSize,
"%s.%06d",(
char*)d.
AsSQLString(),frac);
1233 #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...
Int_t fWorkingMode
array of column names
Ssiz_t Last(char c) const
Find last occurrence of a character c.
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.
int * fParamFormats
length of column
#define CheckStmt(method, res)
void ClearError()
reset error fields
virtual Int_t GetInt(Int_t npar)
Return field value as integer.
virtual Bool_t SetNull(Int_t npar)
Set NULL as parameter value.
void SetError(Int_t code, const char *msg, const char *method=0)
set new values for error fields if method specified, displays error message
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.
Int_t fIterationCount
1 - setting parameters, 2 - retrieving results
char ** fBind
number of statement parameters
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)
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.
const char * Data() const
Bool_t IsSetParsMode() const
virtual Bool_t IsNull(Int_t npar)
Checks if field value is null.
virtual void Close(Option_t *="")
Close statement.
char ** fFieldName
array of data for input
Vc_ALWAYS_INLINE void free(T *p)
Frees memory that was allocated with Vc::malloc.
long double ConvertToNumeric(Int_t npar)
Convert field to numeric value.
virtual Int_t GetNumFields()
Return number of fields in result set.
Int_t fNumBuffers
executed statement
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 void Error(const char *method, const char *msgfmt,...) const
Issue error message.
virtual ULong64_t GetULong64(Int_t npar)
Return field value as unsigned 64-bit integer.
ClassImp(TPgSQLStatement) TPgSQLStatement
Normal constructor.
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.
int * fParamLengths
number of iteration
virtual const char * GetFieldName(Int_t nfield)
Returns field name in result set.
void FreeBuffers()
Release all buffers, used by statement.
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.
#define CheckGetField(method, defres)
Bool_t IsResultSetMode() const
virtual Bool_t NextIteration()
Increment iteration counter for statement, where parameter can be set.
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.
const char * Data() const
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.
Vc_ALWAYS_INLINE_L T *Vc_ALWAYS_INLINE_R malloc(size_t n)
Allocates memory on the Heap with alignment and padding suitable for vectorized access.
TPgSQLStatement(PgSQL_Stmt_t *stmt, Bool_t errout=kTRUE)
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.
const char * AsSQLString() const
Return the date & time in SQL compatible string format, like: 1997-01-15 20:16:28.
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...