2002-06-12 23:13:12 +02:00
|
|
|
/* Copyright (C) 1995-2002 MySQL AB
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
This file contains the implementation of prepare and executes.
|
|
|
|
|
|
|
|
Prepare:
|
|
|
|
|
2002-11-27 03:51:38 +01:00
|
|
|
- Server gets the query from client with command 'COM_PREPARE';
|
|
|
|
in the following format:
|
|
|
|
[COM_PREPARE:1] [query]
|
2002-06-12 23:13:12 +02:00
|
|
|
- Parse the query and recognize any parameter markers '?' and
|
2002-11-27 03:51:38 +01:00
|
|
|
store its information list in lex->param_list
|
|
|
|
- Allocate a new statement for this prepare; and keep this in
|
|
|
|
'thd->prepared_statements' pool.
|
2002-06-12 23:13:12 +02:00
|
|
|
- Without executing the query, return back to client the total
|
|
|
|
number of parameters along with result-set metadata information
|
2002-11-27 03:51:38 +01:00
|
|
|
(if any) in the following format:
|
2003-01-20 23:00:50 +01:00
|
|
|
[STMT_ID:4]
|
|
|
|
[Column_count:2]
|
|
|
|
[Param_count:2]
|
|
|
|
[Columns meta info] (if Column_count > 0)
|
|
|
|
[Params meta info] (if Param_count > 0 ) (TODO : 4.1.1)
|
2002-06-12 23:13:12 +02:00
|
|
|
|
|
|
|
Prepare-execute:
|
|
|
|
|
|
|
|
- Server gets the command 'COM_EXECUTE' to execute the
|
2002-11-22 19:04:42 +01:00
|
|
|
previously prepared query. If there is any param markers; then client
|
|
|
|
will send the data in the following format:
|
2002-11-27 03:51:38 +01:00
|
|
|
[COM_EXECUTE:1]
|
|
|
|
[STMT_ID:4]
|
|
|
|
[NULL_BITS:(param_count+7)/8)]
|
|
|
|
[TYPES_SUPPLIED_BY_CLIENT(0/1):1]
|
|
|
|
[[length]data]
|
|
|
|
[[length]data] .. [[length]data].
|
|
|
|
(Note: Except for string/binary types; all other types will not be
|
|
|
|
supplied with length field)
|
2002-11-22 19:04:42 +01:00
|
|
|
- Replace the param items with this new data. If it is a first execute
|
|
|
|
or types altered by client; then setup the conversion routines.
|
2002-06-12 23:13:12 +02:00
|
|
|
- Execute the query without re-parsing and send back the results
|
|
|
|
to client
|
|
|
|
|
|
|
|
Long data handling:
|
2002-11-27 03:51:38 +01:00
|
|
|
|
2002-06-12 23:13:12 +02:00
|
|
|
- Server gets the long data in pieces with command type 'COM_LONG_DATA'.
|
|
|
|
- The packet recieved will have the format as:
|
2002-11-27 03:51:38 +01:00
|
|
|
[COM_LONG_DATA:1][STMT_ID:4][parameter_number:2][type:2][data]
|
2002-06-12 23:13:12 +02:00
|
|
|
- Checks if the type is specified by client, and if yes reads the type,
|
|
|
|
and stores the data in that format.
|
2002-10-02 12:33:08 +02:00
|
|
|
- It's up to the client to check for read data ended. The server doesn't
|
2002-11-27 03:51:38 +01:00
|
|
|
care; and also server doesn't notify to the client that it got the
|
|
|
|
data or not; if there is any error; then during execute; the error
|
|
|
|
will be returned
|
2002-10-02 12:33:08 +02:00
|
|
|
|
2002-06-12 23:13:12 +02:00
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
#include "mysql_priv.h"
|
|
|
|
#include "sql_acl.h"
|
2003-01-18 21:58:19 +01:00
|
|
|
#include "sql_select.h" // for JOIN
|
2002-10-02 12:33:08 +02:00
|
|
|
#include <m_ctype.h> // for isspace()
|
2002-06-12 23:13:12 +02:00
|
|
|
|
2002-11-22 19:04:42 +01:00
|
|
|
#define IS_PARAM_NULL(pos, param_no) pos[param_no/8] & (1 << param_no & 7)
|
2002-10-02 12:33:08 +02:00
|
|
|
|
2002-11-26 14:18:16 +01:00
|
|
|
extern int yyparse(void *thd);
|
2002-10-02 12:33:08 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Find prepared statement in thd
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
find_prepared_statement()
|
|
|
|
thd Thread handler
|
|
|
|
stmt_id Statement id server specified to the client on prepare
|
|
|
|
|
|
|
|
RETURN VALUES
|
|
|
|
0 error. In this case the error is sent with my_error()
|
|
|
|
ptr Pointer to statement
|
|
|
|
*/
|
|
|
|
|
|
|
|
static PREP_STMT *find_prepared_statement(THD *thd, ulong stmt_id,
|
|
|
|
const char *when)
|
|
|
|
{
|
|
|
|
PREP_STMT *stmt;
|
|
|
|
DBUG_ENTER("find_prepared_statement");
|
|
|
|
DBUG_PRINT("enter",("stmt_id: %d", stmt_id));
|
|
|
|
|
|
|
|
if (thd->last_prepared_stmt && thd->last_prepared_stmt->stmt_id == stmt_id)
|
|
|
|
DBUG_RETURN(thd->last_prepared_stmt);
|
|
|
|
if ((stmt= (PREP_STMT*) tree_search(&thd->prepared_statements, &stmt_id,
|
|
|
|
(void*) 0)))
|
|
|
|
DBUG_RETURN (thd->last_prepared_stmt= stmt);
|
|
|
|
my_error(ER_UNKNOWN_STMT_HANDLER, MYF(0), stmt_id, when);
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compare two prepared statements; Used to find a prepared statement
|
|
|
|
*/
|
|
|
|
|
2002-11-22 19:04:42 +01:00
|
|
|
int compare_prep_stmt(void *not_used, PREP_STMT *stmt, ulong *key)
|
2002-10-02 12:33:08 +02:00
|
|
|
{
|
2002-11-22 19:04:42 +01:00
|
|
|
return (stmt->stmt_id == *key) ? 0 : (stmt->stmt_id < *key) ? -1 : 1;
|
2002-10-02 12:33:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Free prepared statement.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
standard tree_element_free function.
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
We don't have to free the stmt itself as this was stored in the tree
|
|
|
|
and will be freed when the node is deleted
|
|
|
|
*/
|
|
|
|
|
|
|
|
void free_prep_stmt(PREP_STMT *stmt, TREE_FREE mode, void *not_used)
|
2003-01-28 18:24:27 +01:00
|
|
|
{
|
|
|
|
my_free((char *)stmt->param, MYF(MY_ALLOW_ZERO_PTR));
|
2002-10-02 12:33:08 +02:00
|
|
|
free_items(stmt->free_list);
|
2002-11-22 19:04:42 +01:00
|
|
|
free_root(&stmt->mem_root, MYF(0));
|
2002-10-02 12:33:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Send prepared stmt info to client after prepare
|
|
|
|
*/
|
|
|
|
|
2002-11-22 19:04:42 +01:00
|
|
|
static bool send_prep_stmt(PREP_STMT *stmt, uint columns)
|
2002-10-02 12:33:08 +02:00
|
|
|
{
|
2002-11-22 19:04:42 +01:00
|
|
|
NET *net=&stmt->thd->net;
|
2002-10-02 12:33:08 +02:00
|
|
|
char buff[8];
|
|
|
|
int4store(buff, stmt->stmt_id);
|
|
|
|
int2store(buff+4, columns);
|
|
|
|
int2store(buff+6, stmt->param_count);
|
2002-12-16 14:33:29 +01:00
|
|
|
#ifndef EMBEDDED_LIBRARY
|
2003-01-15 09:11:44 +01:00
|
|
|
/* This should be fixed to work with prepared statements
|
|
|
|
*/
|
2002-11-22 19:04:42 +01:00
|
|
|
return (my_net_write(net, buff, sizeof(buff)) || net_flush(net));
|
2002-12-16 14:33:29 +01:00
|
|
|
#else
|
|
|
|
return true;
|
|
|
|
#endif
|
2002-10-02 12:33:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Send information about all item parameters
|
|
|
|
|
|
|
|
TODO: Not yet ready
|
|
|
|
*/
|
|
|
|
|
2002-11-22 19:04:42 +01:00
|
|
|
static bool send_item_params(PREP_STMT *stmt)
|
2002-10-02 12:33:08 +02:00
|
|
|
{
|
2002-11-22 19:04:42 +01:00
|
|
|
#if 0
|
2002-10-02 12:33:08 +02:00
|
|
|
char buff[1];
|
|
|
|
buff[0]=0;
|
2002-11-22 19:04:42 +01:00
|
|
|
if (my_net_write(&stmt->thd->net, buff, sizeof(buff)))
|
2002-06-12 23:13:12 +02:00
|
|
|
return 1;
|
2002-11-22 19:04:42 +01:00
|
|
|
send_eof(stmt->thd);
|
|
|
|
#endif
|
2002-06-12 23:13:12 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Read the length of the parameter data and retun back to
|
|
|
|
caller by positing the pointer to param data
|
|
|
|
*/
|
|
|
|
|
|
|
|
static ulong get_param_length(uchar **packet)
|
|
|
|
{
|
|
|
|
reg1 uchar *pos= *packet;
|
|
|
|
if (*pos < 251)
|
|
|
|
{
|
|
|
|
(*packet)++;
|
|
|
|
return (ulong) *pos;
|
|
|
|
}
|
|
|
|
if (*pos == 252)
|
|
|
|
{
|
|
|
|
(*packet)+=3;
|
|
|
|
return (ulong) uint2korr(pos+1);
|
|
|
|
}
|
|
|
|
if (*pos == 253)
|
|
|
|
{
|
|
|
|
(*packet)+=4;
|
|
|
|
return (ulong) uint3korr(pos+1);
|
|
|
|
}
|
|
|
|
(*packet)+=9; // Must be 254 when here
|
|
|
|
return (ulong) uint4korr(pos+1);
|
|
|
|
}
|
2002-11-22 19:04:42 +01:00
|
|
|
/*
|
|
|
|
Setup param conversion routines
|
2002-06-12 23:13:12 +02:00
|
|
|
|
2002-11-22 19:04:42 +01:00
|
|
|
setup_param_xx()
|
|
|
|
param Parameter Item
|
|
|
|
pos Input data buffer
|
|
|
|
|
|
|
|
All these functions reads the data from pos and sets up that data
|
|
|
|
through 'param' and advances the buffer position to predifined
|
|
|
|
length position.
|
|
|
|
|
|
|
|
Make a note that the NULL handling is examined at first execution
|
|
|
|
(i.e. when input types altered) and for all subsequent executions
|
|
|
|
we don't read any values for this.
|
|
|
|
|
|
|
|
RETURN VALUES
|
|
|
|
|
2002-06-12 23:13:12 +02:00
|
|
|
*/
|
|
|
|
|
2002-11-22 19:04:42 +01:00
|
|
|
static void setup_param_tiny(Item_param *param, uchar **pos)
|
2002-06-12 23:13:12 +02:00
|
|
|
{
|
2002-11-22 19:04:42 +01:00
|
|
|
param->set_int((longlong)(**pos));
|
|
|
|
*pos+= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setup_param_short(Item_param *param, uchar **pos)
|
|
|
|
{
|
|
|
|
param->set_int((longlong)sint2korr(*pos));
|
|
|
|
*pos+= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setup_param_int32(Item_param *param, uchar **pos)
|
|
|
|
{
|
|
|
|
param->set_int((longlong)sint4korr(*pos));
|
|
|
|
*pos+= 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setup_param_int64(Item_param *param, uchar **pos)
|
|
|
|
{
|
|
|
|
param->set_int((longlong)sint8korr(*pos));
|
|
|
|
*pos+= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setup_param_float(Item_param *param, uchar **pos)
|
|
|
|
{
|
|
|
|
float data;
|
|
|
|
float4get(data,*pos);
|
|
|
|
param->set_double((double) data);
|
|
|
|
*pos+= 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setup_param_double(Item_param *param, uchar **pos)
|
|
|
|
{
|
|
|
|
double data;
|
|
|
|
float8get(data,*pos);
|
|
|
|
param->set_double((double) data);
|
|
|
|
*pos+= 8;
|
|
|
|
}
|
|
|
|
|
2003-01-24 07:32:39 +01:00
|
|
|
static void setup_param_time(Item_param *param, uchar **pos)
|
|
|
|
{
|
|
|
|
ulong length;
|
|
|
|
|
|
|
|
if ((length= get_param_length(pos)))
|
|
|
|
{
|
|
|
|
uchar *to= *pos;
|
|
|
|
TIME tm;
|
|
|
|
|
|
|
|
tm.second_part= (length > 8 ) ? (ulong) sint4korr(to+7): 0;
|
|
|
|
|
|
|
|
tm.day= (ulong) sint4korr(to+1);
|
|
|
|
tm.hour= (uint) to[5];
|
|
|
|
tm.minute= (uint) to[6];
|
|
|
|
tm.second= (uint) to[7];
|
|
|
|
|
|
|
|
tm.year= tm.month= 0;
|
|
|
|
tm.neg= (bool)to[0];
|
|
|
|
|
|
|
|
param->set_time(&tm, TIMESTAMP_TIME);
|
|
|
|
}
|
|
|
|
*pos+= length;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setup_param_datetime(Item_param *param, uchar **pos)
|
|
|
|
{
|
|
|
|
uint length= get_param_length(pos);
|
|
|
|
|
|
|
|
if (length)
|
|
|
|
{
|
|
|
|
uchar *to= *pos;
|
|
|
|
TIME tm;
|
|
|
|
|
|
|
|
tm.second_part= (length > 7 ) ? (ulong) sint4korr(to+7): 0;
|
|
|
|
|
|
|
|
if (length > 4)
|
|
|
|
{
|
|
|
|
tm.hour= (uint) to[4];
|
|
|
|
tm.minute= (uint) to[5];
|
|
|
|
tm.second= (uint) to[6];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tm.hour= tm.minute= tm.second= 0;
|
|
|
|
|
|
|
|
tm.year= (uint) sint2korr(to);
|
|
|
|
tm.month= (uint) to[2];
|
|
|
|
tm.day= (uint) to[3];
|
|
|
|
tm.neg= 0;
|
|
|
|
|
|
|
|
param->set_time(&tm, TIMESTAMP_FULL);
|
|
|
|
}
|
|
|
|
*pos+= length;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setup_param_date(Item_param *param, uchar **pos)
|
|
|
|
{
|
|
|
|
ulong length;
|
|
|
|
|
|
|
|
if ((length= get_param_length(pos)))
|
|
|
|
{
|
|
|
|
uchar *to= *pos;
|
|
|
|
TIME tm;
|
|
|
|
|
2003-01-28 18:24:27 +01:00
|
|
|
tm.year= (uint) sint2korr(to);
|
2003-01-24 07:32:39 +01:00
|
|
|
tm.month= (uint) to[2];
|
|
|
|
tm.day= (uint) to[3];
|
|
|
|
|
|
|
|
tm.hour= tm.minute= tm.second= 0;
|
|
|
|
tm.second_part= 0;
|
|
|
|
tm.neg= 0;
|
|
|
|
|
|
|
|
param->set_time(&tm, TIMESTAMP_DATE);
|
|
|
|
}
|
|
|
|
*pos+= length;
|
|
|
|
}
|
|
|
|
|
2002-11-22 19:04:42 +01:00
|
|
|
static void setup_param_str(Item_param *param, uchar **pos)
|
|
|
|
{
|
2003-01-24 07:32:39 +01:00
|
|
|
ulong len= get_param_length(pos);
|
2002-11-22 19:04:42 +01:00
|
|
|
param->set_value((const char *)*pos, len);
|
2003-01-24 07:32:39 +01:00
|
|
|
*pos+= len;
|
2002-11-22 19:04:42 +01:00
|
|
|
}
|
|
|
|
|
2002-11-27 03:51:38 +01:00
|
|
|
static void setup_param_functions(Item_param *param, uchar param_type)
|
2002-11-22 19:04:42 +01:00
|
|
|
{
|
2002-11-27 03:51:38 +01:00
|
|
|
switch (param_type) {
|
2002-06-12 23:13:12 +02:00
|
|
|
case FIELD_TYPE_TINY:
|
2002-11-22 19:04:42 +01:00
|
|
|
param->setup_param_func= setup_param_tiny;
|
2003-01-28 18:24:27 +01:00
|
|
|
param->item_result_type= INT_RESULT;
|
2002-06-12 23:13:12 +02:00
|
|
|
break;
|
|
|
|
case FIELD_TYPE_SHORT:
|
2002-11-22 19:04:42 +01:00
|
|
|
param->setup_param_func= setup_param_short;
|
2003-01-28 18:24:27 +01:00
|
|
|
param->item_result_type= INT_RESULT;
|
2002-06-12 23:13:12 +02:00
|
|
|
break;
|
|
|
|
case FIELD_TYPE_LONG:
|
2002-11-22 19:04:42 +01:00
|
|
|
param->setup_param_func= setup_param_int32;
|
2003-01-28 18:24:27 +01:00
|
|
|
param->item_result_type= INT_RESULT;
|
2002-06-12 23:13:12 +02:00
|
|
|
break;
|
|
|
|
case FIELD_TYPE_LONGLONG:
|
2002-11-22 19:04:42 +01:00
|
|
|
param->setup_param_func= setup_param_int64;
|
2003-01-28 18:24:27 +01:00
|
|
|
param->item_result_type= INT_RESULT;
|
2002-06-12 23:13:12 +02:00
|
|
|
break;
|
|
|
|
case FIELD_TYPE_FLOAT:
|
2002-11-22 19:04:42 +01:00
|
|
|
param->setup_param_func= setup_param_float;
|
2003-01-28 18:24:27 +01:00
|
|
|
param->item_result_type= REAL_RESULT;
|
2002-06-12 23:13:12 +02:00
|
|
|
break;
|
|
|
|
case FIELD_TYPE_DOUBLE:
|
2002-11-22 19:04:42 +01:00
|
|
|
param->setup_param_func= setup_param_double;
|
2003-01-28 18:24:27 +01:00
|
|
|
param->item_result_type= REAL_RESULT;
|
2002-06-12 23:13:12 +02:00
|
|
|
break;
|
2003-01-24 07:32:39 +01:00
|
|
|
case FIELD_TYPE_TIME:
|
|
|
|
param->setup_param_func= setup_param_time;
|
2003-01-28 18:24:27 +01:00
|
|
|
param->item_result_type= STRING_RESULT;
|
2003-01-24 07:32:39 +01:00
|
|
|
break;
|
|
|
|
case FIELD_TYPE_DATE:
|
|
|
|
param->setup_param_func= setup_param_date;
|
2003-01-28 18:24:27 +01:00
|
|
|
param->item_result_type= STRING_RESULT;
|
2003-01-24 07:32:39 +01:00
|
|
|
break;
|
|
|
|
case FIELD_TYPE_DATETIME:
|
|
|
|
case FIELD_TYPE_TIMESTAMP:
|
|
|
|
param->setup_param_func= setup_param_datetime;
|
2003-01-28 18:24:27 +01:00
|
|
|
param->item_result_type= STRING_RESULT;
|
2003-01-24 07:32:39 +01:00
|
|
|
break;
|
2002-06-12 23:13:12 +02:00
|
|
|
default:
|
2002-11-22 19:04:42 +01:00
|
|
|
param->setup_param_func= setup_param_str;
|
2003-01-28 18:24:27 +01:00
|
|
|
param->item_result_type= STRING_RESULT;
|
2002-06-12 23:13:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Update the parameter markers by reading the data
|
|
|
|
from client ..
|
|
|
|
*/
|
|
|
|
|
2002-11-23 03:30:55 +01:00
|
|
|
static bool setup_params_data(PREP_STMT *stmt)
|
2002-11-22 19:04:42 +01:00
|
|
|
{
|
2002-11-23 03:30:55 +01:00
|
|
|
THD *thd= stmt->thd;
|
2002-11-22 19:04:42 +01:00
|
|
|
List<Item> ¶ms= thd->lex.param_list;
|
|
|
|
List_iterator<Item> param_iterator(params);
|
|
|
|
Item_param *param;
|
|
|
|
DBUG_ENTER("setup_params_data");
|
2002-10-02 12:33:08 +02:00
|
|
|
|
2002-12-16 14:33:29 +01:00
|
|
|
#ifndef EMBEDDED_LIBRARY
|
2002-11-22 19:04:42 +01:00
|
|
|
uchar *pos=(uchar*) thd->net.read_pos+1+MYSQL_STMT_HEADER; //skip header
|
2002-12-16 14:33:29 +01:00
|
|
|
#else
|
|
|
|
uchar *pos= 0; //just to compile TODO code for embedded case
|
|
|
|
#endif
|
2002-11-22 19:04:42 +01:00
|
|
|
uchar *read_pos= pos+(stmt->param_count+7) / 8; //skip null bits
|
2002-06-12 23:13:12 +02:00
|
|
|
|
2002-11-22 19:04:42 +01:00
|
|
|
if (*read_pos++) //types supplied / first execute
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
First execute or types altered by the client, setup the
|
|
|
|
conversion routines for all parameters (one time)
|
|
|
|
*/
|
|
|
|
while ((param= (Item_param *)param_iterator++))
|
|
|
|
{
|
2002-12-07 08:39:11 +01:00
|
|
|
setup_param_functions(param,*read_pos);
|
|
|
|
read_pos+= 2;
|
2002-06-12 23:13:12 +02:00
|
|
|
}
|
2002-11-22 19:04:42 +01:00
|
|
|
param_iterator.rewind();
|
|
|
|
}
|
2002-11-27 03:51:38 +01:00
|
|
|
ulong param_no= 0;
|
2002-11-22 19:04:42 +01:00
|
|
|
while ((param= (Item_param *)param_iterator++))
|
2002-06-12 23:13:12 +02:00
|
|
|
{
|
2002-11-22 19:04:42 +01:00
|
|
|
if (!param->long_data_supplied)
|
2002-06-12 23:13:12 +02:00
|
|
|
{
|
2002-11-22 19:04:42 +01:00
|
|
|
if (IS_PARAM_NULL(pos,param_no))
|
2003-01-10 02:56:34 +01:00
|
|
|
param->maybe_null= param->null_value= 1;
|
2002-11-22 19:04:42 +01:00
|
|
|
else
|
2003-01-10 02:56:34 +01:00
|
|
|
{
|
|
|
|
param->maybe_null= param->null_value= 0;
|
2002-11-22 19:04:42 +01:00
|
|
|
param->setup_param_func(param,&read_pos);
|
2003-01-10 02:56:34 +01:00
|
|
|
}
|
2002-06-12 23:13:12 +02:00
|
|
|
}
|
2002-11-22 19:04:42 +01:00
|
|
|
param_no++;
|
2002-06-12 23:13:12 +02:00
|
|
|
}
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Validate the following information for INSERT statement:
|
|
|
|
- field existance
|
|
|
|
- fields count
|
|
|
|
*/
|
|
|
|
|
2002-10-02 12:33:08 +02:00
|
|
|
static bool mysql_test_insert_fields(PREP_STMT *stmt,
|
|
|
|
TABLE_LIST *table_list,
|
2002-06-12 23:13:12 +02:00
|
|
|
List<Item> &fields,
|
2002-11-23 03:30:55 +01:00
|
|
|
List<List_item> &values_list)
|
2002-06-12 23:13:12 +02:00
|
|
|
{
|
2002-10-02 12:33:08 +02:00
|
|
|
THD *thd= stmt->thd;
|
2002-06-12 23:13:12 +02:00
|
|
|
TABLE *table;
|
|
|
|
List_iterator_fast<List_item> its(values_list);
|
|
|
|
List_item *values;
|
|
|
|
DBUG_ENTER("mysql_test_insert_fields");
|
|
|
|
|
2003-02-25 02:22:02 +01:00
|
|
|
my_bool update=(thd->lex.value_list.elements ? UPDATE_ACL : 0);
|
|
|
|
ulong privilege= (thd->lex.duplicates == DUP_REPLACE ?
|
|
|
|
INSERT_ACL | DELETE_ACL : INSERT_ACL | update);
|
|
|
|
|
|
|
|
if (check_access(thd,privilege,table_list->db,
|
|
|
|
&table_list->grant.privilege) ||
|
|
|
|
(grant_option && check_grant(thd,privilege,table_list)) ||
|
|
|
|
open_and_lock_tables(thd, table_list))
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
|
|
|
|
table= table_list->table;
|
2002-06-12 23:13:12 +02:00
|
|
|
|
|
|
|
if ((values= its++))
|
|
|
|
{
|
|
|
|
uint value_count;
|
2002-10-02 12:33:08 +02:00
|
|
|
ulong counter= 0;
|
2002-06-12 23:13:12 +02:00
|
|
|
|
|
|
|
if (check_insert_fields(thd,table,fields,*values,1))
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
|
|
|
|
value_count= values->elements;
|
|
|
|
its.rewind();
|
|
|
|
|
2002-10-02 12:33:08 +02:00
|
|
|
while ((values= its++))
|
2002-06-12 23:13:12 +02:00
|
|
|
{
|
|
|
|
counter++;
|
|
|
|
if (values->elements != value_count)
|
|
|
|
{
|
|
|
|
my_printf_error(ER_WRONG_VALUE_COUNT_ON_ROW,
|
|
|
|
ER(ER_WRONG_VALUE_COUNT_ON_ROW),
|
2002-10-02 12:33:08 +02:00
|
|
|
MYF(0), counter);
|
2002-06-12 23:13:12 +02:00
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-10-02 12:33:08 +02:00
|
|
|
if (send_prep_stmt(stmt, 0) || send_item_params(stmt))
|
|
|
|
DBUG_RETURN(1);
|
2002-06-12 23:13:12 +02:00
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Validate the following information
|
|
|
|
UPDATE - set and where clause DELETE - where clause
|
|
|
|
|
2002-10-02 12:33:08 +02:00
|
|
|
And send update-set clause column list fields info
|
|
|
|
back to client. For DELETE, just validate where clause
|
2002-06-12 23:13:12 +02:00
|
|
|
and return no fields information back to client.
|
|
|
|
*/
|
|
|
|
|
2002-10-02 12:33:08 +02:00
|
|
|
static bool mysql_test_upd_fields(PREP_STMT *stmt, TABLE_LIST *table_list,
|
2002-06-12 23:13:12 +02:00
|
|
|
List<Item> &fields, List<Item> &values,
|
2002-11-23 03:30:55 +01:00
|
|
|
COND *conds)
|
2002-06-12 23:13:12 +02:00
|
|
|
{
|
2002-10-02 12:33:08 +02:00
|
|
|
THD *thd= stmt->thd;
|
2002-06-12 23:13:12 +02:00
|
|
|
DBUG_ENTER("mysql_test_upd_fields");
|
|
|
|
|
2003-02-25 02:22:02 +01:00
|
|
|
if (check_access(thd,UPDATE_ACL,table_list->db,
|
|
|
|
&table_list->grant.privilege) ||
|
|
|
|
(grant_option && check_grant(thd,UPDATE_ACL,table_list)) ||
|
|
|
|
open_and_lock_tables(thd, table_list))
|
2002-06-12 23:13:12 +02:00
|
|
|
DBUG_RETURN(1);
|
|
|
|
|
2003-01-25 01:25:52 +01:00
|
|
|
if (setup_tables(table_list) ||
|
|
|
|
setup_fields(thd, 0, table_list, fields, 1, 0, 0) ||
|
2003-01-25 12:19:46 +01:00
|
|
|
setup_conds(thd, table_list, &conds) || thd->net.report_error)
|
2002-06-12 23:13:12 +02:00
|
|
|
DBUG_RETURN(1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Currently return only column list info only, and we are not
|
|
|
|
sending any info on where clause.
|
|
|
|
*/
|
2002-10-02 12:33:08 +02:00
|
|
|
if (send_prep_stmt(stmt, 0) || send_item_params(stmt))
|
2002-06-12 23:13:12 +02:00
|
|
|
DBUG_RETURN(1);
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Validate the following information:
|
|
|
|
|
|
|
|
SELECT - column list
|
|
|
|
- where clause
|
2002-10-02 12:33:08 +02:00
|
|
|
- order clause
|
2002-06-12 23:13:12 +02:00
|
|
|
- having clause
|
|
|
|
- group by clause
|
|
|
|
- if no column spec i.e. '*', then setup all fields
|
|
|
|
|
|
|
|
And send column list fields info back to client.
|
|
|
|
*/
|
2002-10-02 12:33:08 +02:00
|
|
|
static bool mysql_test_select_fields(PREP_STMT *stmt, TABLE_LIST *tables,
|
2003-01-26 20:30:35 +01:00
|
|
|
uint wild_num,
|
2003-01-18 21:58:19 +01:00
|
|
|
List<Item> &fields, COND *conds,
|
2003-01-26 20:30:35 +01:00
|
|
|
uint og_num, ORDER *order, ORDER *group,
|
2003-01-18 21:58:19 +01:00
|
|
|
Item *having, ORDER *proc,
|
|
|
|
ulong select_options,
|
|
|
|
SELECT_LEX_UNIT *unit,
|
|
|
|
SELECT_LEX *select_lex)
|
2002-06-12 23:13:12 +02:00
|
|
|
{
|
2002-10-02 12:33:08 +02:00
|
|
|
THD *thd= stmt->thd;
|
2003-01-18 21:58:19 +01:00
|
|
|
LEX *lex= &thd->lex;
|
|
|
|
select_result *result= thd->lex.result;
|
2002-06-12 23:13:12 +02:00
|
|
|
DBUG_ENTER("mysql_test_select_fields");
|
|
|
|
|
2003-02-25 02:22:02 +01:00
|
|
|
ulong privilege= lex->exchange ? SELECT_ACL | FILE_ACL : SELECT_ACL;
|
|
|
|
if (tables)
|
|
|
|
{
|
|
|
|
if (check_table_access(thd, privilege, tables))
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
|
|
|
else if (check_access(thd, privilege, "*any*"))
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
|
2003-01-18 21:58:19 +01:00
|
|
|
if ((&lex->select_lex != lex->all_selects_list &&
|
2003-01-26 20:30:35 +01:00
|
|
|
lex->unit.create_total_list(thd, lex, &tables, 0)))
|
2003-01-18 21:58:19 +01:00
|
|
|
DBUG_RETURN(1);
|
|
|
|
|
2003-01-03 12:52:53 +01:00
|
|
|
if (open_and_lock_tables(thd, tables))
|
2002-06-12 23:13:12 +02:00
|
|
|
DBUG_RETURN(1);
|
|
|
|
|
2003-03-04 23:22:30 +01:00
|
|
|
if (lex->describe)
|
2002-06-12 23:13:12 +02:00
|
|
|
{
|
2003-03-04 23:22:30 +01:00
|
|
|
if (send_prep_stmt(stmt, 0) || send_item_params(stmt))
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fix_tables_pointers(thd->lex.all_selects_list);
|
|
|
|
if (!result && !(result= new select_send()))
|
|
|
|
{
|
|
|
|
delete select_lex->having;
|
|
|
|
delete select_lex->where;
|
|
|
|
send_error(thd, ER_OUT_OF_RESOURCES);
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
2003-01-18 21:58:19 +01:00
|
|
|
|
2003-03-04 23:22:30 +01:00
|
|
|
JOIN *join= new JOIN(thd, fields, select_options, result);
|
|
|
|
thd->used_tables= 0; // Updated by setup_fields
|
2003-01-18 21:58:19 +01:00
|
|
|
|
2003-01-26 20:30:35 +01:00
|
|
|
if (join->prepare(&select_lex->ref_pointer_array, tables,
|
|
|
|
wild_num, conds, og_num, order, group, having, proc,
|
2003-03-06 16:02:10 +01:00
|
|
|
select_lex, unit, 0))
|
2002-06-12 23:13:12 +02:00
|
|
|
DBUG_RETURN(1);
|
2003-03-04 23:22:30 +01:00
|
|
|
if (send_prep_stmt(stmt, fields.elements) ||
|
|
|
|
thd->protocol_simple.send_fields(&fields, 0) ||
|
|
|
|
send_item_params(stmt))
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
join->cleanup(thd);
|
|
|
|
}
|
2002-06-12 23:13:12 +02:00
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
2002-10-02 12:33:08 +02:00
|
|
|
|
2002-06-12 23:13:12 +02:00
|
|
|
/*
|
|
|
|
Send the prepare query results back to client
|
|
|
|
*/
|
|
|
|
|
2002-10-02 12:33:08 +02:00
|
|
|
static bool send_prepare_results(PREP_STMT *stmt)
|
2002-06-12 23:13:12 +02:00
|
|
|
{
|
2002-10-02 12:33:08 +02:00
|
|
|
THD *thd= stmt->thd;
|
|
|
|
LEX *lex= &thd->lex;
|
2003-01-28 18:24:27 +01:00
|
|
|
enum enum_sql_command sql_command= thd->lex.sql_command;
|
2002-10-02 12:33:08 +02:00
|
|
|
DBUG_ENTER("send_prepare_results");
|
|
|
|
DBUG_PRINT("enter",("command: %d, param_count: %ld",
|
|
|
|
sql_command, lex->param_count));
|
2002-06-12 23:13:12 +02:00
|
|
|
|
2002-10-02 12:33:08 +02:00
|
|
|
/* Setup prepared stmt */
|
|
|
|
stmt->param_count= lex->param_count;
|
|
|
|
stmt->free_list= thd->free_list; // Save items used in stmt
|
|
|
|
thd->free_list= 0;
|
|
|
|
|
2003-01-28 18:24:27 +01:00
|
|
|
SELECT_LEX *select_lex= &lex->select_lex;
|
2002-06-12 23:13:12 +02:00
|
|
|
TABLE_LIST *tables=(TABLE_LIST*) select_lex->table_list.first;
|
|
|
|
|
2002-10-02 12:33:08 +02:00
|
|
|
switch (sql_command) {
|
2002-06-12 23:13:12 +02:00
|
|
|
|
|
|
|
case SQLCOM_INSERT:
|
2002-10-02 12:33:08 +02:00
|
|
|
if (mysql_test_insert_fields(stmt, tables, lex->field_list,
|
2002-11-23 03:30:55 +01:00
|
|
|
lex->many_values))
|
2002-06-12 23:13:12 +02:00
|
|
|
goto abort;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SQLCOM_UPDATE:
|
2002-10-02 12:33:08 +02:00
|
|
|
if (mysql_test_upd_fields(stmt, tables, select_lex->item_list,
|
2002-11-23 03:30:55 +01:00
|
|
|
lex->value_list, select_lex->where))
|
2002-06-12 23:13:12 +02:00
|
|
|
goto abort;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SQLCOM_DELETE:
|
2002-10-02 12:33:08 +02:00
|
|
|
if (mysql_test_upd_fields(stmt, tables, select_lex->item_list,
|
2002-11-23 03:30:55 +01:00
|
|
|
lex->value_list, select_lex->where))
|
2002-06-12 23:13:12 +02:00
|
|
|
goto abort;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SQLCOM_SELECT:
|
2003-01-26 20:30:35 +01:00
|
|
|
if (mysql_test_select_fields(stmt, tables, select_lex->with_wild,
|
2003-01-18 21:58:19 +01:00
|
|
|
select_lex->item_list,
|
|
|
|
select_lex->where,
|
2003-01-26 20:30:35 +01:00
|
|
|
select_lex->order_list.elements +
|
|
|
|
select_lex->group_list.elements,
|
2003-01-18 21:58:19 +01:00
|
|
|
(ORDER*) select_lex->order_list.first,
|
|
|
|
(ORDER*) select_lex->group_list.first,
|
|
|
|
select_lex->having,
|
|
|
|
(ORDER*)lex->proc_list.first,
|
|
|
|
select_lex->options | thd->options,
|
|
|
|
&(lex->unit), select_lex))
|
2002-06-12 23:13:12 +02:00
|
|
|
goto abort;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Rest fall through to default category, no parsing
|
|
|
|
for non-DML statements
|
|
|
|
*/
|
2002-11-22 19:04:42 +01:00
|
|
|
if (send_prep_stmt(stmt, 0))
|
|
|
|
goto abort;
|
2002-06-12 23:13:12 +02:00
|
|
|
}
|
|
|
|
}
|
2002-10-02 12:33:08 +02:00
|
|
|
DBUG_RETURN(0);
|
2002-06-12 23:13:12 +02:00
|
|
|
|
|
|
|
abort:
|
2002-10-02 12:33:08 +02:00
|
|
|
send_error(thd,thd->killed ? ER_SERVER_SHUTDOWN : 0);
|
|
|
|
DBUG_RETURN(1);
|
2002-06-12 23:13:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Parse the prepare query
|
|
|
|
*/
|
|
|
|
|
2002-10-02 12:33:08 +02:00
|
|
|
static bool parse_prepare_query(PREP_STMT *stmt,
|
2002-11-22 19:04:42 +01:00
|
|
|
char *packet, uint length)
|
2002-06-12 23:13:12 +02:00
|
|
|
{
|
2002-10-02 12:33:08 +02:00
|
|
|
bool error= 1;
|
|
|
|
THD *thd= stmt->thd;
|
|
|
|
DBUG_ENTER("parse_prepare_query");
|
2002-06-12 23:13:12 +02:00
|
|
|
|
|
|
|
mysql_log.write(thd,COM_PREPARE,"%s",packet);
|
|
|
|
mysql_init_query(thd);
|
2002-10-02 12:33:08 +02:00
|
|
|
LEX *lex=lex_start(thd, (uchar*) packet, length);
|
2002-11-22 14:50:53 +01:00
|
|
|
lex->safe_to_cache_query= 0;
|
2003-02-12 20:55:37 +01:00
|
|
|
thd->prepare_command= TRUE;
|
|
|
|
thd->lex.param_count= 0;
|
2003-01-30 21:15:44 +01:00
|
|
|
if (!yyparse((void *)thd) && !thd->is_fatal_error)
|
2002-10-02 12:33:08 +02:00
|
|
|
error= send_prepare_results(stmt);
|
|
|
|
lex_end(lex);
|
|
|
|
DBUG_RETURN(error);
|
2002-06-12 23:13:12 +02:00
|
|
|
}
|
|
|
|
|
2002-11-22 19:04:42 +01:00
|
|
|
/*
|
|
|
|
Initialize parameter items in statement
|
|
|
|
*/
|
2003-01-03 12:52:53 +01:00
|
|
|
|
2003-01-11 09:36:13 +01:00
|
|
|
static bool init_param_items(PREP_STMT *stmt)
|
2002-11-22 19:04:42 +01:00
|
|
|
{
|
2003-01-03 12:52:53 +01:00
|
|
|
List<Item> ¶ms= stmt->thd->lex.param_list;
|
2002-11-22 19:04:42 +01:00
|
|
|
Item_param **to;
|
2003-01-28 18:24:27 +01:00
|
|
|
|
2003-01-30 09:38:11 +01:00
|
|
|
stmt->lex= stmt->thd->lex;
|
2003-01-28 18:24:27 +01:00
|
|
|
if (!stmt->param_count)
|
|
|
|
stmt->param= (Item_param **)0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!(stmt->param= to= (Item_param **)
|
|
|
|
my_malloc(sizeof(Item_param *)*(stmt->param_count+1),
|
|
|
|
MYF(MY_WME))))
|
|
|
|
return 1;
|
|
|
|
List_iterator<Item> param_iterator(params);
|
|
|
|
while ((*(to++)= (Item_param *)param_iterator++));
|
|
|
|
}
|
2002-11-27 03:51:38 +01:00
|
|
|
return 0;
|
2002-11-22 19:04:42 +01:00
|
|
|
}
|
2002-10-02 12:33:08 +02:00
|
|
|
|
2003-01-11 09:36:13 +01:00
|
|
|
/*
|
|
|
|
Initialize stmt execution
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void init_stmt_execute(PREP_STMT *stmt)
|
|
|
|
{
|
|
|
|
THD *thd= stmt->thd;
|
2003-01-30 09:38:11 +01:00
|
|
|
TABLE_LIST *tables= (TABLE_LIST*) thd->lex.select_lex.table_list.first;
|
2003-01-11 09:36:13 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
TODO: When the new table structure is ready, then have a status bit
|
|
|
|
to indicate the table is altered, and re-do the setup_*
|
|
|
|
and open the tables back.
|
2003-02-25 02:22:02 +01:00
|
|
|
*/
|
|
|
|
for (; tables ; tables= tables->next)
|
2003-01-30 09:38:11 +01:00
|
|
|
tables->table= 0; //safety - nasty init
|
2003-01-11 09:36:13 +01:00
|
|
|
}
|
|
|
|
|
2002-06-12 23:13:12 +02:00
|
|
|
/*
|
|
|
|
Parse the query and send the total number of parameters
|
|
|
|
and resultset metadata information back to client (if any),
|
|
|
|
without executing the query i.e. with out any log/disk
|
|
|
|
writes. This will allow the queries to be re-executed
|
|
|
|
without re-parsing during execute.
|
|
|
|
|
|
|
|
If parameter markers are found in the query, then store
|
|
|
|
the information using Item_param along with maintaining a
|
|
|
|
list in lex->param_list, so that a fast and direct
|
|
|
|
retrieveal can be made without going through all field
|
|
|
|
items.
|
|
|
|
*/
|
|
|
|
|
2002-10-02 12:33:08 +02:00
|
|
|
bool mysql_stmt_prepare(THD *thd, char *packet, uint packet_length)
|
2002-06-12 23:13:12 +02:00
|
|
|
{
|
2003-01-28 18:24:27 +01:00
|
|
|
MEM_ROOT thd_root= thd->mem_root;
|
2002-10-02 12:33:08 +02:00
|
|
|
PREP_STMT stmt;
|
|
|
|
DBUG_ENTER("mysql_stmt_prepare");
|
2002-06-12 23:13:12 +02:00
|
|
|
|
2002-10-02 12:33:08 +02:00
|
|
|
bzero((char*) &stmt, sizeof(stmt));
|
2003-01-03 12:52:53 +01:00
|
|
|
|
2002-10-02 12:33:08 +02:00
|
|
|
stmt.stmt_id= ++thd->current_stmt_id;
|
|
|
|
init_sql_alloc(&stmt.mem_root, 8192, 8192);
|
2003-01-03 12:52:53 +01:00
|
|
|
|
|
|
|
stmt.thd= thd;
|
|
|
|
stmt.thd->mem_root= stmt.mem_root;
|
2002-10-02 12:33:08 +02:00
|
|
|
|
2003-01-03 12:52:53 +01:00
|
|
|
if (alloc_query(stmt.thd, packet, packet_length))
|
2002-10-02 12:33:08 +02:00
|
|
|
goto err;
|
2003-01-03 12:52:53 +01:00
|
|
|
|
2002-10-02 12:33:08 +02:00
|
|
|
if (parse_prepare_query(&stmt, thd->query, thd->query_length))
|
|
|
|
goto err;
|
2002-06-12 23:13:12 +02:00
|
|
|
|
|
|
|
if (!(specialflag & SPECIAL_NO_PRIOR))
|
2002-11-22 19:04:42 +01:00
|
|
|
my_pthread_setprio(pthread_self(),WAIT_PRIOR);
|
2002-12-07 08:39:11 +01:00
|
|
|
|
2003-01-03 12:52:53 +01:00
|
|
|
if (init_param_items(&stmt))
|
2002-11-22 19:04:42 +01:00
|
|
|
goto err;
|
2002-12-07 08:39:11 +01:00
|
|
|
|
2003-01-03 12:52:53 +01:00
|
|
|
stmt.mem_root= stmt.thd->mem_root;
|
2002-11-22 19:04:42 +01:00
|
|
|
tree_insert(&thd->prepared_statements, (void *)&stmt, 0, (void *)0);
|
2002-10-02 12:33:08 +02:00
|
|
|
thd->mem_root= thd_root; // restore main mem_root
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
|
|
|
|
err:
|
2003-01-03 12:52:53 +01:00
|
|
|
stmt.mem_root= stmt.thd->mem_root;
|
2002-10-02 12:33:08 +02:00
|
|
|
free_prep_stmt(&stmt, free_free, (void*) 0);
|
2003-01-28 18:24:27 +01:00
|
|
|
thd->mem_root= thd_root; // restore main mem_root
|
2002-10-02 12:33:08 +02:00
|
|
|
DBUG_RETURN(1);
|
2002-06-12 23:13:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Executes previously prepared query
|
|
|
|
|
|
|
|
If there is any parameters(thd->param_count), then replace
|
|
|
|
markers with the data supplied from client, and then
|
|
|
|
execute the query
|
|
|
|
*/
|
|
|
|
|
2002-10-02 12:33:08 +02:00
|
|
|
void mysql_stmt_execute(THD *thd, char *packet)
|
2002-06-12 23:13:12 +02:00
|
|
|
{
|
2002-10-02 12:33:08 +02:00
|
|
|
ulong stmt_id= uint4korr(packet);
|
|
|
|
PREP_STMT *stmt;
|
|
|
|
DBUG_ENTER("mysql_stmt_execute");
|
2002-06-12 23:13:12 +02:00
|
|
|
|
2002-10-02 12:33:08 +02:00
|
|
|
if (!(stmt=find_prepared_statement(thd, stmt_id, "execute")))
|
|
|
|
{
|
|
|
|
send_error(thd);
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we got an error when sending long data */
|
|
|
|
if (stmt->error_in_prepare)
|
|
|
|
{
|
2002-11-22 19:04:42 +01:00
|
|
|
send_error(thd, stmt->last_errno, stmt->last_error);
|
2002-10-02 12:33:08 +02:00
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
2003-01-30 09:38:11 +01:00
|
|
|
LEX thd_lex= thd->lex;
|
|
|
|
thd->lex= stmt->lex;
|
2003-01-11 09:36:13 +01:00
|
|
|
init_stmt_execute(stmt);
|
|
|
|
|
2002-11-23 03:30:55 +01:00
|
|
|
if (stmt->param_count && setup_params_data(stmt))
|
2002-06-12 23:13:12 +02:00
|
|
|
DBUG_VOID_RETURN;
|
2002-10-02 12:33:08 +02:00
|
|
|
|
2002-06-12 23:13:12 +02:00
|
|
|
if (!(specialflag & SPECIAL_NO_PRIOR))
|
|
|
|
my_pthread_setprio(pthread_self(),QUERY_PRIOR);
|
|
|
|
|
2002-10-02 12:33:08 +02:00
|
|
|
/*
|
|
|
|
TODO:
|
2002-06-12 23:13:12 +02:00
|
|
|
Also, have checks on basic executions such as mysql_insert(),
|
|
|
|
mysql_delete(), mysql_update() and mysql_select() to not to
|
|
|
|
have re-check on setup_* and other things ..
|
|
|
|
*/
|
2003-01-30 09:38:11 +01:00
|
|
|
thd->protocol= &thd->protocol_prep; // Switch to binary protocol
|
|
|
|
mysql_execute_command(thd);
|
|
|
|
thd->protocol= &thd->protocol_simple; // Use normal protocol
|
2002-11-22 19:04:42 +01:00
|
|
|
|
2002-06-12 23:13:12 +02:00
|
|
|
if (!(specialflag & SPECIAL_NO_PRIOR))
|
2002-10-02 12:33:08 +02:00
|
|
|
my_pthread_setprio(pthread_self(), WAIT_PRIOR);
|
2003-01-18 21:58:19 +01:00
|
|
|
|
2003-01-30 09:38:11 +01:00
|
|
|
thd->lex= thd_lex;
|
2002-06-12 23:13:12 +02:00
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
2002-10-02 12:33:08 +02:00
|
|
|
|
2002-06-12 23:13:12 +02:00
|
|
|
/*
|
2002-10-02 12:33:08 +02:00
|
|
|
Reset a prepared statement
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
mysql_stmt_reset()
|
|
|
|
thd Thread handle
|
|
|
|
packet Packet with stmt handle
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
This function is useful when one gets an error after calling
|
|
|
|
mysql_stmt_getlongdata() and one wants to reset the handle
|
|
|
|
so that one can call execute again.
|
2002-06-12 23:13:12 +02:00
|
|
|
*/
|
|
|
|
|
2002-10-02 12:33:08 +02:00
|
|
|
void mysql_stmt_reset(THD *thd, char *packet)
|
2002-06-12 23:13:12 +02:00
|
|
|
{
|
2002-10-02 12:33:08 +02:00
|
|
|
ulong stmt_id= uint4korr(packet);
|
|
|
|
PREP_STMT *stmt;
|
|
|
|
DBUG_ENTER("mysql_stmt_reset");
|
2002-06-12 23:13:12 +02:00
|
|
|
|
2002-10-02 12:33:08 +02:00
|
|
|
if (!(stmt=find_prepared_statement(thd, stmt_id, "close")))
|
|
|
|
{
|
|
|
|
send_error(thd);
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
2002-12-07 08:39:11 +01:00
|
|
|
stmt->error_in_prepare= 0;
|
|
|
|
Item_param *item= *stmt->param, *end= item + stmt->param_count;
|
2002-10-02 12:33:08 +02:00
|
|
|
|
|
|
|
/* Free long data if used */
|
|
|
|
if (stmt->long_data_used)
|
|
|
|
{
|
|
|
|
stmt->long_data_used= 0;
|
|
|
|
for (; item < end ; item++)
|
|
|
|
item->reset();
|
|
|
|
}
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Delete a prepared statement from memory
|
|
|
|
*/
|
|
|
|
|
2002-11-27 03:51:38 +01:00
|
|
|
void mysql_stmt_free(THD *thd, char *packet)
|
2002-10-02 12:33:08 +02:00
|
|
|
{
|
|
|
|
ulong stmt_id= uint4korr(packet);
|
|
|
|
PREP_STMT *stmt;
|
2002-11-27 03:51:38 +01:00
|
|
|
DBUG_ENTER("mysql_stmt_free");
|
2002-10-02 12:33:08 +02:00
|
|
|
|
|
|
|
if (!(stmt=find_prepared_statement(thd, stmt_id, "close")))
|
|
|
|
{
|
2002-12-07 08:39:11 +01:00
|
|
|
send_error(thd); // Not seen by the client
|
2002-10-02 12:33:08 +02:00
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
2003-01-28 18:24:27 +01:00
|
|
|
tree_delete(&thd->prepared_statements, (void*) &stmt_id, (void *)0);
|
|
|
|
thd->last_prepared_stmt= (PREP_STMT *)0;
|
2002-06-12 23:13:12 +02:00
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
2002-10-02 12:33:08 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Long data in pieces from client
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
mysql_stmt_get_longdata()
|
|
|
|
thd Thread handle
|
|
|
|
pos String to append
|
|
|
|
packet_length Length of string
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
Get a part of a long data.
|
|
|
|
To make the protocol efficient, we are not sending any return packages
|
|
|
|
here.
|
|
|
|
If something goes wrong, then we will send the error on 'execute'
|
|
|
|
|
|
|
|
We assume that the client takes care of checking that all parts are sent
|
|
|
|
to the server. (No checking that we get a 'end of column' in the server)
|
|
|
|
*/
|
|
|
|
|
|
|
|
void mysql_stmt_get_longdata(THD *thd, char *pos, ulong packet_length)
|
|
|
|
{
|
|
|
|
PREP_STMT *stmt;
|
|
|
|
DBUG_ENTER("mysql_stmt_get_longdata");
|
|
|
|
|
|
|
|
/* The following should never happen */
|
2002-11-22 19:04:42 +01:00
|
|
|
if (packet_length < MYSQL_LONG_DATA_HEADER+1)
|
2002-10-02 12:33:08 +02:00
|
|
|
{
|
|
|
|
my_error(ER_WRONG_ARGUMENTS, MYF(0), "get_longdata");
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong stmt_id= uint4korr(pos);
|
|
|
|
uint param_number= uint2korr(pos+4);
|
2002-12-07 08:39:11 +01:00
|
|
|
pos+= MYSQL_LONG_DATA_HEADER; // Point to data
|
2002-10-02 12:33:08 +02:00
|
|
|
|
|
|
|
if (!(stmt=find_prepared_statement(thd, stmt_id, "get_longdata")))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
There is a chance that the client will never see this as
|
|
|
|
it doesn't expect an answer from this call...
|
|
|
|
*/
|
|
|
|
send_error(thd);
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (param_number >= stmt->param_count)
|
|
|
|
{
|
2002-12-07 08:39:11 +01:00
|
|
|
/* Error will be sent in execute call */
|
|
|
|
stmt->error_in_prepare= 1;
|
|
|
|
stmt->last_errno= ER_WRONG_ARGUMENTS;
|
2002-10-02 12:33:08 +02:00
|
|
|
sprintf(stmt->last_error, ER(ER_WRONG_ARGUMENTS), "get_longdata");
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
2002-12-07 08:39:11 +01:00
|
|
|
Item_param *param= *(stmt->param+param_number);
|
|
|
|
param->set_longdata(pos, packet_length-MYSQL_LONG_DATA_HEADER-1);
|
2002-10-02 12:33:08 +02:00
|
|
|
stmt->long_data_used= 1;
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
2002-11-22 19:04:42 +01:00
|
|
|
|