1
/*****************************************************************************
3
Copyright (C) 1996, 2010, Innobase Oy. All Rights Reserved.
5
This program is free software; you can redistribute it and/or modify it under
6
the terms of the GNU General Public License as published by the Free Software
7
Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License along with
14
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15
St, Fifth Floor, Boston, MA 02110-1301 USA
17
*****************************************************************************/
19
/**************************************************//**
20
@file include/pars0pars.h
23
Created 11/19/1996 Heikki Tuuri
24
*******************************************************/
30
#include "que0types.h"
31
#include "usr0types.h"
32
#include "pars0types.h"
33
#include "row0types.h"
34
#include "trx0types.h"
37
/** Type of the user functions. The first argument is always InnoDB-supplied
38
and varies in type, while 'user_arg' is a user-supplied argument. The
39
meaning of the return type also varies. See the individual use cases, e.g.
40
the FETCH statement, for details on them. */
41
typedef void* (*pars_user_func_cb_t)(void* arg, void* user_arg);
43
/** If the following is set TRUE, the parser will emit debugging
48
/** If the following is set TRUE, the lexer will print the SQL string
50
extern ibool pars_print_lexed;
51
#endif /* UNIV_SQL_DEBUG */
53
/* Global variable used while parsing a single procedure or query : the code is
55
extern sym_tab_t* pars_sym_tab_global;
57
extern pars_res_word_t pars_to_char_token;
58
extern pars_res_word_t pars_to_number_token;
59
extern pars_res_word_t pars_to_binary_token;
60
extern pars_res_word_t pars_binary_to_number_token;
61
extern pars_res_word_t pars_substr_token;
62
extern pars_res_word_t pars_replstr_token;
63
extern pars_res_word_t pars_concat_token;
64
extern pars_res_word_t pars_length_token;
65
extern pars_res_word_t pars_instr_token;
66
extern pars_res_word_t pars_sysdate_token;
67
extern pars_res_word_t pars_printf_token;
68
extern pars_res_word_t pars_assert_token;
69
extern pars_res_word_t pars_rnd_token;
70
extern pars_res_word_t pars_rnd_str_token;
71
extern pars_res_word_t pars_count_token;
72
extern pars_res_word_t pars_sum_token;
73
extern pars_res_word_t pars_distinct_token;
74
extern pars_res_word_t pars_binary_token;
75
extern pars_res_word_t pars_blob_token;
76
extern pars_res_word_t pars_int_token;
77
extern pars_res_word_t pars_char_token;
78
extern pars_res_word_t pars_float_token;
79
extern pars_res_word_t pars_update_token;
80
extern pars_res_word_t pars_asc_token;
81
extern pars_res_word_t pars_desc_token;
82
extern pars_res_word_t pars_open_token;
83
extern pars_res_word_t pars_close_token;
84
extern pars_res_word_t pars_share_token;
85
extern pars_res_word_t pars_unique_token;
86
extern pars_res_word_t pars_clustered_token;
88
extern ulint pars_star_denoter;
90
/* Procedure parameter types */
93
#define PARS_NOT_PARAM 2
98
/*************************************************************//**
99
Parses an SQL string returning the query graph.
100
@return own: the query graph */
105
pars_info_t* info, /*!< in: extra information, or NULL */
106
const char* str); /*!< in: SQL string */
107
/*************************************************************//**
108
Retrieves characters to the lexical analyzer. */
116
char* buf, /*!< in/out: buffer where to copy */
117
int* result, /*!< out: number of characters copied or EOF */
118
int max_size); /*!< in: maximum number of characters which fit
120
/*************************************************************//**
121
Called by yyparse on error. */
126
const char* s); /*!< in: error message string */
127
/*********************************************************************//**
128
Parses a variable declaration.
129
@return own: symbol table node of type SYM_VAR */
132
pars_variable_declaration(
133
/*======================*/
134
sym_node_t* node, /*!< in: symbol table node allocated for the
135
id of the variable */
136
pars_res_word_t* type); /*!< in: pointer to a type token */
137
/*********************************************************************//**
138
Parses a function expression.
139
@return own: function node in a query tree */
144
que_node_t* res_word,/*!< in: function name reserved word */
145
que_node_t* arg); /*!< in: first argument in the argument list */
146
/*********************************************************************//**
147
Parses an operator expression.
148
@return own: function node in a query tree */
153
int func, /*!< in: operator token code */
154
que_node_t* arg1, /*!< in: first argument */
155
que_node_t* arg2); /*!< in: second argument or NULL for an unary
157
/*********************************************************************//**
158
Parses an ORDER BY clause. Order by a single column only is supported.
159
@return own: order-by node in a query tree */
164
sym_node_t* column, /*!< in: column name */
165
pars_res_word_t* asc); /*!< in: &pars_asc_token or pars_desc_token */
166
/*********************************************************************//**
167
Parses a select list; creates a query graph node for the whole SELECT
169
@return own: select node in a query tree */
174
que_node_t* select_list, /*!< in: select list */
175
sym_node_t* into_list); /*!< in: variables list or NULL */
176
/*********************************************************************//**
177
Parses a cursor declaration.
181
pars_cursor_declaration(
182
/*====================*/
183
sym_node_t* sym_node, /*!< in: cursor id node in the symbol
185
sel_node_t* select_node); /*!< in: select node */
186
/*********************************************************************//**
187
Parses a function declaration.
191
pars_function_declaration(
192
/*======================*/
193
sym_node_t* sym_node); /*!< in: function id node in the symbol
195
/*********************************************************************//**
196
Parses a select statement.
197
@return own: select node in a query tree */
200
pars_select_statement(
201
/*==================*/
202
sel_node_t* select_node, /*!< in: select node already containing
204
sym_node_t* table_list, /*!< in: table list */
205
que_node_t* search_cond, /*!< in: search condition or NULL */
206
pars_res_word_t* for_update, /*!< in: NULL or &pars_update_token */
207
pars_res_word_t* consistent_read,/*!< in: NULL or
208
&pars_consistent_token */
209
order_node_t* order_by); /*!< in: NULL or an order-by node */
210
/*********************************************************************//**
211
Parses a column assignment in an update.
212
@return column assignment node */
215
pars_column_assignment(
216
/*===================*/
217
sym_node_t* column, /*!< in: column to assign */
218
que_node_t* exp); /*!< in: value to assign */
219
/*********************************************************************//**
220
Parses a delete or update statement start.
221
@return own: update node in a query tree */
224
pars_update_statement_start(
225
/*========================*/
226
ibool is_delete, /*!< in: TRUE if delete */
227
sym_node_t* table_sym, /*!< in: table name node */
228
col_assign_node_t* col_assign_list);/*!< in: column assignment list, NULL
230
/*********************************************************************//**
231
Parses an update or delete statement.
232
@return own: update node in a query tree */
235
pars_update_statement(
236
/*==================*/
237
upd_node_t* node, /*!< in: update node */
238
sym_node_t* cursor_sym, /*!< in: pointer to a cursor entry in
239
the symbol table or NULL */
240
que_node_t* search_cond); /*!< in: search condition or NULL */
241
/*********************************************************************//**
242
Parses an insert statement.
243
@return own: update node in a query tree */
246
pars_insert_statement(
247
/*==================*/
248
sym_node_t* table_sym, /*!< in: table name node */
249
que_node_t* values_list, /*!< in: value expression list or NULL */
250
sel_node_t* select); /*!< in: select condition or NULL */
251
/*********************************************************************//**
252
Parses a procedure parameter declaration.
253
@return own: symbol table node of type SYM_VAR */
256
pars_parameter_declaration(
257
/*=======================*/
258
sym_node_t* node, /*!< in: symbol table node allocated for the
259
id of the parameter */
261
/*!< in: PARS_INPUT or PARS_OUTPUT */
262
pars_res_word_t* type); /*!< in: pointer to a type token */
263
/*********************************************************************//**
264
Parses an elsif element.
265
@return elsif node */
270
que_node_t* cond, /*!< in: if-condition */
271
que_node_t* stat_list); /*!< in: statement list */
272
/*********************************************************************//**
273
Parses an if-statement.
274
@return if-statement node */
279
que_node_t* cond, /*!< in: if-condition */
280
que_node_t* stat_list, /*!< in: statement list */
281
que_node_t* else_part); /*!< in: else-part statement list */
282
/*********************************************************************//**
283
Parses a for-loop-statement.
284
@return for-statement node */
289
sym_node_t* loop_var, /*!< in: loop variable */
290
que_node_t* loop_start_limit,/*!< in: loop start expression */
291
que_node_t* loop_end_limit, /*!< in: loop end expression */
292
que_node_t* stat_list); /*!< in: statement list */
293
/*********************************************************************//**
294
Parses a while-statement.
295
@return while-statement node */
298
pars_while_statement(
299
/*=================*/
300
que_node_t* cond, /*!< in: while-condition */
301
que_node_t* stat_list); /*!< in: statement list */
302
/*********************************************************************//**
303
Parses an exit statement.
304
@return exit statement node */
307
pars_exit_statement(void);
308
/*=====================*/
309
/*********************************************************************//**
310
Parses a return-statement.
311
@return return-statement node */
314
pars_return_statement(void);
315
/*=======================*/
316
/*********************************************************************//**
317
Parses a procedure call.
318
@return function node */
323
que_node_t* res_word,/*!< in: procedure name reserved word */
324
que_node_t* args); /*!< in: argument list */
325
/*********************************************************************//**
326
Parses an assignment statement.
327
@return assignment statement node */
330
pars_assignment_statement(
331
/*======================*/
332
sym_node_t* var, /*!< in: variable to assign */
333
que_node_t* val); /*!< in: value to assign */
334
/*********************************************************************//**
335
Parses a fetch statement. into_list or user_func (but not both) must be
337
@return fetch statement node */
340
pars_fetch_statement(
341
/*=================*/
342
sym_node_t* cursor, /*!< in: cursor node */
343
sym_node_t* into_list, /*!< in: variables to set, or NULL */
344
sym_node_t* user_func); /*!< in: user function name, or NULL */
345
/*********************************************************************//**
346
Parses an open or close cursor statement.
347
@return fetch statement node */
352
ulint type, /*!< in: ROW_SEL_OPEN_CURSOR
353
or ROW_SEL_CLOSE_CURSOR */
354
sym_node_t* cursor); /*!< in: cursor node */
355
/*********************************************************************//**
356
Parses a row_printf-statement.
357
@return row_printf-statement node */
360
pars_row_printf_statement(
361
/*======================*/
362
sel_node_t* sel_node); /*!< in: select node */
363
/*********************************************************************//**
364
Parses a commit statement.
365
@return own: commit node struct */
368
pars_commit_statement(void);
369
/*=======================*/
370
/*********************************************************************//**
371
Parses a rollback statement.
372
@return own: rollback node struct */
375
pars_rollback_statement(void);
376
/*=========================*/
377
/*********************************************************************//**
378
Parses a column definition at a table creation.
379
@return column sym table node */
384
sym_node_t* sym_node, /*!< in: column node in the
386
pars_res_word_t* type, /*!< in: data type */
387
sym_node_t* len, /*!< in: length of column, or
389
void* is_unsigned, /*!< in: if not NULL, column
390
is of type UNSIGNED. */
391
void* is_not_null); /*!< in: if not NULL, column
392
is of type NOT NULL. */
393
/*********************************************************************//**
394
Parses a table creation operation.
395
@return table create subgraph */
400
sym_node_t* table_sym, /*!< in: table name node in the symbol
402
sym_node_t* column_defs, /*!< in: list of column names */
403
void* not_fit_in_memory);/*!< in: a non-NULL pointer means that
404
this is a table which in simulations
405
should be simulated as not fitting
406
in memory; thread is put to sleep
407
to simulate disk accesses; NOTE that
408
this flag is not stored to the data
409
dictionary on disk, and the database
410
will forget about non-NULL value if
411
it has to reload the table definition
413
/*********************************************************************//**
414
Parses an index creation operation.
415
@return index create subgraph */
420
pars_res_word_t* unique_def, /*!< in: not NULL if a unique index */
421
pars_res_word_t* clustered_def, /*!< in: not NULL if a clustered index */
422
sym_node_t* index_sym, /*!< in: index name node in the symbol
424
sym_node_t* table_sym, /*!< in: table name node in the symbol
426
sym_node_t* column_list); /*!< in: list of column names */
427
/*********************************************************************//**
428
Parses a procedure definition.
429
@return query fork node */
432
pars_procedure_definition(
433
/*======================*/
434
sym_node_t* sym_node, /*!< in: procedure id node in the symbol
436
sym_node_t* param_list, /*!< in: parameter declaration list */
437
que_node_t* stat_list); /*!< in: statement list */
439
/*************************************************************//**
440
Parses a stored procedure call, when this is not within another stored
441
procedure, that is, the client issues a procedure call directly.
442
In MySQL/InnoDB, stored InnoDB procedures are invoked via the
443
parsed procedure tree, not via InnoDB SQL, so this function is not used.
444
@return query graph */
447
pars_stored_procedure_call(
448
/*=======================*/
449
sym_node_t* sym_node); /*!< in: stored procedure name */
450
/******************************************************************//**
451
Completes a query graph by adding query thread and fork nodes
452
above it and prepares the graph for running. The fork created is of
453
type QUE_FORK_MYSQL_INTERFACE.
454
@return query thread node to run */
457
pars_complete_graph_for_exec(
458
/*=========================*/
459
que_node_t* node, /*!< in: root node for an incomplete
461
trx_t* trx, /*!< in: transaction handle */
462
mem_heap_t* heap); /*!< in: memory heap from which allocated */
464
/****************************************************************//**
465
Create parser info struct.
466
@return own: info struct */
469
pars_info_create(void);
470
/*==================*/
472
/****************************************************************//**
473
Free info struct and everything it contains. */
478
pars_info_t* info); /*!< in, own: info struct */
480
/****************************************************************//**
481
Add bound literal. */
484
pars_info_add_literal(
485
/*==================*/
486
pars_info_t* info, /*!< in: info struct */
487
const char* name, /*!< in: name */
488
const void* address, /*!< in: address */
489
ulint length, /*!< in: length of data */
490
ulint type, /*!< in: type, e.g. DATA_FIXBINARY */
491
ulint prtype); /*!< in: precise type, e.g.
494
/****************************************************************//**
495
Equivalent to pars_info_add_literal(info, name, str, strlen(str),
496
DATA_VARCHAR, DATA_ENGLISH). */
499
pars_info_add_str_literal(
500
/*======================*/
501
pars_info_t* info, /*!< in: info struct */
502
const char* name, /*!< in: name */
503
const char* str); /*!< in: string */
505
/****************************************************************//**
509
mach_write_to_4(buf, val);
510
pars_info_add_literal(info, name, buf, 4, DATA_INT, 0);
512
except that the buffer is dynamically allocated from the info struct's
516
pars_info_add_int4_literal(
517
/*=======================*/
518
pars_info_t* info, /*!< in: info struct */
519
const char* name, /*!< in: name */
520
lint val); /*!< in: value */
522
/****************************************************************//**
526
mach_write_to_8(buf, val);
527
pars_info_add_literal(info, name, buf, 8, DATA_FIXBINARY, 0);
529
except that the buffer is dynamically allocated from the info struct's
533
pars_info_add_ull_literal(
534
/*======================*/
535
pars_info_t* info, /*!< in: info struct */
536
const char* name, /*!< in: name */
537
ib_uint64_t val); /*!< in: value */
538
/****************************************************************//**
539
Add user function. */
542
pars_info_add_function(
543
/*===================*/
544
pars_info_t* info, /*!< in: info struct */
545
const char* name, /*!< in: function name */
546
pars_user_func_cb_t func, /*!< in: function address */
547
void* arg); /*!< in: user-supplied argument */
549
/****************************************************************//**
555
pars_info_t* info, /*!< in: info struct */
556
const char* name, /*!< in: name */
557
const char* id); /*!< in: id */
559
/****************************************************************//**
560
Get user function with the given name.
561
@return user func, or NULL if not found */
564
pars_info_get_user_func(
565
/*====================*/
566
pars_info_t* info, /*!< in: info struct */
567
const char* name); /*!< in: function name to find*/
569
/****************************************************************//**
570
Get bound literal with the given name.
571
@return bound literal, or NULL if not found */
574
pars_info_get_bound_lit(
575
/*====================*/
576
pars_info_t* info, /*!< in: info struct */
577
const char* name); /*!< in: bound literal name to find */
579
/****************************************************************//**
580
Get bound id with the given name.
581
@return bound id, or NULL if not found */
584
pars_info_get_bound_id(
585
/*===================*/
586
pars_info_t* info, /*!< in: info struct */
587
const char* name); /*!< in: bound id name to find */
589
/******************************************************************//**
590
Release any resources used by the lexer. */
596
pars_lexer_close(void);
597
/*==================*/
599
/** Extra information supplied for pars_sql(). */
600
struct pars_info_struct {
601
mem_heap_t* heap; /*!< our own memory heap */
603
ib_vector_t* funcs; /*!< user functions, or NUll
604
(pars_user_func_t*) */
605
ib_vector_t* bound_lits; /*!< bound literals, or NULL
606
(pars_bound_lit_t*) */
607
ib_vector_t* bound_ids; /*!< bound ids, or NULL
608
(pars_bound_id_t*) */
610
ibool graph_owns_us; /*!< if TRUE (which is the default),
611
que_graph_free() will free us */
614
/** User-supplied function and argument. */
615
struct pars_user_func_struct {
616
const char* name; /*!< function name */
617
pars_user_func_cb_t func; /*!< function address */
618
void* arg; /*!< user-supplied argument */
621
/** Bound literal. */
622
struct pars_bound_lit_struct {
623
const char* name; /*!< name */
624
const void* address; /*!< address */
625
ulint length; /*!< length of data */
626
ulint type; /*!< type, e.g. DATA_FIXBINARY */
627
ulint prtype; /*!< precise type, e.g. DATA_UNSIGNED */
630
/** Bound identifier. */
631
struct pars_bound_id_struct {
632
const char* name; /*!< name */
633
const char* id; /*!< identifier */
636
/** Struct used to denote a reserved word in a parsing tree */
637
struct pars_res_word_struct{
638
int code; /*!< the token code for the reserved word from
642
/** A predefined function or operator node in a parsing tree; this construct
643
is also used for some non-functions like the assignment ':=' */
644
struct func_node_struct{
645
que_common_t common; /*!< type: QUE_NODE_FUNC */
646
int func; /*!< token code of the function name */
647
ulint func_class; /*!< class of the function */
648
que_node_t* args; /*!< argument(s) of the function */
649
UT_LIST_NODE_T(func_node_t) cond_list;
650
/*!< list of comparison conditions; defined
651
only for comparison operator nodes except,
652
presently, for OPT_SCROLL_TYPE ones */
653
UT_LIST_NODE_T(func_node_t) func_node_list;
654
/*!< list of function nodes in a parsed
658
/** An order-by node in a select */
659
struct order_node_struct{
660
que_common_t common; /*!< type: QUE_NODE_ORDER */
661
sym_node_t* column; /*!< order-by column */
662
ibool asc; /*!< TRUE if ascending, FALSE if descending */
665
/** Procedure definition node */
666
struct proc_node_struct{
667
que_common_t common; /*!< type: QUE_NODE_PROC */
668
sym_node_t* proc_id; /*!< procedure name symbol in the symbol
669
table of this same procedure */
670
sym_node_t* param_list; /*!< input and output parameters */
671
que_node_t* stat_list; /*!< statement list */
672
sym_tab_t* sym_tab; /*!< symbol table of this procedure */
675
/** elsif-element node */
676
struct elsif_node_struct{
677
que_common_t common; /*!< type: QUE_NODE_ELSIF */
678
que_node_t* cond; /*!< if condition */
679
que_node_t* stat_list; /*!< statement list */
682
/** if-statement node */
683
struct if_node_struct{
684
que_common_t common; /*!< type: QUE_NODE_IF */
685
que_node_t* cond; /*!< if condition */
686
que_node_t* stat_list; /*!< statement list */
687
que_node_t* else_part; /*!< else-part statement list */
688
elsif_node_t* elsif_list; /*!< elsif element list */
691
/** while-statement node */
692
struct while_node_struct{
693
que_common_t common; /*!< type: QUE_NODE_WHILE */
694
que_node_t* cond; /*!< while condition */
695
que_node_t* stat_list; /*!< statement list */
698
/** for-loop-statement node */
699
struct for_node_struct{
700
que_common_t common; /*!< type: QUE_NODE_FOR */
701
sym_node_t* loop_var; /*!< loop variable: this is the
702
dereferenced symbol from the
703
variable declarations, not the
704
symbol occurrence in the for loop
706
que_node_t* loop_start_limit;/*!< initial value of loop variable */
707
que_node_t* loop_end_limit; /*!< end value of loop variable */
708
lint loop_end_value; /*!< evaluated value for the end value:
709
it is calculated only when the loop
710
is entered, and will not change within
712
que_node_t* stat_list; /*!< statement list */
715
/** exit statement node */
716
struct exit_node_struct{
717
que_common_t common; /*!< type: QUE_NODE_EXIT */
720
/** return-statement node */
721
struct return_node_struct{
722
que_common_t common; /*!< type: QUE_NODE_RETURN */
725
/** Assignment statement node */
726
struct assign_node_struct{
727
que_common_t common; /*!< type: QUE_NODE_ASSIGNMENT */
728
sym_node_t* var; /*!< variable to set */
729
que_node_t* val; /*!< value to assign */
732
/** Column assignment node */
733
struct col_assign_node_struct{
734
que_common_t common; /*!< type: QUE_NODE_COL_ASSIGN */
735
sym_node_t* col; /*!< column to set */
736
que_node_t* val; /*!< value to assign */
739
/** Classes of functions */
741
#define PARS_FUNC_ARITH 1 /*!< +, -, *, / */
742
#define PARS_FUNC_LOGICAL 2 /*!< AND, OR, NOT */
743
#define PARS_FUNC_CMP 3 /*!< comparison operators */
744
#define PARS_FUNC_PREDEFINED 4 /*!< TO_NUMBER, SUBSTR, ... */
745
#define PARS_FUNC_AGGREGATE 5 /*!< COUNT, DISTINCT, SUM */
746
#define PARS_FUNC_OTHER 6 /*!< these are not real functions,
751
#include "pars0pars.ic"