-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
sql_prepare.cc
4923 lines (4061 loc) · 137 KB
/
sql_prepare.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2002, 2015, Oracle and/or its affiliates.
Copyright (c) 2008, 2015, MariaDB
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; version 2 of the License.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/**
@file
This file contains the implementation of prepared statements.
When one prepares a statement:
- Server gets the query from client with command 'COM_STMT_PREPARE';
in the following format:
[COM_STMT_PREPARE:1] [query]
- Parse the query and recognize any parameter markers '?' and
store its information list in lex->param_list
- Allocate a new statement for this prepare; and keep this in
'thd->stmt_map'.
- Without executing the query, return back to client the total
number of parameters along with result-set metadata information
(if any) in the following format:
@verbatim
[STMT_ID:4]
[Column_count:2]
[Param_count:2]
[Params meta info (stubs only for now)] (if Param_count > 0)
[Columns meta info] (if Column_count > 0)
@endverbatim
During prepare the tables used in a statement are opened, but no
locks are acquired. Table opening will block any DDL during the
operation, and we do not need any locks as we neither read nor
modify any data during prepare. Tables are closed after prepare
finishes.
When one executes a statement:
- Server gets the command 'COM_STMT_EXECUTE' to execute the
previously prepared query. If there are any parameter markers, then the
client will send the data in the following format:
@verbatim
[COM_STMT_EXECUTE:1]
[STMT_ID:4]
[NULL_BITS:(param_count+7)/8)]
[TYPES_SUPPLIED_BY_CLIENT(0/1):1]
[[length]data]
[[length]data] .. [[length]data].
@endverbatim
(Note: Except for string/binary types; all other types will not be
supplied with length field)
- If it is a first execute or types of parameters were altered by client,
then setup the conversion routines.
- Assign parameter items from the supplied data.
- Execute the query without re-parsing and send back the results
to client
During execution of prepared statement tables are opened and locked
the same way they would for normal (non-prepared) statement
execution. Tables are unlocked and closed after the execution.
When one supplies long data for a placeholder:
- Server gets the long data in pieces with command type
'COM_STMT_SEND_LONG_DATA'.
- The packet received will have the format as:
[COM_STMT_SEND_LONG_DATA:1][STMT_ID:4][parameter_number:2][data]
- data from the packet is appended to the long data value buffer for this
placeholder.
- It's up to the client to stop supplying data chunks at any point. The
server doesn't care; also, the server doesn't notify the client whether
it got the data or not; if there is any error, then it will be returned
at statement execute.
*/
#include <my_global.h> /* NO_EMBEDDED_ACCESS_CHECKS */
#include "sql_priv.h"
#include "unireg.h"
#include "sql_class.h" // set_var.h: THD
#include "set_var.h"
#include "sql_prepare.h"
#include "sql_parse.h" // insert_precheck, update_precheck, delete_precheck
#include "sql_base.h" // open_normal_and_derived_tables
#include "sql_cache.h" // query_cache_*
#include "sql_view.h" // create_view_precheck
#include "sql_delete.h" // mysql_prepare_delete
#include "sql_select.h" // for JOIN
#include "sql_insert.h" // upgrade_lock_type_for_insert, mysql_prepare_insert
#include "sql_update.h" // mysql_prepare_update
#include "sql_db.h" // mysql_opt_change_db, mysql_change_db
#include "sql_acl.h" // *_ACL
#include "sql_derived.h" // mysql_derived_prepare,
// mysql_handle_derived
#include "sql_cursor.h"
#include "sql_show.h"
#include "sql_repl.h"
#include "slave.h"
#include "sp_head.h"
#include "sp.h"
#include "sp_cache.h"
#include "sql_handler.h" // mysql_ha_rm_tables
#include "probes_mysql.h"
#ifdef EMBEDDED_LIBRARY
/* include MYSQL_BIND headers */
#include <mysql.h>
#else
#include <mysql_com.h>
#endif
#include "lock.h" // MYSQL_OPEN_FORCE_SHARED_MDL
#include "sql_handler.h"
#include "transaction.h" // trans_rollback_implicit
#include "wsrep_mysqld.h"
/**
A result class used to send cursor rows using the binary protocol.
*/
class Select_fetch_protocol_binary: public select_send
{
Protocol_binary protocol;
public:
Select_fetch_protocol_binary(THD *thd);
virtual bool send_result_set_metadata(List<Item> &list, uint flags);
virtual int send_data(List<Item> &items);
virtual bool send_eof();
#ifdef EMBEDDED_LIBRARY
void begin_dataset()
{
protocol.begin_dataset();
}
#endif
};
/****************************************************************************/
/**
Prepared_statement: a statement that can contain placeholders.
*/
class Prepared_statement: public Statement
{
public:
enum flag_values
{
IS_IN_USE= 1,
IS_SQL_PREPARE= 2
};
THD *thd;
Select_fetch_protocol_binary result;
Item_param **param_array;
Server_side_cursor *cursor;
uint param_count;
uint last_errno;
uint flags;
/*
The value of thd->select_number at the end of the PREPARE phase.
The issue is: each statement execution opens VIEWs, which may cause
select_lex objects to be created, and select_number values to be assigned.
On the other hand, PREPARE assigns select_number values for triggers and
subqueries.
In order for select_number values from EXECUTE not to conflict with
select_number values from PREPARE, we keep the number and set it at each
execution.
*/
uint select_number_after_prepare;
char last_error[MYSQL_ERRMSG_SIZE];
#ifndef EMBEDDED_LIBRARY
bool (*set_params)(Prepared_statement *st, uchar *data, uchar *data_end,
uchar *read_pos, String *expanded_query);
#else
bool (*set_params_data)(Prepared_statement *st, String *expanded_query);
#endif
bool (*set_params_from_vars)(Prepared_statement *stmt,
List<LEX_STRING>& varnames,
String *expanded_query);
public:
Prepared_statement(THD *thd_arg);
virtual ~Prepared_statement();
void setup_set_params();
virtual Query_arena::Type type() const;
virtual void cleanup_stmt();
bool set_name(LEX_STRING *name);
inline void close_cursor() { delete cursor; cursor= 0; }
inline bool is_in_use() { return flags & (uint) IS_IN_USE; }
inline bool is_sql_prepare() const { return flags & (uint) IS_SQL_PREPARE; }
void set_sql_prepare() { flags|= (uint) IS_SQL_PREPARE; }
bool prepare(const char *packet, uint packet_length);
bool execute_loop(String *expanded_query,
bool open_cursor,
uchar *packet_arg, uchar *packet_end_arg);
bool execute_server_runnable(Server_runnable *server_runnable);
/* Destroy this statement */
void deallocate();
private:
/**
The memory root to allocate parsed tree elements (instances of Item,
SELECT_LEX and other classes).
*/
MEM_ROOT main_mem_root;
private:
bool set_db(const char *db, uint db_length);
bool set_parameters(String *expanded_query,
uchar *packet, uchar *packet_end);
bool execute(String *expanded_query, bool open_cursor);
bool reprepare();
bool validate_metadata(Prepared_statement *copy);
void swap_prepared_statement(Prepared_statement *copy);
};
/**
Execute one SQL statement in an isolated context.
*/
class Execute_sql_statement: public Server_runnable
{
public:
Execute_sql_statement(LEX_STRING sql_text);
virtual bool execute_server_code(THD *thd);
private:
LEX_STRING m_sql_text;
};
class Ed_connection;
/**
Protocol_local: a helper class to intercept the result
of the data written to the network.
*/
class Protocol_local :public Protocol
{
public:
Protocol_local(THD *thd, Ed_connection *ed_connection);
~Protocol_local() { free_root(&m_rset_root, MYF(0)); }
protected:
virtual void prepare_for_resend();
virtual bool write();
virtual bool store_null();
virtual bool store_tiny(longlong from);
virtual bool store_short(longlong from);
virtual bool store_long(longlong from);
virtual bool store_longlong(longlong from, bool unsigned_flag);
virtual bool store_decimal(const my_decimal *);
virtual bool store(const char *from, size_t length, CHARSET_INFO *cs);
virtual bool store(const char *from, size_t length,
CHARSET_INFO *fromcs, CHARSET_INFO *tocs);
virtual bool store(MYSQL_TIME *time, int decimals);
virtual bool store_date(MYSQL_TIME *time);
virtual bool store_time(MYSQL_TIME *time, int decimals);
virtual bool store(float value, uint32 decimals, String *buffer);
virtual bool store(double value, uint32 decimals, String *buffer);
virtual bool store(Field *field);
virtual bool send_result_set_metadata(List<Item> *list, uint flags);
virtual bool send_out_parameters(List<Item_param> *sp_params);
#ifdef EMBEDDED_LIBRARY
void remove_last_row();
#endif
virtual enum enum_protocol_type type() { return PROTOCOL_LOCAL; };
virtual bool send_ok(uint server_status, uint statement_warn_count,
ulonglong affected_rows, ulonglong last_insert_id,
const char *message);
virtual bool send_eof(uint server_status, uint statement_warn_count);
virtual bool send_error(uint sql_errno, const char *err_msg, const char* sqlstate);
private:
bool store_string(const char *str, size_t length,
CHARSET_INFO *src_cs, CHARSET_INFO *dst_cs);
bool store_column(const void *data, size_t length);
void opt_add_row_to_rset();
private:
Ed_connection *m_connection;
MEM_ROOT m_rset_root;
List<Ed_row> *m_rset;
size_t m_column_count;
Ed_column *m_current_row;
Ed_column *m_current_column;
};
/******************************************************************************
Implementation
******************************************************************************/
inline bool is_param_null(const uchar *pos, ulong param_no)
{
return pos[param_no/8] & (1 << (param_no & 7));
}
/**
Find a prepared statement in the statement map by id.
Try to find a prepared statement and set THD error if it's not found.
@param thd thread handle
@param id statement id
@param where the place from which this function is called (for
error reporting).
@return
0 if the statement was not found, a pointer otherwise.
*/
static Prepared_statement *
find_prepared_statement(THD *thd, ulong id)
{
/*
To strictly separate namespaces of SQL prepared statements and C API
prepared statements find() will return 0 if there is a named prepared
statement with such id.
*/
Statement *stmt= thd->stmt_map.find(id);
if (stmt == 0 || stmt->type() != Query_arena::PREPARED_STATEMENT)
return NULL;
return (Prepared_statement *) stmt;
}
/**
Send prepared statement id and metadata to the client after prepare.
@todo
Fix this nasty upcast from List<Item_param> to List<Item>
@return
0 in case of success, 1 otherwise
*/
#ifndef EMBEDDED_LIBRARY
static bool send_prep_stmt(Prepared_statement *stmt, uint columns)
{
NET *net= &stmt->thd->net;
uchar buff[12];
uint tmp;
int error;
THD *thd= stmt->thd;
DBUG_ENTER("send_prep_stmt");
DBUG_PRINT("enter",("stmt->id: %lu columns: %d param_count: %d",
stmt->id, columns, stmt->param_count));
buff[0]= 0; /* OK packet indicator */
int4store(buff+1, stmt->id);
int2store(buff+5, columns);
int2store(buff+7, stmt->param_count);
buff[9]= 0; // Guard against a 4.1 client
tmp= MY_MIN(stmt->thd->get_stmt_da()->current_statement_warn_count(), 65535);
int2store(buff+10, tmp);
/*
Send types and names of placeholders to the client
XXX: fix this nasty upcast from List<Item_param> to List<Item>
*/
error= my_net_write(net, buff, sizeof(buff));
if (stmt->param_count && ! error)
{
error= thd->protocol_text.send_result_set_metadata((List<Item> *)
&stmt->lex->param_list,
Protocol::SEND_EOF);
}
if (!error)
/* Flag that a response has already been sent */
thd->get_stmt_da()->disable_status();
DBUG_RETURN(error);
}
#else
static bool send_prep_stmt(Prepared_statement *stmt,
uint columns __attribute__((unused)))
{
THD *thd= stmt->thd;
thd->client_stmt_id= stmt->id;
thd->client_param_count= stmt->param_count;
thd->clear_error();
thd->get_stmt_da()->disable_status();
return 0;
}
#endif /*!EMBEDDED_LIBRARY*/
#ifndef EMBEDDED_LIBRARY
/**
Read the length of the parameter data and return it back to
the caller.
Read data length, position the packet to the first byte after it,
and return the length to the caller.
@param packet a pointer to the data
@param len remaining packet length
@return
Length of data piece.
*/
static ulong get_param_length(uchar **packet, ulong len)
{
reg1 uchar *pos= *packet;
if (len < 1)
return 0;
if (*pos < 251)
{
(*packet)++;
return (ulong) *pos;
}
if (len < 3)
return 0;
if (*pos == 252)
{
(*packet)+=3;
return (ulong) uint2korr(pos+1);
}
if (len < 4)
return 0;
if (*pos == 253)
{
(*packet)+=4;
return (ulong) uint3korr(pos+1);
}
if (len < 5)
return 0;
(*packet)+=9; // Must be 254 when here
/*
In our client-server protocol all numbers bigger than 2^24
stored as 8 bytes with uint8korr. Here we always know that
parameter length is less than 2^4 so don't look at the second
4 bytes. But still we need to obey the protocol hence 9 in the
assignment above.
*/
return (ulong) uint4korr(pos+1);
}
#else
#define get_param_length(packet, len) len
#endif /*!EMBEDDED_LIBRARY*/
/**
Data conversion routines.
All these functions read the data from pos, convert it to requested
type and assign to param; pos is advanced to predefined length.
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.
@param param parameter item
@param pos input data buffer
@param len length of data in the buffer
*/
static void set_param_tiny(Item_param *param, uchar **pos, ulong len)
{
#ifndef EMBEDDED_LIBRARY
if (len < 1)
return;
#endif
int8 value= (int8) **pos;
param->set_int(param->unsigned_flag ? (longlong) ((uint8) value) :
(longlong) value, 4);
*pos+= 1;
}
static void set_param_short(Item_param *param, uchar **pos, ulong len)
{
int16 value;
#ifndef EMBEDDED_LIBRARY
if (len < 2)
return;
value= sint2korr(*pos);
#else
shortget(value, *pos);
#endif
param->set_int(param->unsigned_flag ? (longlong) ((uint16) value) :
(longlong) value, 6);
*pos+= 2;
}
static void set_param_int32(Item_param *param, uchar **pos, ulong len)
{
int32 value;
#ifndef EMBEDDED_LIBRARY
if (len < 4)
return;
value= sint4korr(*pos);
#else
longget(value, *pos);
#endif
param->set_int(param->unsigned_flag ? (longlong) ((uint32) value) :
(longlong) value, 11);
*pos+= 4;
}
static void set_param_int64(Item_param *param, uchar **pos, ulong len)
{
longlong value;
#ifndef EMBEDDED_LIBRARY
if (len < 8)
return;
value= (longlong) sint8korr(*pos);
#else
longlongget(value, *pos);
#endif
param->set_int(value, 21);
*pos+= 8;
}
static void set_param_float(Item_param *param, uchar **pos, ulong len)
{
float data;
#ifndef EMBEDDED_LIBRARY
if (len < 4)
return;
float4get(data,*pos);
#else
floatget(data, *pos);
#endif
param->set_double((double) data);
*pos+= 4;
}
static void set_param_double(Item_param *param, uchar **pos, ulong len)
{
double data;
#ifndef EMBEDDED_LIBRARY
if (len < 8)
return;
float8get(data,*pos);
#else
doubleget(data, *pos);
#endif
param->set_double((double) data);
*pos+= 8;
}
static void set_param_decimal(Item_param *param, uchar **pos, ulong len)
{
ulong length= get_param_length(pos, len);
param->set_decimal((char*)*pos, length);
*pos+= length;
}
#ifndef EMBEDDED_LIBRARY
/*
Read date/time/datetime parameter values from network (binary
protocol). See writing counterparts of these functions in
libmysql.c (store_param_{time,date,datetime}).
*/
/**
@todo
Add warning 'Data truncated' here
*/
static void set_param_time(Item_param *param, uchar **pos, ulong len)
{
MYSQL_TIME tm;
ulong length= get_param_length(pos, len);
if (length >= 8)
{
uchar *to= *pos;
uint day;
tm.neg= (bool) to[0];
day= (uint) sint4korr(to+1);
tm.hour= (uint) to[5] + day * 24;
tm.minute= (uint) to[6];
tm.second= (uint) to[7];
tm.second_part= (length > 8) ? (ulong) sint4korr(to+8) : 0;
if (tm.hour > 838)
{
/* TODO: add warning 'Data truncated' here */
tm.hour= 838;
tm.minute= 59;
tm.second= 59;
}
tm.day= tm.year= tm.month= 0;
}
else
set_zero_time(&tm, MYSQL_TIMESTAMP_TIME);
param->set_time(&tm, MYSQL_TIMESTAMP_TIME, MAX_TIME_FULL_WIDTH);
*pos+= length;
}
static void set_param_datetime(Item_param *param, uchar **pos, ulong len)
{
MYSQL_TIME tm;
ulong length= get_param_length(pos, len);
if (length >= 4)
{
uchar *to= *pos;
tm.neg= 0;
tm.year= (uint) sint2korr(to);
tm.month= (uint) to[2];
tm.day= (uint) to[3];
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.second_part= (length > 7) ? (ulong) sint4korr(to+7) : 0;
}
else
set_zero_time(&tm, MYSQL_TIMESTAMP_DATETIME);
param->set_time(&tm, MYSQL_TIMESTAMP_DATETIME,
MAX_DATETIME_WIDTH * MY_CHARSET_BIN_MB_MAXLEN);
*pos+= length;
}
static void set_param_date(Item_param *param, uchar **pos, ulong len)
{
MYSQL_TIME tm;
ulong length= get_param_length(pos, len);
if (length >= 4)
{
uchar *to= *pos;
tm.year= (uint) sint2korr(to);
tm.month= (uint) to[2];
tm.day= (uint) to[3];
tm.hour= tm.minute= tm.second= 0;
tm.second_part= 0;
tm.neg= 0;
}
else
set_zero_time(&tm, MYSQL_TIMESTAMP_DATE);
param->set_time(&tm, MYSQL_TIMESTAMP_DATE,
MAX_DATE_WIDTH * MY_CHARSET_BIN_MB_MAXLEN);
*pos+= length;
}
#else/*!EMBEDDED_LIBRARY*/
/**
@todo
Add warning 'Data truncated' here
*/
void set_param_time(Item_param *param, uchar **pos, ulong len)
{
MYSQL_TIME tm= *((MYSQL_TIME*)*pos);
tm.hour+= tm.day * 24;
tm.day= tm.year= tm.month= 0;
if (tm.hour > 838)
{
/* TODO: add warning 'Data truncated' here */
tm.hour= 838;
tm.minute= 59;
tm.second= 59;
}
param->set_time(&tm, MYSQL_TIMESTAMP_TIME,
MAX_TIME_WIDTH * MY_CHARSET_BIN_MB_MAXLEN);
}
void set_param_datetime(Item_param *param, uchar **pos, ulong len)
{
MYSQL_TIME tm= *((MYSQL_TIME*)*pos);
tm.neg= 0;
param->set_time(&tm, MYSQL_TIMESTAMP_DATETIME,
MAX_DATETIME_WIDTH * MY_CHARSET_BIN_MB_MAXLEN);
}
void set_param_date(Item_param *param, uchar **pos, ulong len)
{
MYSQL_TIME *to= (MYSQL_TIME*)*pos;
param->set_time(to, MYSQL_TIMESTAMP_DATE,
MAX_DATE_WIDTH * MY_CHARSET_BIN_MB_MAXLEN);
}
#endif /*!EMBEDDED_LIBRARY*/
static void set_param_str(Item_param *param, uchar **pos, ulong len)
{
ulong length= get_param_length(pos, len);
if (length > len)
length= len;
param->set_str((const char *)*pos, length);
*pos+= length;
}
#undef get_param_length
static void setup_one_conversion_function(THD *thd, Item_param *param,
uchar param_type)
{
switch (param_type) {
case MYSQL_TYPE_TINY:
param->set_param_func= set_param_tiny;
param->item_type= Item::INT_ITEM;
param->item_result_type= INT_RESULT;
break;
case MYSQL_TYPE_SHORT:
param->set_param_func= set_param_short;
param->item_type= Item::INT_ITEM;
param->item_result_type= INT_RESULT;
break;
case MYSQL_TYPE_LONG:
param->set_param_func= set_param_int32;
param->item_type= Item::INT_ITEM;
param->item_result_type= INT_RESULT;
break;
case MYSQL_TYPE_LONGLONG:
param->set_param_func= set_param_int64;
param->item_type= Item::INT_ITEM;
param->item_result_type= INT_RESULT;
break;
case MYSQL_TYPE_FLOAT:
param->set_param_func= set_param_float;
param->item_type= Item::REAL_ITEM;
param->item_result_type= REAL_RESULT;
break;
case MYSQL_TYPE_DOUBLE:
param->set_param_func= set_param_double;
param->item_type= Item::REAL_ITEM;
param->item_result_type= REAL_RESULT;
break;
case MYSQL_TYPE_DECIMAL:
case MYSQL_TYPE_NEWDECIMAL:
param->set_param_func= set_param_decimal;
param->item_type= Item::DECIMAL_ITEM;
param->item_result_type= DECIMAL_RESULT;
break;
case MYSQL_TYPE_TIME:
param->set_param_func= set_param_time;
param->item_type= Item::STRING_ITEM;
param->item_result_type= STRING_RESULT;
break;
case MYSQL_TYPE_DATE:
param->set_param_func= set_param_date;
param->item_type= Item::STRING_ITEM;
param->item_result_type= STRING_RESULT;
break;
case MYSQL_TYPE_DATETIME:
case MYSQL_TYPE_TIMESTAMP:
param->set_param_func= set_param_datetime;
param->item_type= Item::STRING_ITEM;
param->item_result_type= STRING_RESULT;
break;
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_LONG_BLOB:
case MYSQL_TYPE_BLOB:
param->set_param_func= set_param_str;
param->value.cs_info.character_set_of_placeholder= &my_charset_bin;
param->value.cs_info.character_set_client=
thd->variables.character_set_client;
DBUG_ASSERT(thd->variables.character_set_client);
param->value.cs_info.final_character_set_of_str_value= &my_charset_bin;
param->item_type= Item::STRING_ITEM;
param->item_result_type= STRING_RESULT;
break;
default:
/*
The client library ensures that we won't get any other typecodes
except typecodes above and typecodes for string types. Marking
label as 'default' lets us to handle malformed packets as well.
*/
{
CHARSET_INFO *fromcs= thd->variables.character_set_client;
CHARSET_INFO *tocs= thd->variables.collation_connection;
uint32 dummy_offset;
param->value.cs_info.character_set_of_placeholder= fromcs;
param->value.cs_info.character_set_client= fromcs;
/*
Setup source and destination character sets so that they
are different only if conversion is necessary: this will
make later checks easier.
*/
param->value.cs_info.final_character_set_of_str_value=
String::needs_conversion(0, fromcs, tocs, &dummy_offset) ?
tocs : fromcs;
param->set_param_func= set_param_str;
/*
Exact value of max_length is not known unless data is converted to
charset of connection, so we have to set it later.
*/
param->item_type= Item::STRING_ITEM;
param->item_result_type= STRING_RESULT;
}
}
param->param_type= (enum enum_field_types) param_type;
}
#ifndef EMBEDDED_LIBRARY
/**
Check whether this parameter data type is compatible with long data.
Used to detect whether a long data stream has been supplied to a
incompatible data type.
*/
inline bool is_param_long_data_type(Item_param *param)
{
return ((param->param_type >= MYSQL_TYPE_TINY_BLOB) &&
(param->param_type <= MYSQL_TYPE_STRING));
}
/**
Routines to assign parameters from data supplied by the client.
Update the parameter markers by reading data from the packet and
and generate a valid query for logging.
@note
This function, along with other _with_log functions is called when one of
binary, slow or general logs is open. Logging of prepared statements in
all cases is performed by means of conventional queries: if parameter
data was supplied from C API, each placeholder in the query is
replaced with its actual value; if we're logging a [Dynamic] SQL
prepared statement, parameter markers are replaced with variable names.
Example:
@verbatim
mysqld_stmt_prepare("UPDATE t1 SET a=a*1.25 WHERE a=?")
--> general logs gets [Prepare] UPDATE t1 SET a*1.25 WHERE a=?"
mysqld_stmt_execute(stmt);
--> general and binary logs get
[Execute] UPDATE t1 SET a*1.25 WHERE a=1"
@endverbatim
If a statement has been prepared using SQL syntax:
@verbatim
PREPARE stmt FROM "UPDATE t1 SET a=a*1.25 WHERE a=?"
--> general log gets
[Query] PREPARE stmt FROM "UPDATE ..."
EXECUTE stmt USING @a
--> general log gets
[Query] EXECUTE stmt USING @a;
@endverbatim
@retval
0 if success
@retval
1 otherwise
*/
static bool insert_params_with_log(Prepared_statement *stmt, uchar *null_array,
uchar *read_pos, uchar *data_end,
String *query)
{
THD *thd= stmt->thd;
Item_param **begin= stmt->param_array;
Item_param **end= begin + stmt->param_count;
Copy_query_with_rewrite acc(thd, stmt->query(), stmt->query_length(), query);
DBUG_ENTER("insert_params_with_log");
for (Item_param **it= begin; it < end; ++it)
{
Item_param *param= *it;
if (param->state != Item_param::LONG_DATA_VALUE)
{
if (is_param_null(null_array, (uint) (it - begin)))
param->set_null();
else
{
if (read_pos >= data_end)
DBUG_RETURN(1);
param->set_param_func(param, &read_pos, (uint) (data_end - read_pos));
if (param->state == Item_param::NO_VALUE)
DBUG_RETURN(1);
if (param->limit_clause_param && param->state != Item_param::INT_VALUE)
{
param->set_int(param->val_int(), MY_INT64_NUM_DECIMAL_DIGITS);
param->item_type= Item::INT_ITEM;
if (!param->unsigned_flag && param->value.integer < 0)
DBUG_RETURN(1);
}
}
}
/*
A long data stream was supplied for this parameter marker.
This was done after prepare, prior to providing a placeholder
type (the types are supplied at execute). Check that the
supplied type of placeholder can accept a data stream.
*/
else if (! is_param_long_data_type(param))
DBUG_RETURN(1);
if (acc.append(param))
DBUG_RETURN(1);
if (param->convert_str_value(thd))
DBUG_RETURN(1); /* out of memory */
}
if (acc.finalize())
DBUG_RETURN(1);
DBUG_RETURN(0);
}
static bool insert_params(Prepared_statement *stmt, uchar *null_array,
uchar *read_pos, uchar *data_end,
String *expanded_query)
{
Item_param **begin= stmt->param_array;
Item_param **end= begin + stmt->param_count;
DBUG_ENTER("insert_params");
for (Item_param **it= begin; it < end; ++it)
{
Item_param *param= *it;
if (param->state != Item_param::LONG_DATA_VALUE)
{
if (is_param_null(null_array, (uint) (it - begin)))
param->set_null();
else
{
if (read_pos >= data_end)
DBUG_RETURN(1);
param->set_param_func(param, &read_pos, (uint) (data_end - read_pos));
if (param->state == Item_param::NO_VALUE)
DBUG_RETURN(1);
}
}
/*
A long data stream was supplied for this parameter marker.
This was done after prepare, prior to providing a placeholder
type (the types are supplied at execute). Check that the
supplied type of placeholder can accept a data stream.
*/
else if (! is_param_long_data_type(param))
DBUG_RETURN(1);
if (param->convert_str_value(stmt->thd))
DBUG_RETURN(1); /* out of memory */
}
DBUG_RETURN(0);
}
static bool setup_conversion_functions(Prepared_statement *stmt,
uchar **data, uchar *data_end)
{
/* skip null bits */
uchar *read_pos= *data + (stmt->param_count+7) / 8;
DBUG_ENTER("setup_conversion_functions");
if (*read_pos++) //types supplied / first execute
{
/*
First execute or types altered by the client, setup the
conversion routines for all parameters (one time)
*/
Item_param **it= stmt->param_array;
Item_param **end= it + stmt->param_count;
THD *thd= stmt->thd;
for (; it < end; ++it)
{
ushort typecode;
const uint signed_bit= 1 << 15;
if (read_pos >= data_end)
DBUG_RETURN(1);
typecode= sint2korr(read_pos);
read_pos+= 2;
(**it).unsigned_flag= MY_TEST(typecode & signed_bit);
setup_one_conversion_function(thd, *it, (uchar) (typecode & ~signed_bit));
}
}
*data= read_pos;
DBUG_RETURN(0);