30 #if MYSQL_VERSION_ID >= 40100 46 ULong_t paramcount = mysql_stmt_param_count(fStmt);
50 SetBuffersNumber(paramcount);
70 mysql_stmt_close(fStmt);
79 #define CheckStmt(method, res) \ 83 SetError(-1,"Statement handle is 0",method); \ 89 #define CheckErrNo(method, force, res) \ 91 unsigned int stmterrno = mysql_stmt_errno(fStmt); \ 92 if ((stmterrno!=0) || force) { \ 93 const char* stmterrmsg = mysql_stmt_error(fStmt); \ 94 if (stmterrno==0) { stmterrno = 11111; stmterrmsg = "MySQL statement error"; } \ 95 SetError(stmterrno, stmterrmsg, method); \ 102 #define CheckGetField(method, res) \ 105 if (!IsResultSetMode()) { \ 106 SetError(-1,"Cannot get statement parameters",method); \ 109 if ((npar<0) || (npar>=fNumBuffers)) { \ 110 SetError(-1,Form("Invalid parameter number %d", npar),method); \ 123 if (IsSetParsMode()) {
124 if (fIterationCount>=0)
125 if (!NextIteration())
return kFALSE;
127 fIterationCount = -1;
132 if (mysql_stmt_execute(fStmt))
145 my_ulonglong res = mysql_stmt_affected_rows(fStmt);
147 if (res == (my_ulonglong) -1)
160 Int_t res = mysql_stmt_param_count(fStmt);
174 if (fWorkingMode!=0) {
175 SetError(-1,
"Cannot store result for that statement",
"StoreResult");
179 if (mysql_stmt_store_result(fStmt))
183 MYSQL_RES* meta = mysql_stmt_result_metadata(fStmt);
185 int count = mysql_num_fields(meta);
187 SetBuffersNumber(count);
189 MYSQL_FIELD *fields = mysql_fetch_fields(meta);
191 for (
int n=0;
n<count;
n++) {
192 SetSQLParamType(
n, fields[
n].
type, (fields[
n].flags & UNSIGNED_FLAG) == 0, fields[
n].length);
193 if (fields[
n].
name!=0) {
194 fBuffer[
n].fFieldName =
new char[strlen(fields[
n].
name)+1];
195 strcpy(fBuffer[
n].fFieldName, fields[
n].
name);
199 mysql_free_result(meta);
202 if (fBind==0)
return kFALSE;
205 if (mysql_stmt_bind_result(fStmt, fBind))
218 return IsResultSetMode() ? fNumBuffers : -1;
226 if (!IsResultSetMode() || (nfield<0) || (nfield>=fNumBuffers))
return 0;
228 return fBuffer[nfield].fFieldName;
236 if ((fStmt==0) || !IsResultSetMode())
return kFALSE;
238 Bool_t res = !mysql_stmt_fetch(fStmt);
257 if (!IsSetParsMode() || (fBind==0)) {
258 SetError(-1,
"Cannot call for that statement",
"NextIteration");
264 if (fIterationCount==0)
return kTRUE;
268 if (mysql_stmt_bind_param(fStmt, fBind))
272 if (mysql_stmt_execute(fStmt))
284 for (
Int_t n=0;
n<fNumBuffers;
n++) {
285 free(fBuffer[
n].fMem);
286 if (fBuffer[
n].fStrBuffer)
287 delete[] fBuffer[
n].fStrBuffer;
288 if (fBuffer[
n].fFieldName)
289 delete[] fBuffer[
n].fFieldName;
308 if (numpars<=0)
return;
310 fNumBuffers = numpars;
313 memset(fBind, 0,
sizeof(
MYSQL_BIND)*fNumBuffers);
315 fBuffer =
new TParamData[fNumBuffers];
316 memset(fBuffer, 0,
sizeof(TParamData)*fNumBuffers);
324 if (fBuffer[npar].fResNull)
return 0;
326 void* addr = fBuffer[npar].fMem;
327 Bool_t sig = fBuffer[npar].fSign;
329 if (addr==0)
return 0;
331 if ((fBind[npar].buffer_type==MYSQL_TYPE_STRING) ||
332 (fBind[npar].buffer_type==MYSQL_TYPE_VAR_STRING))
333 return (
const char*) addr;
335 if (fBuffer[npar].fStrBuffer==0)
336 fBuffer[npar].fStrBuffer =
new char[100];
338 char* buf = fBuffer[npar].fStrBuffer;
340 switch(fBind[npar].buffer_type) {
341 case MYSQL_TYPE_LONG:
342 if (sig)
snprintf(buf,100,
"%d",*((
int*) addr));
343 else snprintf(buf,100,
"%u",*((
unsigned int*) addr));
345 case MYSQL_TYPE_LONGLONG:
349 case MYSQL_TYPE_SHORT:
350 if (sig)
snprintf(buf,100,
"%hd",*((
short*) addr));
else 351 snprintf(buf,100,
"%hu",*((
unsigned short*) addr));
353 case MYSQL_TYPE_TINY:
354 if (sig)
snprintf(buf,100,
"%d",*((
char*) addr));
else 355 snprintf(buf,100,
"%u",*((
unsigned char*) addr));
357 case MYSQL_TYPE_FLOAT:
360 case MYSQL_TYPE_DOUBLE:
363 case MYSQL_TYPE_DATETIME:
364 case MYSQL_TYPE_TIMESTAMP: {
365 MYSQL_TIME* tm = (MYSQL_TIME*) addr;
366 snprintf(buf,100,
"%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
367 tm->year, tm->month, tm->day,
368 tm->hour, tm->minute, tm->second);
371 case MYSQL_TYPE_TIME: {
372 MYSQL_TIME* tm = (MYSQL_TIME*) addr;
373 snprintf(buf,100,
"%2.2d:%2.2d:%2.2d",
374 tm->hour, tm->minute, tm->second);
377 case MYSQL_TYPE_DATE: {
378 MYSQL_TIME* tm = (MYSQL_TIME*) addr;
379 snprintf(buf,100,
"%4.4d-%2.2d-%2.2d",
380 tm->year, tm->month, tm->day);
394 if (fBuffer[npar].fResNull)
return 0;
396 void* addr = fBuffer[npar].fMem;
397 Bool_t sig = fBuffer[npar].fSign;
399 if (addr==0)
return 0;
401 switch(fBind[npar].buffer_type) {
402 case MYSQL_TYPE_LONG:
403 if (sig)
return *((
int*) addr);
else 404 return *((
unsigned int*) addr);
406 case MYSQL_TYPE_LONGLONG:
407 if (sig)
return *((
Long64_t*) addr);
else 410 case MYSQL_TYPE_SHORT:
411 if (sig)
return *((
short*) addr);
else 412 return *((
unsigned short*) addr);
414 case MYSQL_TYPE_TINY:
415 if (sig)
return *((
char*) addr);
else 416 return *((
unsigned char*) addr);
418 case MYSQL_TYPE_FLOAT:
419 return *((
float*) addr);
421 case MYSQL_TYPE_DOUBLE:
422 return *((
double*) addr);
424 #if MYSQL_VERSION_ID >= 50022 425 case MYSQL_TYPE_NEWDECIMAL :
427 case MYSQL_TYPE_STRING:
428 case MYSQL_TYPE_VAR_STRING:
429 case MYSQL_TYPE_BLOB: {
430 char* str = (
char*) addr;
431 ULong_t len = fBuffer[npar].fResLength;
432 if ((str==0) || (*str==0) || (len==0))
return 0;
433 Int_t size = fBuffer[npar].fSize;
439 sscanf(str,
"%Lf",&buf);
443 case MYSQL_TYPE_DATETIME:
444 case MYSQL_TYPE_TIMESTAMP: {
445 MYSQL_TIME* tm = (MYSQL_TIME*) addr;
446 TDatime rtm(tm->year, tm->month, tm->day,
447 tm->hour, tm->minute, tm->second);
451 case MYSQL_TYPE_DATE: {
452 MYSQL_TIME* tm = (MYSQL_TIME*) addr;
453 TDatime rtm(tm->year, tm->month, tm->day, 0, 0, 0);
454 return rtm.GetDate();
457 case MYSQL_TYPE_TIME: {
458 MYSQL_TIME* tm = (MYSQL_TIME*) addr;
459 TDatime rtm(2000, 1, 1, tm->hour, tm->minute, tm->second);
460 return rtm.GetTime();
478 return fBuffer[npar].fResNull;
488 if ((fBuffer[npar].fSqlType==MYSQL_TYPE_LONG) && fBuffer[npar].fSign)
489 return (
Int_t) *((
int*) fBuffer[npar].fMem);
491 return (
Int_t) ConvertToNumeric(npar);
501 if ((fBuffer[npar].fSqlType==MYSQL_TYPE_LONG) && !fBuffer[npar].fSign)
502 return (
UInt_t) *((
unsigned int*) fBuffer[npar].fMem);
504 return (
UInt_t) ConvertToNumeric(npar);
514 if ((fBuffer[npar].fSqlType==MYSQL_TYPE_LONG) && fBuffer[npar].fSign)
515 return (
Long_t) *((
int*) fBuffer[npar].fMem);
517 return (
Long_t) ConvertToNumeric(npar);
527 if ((fBuffer[npar].fSqlType==MYSQL_TYPE_LONGLONG) && fBuffer[npar].fSign)
530 return (
Long64_t) ConvertToNumeric(npar);
540 if ((fBuffer[npar].fSqlType==MYSQL_TYPE_LONGLONG) && !fBuffer[npar].fSign)
543 return (
ULong64_t) ConvertToNumeric(npar);
553 if (fBuffer[npar].fSqlType==MYSQL_TYPE_DOUBLE)
554 return (
Double_t) *((
double*) fBuffer[npar].fMem);
556 return (
Double_t) ConvertToNumeric(npar);
566 if ((fBind[npar].buffer_type==MYSQL_TYPE_STRING)
567 || (fBind[npar].buffer_type==MYSQL_TYPE_BLOB)
568 || (fBind[npar].buffer_type==MYSQL_TYPE_VAR_STRING)
569 #
if MYSQL_VERSION_ID >= 50022
570 || (fBuffer[npar].fSqlType==MYSQL_TYPE_NEWDECIMAL)
573 if (fBuffer[npar].fResNull)
return 0;
574 char* str = (
char*) fBuffer[npar].fMem;
575 ULong_t len = fBuffer[npar].fResLength;
576 Int_t size = fBuffer[npar].fSize;
577 if (1.*len<size) str[len] = 0;
else 582 return ConvertToString(npar);
595 if ((fBind[npar].buffer_type==MYSQL_TYPE_STRING) ||
596 (fBind[npar].buffer_type==MYSQL_TYPE_VAR_STRING) ||
597 (fBind[npar].buffer_type==MYSQL_TYPE_BLOB) ||
598 (fBind[npar].buffer_type==MYSQL_TYPE_TINY_BLOB) ||
599 (fBind[npar].buffer_type==MYSQL_TYPE_MEDIUM_BLOB) ||
600 (fBind[npar].buffer_type==MYSQL_TYPE_LONG_BLOB)) {
601 if (fBuffer[npar].fResNull)
return kTRUE;
602 mem = fBuffer[npar].fMem;
603 size = fBuffer[npar].fResLength;
617 if (fBuffer[npar].fResNull)
return kFALSE;
619 switch(fBind[npar].buffer_type) {
620 case MYSQL_TYPE_DATETIME:
621 case MYSQL_TYPE_TIMESTAMP:
622 case MYSQL_TYPE_DATE: {
623 MYSQL_TIME* tm = (MYSQL_TIME*) fBuffer[npar].fMem;
643 if (fBuffer[npar].fResNull)
return kFALSE;
645 switch(fBind[npar].buffer_type) {
646 case MYSQL_TYPE_DATETIME:
647 case MYSQL_TYPE_TIMESTAMP:
648 case MYSQL_TYPE_TIME: {
649 MYSQL_TIME* tm = (MYSQL_TIME*) fBuffer[npar].fMem;
669 if (fBuffer[npar].fResNull)
return kFALSE;
671 switch(fBind[npar].buffer_type) {
672 case MYSQL_TYPE_DATETIME:
673 case MYSQL_TYPE_TIMESTAMP: {
674 MYSQL_TIME* tm = (MYSQL_TIME*) fBuffer[npar].fMem;
697 if (fBuffer[npar].fResNull)
return kFALSE;
699 switch(fBind[npar].buffer_type) {
700 case MYSQL_TYPE_DATETIME:
701 case MYSQL_TYPE_TIMESTAMP: {
702 MYSQL_TIME* tm = (MYSQL_TIME*) fBuffer[npar].fMem;
726 if ((npar<0) || (npar>=fNumBuffers))
return kFALSE;
728 fBuffer[npar].fMem = 0;
729 fBuffer[npar].fSize = 0;
730 fBuffer[npar].fResLength = 0;
731 fBuffer[npar].fResNull =
false;
732 fBuffer[npar].fStrBuffer = 0;
739 case MYSQL_TYPE_LONG: allocsize =
sizeof(int);
break;
740 case MYSQL_TYPE_LONGLONG: allocsize =
sizeof(
Long64_t);
break;
741 case MYSQL_TYPE_SHORT: allocsize =
sizeof(short);
break;
742 case MYSQL_TYPE_TINY: allocsize =
sizeof(char);
break;
743 case MYSQL_TYPE_FLOAT: allocsize =
sizeof(float);
break;
744 case MYSQL_TYPE_DOUBLE: allocsize =
sizeof(double);
break;
745 #if MYSQL_VERSION_ID >= 50022 746 case MYSQL_TYPE_NEWDECIMAL :
748 case MYSQL_TYPE_STRING: allocsize = sqlsize > 256 ? sqlsize : 256;
break;
749 case MYSQL_TYPE_VAR_STRING: allocsize = sqlsize > 256 ? sqlsize : 256;
break;
750 case MYSQL_TYPE_MEDIUM_BLOB:
751 case MYSQL_TYPE_LONG_BLOB:
752 case MYSQL_TYPE_BLOB: allocsize = sqlsize >= 65525 ? sqlsize : 65535;
break;
753 case MYSQL_TYPE_TINY_BLOB: allocsize = sqlsize > 255 ? sqlsize : 255;
break;
754 case MYSQL_TYPE_TIME:
755 case MYSQL_TYPE_DATE:
756 case MYSQL_TYPE_TIMESTAMP:
757 case MYSQL_TYPE_DATETIME: allocsize =
sizeof(MYSQL_TIME); doreset =
true;
break;
758 default:
SetError(-1,
"Nonsupported SQL type",
"SetSQLParamType");
return kFALSE;
761 if (allocsize > fgAllocSizeLimit) allocsize = fgAllocSizeLimit;
763 fBuffer[npar].fMem =
malloc(allocsize);
764 fBuffer[npar].fSize = allocsize;
765 fBuffer[npar].fSqlType = sqltype;
766 fBuffer[npar].fSign = sig;
768 if ((allocsize>0) && fBuffer[npar].fMem && doreset)
769 memset(fBuffer[npar].fMem, 0, allocsize);
771 fBind[npar].buffer_type = enum_field_types(sqltype);
772 fBind[npar].buffer = fBuffer[npar].fMem;
773 fBind[npar].buffer_length = allocsize;
774 fBind[npar].is_null= &(fBuffer[npar].fResNull);
775 fBind[npar].length = &(fBuffer[npar].fResLength);
776 fBind[npar].is_unsigned = !sig;
789 if (!IsSetParsMode()) {
790 SetError(-1,
"Cannot set parameter for statement", method);
794 if ((npar<0) || (npar>=fNumBuffers)) {
795 SetError(-1,
Form(
"Invalid parameter number %d",npar), method);
799 if ((fIterationCount==0) && (fBuffer[npar].fSqlType==0))
800 if (!SetSQLParamType(npar, sqltype, sig, size)) {
801 SetError(-1,
"Cannot initialize parameter buffer", method);
805 if ((fBuffer[npar].fSqlType!=sqltype) ||
806 (fBuffer[npar].fSign != sig))
return 0;
808 fBuffer[npar].fResNull =
false;
810 return fBuffer[npar].fMem;
824 void* addr = BeforeSet(
"SetNull", npar, MYSQL_TYPE_LONG);
829 if ((npar>=0) && (npar<fNumBuffers))
830 fBuffer[npar].fResNull =
true;
840 void* addr = BeforeSet(
"SetInt", npar, MYSQL_TYPE_LONG);
843 *((
int*) addr) = value;
853 void* addr = BeforeSet(
"SetUInt", npar, MYSQL_TYPE_LONG,
kFALSE);
856 *((
unsigned int*) addr) = value;
866 void* addr = BeforeSet(
"SetLong", npar, MYSQL_TYPE_LONG);
869 *((
int*) addr) = value;
879 void* addr = BeforeSet(
"SetLong64", npar, MYSQL_TYPE_LONGLONG);
892 void* addr = BeforeSet(
"SetULong64", npar, MYSQL_TYPE_LONGLONG,
kFALSE);
905 void* addr = BeforeSet(
"SetDouble", npar, MYSQL_TYPE_DOUBLE,
kFALSE);
908 *((
double*) addr) = value;
918 Int_t len = value ? strlen(value) : 0;
920 void* addr = BeforeSet(
"SetString", npar, MYSQL_TYPE_STRING,
true, maxsize);
922 if (addr==0)
return kFALSE;
924 if (len >= fBuffer[npar].fSize) {
925 free(fBuffer[npar].fMem);
927 fBuffer[npar].fMem =
malloc(len+1);
928 fBuffer[npar].fSize = len + 1;
930 fBind[npar].buffer = fBuffer[npar].fMem;
931 fBind[npar].buffer_length = fBuffer[npar].fSize;
933 addr = fBuffer[npar].fMem;
934 fNeedParBind =
kTRUE;
937 if (value) strcpy((
char*) addr, value);
938 else ((
char*)addr)[0]=
'\0';
940 fBuffer[npar].fResLength = len;
950 if (size>=maxsize) maxsize = size + 1;
952 int bin_type = MYSQL_TYPE_BLOB;
953 if (maxsize > 65525) bin_type = MYSQL_TYPE_MEDIUM_BLOB;
954 if (maxsize > 16777205) bin_type = MYSQL_TYPE_LONG_BLOB;
956 void* addr = BeforeSet(
"SetBinary", npar, bin_type,
true, maxsize);
958 if (addr==0)
return kFALSE;
960 if (size >= fBuffer[npar].fSize) {
961 free(fBuffer[npar].fMem);
963 fBuffer[npar].fMem =
malloc(size+1);
964 fBuffer[npar].fSize = size + 1;
966 fBind[npar].buffer = fBuffer[npar].fMem;
967 fBind[npar].buffer_length = fBuffer[npar].fSize;
969 addr = fBuffer[npar].fMem;
970 fNeedParBind =
kTRUE;
973 memcpy(addr, mem, size);
975 fBuffer[npar].fResLength = size;
985 MYSQL_TIME* addr = (MYSQL_TIME*) BeforeSet(
"SetDate", npar, MYSQL_TYPE_DATE);
1001 MYSQL_TIME* addr = (MYSQL_TIME*) BeforeSet(
"SetTime", npar, MYSQL_TYPE_TIME);
1017 MYSQL_TIME* addr = (MYSQL_TIME*) BeforeSet(
"SetDatime", npar, MYSQL_TYPE_DATETIME);
1021 addr->month = month;
1036 MYSQL_TIME* addr = (MYSQL_TIME*) BeforeSet(
"SetTimestamp", npar, MYSQL_TYPE_TIMESTAMP);
1040 addr->month = month;
1406 #endif // MYSQL_VERSION_ID > 40100 virtual Bool_t SetLong64(Int_t npar, Long64_t value)
Set parameter value as 64-bit integer.
virtual Int_t GetNumParameters()
Return number of statement parameters.
virtual Bool_t SetBinary(Int_t npar, void *mem, Long_t size, Long_t maxsize=0x1000)
Set parameter value as binary data.
const char * ConvertToString(Int_t npar)
Convert field value to string.
#define CheckStmt(method, res)
virtual ULong64_t GetULong64(Int_t npar)
Return field value as unsigned 64-bit integer.
virtual Int_t GetNumAffectedRows()
Return number of affected rows after statement is processed.
virtual Bool_t SetNull(Int_t npar)
Set NULL as parameter value.
virtual Bool_t SetLong(Int_t npar, Long_t value)
Set parameter value as long integer.
virtual Bool_t NextResultRow()
Shift cursor to nect row in result set.
virtual Bool_t SetTime(Int_t npar, Int_t hour, Int_t min, Int_t sec)
Set parameter value as time.
void Close(Option_t *opt="")
Close connection to MySQL DB server.
static const char * GetFloatFormat()
return current printf format for float/double members, default "%e"
void FreeBuffers()
Release all buffers, used by statement.
virtual Bool_t SetULong64(Int_t npar, ULong64_t value)
Set parameter value as unsigned 64-bit integer.
void ClearError()
reset error fields
virtual void Close(Option_t *="")
Close 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.
void SetBuffersNumber(Int_t n)
Allocate buffers for statement parameters/ result fields.
virtual Int_t GetInt(Int_t npar)
Return field value as integer.
virtual Bool_t GetDate(Int_t npar, Int_t &year, Int_t &month, Int_t &day)
Return field value as date.
virtual Bool_t SetString(Int_t npar, const char *value, Int_t maxsize=256)
Set parameter value as string.
#define CheckErrNo(method, force, res)
virtual Int_t GetNumFields()
Return number of fields in result set.
virtual Bool_t SetDouble(Int_t npar, Double_t value)
Set parameter value as double.
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.
char * Form(const char *fmt,...)
virtual Double_t GetDouble(Int_t npar)
Return field value as double.
TMySQLStatement(const TMySQLStatement &)
virtual Bool_t GetTime(Int_t npar, Int_t &hour, Int_t &min, Int_t &sec)
Return field value as time.
long double ConvertToNumeric(Int_t npar)
Convert field to numeric value.
virtual Bool_t SetUInt(Int_t npar, UInt_t value)
Set parameter value as unsigned integer.
unsigned long long ULong64_t
virtual Bool_t SetInt(Int_t npar, Int_t value)
Set parameter value as integer.
virtual Bool_t Process()
Process 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.
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
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.
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 Long_t GetLong(Int_t npar)
Return field value as long integer.
#define CheckGetField(method, defres)
virtual Bool_t SetDate(Int_t npar, Int_t year, Int_t month, Int_t day)
Set parameter value as date.
virtual const char * GetString(Int_t npar)
Return field value as string.
virtual Bool_t IsNull(Int_t npar)
Checks if field value is null.
virtual UInt_t GetUInt(Int_t npar)
Return field value as unsigned integer.
virtual Bool_t NextIteration()
Increment iteration counter for statement, where parameter can be set.
static ULong64_t fgAllocSizeLimit
void * BeforeSet(const char *method, Int_t npar, Int_t sqltype, Bool_t sig=kTRUE, ULong_t size=0)
Check boundary condition before setting value of parameter.
Bool_t SetSQLParamType(Int_t npar, int sqltype, Bool_t sig, ULong_t 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...
This class stores the date and time with a precision of one second in an unsigned 32 bit word (950130...
virtual ~TMySQLStatement()
Destructor.