1
/*****************************************************************************
3
Copyright (c) 1996, 2009, 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., 59 Temple
15
Place, Suite 330, Boston, MA 02111-1307 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. */
113
char* buf, /*!< in/out: buffer where to copy */
114
int* result, /*!< out: number of characters copied or EOF */
115
int max_size); /*!< in: maximum number of characters which fit
117
/*************************************************************//**
118
Called by yyparse on error. */
123
const char* s); /*!< in: error message string */
124
/*********************************************************************//**
125
Parses a variable declaration.
126
@return own: symbol table node of type SYM_VAR */
129
pars_variable_declaration(
130
/*======================*/
131
sym_node_t* node, /*!< in: symbol table node allocated for the
132
id of the variable */
133
pars_res_word_t* type); /*!< in: pointer to a type token */
134
/*********************************************************************//**
135
Parses a function expression.
136
@return own: function node in a query tree */
141
que_node_t* res_word,/*!< in: function name reserved word */
142
que_node_t* arg); /*!< in: first argument in the argument list */
143
/*********************************************************************//**
144
Parses an operator expression.
145
@return own: function node in a query tree */
150
int func, /*!< in: operator token code */
151
que_node_t* arg1, /*!< in: first argument */
152
que_node_t* arg2); /*!< in: second argument or NULL for an unary
154
/*********************************************************************//**
155
Parses an ORDER BY clause. Order by a single column only is supported.
156
@return own: order-by node in a query tree */
161
sym_node_t* column, /*!< in: column name */
162
pars_res_word_t* asc); /*!< in: &pars_asc_token or pars_desc_token */
163
/*********************************************************************//**
164
Parses a select list; creates a query graph node for the whole SELECT
166
@return own: select node in a query tree */
171
que_node_t* select_list, /*!< in: select list */
172
sym_node_t* into_list); /*!< in: variables list or NULL */
173
/*********************************************************************//**
174
Parses a cursor declaration.
178
pars_cursor_declaration(
179
/*====================*/
180
sym_node_t* sym_node, /*!< in: cursor id node in the symbol
182
sel_node_t* select_node); /*!< in: select node */
183
/*********************************************************************//**
184
Parses a function declaration.
188
pars_function_declaration(
189
/*======================*/
190
sym_node_t* sym_node); /*!< in: function id node in the symbol
192
/*********************************************************************//**
193
Parses a select statement.
194
@return own: select node in a query tree */
197
pars_select_statement(
198
/*==================*/
199
sel_node_t* select_node, /*!< in: select node already containing
201
sym_node_t* table_list, /*!< in: table list */
202
que_node_t* search_cond, /*!< in: search condition or NULL */
203
pars_res_word_t* for_update, /*!< in: NULL or &pars_update_token */
204
pars_res_word_t* consistent_read,/*!< in: NULL or
205
&pars_consistent_token */
206
order_node_t* order_by); /*!< in: NULL or an order-by node */
207
/*********************************************************************//**
208
Parses a column assignment in an update.
209
@return column assignment node */
212
pars_column_assignment(
213
/*===================*/
214
sym_node_t* column, /*!< in: column to assign */
215
que_node_t* exp); /*!< in: value to assign */
216
/*********************************************************************//**
217
Parses a delete or update statement start.
218
@return own: update node in a query tree */
221
pars_update_statement_start(
222
/*========================*/
223
ibool is_delete, /*!< in: TRUE if delete */
224
sym_node_t* table_sym, /*!< in: table name node */
225
col_assign_node_t* col_assign_list);/*!< in: column assignment list, NULL
227
/*********************************************************************//**
228
Parses an update or delete statement.
229
@return own: update node in a query tree */
232
pars_update_statement(
233
/*==================*/
234
upd_node_t* node, /*!< in: update node */
235
sym_node_t* cursor_sym, /*!< in: pointer to a cursor entry in
236
the symbol table or NULL */
237
que_node_t* search_cond); /*!< in: search condition or NULL */
238
/*********************************************************************//**
239
Parses an insert statement.
240
@return own: update node in a query tree */
243
pars_insert_statement(
244
/*==================*/
245
sym_node_t* table_sym, /*!< in: table name node */
246
que_node_t* values_list, /*!< in: value expression list or NULL */
247
sel_node_t* select); /*!< in: select condition or NULL */
248
/*********************************************************************//**
249
Parses a procedure parameter declaration.
250
@return own: symbol table node of type SYM_VAR */
253
pars_parameter_declaration(
254
/*=======================*/
255
sym_node_t* node, /*!< in: symbol table node allocated for the
256
id of the parameter */
258
/*!< in: PARS_INPUT or PARS_OUTPUT */
259
pars_res_word_t* type); /*!< in: pointer to a type token */
260
/*********************************************************************//**
261
Parses an elsif element.
262
@return elsif node */
267
que_node_t* cond, /*!< in: if-condition */
268
que_node_t* stat_list); /*!< in: statement list */
269
/*********************************************************************//**
270
Parses an if-statement.
271
@return if-statement node */
276
que_node_t* cond, /*!< in: if-condition */
277
que_node_t* stat_list, /*!< in: statement list */
278
que_node_t* else_part); /*!< in: else-part statement list */
279
/*********************************************************************//**
280
Parses a for-loop-statement.
281
@return for-statement node */
286
sym_node_t* loop_var, /*!< in: loop variable */
287
que_node_t* loop_start_limit,/*!< in: loop start expression */
288
que_node_t* loop_end_limit, /*!< in: loop end expression */
289
que_node_t* stat_list); /*!< in: statement list */
290
/*********************************************************************//**
291
Parses a while-statement.
292
@return while-statement node */
295
pars_while_statement(
296
/*=================*/
297
que_node_t* cond, /*!< in: while-condition */
298
que_node_t* stat_list); /*!< in: statement list */
299
/*********************************************************************//**
300
Parses an exit statement.
301
@return exit statement node */
304
pars_exit_statement(void);
305
/*=====================*/
306
/*********************************************************************//**
307
Parses a return-statement.
308
@return return-statement node */
311
pars_return_statement(void);
312
/*=======================*/
313
/*********************************************************************//**
314
Parses a procedure call.
315
@return function node */
320
que_node_t* res_word,/*!< in: procedure name reserved word */
321
que_node_t* args); /*!< in: argument list */
322
/*********************************************************************//**
323
Parses an assignment statement.
324
@return assignment statement node */
327
pars_assignment_statement(
328
/*======================*/
329
sym_node_t* var, /*!< in: variable to assign */
330
que_node_t* val); /*!< in: value to assign */
331
/*********************************************************************//**
332
Parses a fetch statement. into_list or user_func (but not both) must be
334
@return fetch statement node */
337
pars_fetch_statement(
338
/*=================*/
339
sym_node_t* cursor, /*!< in: cursor node */
340
sym_node_t* into_list, /*!< in: variables to set, or NULL */
341
sym_node_t* user_func); /*!< in: user function name, or NULL */
342
/*********************************************************************//**
343
Parses an open or close cursor statement.
344
@return fetch statement node */
349
ulint type, /*!< in: ROW_SEL_OPEN_CURSOR
350
or ROW_SEL_CLOSE_CURSOR */
351
sym_node_t* cursor); /*!< in: cursor node */
352
/*********************************************************************//**
353
Parses a row_printf-statement.
354
@return row_printf-statement node */
357
pars_row_printf_statement(
358
/*======================*/
359
sel_node_t* sel_node); /*!< in: select node */
360
/*********************************************************************//**
361
Parses a commit statement.
362
@return own: commit node struct */
365
pars_commit_statement(void);
366
/*=======================*/
367
/*********************************************************************//**
368
Parses a rollback statement.
369
@return own: rollback node struct */
372
pars_rollback_statement(void);
373
/*=========================*/
374
/*********************************************************************//**
375
Parses a column definition at a table creation.
376
@return column sym table node */
381
sym_node_t* sym_node, /*!< in: column node in the
383
pars_res_word_t* type, /*!< in: data type */
384
sym_node_t* len, /*!< in: length of column, or
386
void* is_unsigned, /*!< in: if not NULL, column
387
is of type UNSIGNED. */
388
void* is_not_null); /*!< in: if not NULL, column
389
is of type NOT NULL. */
390
/*********************************************************************//**
391
Parses a table creation operation.
392
@return table create subgraph */
397
sym_node_t* table_sym, /*!< in: table name node in the symbol
399
sym_node_t* column_defs, /*!< in: list of column names */
400
void* not_fit_in_memory);/*!< in: a non-NULL pointer means that
401
this is a table which in simulations
402
should be simulated as not fitting
403
in memory; thread is put to sleep
404
to simulate disk accesses; NOTE that
405
this flag is not stored to the data
406
dictionary on disk, and the database
407
will forget about non-NULL value if
408
it has to reload the table definition
410
/*********************************************************************//**
411
Parses an index creation operation.
412
@return index create subgraph */
417
pars_res_word_t* unique_def, /*!< in: not NULL if a unique index */
418
pars_res_word_t* clustered_def, /*!< in: not NULL if a clustered index */
419
sym_node_t* index_sym, /*!< in: index name node in the symbol
421
sym_node_t* table_sym, /*!< in: table name node in the symbol
423
sym_node_t* column_list); /*!< in: list of column names */
424
/*********************************************************************//**
425
Parses a procedure definition.
426
@return query fork node */
429
pars_procedure_definition(
430
/*======================*/
431
sym_node_t* sym_node, /*!< in: procedure id node in the symbol
433
sym_node_t* param_list, /*!< in: parameter declaration list */
434
que_node_t* stat_list); /*!< in: statement list */
436
/*************************************************************//**
437
Parses a stored procedure call, when this is not within another stored
438
procedure, that is, the client issues a procedure call directly.
439
In MySQL/InnoDB, stored InnoDB procedures are invoked via the
440
parsed procedure tree, not via InnoDB SQL, so this function is not used.
441
@return query graph */
444
pars_stored_procedure_call(
445
/*=======================*/
446
sym_node_t* sym_node); /*!< in: stored procedure name */
447
/******************************************************************//**
448
Completes a query graph by adding query thread and fork nodes
449
above it and prepares the graph for running. The fork created is of
450
type QUE_FORK_MYSQL_INTERFACE.
451
@return query thread node to run */
454
pars_complete_graph_for_exec(
455
/*=========================*/
456
que_node_t* node, /*!< in: root node for an incomplete
458
trx_t* trx, /*!< in: transaction handle */
459
mem_heap_t* heap); /*!< in: memory heap from which allocated */
461
/****************************************************************//**
462
Create parser info struct.
463
@return own: info struct */
466
pars_info_create(void);
467
/*==================*/
469
/****************************************************************//**
470
Free info struct and everything it contains. */
475
pars_info_t* info); /*!< in, own: info struct */
477
/****************************************************************//**
478
Add bound literal. */
481
pars_info_add_literal(
482
/*==================*/
483
pars_info_t* info, /*!< in: info struct */
484
const char* name, /*!< in: name */
485
const void* address, /*!< in: address */
486
ulint length, /*!< in: length of data */
487
ulint type, /*!< in: type, e.g. DATA_FIXBINARY */
488
ulint prtype); /*!< in: precise type, e.g.
491
/****************************************************************//**
492
Equivalent to pars_info_add_literal(info, name, str, strlen(str),
493
DATA_VARCHAR, DATA_ENGLISH). */
496
pars_info_add_str_literal(
497
/*======================*/
498
pars_info_t* info, /*!< in: info struct */
499
const char* name, /*!< in: name */
500
const char* str); /*!< in: string */
502
/****************************************************************//**
506
mach_write_to_4(buf, val);
507
pars_info_add_literal(info, name, buf, 4, DATA_INT, 0);
509
except that the buffer is dynamically allocated from the info struct's
513
pars_info_add_int4_literal(
514
/*=======================*/
515
pars_info_t* info, /*!< in: info struct */
516
const char* name, /*!< in: name */
517
lint val); /*!< in: value */
519
/****************************************************************//**
523
mach_write_to_8(buf, val);
524
pars_info_add_literal(info, name, buf, 8, DATA_BINARY, 0);
526
except that the buffer is dynamically allocated from the info struct's
530
pars_info_add_dulint_literal(
531
/*=========================*/
532
pars_info_t* info, /*!< in: info struct */
533
const char* name, /*!< in: name */
534
dulint val); /*!< in: value */
535
/****************************************************************//**
536
Add user function. */
539
pars_info_add_function(
540
/*===================*/
541
pars_info_t* info, /*!< in: info struct */
542
const char* name, /*!< in: function name */
543
pars_user_func_cb_t func, /*!< in: function address */
544
void* arg); /*!< in: user-supplied argument */
546
/****************************************************************//**
552
pars_info_t* info, /*!< in: info struct */
553
const char* name, /*!< in: name */
554
const char* id); /*!< in: id */
556
/****************************************************************//**
557
Get user function with the given name.
558
@return user func, or NULL if not found */
561
pars_info_get_user_func(
562
/*====================*/
563
pars_info_t* info, /*!< in: info struct */
564
const char* name); /*!< in: function name to find*/
566
/****************************************************************//**
567
Get bound literal with the given name.
568
@return bound literal, or NULL if not found */
571
pars_info_get_bound_lit(
572
/*====================*/
573
pars_info_t* info, /*!< in: info struct */
574
const char* name); /*!< in: bound literal name to find */
576
/****************************************************************//**
577
Get bound id with the given name.
578
@return bound id, or NULL if not found */
581
pars_info_get_bound_id(
582
/*===================*/
583
pars_info_t* info, /*!< in: info struct */
584
const char* name); /*!< in: bound id name to find */
587
/** Extra information supplied for pars_sql(). */
588
struct pars_info_struct {
589
mem_heap_t* heap; /*!< our own memory heap */
591
ib_vector_t* funcs; /*!< user functions, or NUll
592
(pars_user_func_t*) */
593
ib_vector_t* bound_lits; /*!< bound literals, or NULL
594
(pars_bound_lit_t*) */
595
ib_vector_t* bound_ids; /*!< bound ids, or NULL
596
(pars_bound_id_t*) */
598
ibool graph_owns_us; /*!< if TRUE (which is the default),
599
que_graph_free() will free us */
602
/** User-supplied function and argument. */
603
struct pars_user_func_struct {
604
const char* name; /*!< function name */
605
pars_user_func_cb_t func; /*!< function address */
606
void* arg; /*!< user-supplied argument */
609
/** Bound literal. */
610
struct pars_bound_lit_struct {
611
const char* name; /*!< name */
612
const void* address; /*!< address */
613
ulint length; /*!< length of data */
614
ulint type; /*!< type, e.g. DATA_FIXBINARY */
615
ulint prtype; /*!< precise type, e.g. DATA_UNSIGNED */
618
/** Bound identifier. */
619
struct pars_bound_id_struct {
620
const char* name; /*!< name */
621
const char* id; /*!< identifier */
624
/** Struct used to denote a reserved word in a parsing tree */
625
struct pars_res_word_struct{
626
int code; /*!< the token code for the reserved word from
630
/** A predefined function or operator node in a parsing tree; this construct
631
is also used for some non-functions like the assignment ':=' */
632
struct func_node_struct{
633
que_common_t common; /*!< type: QUE_NODE_FUNC */
634
int func; /*!< token code of the function name */
635
ulint class; /*!< class of the function */
636
que_node_t* args; /*!< argument(s) of the function */
637
UT_LIST_NODE_T(func_node_t) cond_list;
638
/*!< list of comparison conditions; defined
639
only for comparison operator nodes except,
640
presently, for OPT_SCROLL_TYPE ones */
641
UT_LIST_NODE_T(func_node_t) func_node_list;
642
/*!< list of function nodes in a parsed
646
/** An order-by node in a select */
647
struct order_node_struct{
648
que_common_t common; /*!< type: QUE_NODE_ORDER */
649
sym_node_t* column; /*!< order-by column */
650
ibool asc; /*!< TRUE if ascending, FALSE if descending */
653
/** Procedure definition node */
654
struct proc_node_struct{
655
que_common_t common; /*!< type: QUE_NODE_PROC */
656
sym_node_t* proc_id; /*!< procedure name symbol in the symbol
657
table of this same procedure */
658
sym_node_t* param_list; /*!< input and output parameters */
659
que_node_t* stat_list; /*!< statement list */
660
sym_tab_t* sym_tab; /*!< symbol table of this procedure */
663
/** elsif-element node */
664
struct elsif_node_struct{
665
que_common_t common; /*!< type: QUE_NODE_ELSIF */
666
que_node_t* cond; /*!< if condition */
667
que_node_t* stat_list; /*!< statement list */
670
/** if-statement node */
671
struct if_node_struct{
672
que_common_t common; /*!< type: QUE_NODE_IF */
673
que_node_t* cond; /*!< if condition */
674
que_node_t* stat_list; /*!< statement list */
675
que_node_t* else_part; /*!< else-part statement list */
676
elsif_node_t* elsif_list; /*!< elsif element list */
679
/** while-statement node */
680
struct while_node_struct{
681
que_common_t common; /*!< type: QUE_NODE_WHILE */
682
que_node_t* cond; /*!< while condition */
683
que_node_t* stat_list; /*!< statement list */
686
/** for-loop-statement node */
687
struct for_node_struct{
688
que_common_t common; /*!< type: QUE_NODE_FOR */
689
sym_node_t* loop_var; /*!< loop variable: this is the
690
dereferenced symbol from the
691
variable declarations, not the
692
symbol occurrence in the for loop
694
que_node_t* loop_start_limit;/*!< initial value of loop variable */
695
que_node_t* loop_end_limit; /*!< end value of loop variable */
696
lint loop_end_value; /*!< evaluated value for the end value:
697
it is calculated only when the loop
698
is entered, and will not change within
700
que_node_t* stat_list; /*!< statement list */
703
/** exit statement node */
704
struct exit_node_struct{
705
que_common_t common; /*!< type: QUE_NODE_EXIT */
708
/** return-statement node */
709
struct return_node_struct{
710
que_common_t common; /*!< type: QUE_NODE_RETURN */
713
/** Assignment statement node */
714
struct assign_node_struct{
715
que_common_t common; /*!< type: QUE_NODE_ASSIGNMENT */
716
sym_node_t* var; /*!< variable to set */
717
que_node_t* val; /*!< value to assign */
720
/** Column assignment node */
721
struct col_assign_node_struct{
722
que_common_t common; /*!< type: QUE_NODE_COL_ASSIGN */
723
sym_node_t* col; /*!< column to set */
724
que_node_t* val; /*!< value to assign */
727
/** Classes of functions */
729
#define PARS_FUNC_ARITH 1 /*!< +, -, *, / */
730
#define PARS_FUNC_LOGICAL 2 /*!< AND, OR, NOT */
731
#define PARS_FUNC_CMP 3 /*!< comparison operators */
732
#define PARS_FUNC_PREDEFINED 4 /*!< TO_NUMBER, SUBSTR, ... */
733
#define PARS_FUNC_AGGREGATE 5 /*!< COUNT, DISTINCT, SUM */
734
#define PARS_FUNC_OTHER 6 /*!< these are not real functions,
739
#include "pars0pars.ic"