32#define pgsql_success(x) (((x) == PGRES_EMPTY_QUERY) \
33 || ((x) == PGRES_COMMAND_OK) \
34 || ((x) == PGRES_TUPLES_OK))
41#include "libpq/libpq-fs.h"
43static const Int_t kBindStringSize = 30;
57 fParamLengths(nullptr),
58 fParamFormats(nullptr),
63 if (fStmt->fRes !=
nullptr) {
67 fStmt->fRes = PQdescribePrepared(fStmt->fConn,
"preparedstmt");
68 unsigned long paramcount = PQnparams(fStmt->fRes);
69 fNumResultCols = PQnfields(fStmt->fRes);
74 SetBuffersNumber(paramcount);
77 SetBuffersNumber(fNumResultCols);
99 PGresult *res=PQexec(
fStmt->
fConn,
"DEALLOCATE preparedstmt;");
110#define CheckStmt(method, res) \
114 SetError(-1,"Statement handle is 0",method); \
119#define CheckErrNo(method, force, wtf) \
121 int stmterrno = PQresultStatus(fStmt->fRes); \
122 if ((stmterrno!=0) || force) { \
123 const char* stmterrmsg = PQresultErrorMessage(fStmt->fRes); \
124 if (stmterrno==0) { stmterrno = -1; stmterrmsg = "PgSQL statement error"; } \
125 SetError(stmterrno, stmterrmsg, method); \
130#define CheckErrResult(method, pqresult, retVal) \
132 ExecStatusType stmterrno=PQresultStatus(pqresult); \
133 if (!pgsql_success(stmterrno)) { \
134 const char* stmterrmsg = PQresultErrorMessage(fStmt->fRes); \
135 SetError(stmterrno, stmterrmsg, method); \
141#define RollBackTransaction(method) \
143 PGresult *resnum=PQexec(fStmt->fConn,"COMMIT"); \
144 CheckErrResult("RollBackTransaction", resnum, kFALSE); \
149#define CheckGetField(method, res) \
152 if (!IsResultSetMode()) { \
153 SetError(-1,"Cannot get statement parameters",method); \
156 if ((npar<0) || (npar>=fNumBuffers)) { \
157 SetError(-1,Form("Invalid parameter number %d", npar),method); \
177 (
const char*
const*)
fBind,
183 fStmt->
fRes= PQexecPrepared(
fStmt->
fConn,
"preparedstmt",0,(
const char*
const*)
nullptr,
nullptr,
nullptr,0);
185 ExecStatusType stat = PQresultStatus(
fStmt->
fRes);
229 ExecStatusType stat = PQresultStatus(
fStmt->
fRes);
283 SetError(-1,
"Cannot call for that statement",
"NextIteration");
292 (
const char*
const*)
fBind,
296 ExecStatusType stat = PQresultStatus(
fStmt->
fRes);
338 if (numpars<=0)
return;
344 fBind[i] =
new char[kBindStringSize];
370 return (
long double)0;
474 unsigned char * mptr = PQunescapeBytea((
const unsigned char*)cptr,&sz);
476 delete [] (
unsigned char*) mem;
477 mem = (
void*)
new unsigned char[sz];
495 CheckErrResult(
"GetLargeObject", res,
kFALSE);
501 Error(
"GetLargeObject",
"SQL Error on lo_open: %s", PQerrorMessage(
fStmt->
fConn));
502 RollBackTransaction(
"GetLargeObject");
520 delete [] (
unsigned char*) mem;
521 mem = (
void*)
new unsigned char[sz];
527 if (readBytes != sz) {
528 Error(
"GetLargeObject",
"SQL Error on lo_read: %s", PQerrorMessage(
fStmt->
fConn));
529 RollBackTransaction(
"GetLargeObject");
534 Error(
"GetLargeObject",
"SQL Error on lo_close: %s", PQerrorMessage(
fStmt->
fConn));
535 RollBackTransaction(
"GetLargeObject");
541 ExecStatusType stat = PQresultStatus(res);
543 Error(
"GetLargeObject",
"SQL Error on COMMIT: %s", PQerrorMessage(
fStmt->
fConn));
544 RollBackTransaction(
"GetLargeObject");
560 month =
d.GetMonth();
563 Int_t min =
d.GetMinute();
564 Int_t sec =
d.GetSecond();
580 Int_t month =
d.GetMonth();
594 month =
d.GetMonth();
613 if ((tzP !=
kNPOS) && (tzP > p) ) {
618 if ((tzM !=
kNPOS) && (tzM > p) ) {
623 if (hasZone ==
kTRUE) {
625 Int_t hourOffset, minuteOffset = 0;
626 Int_t conversions=sscanf(s_zone->
Data(),
"%2d:%2d", &hourOffset, &minuteOffset);
627 Int_t secondOffset = hourOffset*3600;
630 secondOffset += (
TMath::Sign(minuteOffset, hourOffset))*60;
633 TTimeStamp ts(year, month, day, hour, min, sec, 0,
kTRUE, -secondOffset);
634 UInt_t uyear, umonth, uday, uhour, umin, usec;
635 ts.GetDate(
kTRUE, 0, &uyear, &umonth, &uday);
636 ts.GetTime(
kTRUE, 0, &uhour, &umin, &usec);
659 month =
d.GetMonth();
685 Int_t year, month, day, hour, min, sec, microsec;
686 GetTimestamp(npar, year, month, day, hour, min, sec, microsec);
689 SetError(-1,
"Date before year 1970 does not supported by TTimeStamp type",
"GetTimestamp");
693 tm.
Set(year, month, day, hour, min, sec, microsec*1000,
kTRUE, 0);
708 fBind[npar] =
nullptr;
709 }
else if (maxsize > kBindStringSize) {
711 fBind[npar] =
new char[maxsize];
712 }
else if (!
fBind[npar]) {
713 fBind[npar] =
new char[kBindStringSize];
810 if (
fBind[npar] && value)
811 strlcpy(
fBind[npar], value, (maxsize > kBindStringSize) ? maxsize : kBindStringSize);
825 if (
fBind[npar] && mem)
839 CheckErrResult(
"GetLargeObject", res,
kFALSE);
844 Error(
"SetLargeObject",
"Error in SetLargeObject: %s", PQerrorMessage(
fStmt->
fConn));
845 RollBackTransaction(
"GetLargeObject");
851 Error(
"SetLargeObject",
"Error in SetLargeObject: %s", PQerrorMessage(
fStmt->
fConn));
852 RollBackTransaction(
"GetLargeObject");
858 if (writtenBytes !=
size) {
859 Error(
"SetLargeObject",
"SQL Error on lo_write: %s", PQerrorMessage(
fStmt->
fConn));
860 RollBackTransaction(
"GetLargeObject");
865 Error(
"SetLargeObject",
"SQL Error on lo_close: %s", PQerrorMessage(
fStmt->
fConn));
866 RollBackTransaction(
"GetLargeObject");
871 ExecStatusType stat = PQresultStatus(res);
873 Error(
"SetLargeObject",
"SQL Error on COMMIT: %s", PQerrorMessage(
fStmt->
fConn));
892 snprintf(
fBind[npar],kBindStringSize,
"%s",(
char*)
d.AsSQLString());
905 snprintf(
fBind[npar],kBindStringSize,
"%s",(
char*)
d.AsSQLString());
916 TDatime d(year,month,day,hour,min,sec);
917 snprintf(
fBind[npar],kBindStringSize,
"%s+00",(
char*)
d.AsSQLString());
931 TDatime d(year,month,day,hour,min,sec);
932 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
#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.
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.
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.