Merge pull request #431 from xmujay/0609_monitor
[phpmyadmin/aamir.git] / libraries / parse_analyze.inc.php
blob78f58987dce88ede36f3d789ff2a8d29f3ebfa44
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Parse and analyse a SQL query
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
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 '',
27 // PRIMARY KEY (`id`)
28 // )
30 // Fills some variables from the analysed SQL
31 // A table has to be created, renamed, dropped -> navi frame should be reloaded
32 $reload = isset($analyzed_sql[0]['queryflags']['reload']);
34 // check for drop database
35 $drop_database = isset($analyzed_sql[0]['queryflags']['drop_database']);
37 // for the presence of EXPLAIN
38 $is_explain = isset($analyzed_sql[0]['queryflags']['is_explain']);
40 // for the presence of DELETE
41 $is_delete = isset($analyzed_sql[0]['queryflags']['is_delete']);
43 // for the presence of UPDATE, DELETE or INSERT|LOAD DATA|REPLACE
44 $is_affected = isset($analyzed_sql[0]['queryflags']['is_affected']);
46 // for the presence of REPLACE
47 $is_replace = isset($analyzed_sql[0]['queryflags']['is_replace']);
49 // for the presence of INSERT
50 $is_insert = isset($analyzed_sql[0]['queryflags']['is_insert']);
52 // for the presence of CHECK|ANALYZE|REPAIR|OPTIMIZE TABLE
53 $is_maint = isset($analyzed_sql[0]['queryflags']['is_maint']);
55 // for the presence of SHOW
56 $is_show = isset($analyzed_sql[0]['queryflags']['is_show']);
58 // for the presence of PRRCEDURE ANALYSE
59 $is_analyse = isset($analyzed_sql[0]['queryflags']['is_analyse']);
61 // for the presence of INTO OUTFILE
62 $is_export = isset($analyzed_sql[0]['queryflags']['is_export']);
64 // for the presence of GROUP BY|HAVING|SELECT DISTINCT
65 $is_group = isset($analyzed_sql[0]['queryflags']['is_group']);
67 // for the presence of SUM|AVG|STD|STDDEV|MIN|MAX|BIT_OR|BIT_AND
68 $is_func = isset($analyzed_sql[0]['queryflags']['is_func']);
70 // for the presence of SELECT COUNT
71 $is_count = isset($analyzed_sql[0]['queryflags']['is_count']);
73 // check for a real SELECT ... FROM
74 $is_select = isset($analyzed_sql[0]['queryflags']['select_from']);
76 // If the query is a Select, extract the db and table names and modify
77 // $db and $table, to have correct page headers, links and left frame.
78 // db and table name may be enclosed with backquotes, db is optionnal,
79 // query may contain aliases.
81 /**
82 * @todo if there are more than one table name in the Select:
83 * - do not extract the first table name
84 * - do not show a table name in the page header
85 * - do not display the sub-pages links)
87 if ($is_select) {
88 $prev_db = $db;
89 if (isset($analyzed_sql[0]['table_ref'][0]['table_true_name'])) {
90 $table = $analyzed_sql[0]['table_ref'][0]['table_true_name'];
92 if (isset($analyzed_sql[0]['table_ref'][0]['db'])
93 && strlen($analyzed_sql[0]['table_ref'][0]['db'])
94 ) {
95 $db = $analyzed_sql[0]['table_ref'][0]['db'];
96 } else {
97 $db = $prev_db;
99 // Don't change reload, if we already decided to reload in import
100 if (empty($reload) && empty($GLOBALS['is_ajax_request'])) {
101 $reload = ($db == $prev_db) ? 0 : 1;