30#if MYSQL_VERSION_ID >= 40100
40 ULong_t paramcount = mysql_stmt_param_count(fStmt);
44 SetBuffersNumber(paramcount);
64 mysql_stmt_close(
fStmt);
73#define CheckStmt(method, res) \
77 SetError(-1,"Statement handle is 0",method); \
83#define CheckErrNo(method, force, res) \
85 unsigned int stmterrno = mysql_stmt_errno(fStmt); \
86 if ((stmterrno!=0) || force) { \
87 const char* stmterrmsg = mysql_stmt_error(fStmt); \
88 if (stmterrno==0) { stmterrno = 11111; stmterrmsg = "MySQL statement error"; } \
89 SetError(stmterrno, stmterrmsg, method); \
96#define CheckGetField(method, res) \
99 if (!IsResultSetMode()) { \
100 SetError(-1,"Cannot get statement parameters",method); \
103 if ((npar<0) || (npar>=fNumBuffers)) { \
104 SetError(-1,Form("Invalid parameter number %d", npar),method); \
126 if (mysql_stmt_execute(
fStmt))
139 my_ulonglong res = mysql_stmt_affected_rows(
fStmt);
141 if (res == (my_ulonglong) -1)
169 SetError(-1,
"Cannot store result for that statement",
"StoreResult");
173 if (mysql_stmt_store_result(
fStmt))
177 MYSQL_RES* meta = mysql_stmt_result_metadata(
fStmt);
179 int count = mysql_num_fields(meta);
183 MYSQL_FIELD *fields = mysql_fetch_fields(meta);
185 for (
int n=0;
n<count;
n++) {
191 mysql_free_result(meta);
250 SetError(-1,
"Cannot call for that statement",
"NextIteration");
264 if (mysql_stmt_execute(
fStmt))
296 if (numpars<=0)
return;
330 if ((
fBind[npar].buffer_type==MYSQL_TYPE_STRING) ||
331 (
fBind[npar].buffer_type==MYSQL_TYPE_VAR_STRING))
332 return (
const char *) addr;
334 constexpr int kSize = 100;
338 switch(
fBind[npar].buffer_type) {
339 case MYSQL_TYPE_LONG:
341 else len =
snprintf(buf,
kSize,
"%u",*((
unsigned int*) addr));
343 case MYSQL_TYPE_LONGLONG:
347 case MYSQL_TYPE_SHORT:
348 if (sig) len =
snprintf(buf,
kSize,
"%hd",*((
short*) addr));
else
351 case MYSQL_TYPE_TINY:
352 if (sig) len =
snprintf(buf,
kSize,
"%d",*((
char*) addr));
else
355 case MYSQL_TYPE_FLOAT:
358 case MYSQL_TYPE_DOUBLE:
361 case MYSQL_TYPE_DATETIME:
362 case MYSQL_TYPE_TIMESTAMP: {
363 MYSQL_TIME* tm = (MYSQL_TIME*) addr;
364 len =
snprintf(buf,
kSize,
"%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
365 tm->year, tm->month, tm->day,
366 tm->hour, tm->minute, tm->second);
369 case MYSQL_TYPE_TIME: {
370 MYSQL_TIME* tm = (MYSQL_TIME*) addr;
372 tm->hour, tm->minute, tm->second);
375 case MYSQL_TYPE_DATE: {
376 MYSQL_TIME* tm = (MYSQL_TIME*) addr;
378 tm->year, tm->month, tm->day);
386 SetError(-1,
Form(
"Cannot convert param %d into string - buffer too small", npar));
398 if (
fBuffer[npar].fResNull)
return 0;
403 if (addr==0)
return 0;
405 switch(
fBind[npar].buffer_type) {
406 case MYSQL_TYPE_LONG:
407 if (sig)
return *((
int*) addr);
else
408 return *((
unsigned int*) addr);
410 case MYSQL_TYPE_LONGLONG:
411 if (sig)
return *((
Long64_t*) addr);
else
414 case MYSQL_TYPE_SHORT:
415 if (sig)
return *((
short*) addr);
else
416 return *((
unsigned short*) addr);
418 case MYSQL_TYPE_TINY:
419 if (sig)
return *((
char*) addr);
else
420 return *((
unsigned char*) addr);
422 case MYSQL_TYPE_FLOAT:
423 return *((
float*) addr);
425 case MYSQL_TYPE_DOUBLE:
426 return *((
double*) addr);
428#if MYSQL_VERSION_ID >= 50022
429 case MYSQL_TYPE_NEWDECIMAL :
431 case MYSQL_TYPE_STRING:
432 case MYSQL_TYPE_VAR_STRING:
433 case MYSQL_TYPE_BLOB: {
434 char* str = (
char*) addr;
436 if ((str==0) || (*str==0) || (len==0))
return 0;
443 sscanf(str,
"%Lf",&buf);
447 case MYSQL_TYPE_DATETIME:
448 case MYSQL_TYPE_TIMESTAMP: {
449 MYSQL_TIME* tm = (MYSQL_TIME*) addr;
450 TDatime rtm(tm->year, tm->month, tm->day,
451 tm->hour, tm->minute, tm->second);
455 case MYSQL_TYPE_DATE: {
456 MYSQL_TIME* tm = (MYSQL_TIME*) addr;
457 TDatime rtm(tm->year, tm->month, tm->day, 0, 0, 0);
458 return rtm.GetDate();
461 case MYSQL_TYPE_TIME: {
462 MYSQL_TIME* tm = (MYSQL_TIME*) addr;
463 TDatime rtm(2000, 1, 1, tm->hour, tm->minute, tm->second);
464 return rtm.GetTime();
490 if ((
fBuffer[npar].fSqlType==MYSQL_TYPE_LONG) &&
fBuffer[npar].fSign)
503 if ((
fBuffer[npar].fSqlType==MYSQL_TYPE_LONG) && !
fBuffer[npar].fSign)
516 if ((
fBuffer[npar].fSqlType==MYSQL_TYPE_LONG) &&
fBuffer[npar].fSign)
529 if ((
fBuffer[npar].fSqlType==MYSQL_TYPE_LONGLONG) &&
fBuffer[npar].fSign)
542 if ((
fBuffer[npar].fSqlType==MYSQL_TYPE_LONGLONG) && !
fBuffer[npar].fSign)
555 if (
fBuffer[npar].fSqlType==MYSQL_TYPE_DOUBLE)
568 if ((
fBind[npar].buffer_type==MYSQL_TYPE_STRING)
569 || (
fBind[npar].buffer_type==MYSQL_TYPE_BLOB)
570 || (
fBind[npar].buffer_type==MYSQL_TYPE_VAR_STRING)
571#
if MYSQL_VERSION_ID >= 50022
572 || (
fBuffer[npar].fSqlType==MYSQL_TYPE_NEWDECIMAL)
575 if (
fBuffer[npar].fResNull)
return nullptr;
576 char *str = (
char *)
fBuffer[npar].fMem;
579 if (1.*len<size) str[len] = 0;
else
597 if ((
fBind[npar].buffer_type==MYSQL_TYPE_STRING) ||
598 (
fBind[npar].buffer_type==MYSQL_TYPE_VAR_STRING) ||
599 (
fBind[npar].buffer_type==MYSQL_TYPE_BLOB) ||
600 (
fBind[npar].buffer_type==MYSQL_TYPE_TINY_BLOB) ||
601 (
fBind[npar].buffer_type==MYSQL_TYPE_MEDIUM_BLOB) ||
602 (
fBind[npar].buffer_type==MYSQL_TYPE_LONG_BLOB)) {
621 switch(
fBind[npar].buffer_type) {
622 case MYSQL_TYPE_DATETIME:
623 case MYSQL_TYPE_TIMESTAMP:
624 case MYSQL_TYPE_DATE: {
625 MYSQL_TIME* tm = (MYSQL_TIME*)
fBuffer[npar].fMem;
647 switch(
fBind[npar].buffer_type) {
648 case MYSQL_TYPE_DATETIME:
649 case MYSQL_TYPE_TIMESTAMP:
650 case MYSQL_TYPE_TIME: {
651 MYSQL_TIME* tm = (MYSQL_TIME*)
fBuffer[npar].fMem;
673 switch(
fBind[npar].buffer_type) {
674 case MYSQL_TYPE_DATETIME:
675 case MYSQL_TYPE_TIMESTAMP: {
676 MYSQL_TIME* tm = (MYSQL_TIME*)
fBuffer[npar].fMem;
701 switch(
fBind[npar].buffer_type) {
702 case MYSQL_TYPE_DATETIME:
703 case MYSQL_TYPE_TIMESTAMP: {
704 MYSQL_TIME* tm = (MYSQL_TIME*)
fBuffer[npar].fMem;
741 case MYSQL_TYPE_LONG: allocsize =
sizeof(
int);
break;
742 case MYSQL_TYPE_LONGLONG: allocsize =
sizeof(
Long64_t);
break;
743 case MYSQL_TYPE_SHORT: allocsize =
sizeof(
short);
break;
744 case MYSQL_TYPE_TINY: allocsize =
sizeof(char);
break;
745 case MYSQL_TYPE_FLOAT: allocsize =
sizeof(float);
break;
746 case MYSQL_TYPE_DOUBLE: allocsize =
sizeof(
double);
break;
747#if MYSQL_VERSION_ID >= 50022
748 case MYSQL_TYPE_NEWDECIMAL :
750 case MYSQL_TYPE_STRING: allocsize = sqlsize > 256 ? sqlsize : 256;
break;
751 case MYSQL_TYPE_VAR_STRING: allocsize = sqlsize > 256 ? sqlsize : 256;
break;
752 case MYSQL_TYPE_MEDIUM_BLOB:
753 case MYSQL_TYPE_LONG_BLOB:
754 case MYSQL_TYPE_BLOB: allocsize = sqlsize >= 65525 ? sqlsize : 65535;
break;
755 case MYSQL_TYPE_TINY_BLOB: allocsize = sqlsize > 255 ? sqlsize : 255;
break;
756 case MYSQL_TYPE_TIME:
757 case MYSQL_TYPE_DATE:
758 case MYSQL_TYPE_TIMESTAMP:
759 case MYSQL_TYPE_DATETIME: allocsize =
sizeof(MYSQL_TIME); doreset =
true;
break;
760 default:
SetError(-1,
"Nonsupported SQL type",
"SetSQLParamType");
return kFALSE;
770 if ((allocsize>0) &&
fBuffer[npar].fMem && doreset)
771 memset(
fBuffer[npar].fMem, 0, allocsize);
773 fBind[npar].buffer_type = enum_field_types(sqltype);
775 fBind[npar].buffer_length = allocsize;
778 fBind[npar].is_unsigned = !sig;
792 SetError(-1,
"Cannot set parameter for statement", method);
797 SetError(-1,
Form(
"Invalid parameter number %d",npar), method);
803 SetError(-1,
"Cannot initialize parameter buffer", method);
807 if ((
fBuffer[npar].fSqlType!=sqltype) ||
808 (
fBuffer[npar].fSign != sig))
return 0;
826 void* addr =
BeforeSet(
"SetNull", npar, MYSQL_TYPE_LONG);
842 void* addr =
BeforeSet(
"SetInt", npar, MYSQL_TYPE_LONG);
845 *((
int*) addr) = value;
858 *((
unsigned int*) addr) = value;
868 void* addr =
BeforeSet(
"SetLong", npar, MYSQL_TYPE_LONG);
871 *((
int*) addr) = value;
881 void* addr =
BeforeSet(
"SetLong64", npar, MYSQL_TYPE_LONGLONG);
894 void* addr =
BeforeSet(
"SetULong64", npar, MYSQL_TYPE_LONGLONG,
kFALSE);
910 *((
double*) addr) = value;
920 Int_t len = value ? strlen(value) : 0;
922 void* addr =
BeforeSet(
"SetString", npar, MYSQL_TYPE_STRING,
true, maxsize);
924 if (addr==0)
return kFALSE;
939 if (value) strcpy((
char*) addr, value);
940 else ((
char*)addr)[0]=
'\0';
952 if (size>=maxsize) maxsize = size + 1;
954 int bin_type = MYSQL_TYPE_BLOB;
955 if (maxsize > 65525) bin_type = MYSQL_TYPE_MEDIUM_BLOB;
956 if (maxsize > 16777205) bin_type = MYSQL_TYPE_LONG_BLOB;
958 void* addr =
BeforeSet(
"SetBinary", npar, bin_type,
true, maxsize);
960 if (addr==0)
return kFALSE;
975 memcpy(addr, mem, size);
987 MYSQL_TIME* addr = (MYSQL_TIME*)
BeforeSet(
"SetDate", npar, MYSQL_TYPE_DATE);
1003 MYSQL_TIME* addr = (MYSQL_TIME*)
BeforeSet(
"SetTime", npar, MYSQL_TYPE_TIME);
1019 MYSQL_TIME* addr = (MYSQL_TIME*)
BeforeSet(
"SetDatime", npar, MYSQL_TYPE_DATETIME);
1023 addr->month = month;
1038 MYSQL_TIME* addr = (MYSQL_TIME*)
BeforeSet(
"SetTimestamp", npar, MYSQL_TYPE_TIMESTAMP);
1042 addr->month = month;
unsigned long long ULong64_t
#define CheckErrNo(method, force, res)
#define CheckGetField(method, defres)
#define CheckStmt(method, res)
char * Form(const char *fmt,...)
This class stores the date and time with a precision of one second in an unsigned 32 bit word (950130...
static ULong64_t fgAllocSizeLimit
void SetBuffersNumber(Int_t n)
Allocate buffers for statement parameters/ result fields.
Bool_t SetInt(Int_t npar, Int_t value) final
Set parameter value as integer.
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 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.
UInt_t GetUInt(Int_t npar) final
Return field value as unsigned integer.
Bool_t NextIteration() final
Increment iteration counter for statement, where parameter can be set.
Double_t GetDouble(Int_t npar) final
Return field value as double.
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 IsSetParsMode() const
indicates when parameters bind should be called
const char * ConvertToString(Int_t npar)
Convert field value to string.
Bool_t GetDate(Int_t npar, Int_t &year, Int_t &month, Int_t &day) final
Return field value as date.
Bool_t SetDouble(Int_t npar, Double_t value) final
Set parameter value as double.
Bool_t SetUInt(Int_t npar, UInt_t value) final
Set parameter value as unsigned integer.
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.
Bool_t GetTime(Int_t npar, Int_t &hour, Int_t &min, Int_t &sec) final
Return field value as time.
Bool_t Process() final
Process statement.
Bool_t SetBinary(Int_t npar, void *mem, Long_t size, Long_t maxsize=0x1000) final
Set parameter value as binary data.
void Close(Option_t *="") final
Close statement.
Bool_t SetTime(Int_t npar, Int_t hour, Int_t min, Int_t sec) final
Set parameter value as time.
Int_t fWorkingMode
parameter definition structures
Int_t GetNumParameters() final
Return number of statement parameters.
void FreeBuffers()
Release all buffers, used by statement.
Bool_t IsNull(Int_t npar) final
Checks if field value is null.
Int_t fIterationCount
1 - setting parameters, 2 - retrieving results
ULong64_t GetULong64(Int_t npar) final
Return field value as unsigned 64-bit integer.
Long64_t GetLong64(Int_t npar) final
Return field value as 64-bit integer.
Bool_t IsResultSetMode() const
virtual ~TMySQLStatement()
Destructor.
const char * GetFieldName(Int_t nfield) final
Returns field name in result set.
Bool_t SetSQLParamType(Int_t npar, int sqltype, Bool_t sig, ULong_t sqlsize=0)
Set parameter type to be used as buffer.
Int_t GetInt(Int_t npar) final
Return field value as integer.
TParamData * fBuffer
array of bind data
Int_t GetNumFields() final
Return number of fields in result set.
Bool_t fNeedParBind
number of iteration
Bool_t SetLong(Int_t npar, Long_t value) final
Set parameter value as long integer.
Bool_t SetDate(Int_t npar, Int_t year, Int_t month, Int_t day) final
Set parameter value as date.
Bool_t StoreResult() final
Store result of statement processing to access them via GetInt(), GetDouble() and so on methods.
Int_t fNumBuffers
executed statement
Bool_t SetString(Int_t npar, const char *value, Int_t maxsize=256) final
Set parameter value as string.
Long_t GetLong(Int_t npar) final
Return field value as long integer.
TMySQLStatement(const TMySQLStatement &)=delete
Bool_t SetLong64(Int_t npar, Long64_t value) final
Set parameter value as 64-bit integer.
Int_t GetNumAffectedRows() final
Return number of affected rows after statement is processed.
Bool_t GetBinary(Int_t npar, void *&mem, Long_t &size) final
Return field value as binary array.
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.
long double ConvertToNumeric(Int_t npar)
Convert field to numeric value.
const char * GetString(Int_t npar) final
Return field value as string.
Bool_t SetNull(Int_t npar) final
Set NULL as parameter value.
Bool_t SetULong64(Int_t npar, ULong64_t value) final
Set parameter value as unsigned 64-bit integer.
Bool_t NextResultRow() final
Shift cursor to nect row in result set.
MYSQL_BIND * fBind
number of statement parameters
static const char * GetFloatFormat()
return current printf format for float/double members, default "%e"
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
Bool_t fSign
sqltype of parameter
std::string fFieldName
special buffer to be used for string conversions
ULong_t fResLength
signed - not signed type
Int_t fSize
allocated data buffer
my_bool fResNull
length argument
std::string fStrBuffer
indicates if argument is null
Int_t fSqlType
size of allocated data