1270
1265
'emb_' reside in libmysqld/lib_sql.cc.
1271
1266
*********************************************************************/
1273
/******************* Declarations ***********************************/
1275
/* Default number of rows fetched per one COM_STMT_FETCH command. */
1277
#define DEFAULT_PREFETCH_ROWS (ulong) 1
1280
These functions are called by function pointer MYSQL_STMT::read_row_func.
1281
Each function corresponds to one of the read methods:
1282
- mysql_stmt_fetch without prior mysql_stmt_store_result,
1283
- mysql_stmt_fetch when result is stored,
1284
- mysql_stmt_fetch when there are no rows (always returns MYSQL_NO_DATA)
1288
This function is used in mysql_stmt_store_result if
1289
STMT_ATTR_UPDATE_MAX_LENGTH attribute is set.
1291
static void stmt_update_metadata(MYSQL_STMT *stmt, MYSQL_ROWS *data);
1293
/* Auxilary function used to reset statement handle. */
1295
#define RESET_SERVER_SIDE 1
1296
#define RESET_LONG_DATA 2
1297
#define RESET_STORE_RESULT 4
1300
Maximum sizes of MYSQL_TYPE_DATE, MYSQL_TYPE_TIME, MYSQL_TYPE_DATETIME
1301
values stored in network buffer.
1304
/* 1 (length) + 2 (year) + 1 (month) + 1 (day) */
1305
#define MAX_DATE_REP_LENGTH 5
1308
1 (length) + 1 (is negative) + 4 (day count) + 1 (hour)
1309
+ 1 (minute) + 1 (seconds) + 4 (microseconds)
1311
#define MAX_TIME_REP_LENGTH 13
1314
1 (length) + 2 (year) + 1 (month) + 1 (day) +
1315
1 (hour) + 1 (minute) + 1 (second) + 4 (microseconds)
1317
#define MAX_DATETIME_REP_LENGTH 12
1319
#define MAX_DOUBLE_STRING_REP_LENGTH 331
1321
/* A macro to check truncation errors */
1323
#define IS_TRUNCATED(value, is_unsigned, min, max, umax) \
1324
((is_unsigned) ? (((value) > (umax) || (value) < 0) ? 1 : 0) : \
1325
(((value) > (max) || (value) < (min)) ? 1 : 0))
1327
#define BIND_RESULT_DONE 1
1329
We report truncations only if at least one of MYSQL_BIND::error
1330
pointers is set. In this case stmt->bind_result_done |-ed with
1333
#define REPORT_DATA_TRUNCATION 2
1335
/**************** Misc utility functions ****************************/
1338
Reallocate the NET package to have at least length bytes available.
1342
net The NET structure to modify.
1343
length Ensure that net->buff has space for at least
1344
this number of bytes.
1348
1 Error, i.e. out of memory or requested packet size is bigger
1349
than max_allowed_packet. The error code is stored in net->last_errno.
1352
static my_bool my_realloc_str(NET *net, ulong length)
1354
ulong buf_length= (ulong) (net->write_pos - net->buff);
1356
DBUG_ENTER("my_realloc_str");
1357
if (buf_length + length > net->max_packet)
1359
res= net_realloc(net, buf_length + length);
1362
strmov(net->sqlstate, unknown_sqlstate);
1363
strmov(net->last_error, ER(net->last_errno));
1365
net->write_pos= net->buff+ buf_length;
1371
static void stmt_clear_error(MYSQL_STMT *stmt)
1373
if (stmt->last_errno)
1375
stmt->last_errno= 0;
1376
stmt->last_error[0]= '\0';
1377
strmov(stmt->sqlstate, not_error_sqlstate);
1382
Set statement error code, sqlstate, and error message
1383
from given errcode and sqlstate.
1386
void set_stmt_error(MYSQL_STMT * stmt, int errcode,
1387
const char *sqlstate, const char *err)
1389
DBUG_ENTER("set_stmt_error");
1390
DBUG_PRINT("enter", ("error: %d '%s'", errcode, ER(errcode)));
1391
DBUG_ASSERT(stmt != 0);
1396
stmt->last_errno= errcode;
1397
strmov(stmt->last_error, ER(errcode));
1398
strmov(stmt->sqlstate, sqlstate);
1405
Set statement error code, sqlstate, and error message from NET.
1407
@param stmt a statement handle. Copy the error here.
1408
@param net mysql->net. Source of the error.
1411
void set_stmt_errmsg(MYSQL_STMT *stmt, NET *net)
1413
DBUG_ENTER("set_stmt_errmsg");
1414
DBUG_PRINT("enter", ("error: %d/%s '%s'",
1418
DBUG_ASSERT(stmt != 0);
1420
stmt->last_errno= net->last_errno;
1421
if (net->last_error && net->last_error[0])
1422
strmov(stmt->last_error, net->last_error);
1423
strmov(stmt->sqlstate, net->sqlstate);
1429
Read and unpack server reply to COM_STMT_PREPARE command (sent from
1430
mysql_stmt_prepare).
1433
cli_read_prepare_result()
1434
mysql connection handle
1435
stmt statement handle
1442
my_bool cli_read_prepare_result(MYSQL *mysql, MYSQL_STMT *stmt)
1445
uint field_count, param_count;
1446
ulong packet_length;
1447
MYSQL_DATA *fields_data;
1448
DBUG_ENTER("cli_read_prepare_result");
1450
if ((packet_length= cli_safe_read(mysql)) == packet_error)
1452
mysql->warning_count= 0;
1454
pos= (uchar*) mysql->net.read_pos;
1455
stmt->stmt_id= uint4korr(pos+1); pos+= 5;
1456
/* Number of columns in result set */
1457
field_count= uint2korr(pos); pos+= 2;
1458
/* Number of placeholders in the statement */
1459
param_count= uint2korr(pos); pos+= 2;
1460
if (packet_length >= 12)
1461
mysql->warning_count= uint2korr(pos+1);
1463
if (param_count != 0)
1465
MYSQL_DATA *param_data;
1467
/* skip parameters data: we don't support it yet */
1468
if (!(param_data= (*mysql->methods->read_rows)(mysql, (MYSQL_FIELD*)0, 7)))
1470
free_rows(param_data);
1473
if (field_count != 0)
1475
if (!(mysql->server_status & SERVER_STATUS_AUTOCOMMIT))
1476
mysql->server_status|= SERVER_STATUS_IN_TRANS;
1478
if (!(fields_data= (*mysql->methods->read_rows)(mysql,(MYSQL_FIELD*)0,7)))
1480
if (!(stmt->fields= unpack_fields(fields_data,&stmt->mem_root,
1482
mysql->server_capabilities)))
1485
stmt->field_count= field_count;
1486
stmt->param_count= (ulong) param_count;
1487
DBUG_PRINT("exit",("field_count: %u param_count: %u warning_count: %u",
1488
field_count, param_count, (uint) mysql->warning_count));
1493
static void net_store_datetime(NET *net, MYSQL_TIME *tm)
1495
char buff[MAX_DATETIME_REP_LENGTH], *pos;
1500
int2store(pos, tm->year);
1501
pos[2]= (uchar) tm->month;
1502
pos[3]= (uchar) tm->day;
1503
pos[4]= (uchar) tm->hour;
1504
pos[5]= (uchar) tm->minute;
1505
pos[6]= (uchar) tm->second;
1506
int4store(pos+7, tm->second_part);
1507
if (tm->second_part)
1509
else if (tm->hour || tm->minute || tm->second)
1511
else if (tm->year || tm->month || tm->day)
1515
buff[0]= (char) length++;
1516
memcpy((char *)net->write_pos, buff, length);
1517
net->write_pos+= length;
1521
Read one row from network: unbuffered non-cursor fetch.
1522
If last row was read, or error occured, erase this statement
1523
from record pointing to object unbuffered fetch is performed from.
1526
stmt_read_row_unbuffered()
1527
stmt statement handle
1528
row pointer to write pointer to row data;
1531
0 - success; *row contains valid address of a row;
1532
row data is stored in network buffer
1533
1 - error; error code is written to
1534
stmt->last_{errno,error}; *row is not changed
1535
MYSQL_NO_DATA - end of file was read from network;
1539
static int stmt_read_row_unbuffered(MYSQL_STMT *stmt, unsigned char **row)
1542
MYSQL *mysql= stmt->mysql;
1544
This function won't be called if stmt->field_count is zero
1545
or execution wasn't done: this is ensured by mysql_stmt_execute.
1549
set_stmt_error(stmt, CR_SERVER_LOST, unknown_sqlstate, NULL);
1552
if (mysql->status != MYSQL_STATUS_GET_RESULT)
1554
set_stmt_error(stmt, stmt->unbuffered_fetch_cancelled ?
1555
CR_FETCH_CANCELED : CR_COMMANDS_OUT_OF_SYNC,
1556
unknown_sqlstate, NULL);
1559
if ((*mysql->methods->unbuffered_fetch)(mysql, (char**) row))
1561
set_stmt_errmsg(stmt, &mysql->net);
1563
If there was an error, there are no more pending rows:
1564
reset statement status to not hang up in following
1565
mysql_stmt_close (it will try to flush result set before
1566
closing the statement).
1568
mysql->status= MYSQL_STATUS_READY;
1573
mysql->status= MYSQL_STATUS_READY;
1579
if (mysql->unbuffered_fetch_owner == &stmt->unbuffered_fetch_cancelled)
1580
mysql->unbuffered_fetch_owner= 0;
1586
Default read row function to not SIGSEGV in client in
1587
case of wrong sequence of API calls.
1591
stmt_read_row_no_data(MYSQL_STMT *stmt __attribute__((unused)),
1592
unsigned char **row __attribute__((unused)))
1594
return MYSQL_NO_DATA;
1598
stmt_read_row_no_result_set(MYSQL_STMT *stmt __attribute__((unused)),
1599
unsigned char **row __attribute__((unused)))
1601
set_stmt_error(stmt, CR_NO_RESULT_SET, unknown_sqlstate, NULL);
1606
1269
static my_bool int_is_null_true= 1; /* Used for MYSQL_TYPE_NULL */
1607
1270
static my_bool int_is_null_false= 0;