2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Parse and analyse a SQL query
8 if (! defined('PHPMYADMIN')) {
15 $GLOBALS['unparsed_sql'] = $sql_query;
16 $parsed_sql = PMA_SQP_parse($sql_query);
17 $analyzed_sql = PMA_SQP_analyze($parsed_sql);
19 // for bug 780516: now that we use case insensitive preg_match
20 // or flags from the analyser, do not put back the reformatted query
21 // into $sql_query, to make this kind of query work without
22 // capitalizing keywords:
24 // CREATE TABLE SG_Persons (
25 // id int(10) unsigned NOT NULL auto_increment,
26 // first varchar(64) NOT NULL default '',
30 // Fills some variables from the analysed SQL
31 // A table has to be created, renamed, dropped:
32 // the navigation panel should be reloaded
33 $reload = isset($analyzed_sql[0]['queryflags']['reload']);
35 // check for drop database
36 $drop_database = isset($analyzed_sql[0]['queryflags']['drop_database']);
38 // for the presence of EXPLAIN
39 $is_explain = isset($analyzed_sql[0]['queryflags']['is_explain']);
41 // for the presence of DELETE
42 $is_delete = isset($analyzed_sql[0]['queryflags']['is_delete']);
44 // for the presence of UPDATE, DELETE or INSERT|LOAD DATA|REPLACE
45 $is_affected = isset($analyzed_sql[0]['queryflags']['is_affected']);
47 // for the presence of REPLACE
48 $is_replace = isset($analyzed_sql[0]['queryflags']['is_replace']);
50 // for the presence of INSERT
51 $is_insert = isset($analyzed_sql[0]['queryflags']['is_insert']);
53 // for the presence of CHECK|ANALYZE|REPAIR|OPTIMIZE|CHECKSUM TABLE
54 $is_maint = isset($analyzed_sql[0]['queryflags']['is_maint']);
56 // for the presence of SHOW
57 $is_show = isset($analyzed_sql[0]['queryflags']['is_show']);
59 // for the presence of PROCEDURE ANALYSE
60 $is_analyse = isset($analyzed_sql[0]['queryflags']['is_analyse']);
62 // for the presence of INTO OUTFILE
63 $is_export = isset($analyzed_sql[0]['queryflags']['is_export']);
65 // for the presence of GROUP BY|HAVING|SELECT DISTINCT
66 $is_group = isset($analyzed_sql[0]['queryflags']['is_group']);
68 // for the presence of SUM|AVG|STD|STDDEV|MIN|MAX|BIT_OR|BIT_AND
69 $is_func = isset($analyzed_sql[0]['queryflags']['is_func']);
71 // for the presence of SELECT COUNT
72 $is_count = isset($analyzed_sql[0]['queryflags']['is_count']);
74 // check for a real SELECT ... FROM
75 $is_select = isset($analyzed_sql[0]['queryflags']['select_from']);
77 // the query contains a subquery
78 $is_subquery = isset($analyzed_sql[0]['queryflags']['is_subquery']);
81 // Since multiple query execution is anyway handled,
82 // ignore the WHERE clause of the first sql statement
83 // which might contain a phrase like 'call '
84 if (isset($analyzed_sql[0]['queryflags']['is_procedure'])
85 && empty($analyzed_sql[0]['where_clause'])
89 $is_procedure = false;
92 // aggregates all the results into one array
93 $analyzed_sql_results = array(
94 "parsed_sql" => $parsed_sql,
95 "analyzed_sql" => $analyzed_sql,
97 "drop_database" => $drop_database,
98 "is_explain" => $is_explain,
99 "is_delete" => $is_delete,
100 "is_affected" => $is_affected,
101 "is_replace" => $is_replace,
102 "is_insert" => $is_insert,
103 "is_maint" => $is_maint,
104 "is_show" => $is_show,
105 "is_analyse" => $is_analyse,
106 "is_export" => $is_export,
107 "is_group" => $is_group,
108 "is_func" => $is_func,
109 "is_count" => $is_count,
110 "is_select" => $is_select,
111 "is_procedure" => $is_procedure,
112 "is_subquery" => $is_subquery
116 // If the query is a Select, extract the db and table names and modify
117 // $db and $table, to have correct page headers, links and left frame.
118 // db and table name may be enclosed with backquotes, db is optional,
119 // query may contain aliases.
122 * @todo if there are more than one table name in the Select:
123 * - do not extract the first table name
124 * - do not show a table name in the page header
125 * - do not display the sub-pages links)
129 if (isset($analyzed_sql[0]['table_ref'][0]['table_true_name'])) {
130 $table = $analyzed_sql[0]['table_ref'][0]['table_true_name'];
132 if (isset($analyzed_sql[0]['table_ref'][0]['db'])
133 && /*overload*/mb_strlen($analyzed_sql[0]['table_ref'][0]['db'])
135 $db = $analyzed_sql[0]['table_ref'][0]['db'];
139 // Don't change reload, if we already decided to reload in import
140 if (empty($reload) && empty($GLOBALS['is_ajax_request'])) {
141 $reload = ($db == $prev_db) ?
0 : 1;