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();
476 return fBuffer[npar].fResNull;
486 if ((fBuffer[npar].fSqlType==MYSQL_TYPE_LONG) && fBuffer[npar].fSign)
487 return (
Int_t) *((
int*) fBuffer[npar].fMem);
489 return (
Int_t) ConvertToNumeric(npar);
499 if ((fBuffer[npar].fSqlType==MYSQL_TYPE_LONG) && !fBuffer[npar].fSign)
500 return (
UInt_t) *((
unsigned int*) fBuffer[npar].fMem);
502 return (
UInt_t) ConvertToNumeric(npar);
512 if ((fBuffer[npar].fSqlType==MYSQL_TYPE_LONG) && fBuffer[npar].fSign)
513 return (
Long_t) *((
int*) fBuffer[npar].fMem);
515 return (
Long_t) ConvertToNumeric(npar);
525 if ((fBuffer[npar].fSqlType==MYSQL_TYPE_LONGLONG) && fBuffer[npar].fSign)
528 return (
Long64_t) ConvertToNumeric(npar);
538 if ((fBuffer[npar].fSqlType==MYSQL_TYPE_LONGLONG) && !fBuffer[npar].fSign)
541 return (
ULong64_t) ConvertToNumeric(npar);
551 if (fBuffer[npar].fSqlType==MYSQL_TYPE_DOUBLE)
552 return (
Double_t) *((
double*) fBuffer[npar].fMem);
554 return (
Double_t) ConvertToNumeric(npar);
564 if ((fBind[npar].buffer_type==MYSQL_TYPE_STRING)
565 || (fBind[npar].buffer_type==MYSQL_TYPE_BLOB)
566 || (fBind[npar].buffer_type==MYSQL_TYPE_VAR_STRING)
567 #
if MYSQL_VERSION_ID >= 50022
568 || (fBuffer[npar].fSqlType==MYSQL_TYPE_NEWDECIMAL)
571 if (fBuffer[npar].fResNull)
return 0;
572 char* str = (
char*) fBuffer[npar].fMem;
573 ULong_t len = fBuffer[npar].fResLength;
574 Int_t size = fBuffer[npar].fSize;
575 if (1.*len<size) str[len] = 0;
else 580 return ConvertToString(npar);
593 if ((fBind[npar].buffer_type==MYSQL_TYPE_STRING) ||
594 (fBind[npar].buffer_type==MYSQL_TYPE_VAR_STRING) ||
595 (fBind[npar].buffer_type==MYSQL_TYPE_BLOB) ||
596 (fBind[npar].buffer_type==MYSQL_TYPE_TINY_BLOB) ||
597 (fBind[npar].buffer_type==MYSQL_TYPE_MEDIUM_BLOB) ||
598 (fBind[npar].buffer_type==MYSQL_TYPE_LONG_BLOB)) {
599 if (fBuffer[npar].fResNull)
return kTRUE;
600 mem = fBuffer[npar].fMem;
601 size = fBuffer[npar].fResLength;
615 if (fBuffer[npar].fResNull)
return kFALSE;
617 switch(fBind[npar].buffer_type) {
618 case MYSQL_TYPE_DATETIME:
619 case MYSQL_TYPE_TIMESTAMP:
620 case MYSQL_TYPE_DATE: {
621 MYSQL_TIME* tm = (MYSQL_TIME*) fBuffer[npar].fMem;
641 if (fBuffer[npar].fResNull)
return kFALSE;
643 switch(fBind[npar].buffer_type) {
644 case MYSQL_TYPE_DATETIME:
645 case MYSQL_TYPE_TIMESTAMP:
646 case MYSQL_TYPE_TIME: {
647 MYSQL_TIME* tm = (MYSQL_TIME*) fBuffer[npar].fMem;
667 if (fBuffer[npar].fResNull)
return kFALSE;
669 switch(fBind[npar].buffer_type) {
670 case MYSQL_TYPE_DATETIME:
671 case MYSQL_TYPE_TIMESTAMP: {
672 MYSQL_TIME* tm = (MYSQL_TIME*) fBuffer[npar].fMem;
695 if (fBuffer[npar].fResNull)
return kFALSE;
697 switch(fBind[npar].buffer_type) {
698 case MYSQL_TYPE_DATETIME:
699 case MYSQL_TYPE_TIMESTAMP: {
700 MYSQL_TIME* tm = (MYSQL_TIME*) fBuffer[npar].fMem;
724 if ((npar<0) || (npar>=fNumBuffers))
return kFALSE;
726 fBuffer[npar].fMem = 0;
727 fBuffer[npar].fSize = 0;
728 fBuffer[npar].fResLength = 0;
729 fBuffer[npar].fResNull =
false;
730 fBuffer[npar].fStrBuffer = 0;
737 case MYSQL_TYPE_LONG: allocsize =
sizeof(int);
break;
738 case MYSQL_TYPE_LONGLONG: allocsize =
sizeof(
Long64_t);
break;
739 case MYSQL_TYPE_SHORT: allocsize =
sizeof(short);
break;
740 case MYSQL_TYPE_TINY: allocsize =
sizeof(char);
break;
741 case MYSQL_TYPE_FLOAT: allocsize =
sizeof(float);
break;
742 case MYSQL_TYPE_DOUBLE: allocsize =
sizeof(double);
break;
743 #if MYSQL_VERSION_ID >= 50022 744 case MYSQL_TYPE_NEWDECIMAL :
746 case MYSQL_TYPE_STRING: allocsize = sqlsize > 256 ? sqlsize : 256;
break;
747 case MYSQL_TYPE_VAR_STRING: allocsize = sqlsize > 256 ? sqlsize : 256;
break;
748 case MYSQL_TYPE_MEDIUM_BLOB:
749 case MYSQL_TYPE_LONG_BLOB:
750 case MYSQL_TYPE_BLOB: allocsize = sqlsize >= 65525 ? sqlsize : 65535;
break;
751 case MYSQL_TYPE_TINY_BLOB: allocsize = sqlsize > 255 ? sqlsize : 255;
break;
752 case MYSQL_TYPE_TIME:
753 case MYSQL_TYPE_DATE:
754 case MYSQL_TYPE_TIMESTAMP:
755 case MYSQL_TYPE_DATETIME: allocsize =
sizeof(MYSQL_TIME); doreset =
true;
break;
756 default:
SetError(-1,
"Nonsupported SQL type",
"SetSQLParamType");
return kFALSE;
759 if (allocsize > fgAllocSizeLimit) allocsize = fgAllocSizeLimit;
761 fBuffer[npar].fMem =
malloc(allocsize);
762 fBuffer[npar].fSize = allocsize;
763 fBuffer[npar].fSqlType = sqltype;
764 fBuffer[npar].fSign = sig;
766 if ((allocsize>0) && fBuffer[npar].fMem && doreset)
767 memset(fBuffer[npar].fMem, 0, allocsize);
769 fBind[npar].buffer_type = enum_field_types(sqltype);
770 fBind[npar].buffer = fBuffer[npar].fMem;
771 fBind[npar].buffer_length = allocsize;
772 fBind[npar].is_null= &(fBuffer[npar].fResNull);
773 fBind[npar].length = &(fBuffer[npar].fResLength);
774 fBind[npar].is_unsigned = !sig;
787 if (!IsSetParsMode()) {
788 SetError(-1,
"Cannot set parameter for statement", method);
792 if ((npar<0) || (npar>=fNumBuffers)) {
793 SetError(-1,
Form(
"Invalid parameter number %d",npar), method);
797 if ((fIterationCount==0) && (fBuffer[npar].fSqlType==0))
798 if (!SetSQLParamType(npar, sqltype, sig, size)) {
799 SetError(-1,
"Cannot initialize parameter buffer", method);
803 if ((fBuffer[npar].fSqlType!=sqltype) ||
804 (fBuffer[npar].fSign != sig))
return 0;
806 fBuffer[npar].fResNull =
false;
808 return fBuffer[npar].fMem;
822 void* addr = BeforeSet(
"SetNull", npar, MYSQL_TYPE_LONG);
827 if ((npar>=0) && (npar<fNumBuffers))
828 fBuffer[npar].fResNull =
true;
838 void* addr = BeforeSet(
"SetInt", npar, MYSQL_TYPE_LONG);
841 *((
int*) addr) = value;
851 void* addr = BeforeSet(
"SetUInt", npar, MYSQL_TYPE_LONG,
kFALSE);
854 *((
unsigned int*) addr) = value;
864 void* addr = BeforeSet(
"SetLong", npar, MYSQL_TYPE_LONG);
867 *((
int*) addr) = value;
877 void* addr = BeforeSet(
"SetLong64", npar, MYSQL_TYPE_LONGLONG);
890 void* addr = BeforeSet(
"SetULong64", npar, MYSQL_TYPE_LONGLONG,
kFALSE);
903 void* addr = BeforeSet(
"SetDouble", npar, MYSQL_TYPE_DOUBLE,
kFALSE);
906 *((
double*) addr) = value;
916 Int_t len = value ? strlen(value) : 0;
918 void* addr = BeforeSet(
"SetString", npar, MYSQL_TYPE_STRING,
true, maxsize);
920 if (addr==0)
return kFALSE;
922 if (len >= fBuffer[npar].fSize) {
923 free(fBuffer[npar].fMem);
925 fBuffer[npar].fMem =
malloc(len+1);
926 fBuffer[npar].fSize = len + 1;
928 fBind[npar].buffer = fBuffer[npar].fMem;
929 fBind[npar].buffer_length = fBuffer[npar].fSize;
931 addr = fBuffer[npar].fMem;
932 fNeedParBind =
kTRUE;
935 if (value) strcpy((
char*) addr, value);
936 else ((
char*)addr)[0]=
'\0';
938 fBuffer[npar].fResLength = len;
948 if (size>=maxsize) maxsize = size + 1;
950 int bin_type = MYSQL_TYPE_BLOB;
951 if (maxsize > 65525) bin_type = MYSQL_TYPE_MEDIUM_BLOB;
952 if (maxsize > 16777205) bin_type = MYSQL_TYPE_LONG_BLOB;
954 void* addr = BeforeSet(
"SetBinary", npar, bin_type,
true, maxsize);
956 if (addr==0)
return kFALSE;
958 if (size >= fBuffer[npar].fSize) {
959 free(fBuffer[npar].fMem);
961 fBuffer[npar].fMem =
malloc(size+1);
962 fBuffer[npar].fSize = size + 1;
964 fBind[npar].buffer = fBuffer[npar].fMem;
965 fBind[npar].buffer_length = fBuffer[npar].fSize;
967 addr = fBuffer[npar].fMem;
968 fNeedParBind =
kTRUE;
971 memcpy(addr, mem, size);
973 fBuffer[npar].fResLength = size;
983 MYSQL_TIME* addr = (MYSQL_TIME*) BeforeSet(
"SetDate", npar, MYSQL_TYPE_DATE);
999 MYSQL_TIME* addr = (MYSQL_TIME*) BeforeSet(
"SetTime", npar, MYSQL_TYPE_TIME);
1015 MYSQL_TIME* addr = (MYSQL_TIME*) BeforeSet(
"SetDatime", npar, MYSQL_TYPE_DATETIME);
1019 addr->month = month;
1034 MYSQL_TIME* addr = (MYSQL_TIME*) BeforeSet(
"SetTimestamp", npar, MYSQL_TYPE_TIMESTAMP);
1038 addr->month = month;
1404 #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.