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
80
/*****************************************************************
81
Parses an SQL string returning the query graph. */
86
/* out, own: the query graph */
87
pars_info_t* info, /* in: extra information, or NULL */
88
const char* str); /* in: SQL string */
89
/*****************************************************************
90
Retrieves characters to the lexical analyzer. */
95
char* buf, /* in/out: buffer where to copy */
96
int* result, /* out: number of characters copied or EOF */
97
int max_size); /* in: maximum number of characters which fit
99
/*****************************************************************
100
Called by yyparse on error. */
105
const char* s); /* in: error message string */
106
/*************************************************************************
107
Parses a variable declaration. */
110
pars_variable_declaration(
111
/*======================*/
112
/* out, own: symbol table node of type
114
sym_node_t* node, /* in: symbol table node allocated for the
115
id of the variable */
116
pars_res_word_t* type); /* in: pointer to a type token */
117
/*************************************************************************
118
Parses a function expression. */
123
/* out, own: function node in a query tree */
124
que_node_t* res_word,/* in: function name reserved word */
125
que_node_t* arg); /* in: first argument in the argument list */
126
/*************************************************************************
127
Parses an operator expression. */
132
/* out, own: function node in a query tree */
133
int func, /* in: operator token code */
134
que_node_t* arg1, /* in: first argument */
135
que_node_t* arg2); /* in: second argument or NULL for an unary
137
/*************************************************************************
138
Parses an ORDER BY clause. Order by a single column only is supported. */
143
/* out, own: order-by node in a query tree */
144
sym_node_t* column, /* in: column name */
145
pars_res_word_t* asc); /* in: &pars_asc_token or pars_desc_token */
146
/*************************************************************************
147
Parses a select list; creates a query graph node for the whole SELECT
153
/* out, own: select node in a query
155
que_node_t* select_list, /* in: select list */
156
sym_node_t* into_list); /* in: variables list or NULL */
157
/*************************************************************************
158
Parses a cursor declaration. */
161
pars_cursor_declaration(
162
/*====================*/
164
sym_node_t* sym_node, /* in: cursor id node in the symbol
166
sel_node_t* select_node); /* in: select node */
167
/*************************************************************************
168
Parses a function declaration. */
171
pars_function_declaration(
172
/*======================*/
174
sym_node_t* sym_node); /* in: function id node in the symbol
176
/*************************************************************************
177
Parses a select statement. */
180
pars_select_statement(
181
/*==================*/
182
/* out, own: select node in a query
184
sel_node_t* select_node, /* in: select node already containing
186
sym_node_t* table_list, /* in: table list */
187
que_node_t* search_cond, /* in: search condition or NULL */
188
pars_res_word_t* for_update, /* in: NULL or &pars_update_token */
189
pars_res_word_t* consistent_read,/* in: NULL or
190
&pars_consistent_token */
191
order_node_t* order_by); /* in: NULL or an order-by node */
192
/*************************************************************************
193
Parses a column assignment in an update. */
196
pars_column_assignment(
197
/*===================*/
198
/* out: column assignment node */
199
sym_node_t* column, /* in: column to assign */
200
que_node_t* exp); /* in: value to assign */
201
/*************************************************************************
202
Parses a delete or update statement start. */
205
pars_update_statement_start(
206
/*========================*/
207
/* out, own: update node in a query
209
ibool is_delete, /* in: TRUE if delete */
210
sym_node_t* table_sym, /* in: table name node */
211
col_assign_node_t* col_assign_list);/* in: column assignment list, NULL
213
/*************************************************************************
214
Parses an update or delete statement. */
217
pars_update_statement(
218
/*==================*/
219
/* out, own: update node in a query
221
upd_node_t* node, /* in: update node */
222
sym_node_t* cursor_sym, /* in: pointer to a cursor entry in
223
the symbol table or NULL */
224
que_node_t* search_cond); /* in: search condition or NULL */
225
/*************************************************************************
226
Parses an insert statement. */
229
pars_insert_statement(
230
/*==================*/
231
/* out, own: update node in a query
233
sym_node_t* table_sym, /* in: table name node */
234
que_node_t* values_list, /* in: value expression list or NULL */
235
sel_node_t* select); /* in: select condition or NULL */
236
/*************************************************************************
237
Parses a procedure parameter declaration. */
240
pars_parameter_declaration(
241
/*=======================*/
242
/* out, own: symbol table node of type
244
sym_node_t* node, /* in: symbol table node allocated for the
245
id of the parameter */
247
/* in: PARS_INPUT or PARS_OUTPUT */
248
pars_res_word_t* type); /* in: pointer to a type token */
249
/*************************************************************************
250
Parses an elsif element. */
255
/* out: elsif node */
256
que_node_t* cond, /* in: if-condition */
257
que_node_t* stat_list); /* in: statement list */
258
/*************************************************************************
259
Parses an if-statement. */
264
/* out: if-statement node */
265
que_node_t* cond, /* in: if-condition */
266
que_node_t* stat_list, /* in: statement list */
267
que_node_t* else_part); /* in: else-part statement list */
268
/*************************************************************************
269
Parses a for-loop-statement. */
274
/* out: for-statement node */
275
sym_node_t* loop_var, /* in: loop variable */
276
que_node_t* loop_start_limit,/* in: loop start expression */
277
que_node_t* loop_end_limit, /* in: loop end expression */
278
que_node_t* stat_list); /* in: statement list */
279
/*************************************************************************
280
Parses a while-statement. */
283
pars_while_statement(
284
/*=================*/
285
/* out: while-statement node */
286
que_node_t* cond, /* in: while-condition */
287
que_node_t* stat_list); /* in: statement list */
288
/*************************************************************************
289
Parses an exit statement. */
292
pars_exit_statement(void);
293
/*=====================*/
294
/* out: exit statement node */
295
/*************************************************************************
296
Parses a return-statement. */
299
pars_return_statement(void);
300
/*=======================*/
301
/* out: return-statement node */
302
/*************************************************************************
303
Parses a procedure call. */
308
/* out: function node */
309
que_node_t* res_word,/* in: procedure name reserved word */
310
que_node_t* args); /* in: argument list */
311
/*************************************************************************
312
Parses an assignment statement. */
315
pars_assignment_statement(
316
/*======================*/
317
/* out: assignment statement node */
318
sym_node_t* var, /* in: variable to assign */
319
que_node_t* val); /* in: value to assign */
320
/*************************************************************************
321
Parses a fetch statement. into_list or user_func (but not both) must be
325
pars_fetch_statement(
326
/*=================*/
327
/* out: fetch statement node */
328
sym_node_t* cursor, /* in: cursor node */
329
sym_node_t* into_list, /* in: variables to set, or NULL */
330
sym_node_t* user_func); /* in: user function name, or NULL */
331
/*************************************************************************
332
Parses an open or close cursor statement. */
337
/* out: fetch statement node */
338
ulint type, /* in: ROW_SEL_OPEN_CURSOR
339
or ROW_SEL_CLOSE_CURSOR */
340
sym_node_t* cursor); /* in: cursor node */
341
/*************************************************************************
342
Parses a row_printf-statement. */
345
pars_row_printf_statement(
346
/*======================*/
347
/* out: row_printf-statement node */
348
sel_node_t* sel_node); /* in: select node */
349
/*************************************************************************
350
Parses a commit statement. */
353
pars_commit_statement(void);
354
/*=======================*/
355
/*************************************************************************
356
Parses a rollback statement. */
359
pars_rollback_statement(void);
360
/*=========================*/
361
/*************************************************************************
362
Parses a column definition at a table creation. */
367
/* out: column sym table
369
sym_node_t* sym_node, /* in: column node in the
371
pars_res_word_t* type, /* in: data type */
372
sym_node_t* len, /* in: length of column, or
374
void* is_unsigned, /* in: if not NULL, column
375
is of type UNSIGNED. */
376
void* is_not_null); /* in: if not NULL, column
377
is of type NOT NULL. */
378
/*************************************************************************
379
Parses a table creation operation. */
384
/* out: table create subgraph */
385
sym_node_t* table_sym, /* in: table name node in the symbol
387
sym_node_t* column_defs, /* in: list of column names */
388
void* not_fit_in_memory);/* in: a non-NULL pointer means that
389
this is a table which in simulations
390
should be simulated as not fitting
391
in memory; thread is put to sleep
392
to simulate disk accesses; NOTE that
393
this flag is not stored to the data
394
dictionary on disk, and the database
395
will forget about non-NULL value if
396
it has to reload the table definition
398
/*************************************************************************
399
Parses an index creation operation. */
404
/* out: index create subgraph */
405
pars_res_word_t* unique_def, /* in: not NULL if a unique index */
406
pars_res_word_t* clustered_def, /* in: not NULL if a clustered index */
407
sym_node_t* index_sym, /* in: index name node in the symbol
409
sym_node_t* table_sym, /* in: table name node in the symbol
411
sym_node_t* column_list); /* in: list of column names */
412
/*************************************************************************
413
Parses a procedure definition. */
416
pars_procedure_definition(
417
/*======================*/
418
/* out: query fork node */
419
sym_node_t* sym_node, /* in: procedure id node in the symbol
421
sym_node_t* param_list, /* in: parameter declaration list */
422
que_node_t* stat_list); /* in: statement list */
424
/*****************************************************************
425
Parses a stored procedure call, when this is not within another stored
426
procedure, that is, the client issues a procedure call directly.
427
In MySQL/InnoDB, stored InnoDB procedures are invoked via the
428
parsed procedure tree, not via InnoDB SQL, so this function is not used. */
431
pars_stored_procedure_call(
432
/*=======================*/
433
/* out: query graph */
434
sym_node_t* sym_node); /* in: stored procedure name */
435
/**********************************************************************
436
Completes a query graph by adding query thread and fork nodes
437
above it and prepares the graph for running. The fork created is of
438
type QUE_FORK_MYSQL_INTERFACE. */
441
pars_complete_graph_for_exec(
442
/*=========================*/
443
/* out: query thread node to run */
444
que_node_t* node, /* in: root node for an incomplete
446
trx_t* trx, /* in: transaction handle */
447
mem_heap_t* heap); /* in: memory heap from which allocated */
449
/********************************************************************
450
Create parser info struct.*/
453
pars_info_create(void);
454
/*==================*/
455
/* out, own: info struct */
457
/********************************************************************
458
Free info struct and everything it contains.*/
463
pars_info_t* info); /* in: info struct */
465
/********************************************************************
466
Add bound literal. */
469
pars_info_add_literal(
470
/*==================*/
471
pars_info_t* info, /* in: info struct */
472
const char* name, /* in: name */
473
const void* address, /* in: address */
474
ulint length, /* in: length of data */
475
ulint type, /* in: type, e.g. DATA_FIXBINARY */
476
ulint prtype); /* in: precise type, e.g.
479
/********************************************************************
480
Equivalent to pars_info_add_literal(info, name, str, strlen(str),
481
DATA_VARCHAR, DATA_ENGLISH). */
484
pars_info_add_str_literal(
485
/*======================*/
486
pars_info_t* info, /* in: info struct */
487
const char* name, /* in: name */
488
const char* str); /* in: string */
490
/********************************************************************
494
mach_write_to_4(buf, val);
495
pars_info_add_literal(info, name, buf, 4, DATA_INT, 0);
497
except that the buffer is dynamically allocated from the info struct's
501
pars_info_add_int4_literal(
502
/*=======================*/
503
pars_info_t* info, /* in: info struct */
504
const char* name, /* in: name */
505
lint val); /* in: value */
507
/********************************************************************
511
mach_write_to_8(buf, val);
512
pars_info_add_literal(info, name, buf, 8, DATA_BINARY, 0);
514
except that the buffer is dynamically allocated from the info struct's
518
pars_info_add_dulint_literal(
519
/*=========================*/
520
pars_info_t* info, /* in: info struct */
521
const char* name, /* in: name */
522
dulint val); /* in: value */
523
/********************************************************************
524
Add user function. */
527
pars_info_add_function(
528
/*===================*/
529
pars_info_t* info, /* in: info struct */
530
const char* name, /* in: function name */
531
pars_user_func_cb_t func, /* in: function address */
532
void* arg); /* in: user-supplied argument */
534
/********************************************************************
540
pars_info_t* info, /* in: info struct */
541
const char* name, /* in: name */
542
const char* id); /* in: id */
544
/********************************************************************
545
Get user function with the given name.*/
548
pars_info_get_user_func(
549
/*====================*/
550
/* out: user func, or NULL if not
552
pars_info_t* info, /* in: info struct */
553
const char* name); /* in: function name to find*/
555
/********************************************************************
556
Get bound literal with the given name.*/
559
pars_info_get_bound_lit(
560
/*====================*/
561
/* out: bound literal, or NULL if
563
pars_info_t* info, /* in: info struct */
564
const char* name); /* in: bound literal name to find */
566
/********************************************************************
567
Get bound id with the given name.*/
570
pars_info_get_bound_id(
571
/*===================*/
572
/* out: bound id, or NULL if not
574
pars_info_t* info, /* in: info struct */
575
const char* name); /* in: bound id name to find */
578
/* Extra information supplied for pars_sql(). */
579
struct pars_info_struct {
580
mem_heap_t* heap; /* our own memory heap */
582
ib_vector_t* funcs; /* user functions, or NUll
583
(pars_user_func_t*) */
584
ib_vector_t* bound_lits; /* bound literals, or NULL
585
(pars_bound_lit_t*) */
586
ib_vector_t* bound_ids; /* bound ids, or NULL
587
(pars_bound_id_t*) */
589
ibool graph_owns_us; /* if TRUE (which is the default),
590
que_graph_free() will free us */
593
/* User-supplied function and argument. */
594
struct pars_user_func_struct {
595
const char* name; /* function name */
596
pars_user_func_cb_t func; /* function address */
597
void* arg; /* user-supplied argument */
601
struct pars_bound_lit_struct {
602
const char* name; /* name */
603
const void* address; /* address */
604
ulint length; /* length of data */
605
ulint type; /* type, e.g. DATA_FIXBINARY */
606
ulint prtype; /* precise type, e.g. DATA_UNSIGNED */
610
struct pars_bound_id_struct {
611
const char* name; /* name */
612
const char* id; /* id */
615
/* Struct used to denote a reserved word in a parsing tree */
616
struct pars_res_word_struct{
617
int code; /* the token code for the reserved word from
621
/* A predefined function or operator node in a parsing tree; this construct
622
is also used for some non-functions like the assignment ':=' */
623
struct func_node_struct{
624
que_common_t common; /* type: QUE_NODE_FUNC */
625
int func; /* token code of the function name */
626
ulint class; /* class of the function */
627
que_node_t* args; /* argument(s) of the function */
628
UT_LIST_NODE_T(func_node_t) cond_list;
629
/* list of comparison conditions; defined
630
only for comparison operator nodes except,
631
presently, for OPT_SCROLL_TYPE ones */
632
UT_LIST_NODE_T(func_node_t) func_node_list;
633
/* list of function nodes in a parsed
637
/* An order-by node in a select */
638
struct order_node_struct{
639
que_common_t common; /* type: QUE_NODE_ORDER */
640
sym_node_t* column; /* order-by column */
641
ibool asc; /* TRUE if ascending, FALSE if descending */
644
/* Procedure definition node */
645
struct proc_node_struct{
646
que_common_t common; /* type: QUE_NODE_PROC */
647
sym_node_t* proc_id; /* procedure name symbol in the symbol
648
table of this same procedure */
649
sym_node_t* param_list; /* input and output parameters */
650
que_node_t* stat_list; /* statement list */
651
sym_tab_t* sym_tab; /* symbol table of this procedure */
654
/* elsif-element node */
655
struct elsif_node_struct{
656
que_common_t common; /* type: QUE_NODE_ELSIF */
657
que_node_t* cond; /* if condition */
658
que_node_t* stat_list; /* statement list */
661
/* if-statement node */
662
struct if_node_struct{
663
que_common_t common; /* type: QUE_NODE_IF */
664
que_node_t* cond; /* if condition */
665
que_node_t* stat_list; /* statement list */
666
que_node_t* else_part; /* else-part statement list */
667
elsif_node_t* elsif_list; /* elsif element list */
670
/* while-statement node */
671
struct while_node_struct{
672
que_common_t common; /* type: QUE_NODE_WHILE */
673
que_node_t* cond; /* while condition */
674
que_node_t* stat_list; /* statement list */
677
/* for-loop-statement node */
678
struct for_node_struct{
679
que_common_t common; /* type: QUE_NODE_FOR */
680
sym_node_t* loop_var; /* loop variable: this is the
681
dereferenced symbol from the
682
variable declarations, not the
683
symbol occurrence in the for loop
685
que_node_t* loop_start_limit;/* initial value of loop variable */
686
que_node_t* loop_end_limit; /* end value of loop variable */
687
int loop_end_value; /* evaluated value for the end value:
688
it is calculated only when the loop
689
is entered, and will not change within
691
que_node_t* stat_list; /* statement list */
694
/* exit statement node */
695
struct exit_node_struct{
696
que_common_t common; /* type: QUE_NODE_EXIT */
699
/* return-statement node */
700
struct return_node_struct{
701
que_common_t common; /* type: QUE_NODE_RETURN */
704
/* Assignment statement node */
705
struct assign_node_struct{
706
que_common_t common; /* type: QUE_NODE_ASSIGNMENT */
707
sym_node_t* var; /* variable to set */
708
que_node_t* val; /* value to assign */
711
/* Column assignment node */
712
struct col_assign_node_struct{
713
que_common_t common; /* type: QUE_NODE_COL_ASSIGN */
714
sym_node_t* col; /* column to set */
715
que_node_t* val; /* value to assign */
718
/* Classes of functions */
719
#define PARS_FUNC_ARITH 1 /* +, -, *, / */
720
#define PARS_FUNC_LOGICAL 2
721
#define PARS_FUNC_CMP 3
722
#define PARS_FUNC_PREDEFINED 4 /* TO_NUMBER, SUBSTR, ... */
723
#define PARS_FUNC_AGGREGATE 5 /* COUNT, DISTINCT, SUM */
724
#define PARS_FUNC_OTHER 6 /* these are not real functions,
728
#include "pars0pars.ic"