1
/******************************************************
6
Created 11/19/1996 Heikki Tuuri
7
*******************************************************/
13
#include "que0types.h"
14
#include "usr0types.h"
15
#include "pars0types.h"
16
#include "row0types.h"
17
#include "trx0types.h"
20
/* Type of the user functions. The first argument is always InnoDB-supplied
21
and varies in type, while 'user_arg' is a user-supplied argument. The
22
meaning of the return type also varies. See the individual use cases, e.g.
23
the FETCH statement, for details on them. */
24
typedef void* (*pars_user_func_cb_t)(void* arg, void* user_arg);
28
/* If the following is set TRUE, the lexer will print the SQL string
32
extern ibool pars_print_lexed;
33
#endif /* UNIV_SQL_DEBUG */
35
/* Global variable used while parsing a single procedure or query : the code is
37
extern sym_tab_t* pars_sym_tab_global;
39
extern pars_res_word_t pars_to_char_token;
40
extern pars_res_word_t pars_to_number_token;
41
extern pars_res_word_t pars_to_binary_token;
42
extern pars_res_word_t pars_binary_to_number_token;
43
extern pars_res_word_t pars_substr_token;
44
extern pars_res_word_t pars_replstr_token;
45
extern pars_res_word_t pars_concat_token;
46
extern pars_res_word_t pars_length_token;
47
extern pars_res_word_t pars_instr_token;
48
extern pars_res_word_t pars_sysdate_token;
49
extern pars_res_word_t pars_printf_token;
50
extern pars_res_word_t pars_assert_token;
51
extern pars_res_word_t pars_rnd_token;
52
extern pars_res_word_t pars_rnd_str_token;
53
extern pars_res_word_t pars_count_token;
54
extern pars_res_word_t pars_sum_token;
55
extern pars_res_word_t pars_distinct_token;
56
extern pars_res_word_t pars_binary_token;
57
extern pars_res_word_t pars_blob_token;
58
extern pars_res_word_t pars_int_token;
59
extern pars_res_word_t pars_char_token;
60
extern pars_res_word_t pars_float_token;
61
extern pars_res_word_t pars_update_token;
62
extern pars_res_word_t pars_asc_token;
63
extern pars_res_word_t pars_desc_token;
64
extern pars_res_word_t pars_open_token;
65
extern pars_res_word_t pars_close_token;
66
extern pars_res_word_t pars_share_token;
67
extern pars_res_word_t pars_unique_token;
68
extern pars_res_word_t pars_clustered_token;
70
extern ulint pars_star_denoter;
72
/* Procedure parameter types */
75
#define PARS_NOT_PARAM 2
78
/*****************************************************************
79
Parses an SQL string returning the query graph. */
84
/* out, own: the query graph */
85
pars_info_t* info, /* in: extra information, or NULL */
86
const char* str); /* in: SQL string */
87
/*****************************************************************
88
Retrieves characters to the lexical analyzer. */
93
char* buf, /* in/out: buffer where to copy */
94
int* result, /* out: number of characters copied or EOF */
95
int max_size); /* in: maximum number of characters which fit
97
/*****************************************************************
98
Called by yyparse on error. */
103
const char* s); /* in: error message string */
104
/*************************************************************************
105
Parses a variable declaration. */
108
pars_variable_declaration(
109
/*======================*/
110
/* out, own: symbol table node of type
112
sym_node_t* node, /* in: symbol table node allocated for the
113
id of the variable */
114
pars_res_word_t* type); /* in: pointer to a type token */
115
/*************************************************************************
116
Parses a function expression. */
121
/* out, own: function node in a query tree */
122
que_node_t* res_word,/* in: function name reserved word */
123
que_node_t* arg); /* in: first argument in the argument list */
124
/*************************************************************************
125
Parses an operator expression. */
130
/* out, own: function node in a query tree */
131
int func, /* in: operator token code */
132
que_node_t* arg1, /* in: first argument */
133
que_node_t* arg2); /* in: second argument or NULL for an unary
135
/*************************************************************************
136
Parses an ORDER BY clause. Order by a single column only is supported. */
141
/* out, own: order-by node in a query tree */
142
sym_node_t* column, /* in: column name */
143
pars_res_word_t* asc); /* in: &pars_asc_token or pars_desc_token */
144
/*************************************************************************
145
Parses a select list; creates a query graph node for the whole SELECT
151
/* out, own: select node in a query
153
que_node_t* select_list, /* in: select list */
154
sym_node_t* into_list); /* in: variables list or NULL */
155
/*************************************************************************
156
Parses a cursor declaration. */
159
pars_cursor_declaration(
160
/*====================*/
162
sym_node_t* sym_node, /* in: cursor id node in the symbol
164
sel_node_t* select_node); /* in: select node */
165
/*************************************************************************
166
Parses a function declaration. */
169
pars_function_declaration(
170
/*======================*/
172
sym_node_t* sym_node); /* in: function id node in the symbol
174
/*************************************************************************
175
Parses a select statement. */
178
pars_select_statement(
179
/*==================*/
180
/* out, own: select node in a query
182
sel_node_t* select_node, /* in: select node already containing
184
sym_node_t* table_list, /* in: table list */
185
que_node_t* search_cond, /* in: search condition or NULL */
186
pars_res_word_t* for_update, /* in: NULL or &pars_update_token */
187
pars_res_word_t* consistent_read,/* in: NULL or
188
&pars_consistent_token */
189
order_node_t* order_by); /* in: NULL or an order-by node */
190
/*************************************************************************
191
Parses a column assignment in an update. */
194
pars_column_assignment(
195
/*===================*/
196
/* out: column assignment node */
197
sym_node_t* column, /* in: column to assign */
198
que_node_t* exp); /* in: value to assign */
199
/*************************************************************************
200
Parses a delete or update statement start. */
203
pars_update_statement_start(
204
/*========================*/
205
/* out, own: update node in a query
207
ibool is_delete, /* in: TRUE if delete */
208
sym_node_t* table_sym, /* in: table name node */
209
col_assign_node_t* col_assign_list);/* in: column assignment list, NULL
211
/*************************************************************************
212
Parses an update or delete statement. */
215
pars_update_statement(
216
/*==================*/
217
/* out, own: update node in a query
219
upd_node_t* node, /* in: update node */
220
sym_node_t* cursor_sym, /* in: pointer to a cursor entry in
221
the symbol table or NULL */
222
que_node_t* search_cond); /* in: search condition or NULL */
223
/*************************************************************************
224
Parses an insert statement. */
227
pars_insert_statement(
228
/*==================*/
229
/* out, own: update node in a query
231
sym_node_t* table_sym, /* in: table name node */
232
que_node_t* values_list, /* in: value expression list or NULL */
233
sel_node_t* select); /* in: select condition or NULL */
234
/*************************************************************************
235
Parses a procedure parameter declaration. */
238
pars_parameter_declaration(
239
/*=======================*/
240
/* out, own: symbol table node of type
242
sym_node_t* node, /* in: symbol table node allocated for the
243
id of the parameter */
245
/* in: PARS_INPUT or PARS_OUTPUT */
246
pars_res_word_t* type); /* in: pointer to a type token */
247
/*************************************************************************
248
Parses an elsif element. */
253
/* out: elsif node */
254
que_node_t* cond, /* in: if-condition */
255
que_node_t* stat_list); /* in: statement list */
256
/*************************************************************************
257
Parses an if-statement. */
262
/* out: if-statement node */
263
que_node_t* cond, /* in: if-condition */
264
que_node_t* stat_list, /* in: statement list */
265
que_node_t* else_part); /* in: else-part statement list */
266
/*************************************************************************
267
Parses a for-loop-statement. */
272
/* out: for-statement node */
273
sym_node_t* loop_var, /* in: loop variable */
274
que_node_t* loop_start_limit,/* in: loop start expression */
275
que_node_t* loop_end_limit, /* in: loop end expression */
276
que_node_t* stat_list); /* in: statement list */
277
/*************************************************************************
278
Parses a while-statement. */
281
pars_while_statement(
282
/*=================*/
283
/* out: while-statement node */
284
que_node_t* cond, /* in: while-condition */
285
que_node_t* stat_list); /* in: statement list */
286
/*************************************************************************
287
Parses an exit statement. */
290
pars_exit_statement(void);
291
/*=====================*/
292
/* out: exit statement node */
293
/*************************************************************************
294
Parses a return-statement. */
297
pars_return_statement(void);
298
/*=======================*/
299
/* out: return-statement node */
300
/*************************************************************************
301
Parses a procedure call. */
306
/* out: function node */
307
que_node_t* res_word,/* in: procedure name reserved word */
308
que_node_t* args); /* in: argument list */
309
/*************************************************************************
310
Parses an assignment statement. */
313
pars_assignment_statement(
314
/*======================*/
315
/* out: assignment statement node */
316
sym_node_t* var, /* in: variable to assign */
317
que_node_t* val); /* in: value to assign */
318
/*************************************************************************
319
Parses a fetch statement. into_list or user_func (but not both) must be
323
pars_fetch_statement(
324
/*=================*/
325
/* out: fetch statement node */
326
sym_node_t* cursor, /* in: cursor node */
327
sym_node_t* into_list, /* in: variables to set, or NULL */
328
sym_node_t* user_func); /* in: user function name, or NULL */
329
/*************************************************************************
330
Parses an open or close cursor statement. */
335
/* out: fetch statement node */
336
ulint type, /* in: ROW_SEL_OPEN_CURSOR
337
or ROW_SEL_CLOSE_CURSOR */
338
sym_node_t* cursor); /* in: cursor node */
339
/*************************************************************************
340
Parses a row_printf-statement. */
343
pars_row_printf_statement(
344
/*======================*/
345
/* out: row_printf-statement node */
346
sel_node_t* sel_node); /* in: select node */
347
/*************************************************************************
348
Parses a commit statement. */
351
pars_commit_statement(void);
352
/*=======================*/
353
/*************************************************************************
354
Parses a rollback statement. */
357
pars_rollback_statement(void);
358
/*=========================*/
359
/*************************************************************************
360
Parses a column definition at a table creation. */
365
/* out: column sym table
367
sym_node_t* sym_node, /* in: column node in the
369
pars_res_word_t* type, /* in: data type */
370
sym_node_t* len, /* in: length of column, or
372
void* is_unsigned, /* in: if not NULL, column
373
is of type UNSIGNED. */
374
void* is_not_null); /* in: if not NULL, column
375
is of type NOT NULL. */
376
/*************************************************************************
377
Parses a table creation operation. */
382
/* out: table create subgraph */
383
sym_node_t* table_sym, /* in: table name node in the symbol
385
sym_node_t* column_defs, /* in: list of column names */
386
void* not_fit_in_memory);/* in: a non-NULL pointer means that
387
this is a table which in simulations
388
should be simulated as not fitting
389
in memory; thread is put to sleep
390
to simulate disk accesses; NOTE that
391
this flag is not stored to the data
392
dictionary on disk, and the database
393
will forget about non-NULL value if
394
it has to reload the table definition
396
/*************************************************************************
397
Parses an index creation operation. */
402
/* out: index create subgraph */
403
pars_res_word_t* unique_def, /* in: not NULL if a unique index */
404
pars_res_word_t* clustered_def, /* in: not NULL if a clustered index */
405
sym_node_t* index_sym, /* in: index name node in the symbol
407
sym_node_t* table_sym, /* in: table name node in the symbol
409
sym_node_t* column_list); /* in: list of column names */
410
/*************************************************************************
411
Parses a procedure definition. */
414
pars_procedure_definition(
415
/*======================*/
416
/* out: query fork node */
417
sym_node_t* sym_node, /* in: procedure id node in the symbol
419
sym_node_t* param_list, /* in: parameter declaration list */
420
que_node_t* stat_list); /* in: statement list */
422
/*****************************************************************
423
Parses a stored procedure call, when this is not within another stored
424
procedure, that is, the client issues a procedure call directly.
425
In MySQL/InnoDB, stored InnoDB procedures are invoked via the
426
parsed procedure tree, not via InnoDB SQL, so this function is not used. */
429
pars_stored_procedure_call(
430
/*=======================*/
431
/* out: query graph */
432
sym_node_t* sym_node); /* in: stored procedure name */
433
/**********************************************************************
434
Completes a query graph by adding query thread and fork nodes
435
above it and prepares the graph for running. The fork created is of
436
type QUE_FORK_MYSQL_INTERFACE. */
439
pars_complete_graph_for_exec(
440
/*=========================*/
441
/* out: query thread node to run */
442
que_node_t* node, /* in: root node for an incomplete
444
trx_t* trx, /* in: transaction handle */
445
mem_heap_t* heap); /* in: memory heap from which allocated */
447
/********************************************************************
448
Create parser info struct.*/
451
pars_info_create(void);
452
/*==================*/
453
/* out, own: info struct */
455
/********************************************************************
456
Free info struct and everything it contains.*/
461
pars_info_t* info); /* in: info struct */
463
/********************************************************************
464
Add bound literal. */
467
pars_info_add_literal(
468
/*==================*/
469
pars_info_t* info, /* in: info struct */
470
const char* name, /* in: name */
471
const void* address, /* in: address */
472
ulint length, /* in: length of data */
473
ulint type, /* in: type, e.g. DATA_FIXBINARY */
474
ulint prtype); /* in: precise type, e.g.
477
/********************************************************************
478
Equivalent to pars_info_add_literal(info, name, str, strlen(str),
479
DATA_VARCHAR, DATA_ENGLISH). */
482
pars_info_add_str_literal(
483
/*======================*/
484
pars_info_t* info, /* in: info struct */
485
const char* name, /* in: name */
486
const char* str); /* in: string */
488
/********************************************************************
492
mach_write_to_4(buf, val);
493
pars_info_add_literal(info, name, buf, 4, DATA_INT, 0);
495
except that the buffer is dynamically allocated from the info struct's
499
pars_info_add_int4_literal(
500
/*=======================*/
501
pars_info_t* info, /* in: info struct */
502
const char* name, /* in: name */
503
lint val); /* in: value */
505
/********************************************************************
509
mach_write_to_8(buf, val);
510
pars_info_add_literal(info, name, buf, 8, DATA_BINARY, 0);
512
except that the buffer is dynamically allocated from the info struct's
516
pars_info_add_dulint_literal(
517
/*=========================*/
518
pars_info_t* info, /* in: info struct */
519
const char* name, /* in: name */
520
dulint val); /* in: value */
521
/********************************************************************
522
Add user function. */
525
pars_info_add_function(
526
/*===================*/
527
pars_info_t* info, /* in: info struct */
528
const char* name, /* in: function name */
529
pars_user_func_cb_t func, /* in: function address */
530
void* arg); /* in: user-supplied argument */
532
/********************************************************************
538
pars_info_t* info, /* in: info struct */
539
const char* name, /* in: name */
540
const char* id); /* in: id */
542
/********************************************************************
543
Get user function with the given name.*/
546
pars_info_get_user_func(
547
/*====================*/
548
/* out: user func, or NULL if not
550
pars_info_t* info, /* in: info struct */
551
const char* name); /* in: function name to find*/
553
/********************************************************************
554
Get bound literal with the given name.*/
557
pars_info_get_bound_lit(
558
/*====================*/
559
/* out: bound literal, or NULL if
561
pars_info_t* info, /* in: info struct */
562
const char* name); /* in: bound literal name to find */
564
/********************************************************************
565
Get bound id with the given name.*/
568
pars_info_get_bound_id(
569
/*===================*/
570
/* out: bound id, or NULL if not
572
pars_info_t* info, /* in: info struct */
573
const char* name); /* in: bound id name to find */
576
/* Extra information supplied for pars_sql(). */
577
struct pars_info_struct {
578
mem_heap_t* heap; /* our own memory heap */
580
ib_vector_t* funcs; /* user functions, or NUll
581
(pars_user_func_t*) */
582
ib_vector_t* bound_lits; /* bound literals, or NULL
583
(pars_bound_lit_t*) */
584
ib_vector_t* bound_ids; /* bound ids, or NULL
585
(pars_bound_id_t*) */
587
ibool graph_owns_us; /* if TRUE (which is the default),
588
que_graph_free() will free us */
591
/* User-supplied function and argument. */
592
struct pars_user_func_struct {
593
const char* name; /* function name */
594
pars_user_func_cb_t func; /* function address */
595
void* arg; /* user-supplied argument */
599
struct pars_bound_lit_struct {
600
const char* name; /* name */
601
const void* address; /* address */
602
ulint length; /* length of data */
603
ulint type; /* type, e.g. DATA_FIXBINARY */
604
ulint prtype; /* precise type, e.g. DATA_UNSIGNED */
608
struct pars_bound_id_struct {
609
const char* name; /* name */
610
const char* id; /* id */
613
/* Struct used to denote a reserved word in a parsing tree */
614
struct pars_res_word_struct{
615
int code; /* the token code for the reserved word from
619
/* A predefined function or operator node in a parsing tree; this construct
620
is also used for some non-functions like the assignment ':=' */
621
struct func_node_struct{
622
que_common_t common; /* type: QUE_NODE_FUNC */
623
int func; /* token code of the function name */
624
ulint klass; /* class of the function */
625
que_node_t* args; /* argument(s) of the function */
626
UT_LIST_NODE_T(func_node_t) cond_list;
627
/* list of comparison conditions; defined
628
only for comparison operator nodes except,
629
presently, for OPT_SCROLL_TYPE ones */
630
UT_LIST_NODE_T(func_node_t) func_node_list;
631
/* list of function nodes in a parsed
635
/* An order-by node in a select */
636
struct order_node_struct{
637
que_common_t common; /* type: QUE_NODE_ORDER */
638
sym_node_t* column; /* order-by column */
639
ibool asc; /* TRUE if ascending, FALSE if descending */
642
/* Procedure definition node */
643
struct proc_node_struct{
644
que_common_t common; /* type: QUE_NODE_PROC */
645
sym_node_t* proc_id; /* procedure name symbol in the symbol
646
table of this same procedure */
647
sym_node_t* param_list; /* input and output parameters */
648
que_node_t* stat_list; /* statement list */
649
sym_tab_t* sym_tab; /* symbol table of this procedure */
652
/* elsif-element node */
653
struct elsif_node_struct{
654
que_common_t common; /* type: QUE_NODE_ELSIF */
655
que_node_t* cond; /* if condition */
656
que_node_t* stat_list; /* statement list */
659
/* if-statement node */
660
struct if_node_struct{
661
que_common_t common; /* type: QUE_NODE_IF */
662
que_node_t* cond; /* if condition */
663
que_node_t* stat_list; /* statement list */
664
que_node_t* else_part; /* else-part statement list */
665
elsif_node_t* elsif_list; /* elsif element list */
668
/* while-statement node */
669
struct while_node_struct{
670
que_common_t common; /* type: QUE_NODE_WHILE */
671
que_node_t* cond; /* while condition */
672
que_node_t* stat_list; /* statement list */
675
/* for-loop-statement node */
676
struct for_node_struct{
677
que_common_t common; /* type: QUE_NODE_FOR */
678
sym_node_t* loop_var; /* loop variable: this is the
679
dereferenced symbol from the
680
variable declarations, not the
681
symbol occurrence in the for loop
683
que_node_t* loop_start_limit;/* initial value of loop variable */
684
que_node_t* loop_end_limit; /* end value of loop variable */
685
int loop_end_value; /* evaluated value for the end value:
686
it is calculated only when the loop
687
is entered, and will not change within
689
que_node_t* stat_list; /* statement list */
692
/* exit statement node */
693
struct exit_node_struct{
694
que_common_t common; /* type: QUE_NODE_EXIT */
697
/* return-statement node */
698
struct return_node_struct{
699
que_common_t common; /* type: QUE_NODE_RETURN */
702
/* Assignment statement node */
703
struct assign_node_struct{
704
que_common_t common; /* type: QUE_NODE_ASSIGNMENT */
705
sym_node_t* var; /* variable to set */
706
que_node_t* val; /* value to assign */
709
/* Column assignment node */
710
struct col_assign_node_struct{
711
que_common_t common; /* type: QUE_NODE_COL_ASSIGN */
712
sym_node_t* col; /* column to set */
713
que_node_t* val; /* value to assign */
716
/* Classes of functions */
717
#define PARS_FUNC_ARITH 1 /* +, -, *, / */
718
#define PARS_FUNC_LOGICAL 2
719
#define PARS_FUNC_CMP 3
720
#define PARS_FUNC_PREDEFINED 4 /* TO_NUMBER, SUBSTR, ... */
721
#define PARS_FUNC_AGGREGATE 5 /* COUNT, DISTINCT, SUM */
722
#define PARS_FUNC_OTHER 6 /* these are not real functions,
726
#include "pars0pars.ic"