31#include "libpq/libpq-fs.h"
33static 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);
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;
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");
549 month =
d.GetMonth();
552 Int_t min =
d.GetMinute();
553 Int_t sec =
d.GetSecond();
569 Int_t month =
d.GetMonth();
583 month =
d.GetMonth();
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);
648 month =
d.GetMonth();
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);
786 CheckErrResult(
"GetLargeObject", res,
kFALSE);
791 Error(
"SetLargeObject",
"Error in SetLargeObject: %s", PQerrorMessage(
fStmt->
fConn));
792 RollBackTransaction(
"GetLargeObject");
798 Error(
"SetLargeObject",
"Error in SetLargeObject: %s", PQerrorMessage(
fStmt->
fConn));
799 RollBackTransaction(
"GetLargeObject");
805 if (writtenBytes != size) {
806 Error(
"SetLargeObject",
"SQL Error on lo_write: %s", PQerrorMessage(
fStmt->
fConn));
807 RollBackTransaction(
"GetLargeObject");
812 Error(
"SetLargeObject",
"SQL Error on lo_close: %s", PQerrorMessage(
fStmt->
fConn));
813 RollBackTransaction(
"GetLargeObject");
818 ExecStatusType stat = PQresultStatus(res);
820 Error(
"SetLargeObject",
"SQL Error on COMMIT: %s", PQerrorMessage(
fStmt->
fConn));
837 snprintf(
fBind[npar],kBindStringSize,
"%s",(
char*)
d.AsSQLString());
848 snprintf(
fBind[npar],kBindStringSize,
"%s",(
char*)
d.AsSQLString());
858 snprintf(
fBind[npar],kBindStringSize,
"%s",(
char*)
d.AsSQLString());
870 TDatime d(year,month,day,hour,min,sec);
871 snprintf(
fBind[npar],kBindStringSize,
"%s.%06d",(
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)
Bool_t SetSQLParamType(Int_t npar, int sqltype, bool sig, int sqlsize=0)
Set parameter type to be used as buffer.
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.
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.