PageRenderTime 50ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/parser_sql/php-sql-parser.php

https://bitbucket.org/enurkov/prestashop
PHP | 1970 lines | 1699 code | 173 blank | 98 comment | 247 complexity | d1b051098fd75c2273675d8723cce635 MD5 | raw file
  1. <?php
  2. if(!defined('HAVE_PHP_SQL_PARSER')) {
  3. class parserSql {
  4. var $reserved = array();
  5. var $functions = array();
  6. function __construct($sql = false) {
  7. #LOAD THE LIST OF RESERVED WORDS
  8. $this->load_reserved_words();
  9. if($sql) $this->parse($sql);
  10. }
  11. function parse($sql) {
  12. $sql = trim($sql);
  13. #lex the SQL statement
  14. $in = $this->split_sql($sql);
  15. #sometimes the parser needs to skip ahead until a particular
  16. #token is found
  17. $skip_until = false;
  18. #this is the output tree which is being parsed
  19. $out = array();
  20. #This is the last type of union used (UNION or UNION ALL)
  21. #indicates a) presence of at least one union in this query
  22. # b) the type of union if this is the first or last query
  23. $union = false;
  24. #Sometimes a "query" consists of more than one query (like a UNION query)
  25. #this array holds all the queries
  26. $queries=array();
  27. #This is the highest level lexical analysis. This is the part of the
  28. #code which finds UNION and UNION ALL query parts
  29. foreach($in as $key => $token) {
  30. $token=trim($token);
  31. if($skip_until) {
  32. if($token) {
  33. if(strtoupper($token) == $skip_until) {
  34. $skip_until = false;
  35. continue;
  36. }
  37. } else {
  38. continue;
  39. }
  40. }
  41. if(strtoupper($token) == "UNION") {
  42. $union = 'UNION';
  43. for($i=$key+1;$i<count($in);++$i) {
  44. if(trim($in[$i]) == '') continue;
  45. if(strtoupper($in[$i]) == 'ALL') {
  46. $skip_until = 'ALL';
  47. $union = 'UNION ALL';
  48. continue ;
  49. } else {
  50. break;
  51. }
  52. }
  53. $queries[$union][] = $out;
  54. $out = array();
  55. } else {
  56. $out[]=$token;
  57. }
  58. }
  59. if(!empty($out)) {
  60. if ($union) {
  61. $queries[$union][] = $out;
  62. } else {
  63. $queries[] = $out;
  64. }
  65. }
  66. /*MySQL supports a special form of UNION:
  67. (select ...)
  68. union
  69. (select ...)
  70. This block handles this query syntax. Only one such subquery
  71. is supported in each UNION block. (select)(select)union(select) is not legal.
  72. The extra queries will be silently ignored.
  73. */
  74. $union_types = array('UNION','UNION ALL');
  75. foreach($union_types as $union_type) {
  76. if(!empty($queries[$union_type])) {
  77. foreach($queries[$union_type] as $i => $tok_list) {
  78. foreach($tok_list as $z => $tok) {
  79. $tok = trim($tok);
  80. if(!$tok) continue;
  81. if(preg_match('/^\\(\\s*select\\s*/i', $tok)) {
  82. $queries[$union_type][$i] = $this->parse(substr($tok,1,-1));
  83. break;
  84. } else {
  85. $queries[$union_type][$i] = $this->process_sql($queries[$union_type][$i]);
  86. break;
  87. }
  88. }
  89. }
  90. }
  91. }
  92. /* If there was no UNION or UNION ALL in the query, then the query is
  93. stored at $queries[0].
  94. */
  95. if(!empty($queries[0])) {
  96. $queries[0] = $this->process_sql($queries[0]);
  97. }
  98. if(count($queries) == 1 && !$union) {
  99. $queries = $queries[0];
  100. }
  101. $this->parsed = $queries;
  102. return $this->parsed;
  103. }
  104. #This function counts open and close parenthesis and
  105. #returns their location. This might be faster as a regex
  106. private function count_paren($token,$chars=array('(',')')) {
  107. $len = strlen($token);
  108. $open=array();
  109. $close=array();
  110. for($i=0;$i<$len;++$i){
  111. if($token[$i] == $chars[0]) {
  112. $open[] = $i;
  113. } elseif($token[$i] == $chars[1]) {
  114. $close[] = $i;
  115. }
  116. }
  117. return array('open' => $open, 'close' => $close, 'balanced' =>( count($close) - count($open)));
  118. }
  119. #This function counts open and close parenthesis and
  120. #returns their location. This might be faster as a regex
  121. private function count_backtick($token) {
  122. $len = strlen($token);
  123. $cnt=0;
  124. for($i=0;$i<$len;++$i){
  125. if($token[$i] == '`') ++$cnt;
  126. }
  127. return $cnt;
  128. }
  129. #This is the lexer
  130. #this function splits up a SQL statement into easy to "parse"
  131. #tokens for the SQL processor
  132. private function split_sql($sql) {
  133. if(!is_string($sql)) {
  134. echo "SQL:\n";
  135. print_r($sql);
  136. exit;
  137. }
  138. $sql = str_replace(array('\\\'','\\"',"\r\n","\n","()"),array("''",'""'," "," "," "), $sql);
  139. $regex=<<<EOREGEX
  140. /(`(?:[^`]|``)`|[@A-Za-z0-9_.`-]+(?:\(\s*\)){0,1})
  141. |(\+|-|\*|\/|!=|<>|>|<|&&|\|\||=|\^)
  142. |(\(.*?\)) # Match FUNCTION(...) OR BAREWORDS
  143. |('(?:[^']|'')*'+)
  144. |("(?:[^"]|"")*"+)
  145. |([^ ,]+)
  146. /ix
  147. EOREGEX
  148. ;
  149. $tokens = preg_split($regex, $sql,-1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
  150. $token_count = count($tokens);
  151. /* The above regex has one problem, because the parenthetical match is not greedy.
  152. Thus, when matching grouped expresions such as ( (a and b) or c) the
  153. tokenizer will produce "( (a and b)", " ", "or", " " , "c,")"
  154. This block detects the number of open/close parens in the given token. If the parens are balanced
  155. (balanced == 0) then we don't need to do anything.
  156. otherwise, we need to balance the expression.
  157. */
  158. $reset = false;
  159. for($i=0;$i<$token_count;++$i) {
  160. if(empty($tokens[$i])) continue;
  161. $token = $tokens[$i];
  162. $trim = trim($token);
  163. if($trim) {
  164. if($trim[0] != '('
  165. && substr($trim,-1) == ')') {
  166. $trim=trim(substr($trim,0,
  167. strpos($trim,'(')));
  168. }
  169. $tokens[$i]=$trim;
  170. $token=$trim;
  171. }
  172. if($token && $token[0] == '(') {
  173. $info = $this->count_paren($token);
  174. if($info['balanced'] == 0) {
  175. continue;
  176. }
  177. #we need to find this many closing parens
  178. $needed = abs($info['balanced']);
  179. $n = $i;
  180. while($needed > 0 && $n <$token_count-1) {
  181. ++$n;
  182. #echo "LOOKING FORWARD TO $n [ " . $tokens[$n] . "]\n";
  183. $token2 = $tokens[$n];
  184. $info2 = $this->count_paren($token2);
  185. $closes = count($info2['close']);
  186. if($closes != $needed) {
  187. $tokens[$i] .= $tokens[$n];
  188. unset($tokens[$n]);
  189. $reset = true;
  190. $info2 = $this->count_paren($tokens[$i]);
  191. $needed = abs($info2['balanced']);
  192. # echo "CLOSES LESS THAN NEEDED (still need $needed)\n";
  193. } else {
  194. /*get the string pos of the last close paren we need*/
  195. $pos = $info2['close'][count($info2['close'])-1];
  196. $str1 = $str2 = "";
  197. if($pos == 0) {
  198. $str1 = ')';
  199. } else {
  200. $str1 = substr($tokens[$n],0,$pos) . ')';
  201. $str2 = substr($tokens[$n],$pos+1);
  202. }
  203. #echo "CLOSES FOUND AT $n, offset:$pos [$str1] [$str2]\n";
  204. if(strlen($str2) > 0) {
  205. $tokens[$n] = $str2;
  206. } else {
  207. unset($tokens[$n]);
  208. $reset = true;
  209. }
  210. $tokens[$i] .= $str1;
  211. $info2 = $this->count_paren($tokens[$i]);
  212. $needed = abs($info2['balanced']);
  213. }
  214. }
  215. }
  216. }
  217. #the same problem appears with backticks :(
  218. /* reset the array if we deleted any tokens above */
  219. if ($reset) $tokens = array_values($tokens);
  220. $token_count=count($tokens);
  221. for($i=0;$i<$token_count;++$i) {
  222. if(empty($tokens[$i])) continue;
  223. $token=$tokens[$i];
  224. $needed=true;
  225. $reset=false;
  226. if($needed && $token && strpos($token,'`') !== false) {
  227. $info = $this->count_backtick($token);
  228. if($info %2 == 0) { #even number of backticks means we are balanced
  229. continue;
  230. }
  231. $needed=1;
  232. $n = $i;
  233. while($needed && $n <$token_count-1) {
  234. $reset=true;
  235. #echo "BACKTICK COUNT[$i]: $info old: {$tokens[$i]}, new: ($token)\n";
  236. ++$n;
  237. $token .= $tokens[$n];
  238. unset($tokens[$n]);
  239. $needed = $this->count_backtick($token) % 2;
  240. }
  241. }
  242. if($reset) $tokens[$i] = $token;
  243. }
  244. /* reset the array if we deleted any tokens above */
  245. $tokens = array_values($tokens);
  246. return $tokens;
  247. }
  248. /* This function breaks up the SQL statement into logical sections.
  249. Some sections are then further handled by specialized functions.
  250. */
  251. private function process_sql(&$tokens,$start_at = 0, $stop_at = false) {
  252. $prev_category = "";
  253. $start = microtime(true);
  254. $token_category = "";
  255. $skip_next=false;
  256. $token_count = count($tokens);
  257. if(!$stop_at) {
  258. $stop_at = $token_count;
  259. }
  260. $out = false;
  261. for($token_number = $start_at;$token_number<$stop_at;++$token_number) {
  262. $token = trim($tokens[$token_number]);
  263. if($token && $token[0] == '(' && $token_category == "") {
  264. $token_category = 'SELECT';
  265. }
  266. /* If it isn't obvious, when $skip_next is set, then we ignore the next real
  267. token, that is we ignore whitespace.
  268. */
  269. if($skip_next) {
  270. #whitespace does not count as a next token
  271. if($token == "") {
  272. continue;
  273. }
  274. #to skip the token we replace it with whitespace
  275. $new_token = "";
  276. $skip_next = false;
  277. }
  278. $upper = strtoupper($token);
  279. switch($upper) {
  280. /* Tokens that get their own sections. These keywords have subclauses. */
  281. case 'SELECT':
  282. case 'ORDER':
  283. case 'LIMIT':
  284. case 'SET':
  285. case 'DUPLICATE':
  286. case 'VALUES':
  287. case 'GROUP':
  288. case 'ORDER':
  289. case 'HAVING':
  290. case 'INTO':
  291. case 'WHERE':
  292. case 'RENAME':
  293. case 'CALL':
  294. case 'PROCEDURE':
  295. case 'FUNCTION':
  296. case 'DATABASE':
  297. case 'SERVER':
  298. case 'LOGFILE':
  299. case 'DEFINER':
  300. case 'RETURNS':
  301. case 'EVENT':
  302. case 'TABLESPACE':
  303. case 'VIEW':
  304. case 'TRIGGER':
  305. case 'DATA':
  306. case 'DO':
  307. case 'PASSWORD':
  308. case 'USER':
  309. case 'PLUGIN':
  310. case 'FROM':
  311. case 'FLUSH':
  312. case 'KILL':
  313. case 'RESET':
  314. case 'START':
  315. case 'STOP':
  316. case 'PURGE':
  317. case 'EXECUTE':
  318. case 'PREPARE':
  319. case 'DEALLOCATE':
  320. if($token == 'DEALLOCATE') {
  321. $skip_next = true;
  322. }
  323. /* this FROM is different from FROM in other DML (not join related) */
  324. if($token_category == 'PREPARE' && $upper == 'FROM') {
  325. continue 2;
  326. }
  327. $token_category = $upper;
  328. #$join_type = 'JOIN';
  329. if($upper == 'FROM' && $token_category == 'FROM') {
  330. /* DO NOTHING*/
  331. } else {
  332. continue 2;
  333. }
  334. break;
  335. /* These tokens get their own section, but have no subclauses.
  336. These tokens identify the statement but have no specific subclauses of their own. */
  337. case 'DELETE':
  338. case 'ALTER':
  339. case 'INSERT':
  340. case 'REPLACE':
  341. case 'TRUNCATE':
  342. case 'CREATE':
  343. case 'TRUNCATE':
  344. case 'OPTIMIZE':
  345. case 'GRANT':
  346. case 'REVOKE':
  347. case 'SHOW':
  348. case 'HANDLER':
  349. case 'LOAD':
  350. case 'ROLLBACK':
  351. case 'SAVEPOINT':
  352. case 'UNLOCK':
  353. case 'INSTALL':
  354. case 'UNINSTALL':
  355. case 'ANALZYE':
  356. case 'BACKUP':
  357. case 'CHECK':
  358. case 'CHECKSUM':
  359. case 'REPAIR':
  360. case 'RESTORE':
  361. case 'CACHE':
  362. case 'DESCRIBE':
  363. case 'EXPLAIN':
  364. case 'USE':
  365. case 'HELP':
  366. $token_category = $upper; /* set the category in case these get subclauses
  367. in a future version of MySQL */
  368. $out[$upper][0] = $upper;
  369. continue 2;
  370. break;
  371. /* This is either LOCK TABLES or SELECT ... LOCK IN SHARE MODE*/
  372. case 'LOCK':
  373. if($token_category == "") {
  374. $token_category = $upper;
  375. $out[$upper][0] = $upper;
  376. } else {
  377. $token = 'LOCK IN SHARE MODE';
  378. $skip_next=true;
  379. $out['OPTIONS'][] = $token;
  380. }
  381. continue 2;
  382. break;
  383. case 'USING':
  384. /* USING in FROM clause is different from USING w/ prepared statement*/
  385. if($token_category == 'EXECUTE') {
  386. $token_category=$upper;
  387. continue 2;
  388. }
  389. if($token_category == 'FROM' && !empty($out['DELETE'])) {
  390. $token_category=$upper;
  391. continue 2;
  392. }
  393. break;
  394. /* DROP TABLE is different from ALTER TABLE DROP ... */
  395. case 'DROP':
  396. if($token_category != 'ALTER') {
  397. $token_category = $upper;
  398. $out[$upper][0] = $upper;
  399. continue 2;
  400. }
  401. break;
  402. case 'FOR':
  403. $skip_next=true;
  404. $out['OPTIONS'][] = 'FOR UPDATE';
  405. continue 2;
  406. break;
  407. case 'UPDATE':
  408. if($token_category == "" ) {
  409. $token_category = $upper;
  410. continue 2;
  411. }
  412. if($token_category == 'DUPLICATE') {
  413. continue 2;
  414. }
  415. break;
  416. break;
  417. case 'START':
  418. $token = "BEGIN";
  419. $out[$upper][0] = $upper;
  420. $skip_next = true;
  421. break;
  422. /* These tokens are ignored. */
  423. case 'BY':
  424. case 'ALL':
  425. case 'SHARE':
  426. case 'MODE':
  427. case 'TO':
  428. case ';':
  429. continue 2;
  430. break;
  431. case 'KEY':
  432. if($token_category == 'DUPLICATE') {
  433. continue 2;
  434. }
  435. break;
  436. /* These tokens set particular options for the statement. They never stand alone.*/
  437. case 'DISTINCTROW':
  438. $token='DISTINCT';
  439. case 'DISTINCT':
  440. case 'HIGH_PRIORITY':
  441. case 'LOW_PRIORITY':
  442. case 'DELAYED':
  443. case 'IGNORE':
  444. case 'FORCE':
  445. case 'STRAIGHT_JOIN':
  446. case 'SQL_SMALL_RESULT':
  447. case 'SQL_BIG_RESULT':
  448. case 'QUICK':
  449. case 'SQL_BUFFER_RESULT':
  450. case 'SQL_CACHE':
  451. case 'SQL_NO_CACHE':
  452. case 'SQL_CALC_FOUND_ROWS':
  453. $out['OPTIONS'][] = $upper;
  454. continue 2;
  455. break;
  456. case 'WITH':
  457. if($token_category == 'GROUP') {
  458. $skip_next=true;
  459. $out['OPTIONS'][] = 'WITH ROLLUP';
  460. continue 2;
  461. }
  462. break;
  463. case 'AS':
  464. break;
  465. case '':
  466. case ',':
  467. case ';':
  468. break;
  469. default:
  470. break;
  471. }
  472. if($prev_category == $token_category) {
  473. $out[$token_category][] = $token;
  474. }
  475. $prev_category = $token_category;
  476. }
  477. if(!$out) return false;
  478. #process the SELECT clause
  479. if(!empty($out['SELECT'])) $out['SELECT'] = $this->process_select($out['SELECT']);
  480. if(!empty($out['FROM'])) $out['FROM'] = $this->process_from($out['FROM']);
  481. if(!empty($out['USING'])) $out['USING'] = $this->process_from($out['USING']);
  482. if(!empty($out['UPDATE'])) $out['UPDATE'] = $this->process_from($out['UPDATE']);
  483. if(!empty($out['GROUP'])) $out['GROUP'] = $this->process_group($out['GROUP'], $out['SELECT']);
  484. if(!empty($out['ORDER'])) $out['ORDER'] = $this->process_group($out['ORDER'], $out['SELECT']);
  485. if(!empty($out['LIMIT'])) $out['LIMIT'] = $this->process_limit($out['LIMIT']);
  486. if(!empty($out['WHERE'])) $out['WHERE'] = $this->process_expr_list($out['WHERE']);
  487. if(!empty($out['HAVING'])) $out['HAVING'] = $this->process_expr_list($out['HAVING']);
  488. if(!empty($out['SET'])) $out['SET'] = $this->process_set_list($out['SET']);
  489. if(!empty($out['DUPLICATE'])) {
  490. $out['ON DUPLICATE KEY UPDATE'] = $this->process_set_list($out['DUPLICATE']);
  491. unset($out['DUPLICATE']);
  492. }
  493. if(!empty($out['INSERT'])) $out = $this->process_insert($out);
  494. if(!empty($out['REPLACE'])) $out = $this->process_insert($out,'REPLACE');
  495. if(!empty($out['DELETE'])) $out = $this->process_delete($out);
  496. return $out;
  497. }
  498. /* A SET list is simply a list of key = value expressions separated by comma (,).
  499. This function produces a list of the key/value expressions.
  500. */
  501. private function process_set_list($tokens) {
  502. $column="";
  503. $expression="";
  504. foreach($tokens as $token) {
  505. $token=trim($token);
  506. if(!$column) {
  507. if($token === false || empty($token)) continue;
  508. $column .= $token;
  509. continue;
  510. }
  511. if($token == '=') continue;
  512. if($token == ',') {
  513. $expr[] = array('column' => trim($column), 'expr' => trim($expression));
  514. $expression = $column = "";
  515. continue;
  516. }
  517. $expression .= $token;
  518. }
  519. if($expression) {
  520. $expr[] = array('column' => trim($column), 'expr' => trim($expression));
  521. }
  522. return $expr;
  523. }
  524. /* This function processes the LIMIT section.
  525. start,end are set. If only end is provided in the query
  526. then start is set to 0.
  527. */
  528. private function process_limit($tokens) {
  529. $start = 0;
  530. $end = 0;
  531. if($pos = array_search(',',$tokens)) {
  532. for($i=0;$i<$pos;++$i) {
  533. if($tokens[$i] != '') {
  534. $start = $tokens[$i];
  535. break;
  536. }
  537. }
  538. $pos = $pos + 1;
  539. } else {
  540. $pos = 0;
  541. }
  542. for($i=$pos;$i<count($tokens);++$i) {
  543. if($tokens[$i] != '') {
  544. $end = $tokens[$i];
  545. break;
  546. }
  547. }
  548. return array('start' => $start, 'end' => $end);
  549. }
  550. /* This function processes the SELECT section. It splits the clauses at the commas.
  551. Each clause is then processed by process_select_expr() and the results are added to
  552. the expression list.
  553. Finally, at the end, the epxression list is returned.
  554. */
  555. private function process_select(&$tokens) {
  556. $expression = "";
  557. $expr = array();
  558. foreach($tokens as $token) {
  559. if($token == ',') {
  560. $expr[] = $this->process_select_expr(trim($expression));
  561. $expression = "";
  562. } else {
  563. if($token === "" || $token===false) $token=" ";
  564. $expression .= $token ;
  565. }
  566. }
  567. if($expression) $expr[] = $this->process_select_expr(trim($expression));
  568. return $expr;
  569. }
  570. /* This fuction processes each SELECT clause. We determine what (if any) alias
  571. is provided, and we set the type of expression.
  572. */
  573. private function process_select_expr($expression) {
  574. $capture = false;
  575. $alias = "";
  576. $base_expression = $expression;
  577. $upper = trim(strtoupper($expression));
  578. #if necessary, unpack the expression
  579. if($upper[0] == '(') {
  580. #$expression = substr($expression,1,-1);
  581. $base_expression = $expression;
  582. }
  583. $tokens = $this->split_sql($expression);
  584. $token_count = count($tokens);
  585. /* Determine if there is an explicit alias after the AS clause.
  586. If AS is found, then the next non-whitespace token is captured as the alias.
  587. The tokens after (and including) the AS are removed.
  588. */
  589. $base_expr = "";
  590. $stripped=array();
  591. $capture=false;
  592. $alias = "";
  593. $processed=false;
  594. for($i=0;$i<$token_count;++$i) {
  595. $token = strtoupper($tokens[$i]);
  596. if(trim($token)) {
  597. $stripped[] = $tokens[$i];
  598. }
  599. if($token == 'AS') {
  600. unset($tokens[$i]);
  601. $capture = true;
  602. continue;
  603. }
  604. if($capture) {
  605. if(trim($token)) {
  606. $alias .= $tokens[$i];
  607. }
  608. unset($tokens[$i]);
  609. continue;
  610. }
  611. $base_expr .= $tokens[$i];
  612. }
  613. $stripped = $this->process_expr_list($stripped);
  614. $last = array_pop($stripped);
  615. if(!$alias && $last['expr_type'] == 'colref') {
  616. $prev = array_pop($stripped);
  617. if($prev['expr_type'] == 'operator' ||
  618. $prev['expr_type'] == 'const' ||
  619. $prev['expr_type'] == 'function' ||
  620. $prev['expr_type'] == 'expression' ||
  621. #$prev['expr_type'] == 'aggregate_function' ||
  622. $prev['expr_type'] == 'subquery' ||
  623. $prev['expr_type'] == 'colref') {
  624. $alias = $last['base_expr'];
  625. #remove the last token
  626. array_pop($tokens);
  627. $base_expr = join("", $tokens);
  628. }
  629. }
  630. if(!$alias) {
  631. $base_expr=join("", $tokens);
  632. $alias = $base_expr;
  633. }
  634. /* Properly escape the alias if it is not escaped */
  635. if ($alias[0] != '`') {
  636. $alias = '`' . str_replace('`','``',$alias) . '`';
  637. }
  638. $processed = false;
  639. $type='expression';
  640. if(substr(trim($base_expr),0,1) == '(') {
  641. $base_expr = substr($expression,1,-1);
  642. if(preg_match('/^sel/i', $base_expr)) {
  643. $type='subquery';
  644. $processed = $this->parse($base_expr);
  645. }
  646. }
  647. if(!$processed) {
  648. $processed = $this->process_expr_list($tokens);
  649. }
  650. if(count($processed) == 1) {
  651. $type = $processed[0]['expr_type'];
  652. $processed = false;
  653. }
  654. return array('expr_type'=>$type,'alias' => $alias, 'base_expr' => $base_expr, 'sub_tree' => $processed);
  655. }
  656. private function process_from(&$tokens) {
  657. $expression = "";
  658. $expr = array();
  659. $token_count=0;
  660. $table = "";
  661. $alias = "";
  662. $skip_next=false;
  663. $i=0;
  664. $join_type = '';
  665. $ref_type="";
  666. $ref_expr="";
  667. $base_expr="";
  668. $sub_tree = false;
  669. $subquery = "";
  670. $first_join=true;
  671. $modifier="";
  672. $saved_join_type="";
  673. foreach($tokens as $token) {
  674. $base_expr = false;
  675. $upper = strtoupper(trim($token));
  676. if($skip_next && $token) {
  677. $token_count++;
  678. $skip_next = false;
  679. continue;
  680. } else {
  681. if($skip_next) {
  682. continue;
  683. }
  684. }
  685. if(preg_match("/^\\s*\\(\\s*select/i",$token)) {
  686. $type = 'subquery';
  687. $table = "DEPENDENT-SUBQUERY";
  688. $sub_tree = $this->parse(trim($token,'() '));
  689. $subquery = $token;
  690. }
  691. switch($upper) {
  692. case 'OUTER':
  693. case 'LEFT':
  694. case 'RIGHT':
  695. case 'NATURAL':
  696. case 'CROSS':
  697. case ',':
  698. case 'JOIN':
  699. break;
  700. default:
  701. $expression .= $token == '' ? " " : $token;
  702. if($ref_type) {
  703. $ref_expr .= $token == '' ? " " : $token;
  704. }
  705. break;
  706. }
  707. switch($upper) {
  708. case 'AS':
  709. $token_count++;
  710. $n=1;
  711. $alias = "";
  712. while($alias == "") {
  713. $alias = trim($tokens[$i+$n]);
  714. ++$n;
  715. }
  716. continue;
  717. break;
  718. case 'INDEX':
  719. if($token_category == 'CREATE') {
  720. $token_category = $upper;
  721. continue 2;
  722. }
  723. break;
  724. case 'USING':
  725. case 'ON':
  726. $ref_type = $upper;
  727. $ref_expr = "";
  728. case 'CROSS':
  729. case 'USE':
  730. case 'FORCE':
  731. case 'IGNORE':
  732. case 'INNER':
  733. case 'OUTER':
  734. # $expression .= $token;
  735. $token_count++;
  736. continue;
  737. break;
  738. case 'FOR':
  739. $token_count++;
  740. $skip_next = true;
  741. continue;
  742. break;
  743. case 'LEFT':
  744. case 'RIGHT':
  745. case 'STRAIGHT_JOIN':
  746. $join_type=$saved_join_type;
  747. $modifier = $upper . " ";
  748. break;
  749. case ',':
  750. $modifier = 'CROSS';
  751. case 'JOIN':
  752. if($first_join) {
  753. $join_type = 'JOIN';
  754. $saved_join_type = ($modifier ? $modifier : 'JOIN');
  755. } else {
  756. $new_join_type = ($modifier ? $modifier : 'JOIN');
  757. $join_type = $saved_join_type;
  758. $saved_join_type = $new_join_type;
  759. unset($new_join_type);
  760. }
  761. $first_join = false;
  762. if(!trim($alias)) $alias = $table;
  763. if($subquery) {
  764. $sub_tree = $this->parse(trim($subquery,'()'));
  765. $base_expr=$subquery;
  766. }
  767. if(substr(trim($table),0,1) == '(') {
  768. $base_expr=trim($table,'() ');
  769. $join_type = 'JOIN';
  770. $sub_tree = $this->process_from($this->split_sql($base_expr));
  771. $alias="";
  772. }
  773. if($join_type == "") $join_type='JOIN';
  774. $expr[] = array('table'=>$table, 'alias'=>$alias,'join_type'=>$join_type,'ref_type'=> $ref_type,'ref_clause'=>trim($ref_expr,'() '), 'base_expr' => $base_expr, 'sub_tree' => $sub_tree);
  775. $modifier = "";
  776. #$join_type=$saved_join_type;
  777. $token_count = 0;
  778. $table = $alias = $expression = $base_expr = $ref_type = $ref_expr = "";
  779. $sub_tree=false;
  780. $subquery = "";
  781. break;
  782. default:
  783. if($token === false || empty($token) || $token === "") continue;
  784. if($token_count == 0 ) {
  785. if(!$table) {
  786. $table = $token ;
  787. }
  788. } else if($token_count == 1) {
  789. $alias = $token;
  790. }
  791. $token_count++;
  792. break;
  793. }
  794. ++$i;
  795. }
  796. if(substr(trim($table),0,1) == '(') {
  797. $base_expr=trim($table,'() ');
  798. $join_type = 'JOIN';
  799. $sub_tree = $this->process_from($this->split_sql($base_expr));
  800. $alias = "";
  801. } else {
  802. if(!trim($alias)) $alias = $table;
  803. }
  804. if($join_type == "") $saved_join_type='JOIN';
  805. $expr[] = array('table'=>$table, 'alias'=>$alias,'join_type'=>$saved_join_type,'ref_type'=> $ref_type,'ref_clause'=> trim($ref_expr,'() '), 'base_expr' => $base_expr, 'sub_tree' => $sub_tree);
  806. return $expr;
  807. }
  808. private function process_group(&$tokens, &$select) {
  809. $out=array();
  810. $expression = "";
  811. $direction="ASC";
  812. $type = "expression";
  813. if(!$tokens) return false;
  814. foreach($tokens as $token) {
  815. switch(strtoupper($token)) {
  816. case ',':
  817. $expression = trim($expression);
  818. if($expression[0] != '`' || substr($expression,-1) != '`') {
  819. $escaped = str_replace('`','``',$expression);
  820. } else {
  821. $escaped = $expression;
  822. }
  823. $escaped = '`' . $escaped . '`';
  824. if(is_numeric(trim($expression))) {
  825. $type = 'pos';
  826. } else {
  827. #search to see if the expression matches an alias
  828. foreach($select as $clause) {
  829. if($clause['alias'] == $escaped) {
  830. $type = 'alias';
  831. }
  832. }
  833. if(!$type) $type = "expression";
  834. }
  835. $out[]=array('type'=>$type,'base_expr'=>$expression,'direction'=>$direction);
  836. $escaped = "";
  837. $expression = "";
  838. $direction = "ASC";
  839. $type = "";
  840. break;
  841. case 'ASC':
  842. $direction = "ASC";
  843. break;
  844. case 'DESC':
  845. $direction = "DESC";
  846. break;
  847. default:
  848. $expression .= $token == '' ? ' ' : $token;
  849. }
  850. }
  851. if($expression) {
  852. $expression = trim($expression);
  853. if($expression[0] != '`' || substr($expression,-1) != '`') {
  854. $escaped = str_replace('`','``',$expression);
  855. } else {
  856. $escaped = $expression;
  857. }
  858. $escaped = '`' . $escaped . '`';
  859. if(is_numeric(trim($expression))) {
  860. $type = 'pos';
  861. } else {
  862. #search to see if the expression matches an alias
  863. if(!$type && $select) {
  864. foreach($select as $clause) {
  865. if(!is_array($clause)) continue;
  866. if($clause['alias'] == $escaped) {
  867. $type = 'alias';
  868. }
  869. }
  870. } else {
  871. $type="expression";
  872. }
  873. if(!$type) $type = "expression";
  874. }
  875. $out[]=array('type'=>$type,'base_expr'=>$expression,'direction'=>$direction);
  876. }
  877. return $out;
  878. }
  879. /* Some sections are just lists of expressions, like the WHERE and HAVING clauses. This function
  880. processes these sections. Recursive.
  881. */
  882. private function process_expr_list($tokens) {
  883. $expr = "";
  884. $type = "";
  885. $prev_token = "";
  886. $skip_next = false;
  887. $sub_expr = "";
  888. $in_lists = array();
  889. foreach($tokens as $key => $token) {
  890. if(strlen(trim($token))==0) continue;
  891. if($skip_next) {
  892. $skip_next = false;
  893. continue;
  894. }
  895. $processed = false;
  896. $upper = strtoupper(trim($token));
  897. if(trim($token)) $token=trim($token);
  898. /* is it a subquery?*/
  899. if(preg_match("/^\\s*\\(\\s*SELECT/i", $token)) {
  900. $type = 'subquery';
  901. #tokenize and parse the subquery.
  902. #we remove the enclosing parenthesis for the tokenizer
  903. $processed = $this->parse(trim($token,' ()'));
  904. /* is it an inlist */
  905. } elseif( $upper[0] == '(' && substr($upper,-1) == ')' ) {
  906. if($prev_token == 'IN') {
  907. $type = "in-list";
  908. $processed = $this->split_sql(substr($token,1,-1));
  909. $list = array();
  910. foreach($processed as $v) {
  911. if($v == ',') continue;
  912. $list[]=$v;
  913. }
  914. $processed = $list;
  915. unset($list);
  916. $prev_token = "";
  917. }
  918. elseif($prev_token == 'AGAINST') {
  919. $type = "match-arguments";
  920. $list = $this->split_sql(substr($token,1,-1));
  921. if(count($list) > 1){
  922. $match_mode = implode('',array_slice($list,1));
  923. $processed = array($list[0], $match_mode);
  924. }
  925. else
  926. $processed = $list[0];
  927. $prev_token = "";
  928. }
  929. /* it is either an operator, a colref or a constant */
  930. } else {
  931. switch($upper) {
  932. case 'AND':
  933. case '&&':
  934. case 'BETWEEN':
  935. case 'AND':
  936. case 'BINARY':
  937. case '&':
  938. case '~':
  939. case '|':
  940. case '^':
  941. case 'CASE':
  942. case 'WHEN':
  943. case 'END':
  944. case 'DIV':
  945. case '/':
  946. case '<=>':
  947. case '=':
  948. case '>=':
  949. case '>':
  950. case 'IS':
  951. case 'NOT':
  952. case 'NULL':
  953. case '<<':
  954. case '<=':
  955. case '<':
  956. case 'LIKE':
  957. case '-':
  958. case '%':
  959. case '!=':
  960. case '<>':
  961. case 'REGEXP':
  962. case '!':
  963. case '||':
  964. case 'OR':
  965. case '+':
  966. case '>>':
  967. case 'RLIKE':
  968. case 'SOUNDS':
  969. case '*':
  970. case '-':
  971. case 'XOR':
  972. case 'IN':
  973. $processed = false;
  974. $type = "operator";
  975. break;
  976. default:
  977. switch($token[0]) {
  978. case "'":
  979. case '"':
  980. $type = 'const';
  981. break;
  982. case '`':
  983. $type = 'colref';
  984. break;
  985. default:
  986. if(is_numeric($token)) {
  987. $type = 'const';
  988. } else {
  989. $type = 'colref';
  990. }
  991. break;
  992. }
  993. #$processed = $token;
  994. $processed = false;
  995. }
  996. }
  997. /* is a reserved word? */
  998. if(($type != 'operator' && $type != 'in-list' && $type != 'sub_expr') && in_array($upper, $this->reserved)) {
  999. $token = $upper;
  1000. if(!in_array($upper,$this->functions)) {
  1001. $type = 'reserved';
  1002. } else {
  1003. switch($token) {
  1004. case 'AVG':
  1005. case 'SUM':
  1006. case 'COUNT':
  1007. case 'MIN':
  1008. case 'MAX':
  1009. case 'STDDEV':
  1010. case 'STDDEV_SAMP':
  1011. case 'STDDEV_POP':
  1012. case 'VARIANCE':
  1013. case 'VAR_SAMP':
  1014. case 'VAR_POP':
  1015. case 'GROUP_CONCAT':
  1016. case 'BIT_AND':
  1017. case 'BIT_OR':
  1018. case 'BIT_XOR':
  1019. $type = 'aggregate_function';
  1020. if(!empty($tokens[$key+1])) $sub_expr = $tokens[$key+1];
  1021. #$skip_next=true;
  1022. break;
  1023. default:
  1024. $type = 'function';
  1025. if(!empty($tokens[$key+1])) $sub_expr = $tokens[$key+1]; else $sub_expr="()";
  1026. #$skip_next=true;
  1027. break;
  1028. }
  1029. }
  1030. }
  1031. if(!$type) {
  1032. if($upper[0] == '(') {
  1033. $local_expr = substr(trim($token),1,-1);
  1034. } else {
  1035. $local_expr = $token;
  1036. }
  1037. $processed = $this->process_expr_list($this->split_sql($local_expr));
  1038. $type = 'expression';
  1039. if(count($processed) == 1) {
  1040. $type = $processed[0]['expr_type'];
  1041. $base_expr = $processed[0]['base_expr'];
  1042. $processed = $processed[0]['sub_tree'];
  1043. }
  1044. }
  1045. $sub_expr=trim($sub_expr);
  1046. $sub_expr = "";
  1047. $expr[] = array( 'expr_type' => $type, 'base_expr' => $token, 'sub_tree' => $processed);
  1048. $prev_token = $upper;
  1049. $expr_type = "";
  1050. $type = "";
  1051. }
  1052. if($sub_expr) {
  1053. $processed['sub_tree'] = $this->process_expr_list($this->split_sql(substr($sub_expr,1,-1)));
  1054. }
  1055. if(!is_array($processed)) {
  1056. print_r($processed);
  1057. $processed = false;
  1058. }
  1059. if($expr_type) {
  1060. $expr[] = array( 'expr_type' => $type, 'base_expr' => $token, 'sub_tree' => $processed);
  1061. }
  1062. $mod = false;
  1063. /*
  1064. for($i=0;$i<count($expr);++$i){
  1065. if($expr[$i]['expr_type'] == 'function' ||
  1066. $expr[$i]['expr_type'] == 'aggregate_function') {
  1067. if(!empty($expr[$i+1])) {
  1068. $expr[$i]['sub_tree']=$expr[$i+1]['sub_tree'];
  1069. unset($expr[$i+1]);
  1070. $mod = 1;
  1071. ++$i; // BAD FORM TO MODIFY THE LOOP COUNTER
  1072. }
  1073. }
  1074. }
  1075. */
  1076. if($mod) $expr=array_values($expr);
  1077. return $expr;
  1078. }
  1079. private function process_update($tokens) {
  1080. }
  1081. private function process_delete($tokens) {
  1082. $tables = array();
  1083. $del = $tokens['DELETE'];
  1084. foreach($tokens['DELETE'] as $expression) {
  1085. if ($expression != 'DELETE' && trim($expression,' .*') != "" && $expression != ',') {
  1086. $tables[] = trim($expression,'.* ');
  1087. }
  1088. }
  1089. if(empty($tables)) {
  1090. foreach($tokens['FROM'] as $table) {
  1091. $tables[] = $table['table'];
  1092. }
  1093. }
  1094. $tokens['DELETE'] = array('TABLES' => $tables);
  1095. return $tokens;
  1096. }
  1097. function process_insert($tokens, $token_category = 'INSERT') {
  1098. $table = "";
  1099. $cols = "";
  1100. if(isset($tokens['INTO']))
  1101. {
  1102. $into = $tokens['INTO'];
  1103. foreach($into as $token) {
  1104. if(!trim($token)) continue;
  1105. if(!$table) {
  1106. $table = $token;
  1107. }elseif(!$cols) {
  1108. $cols = $token;
  1109. }
  1110. }
  1111. }
  1112. if(!$cols) {
  1113. $cols = 'ALL';
  1114. } else {
  1115. $cols = explode(",", trim($cols,'() '));
  1116. }
  1117. unset($tokens['INTO']);
  1118. $tokens[$token_category] = array('table'=>$table, 'cols'=>$cols);
  1119. return $tokens;
  1120. }
  1121. function load_reserved_words() {
  1122. $this->functions = array(
  1123. 'abs',
  1124. 'acos',
  1125. 'adddate',
  1126. 'addtime',
  1127. 'aes_encrypt',
  1128. 'aes_decrypt',
  1129. 'against',
  1130. 'ascii',
  1131. 'asin',
  1132. 'atan',
  1133. 'avg',
  1134. 'benchmark',
  1135. 'bin',
  1136. 'bit_and',
  1137. 'bit_or',
  1138. 'bitcount',
  1139. 'bitlength',
  1140. 'cast',
  1141. 'ceiling',
  1142. 'char',
  1143. 'char_length',
  1144. 'character_length',
  1145. 'charset',
  1146. 'coalesce',
  1147. 'coercibility',
  1148. 'collation',
  1149. 'compress',
  1150. 'concat',
  1151. 'concat_ws',
  1152. 'conection_id',
  1153. 'conv',
  1154. 'convert',
  1155. 'convert_tz',
  1156. 'cos',
  1157. 'cot',
  1158. 'count',
  1159. 'crc32',
  1160. 'curdate',
  1161. 'current_user',
  1162. 'currval',
  1163. 'curtime',
  1164. 'database',
  1165. 'date_add',
  1166. 'date_diff',
  1167. 'date_format',
  1168. 'date_sub',
  1169. 'day',
  1170. 'dayname',
  1171. 'dayofmonth',
  1172. 'dayofweek',
  1173. 'dayofyear',
  1174. 'decode',
  1175. 'default',
  1176. 'degrees',
  1177. 'des_decrypt',
  1178. 'des_encrypt',
  1179. 'elt',
  1180. 'encode',
  1181. 'encrypt',
  1182. 'exp',
  1183. 'export_set',
  1184. 'extract',
  1185. 'field',
  1186. 'find_in_set',
  1187. 'floor',
  1188. 'format',
  1189. 'found_rows',
  1190. 'from_days',
  1191. 'from_unixtime',
  1192. 'get_format',
  1193. 'get_lock',
  1194. 'group_concat',
  1195. 'greatest',
  1196. 'hex',
  1197. 'hour',
  1198. 'if',
  1199. 'ifnull',
  1200. 'in',
  1201. 'inet_aton',
  1202. 'inet_ntoa',
  1203. 'insert',
  1204. 'instr',
  1205. 'interval',
  1206. 'is_free_lock',
  1207. 'is_used_lock',
  1208. 'last_day',
  1209. 'last_insert_id',
  1210. 'lcase',
  1211. 'least',
  1212. 'left',
  1213. 'length',
  1214. 'ln',
  1215. 'load_file',
  1216. 'localtime',
  1217. 'localtimestamp',
  1218. 'locate',
  1219. 'log',
  1220. 'log2',
  1221. 'log10',
  1222. 'lower',
  1223. 'lpad',
  1224. 'ltrim',
  1225. 'make_set',
  1226. 'makedate',
  1227. 'maketime',
  1228. 'master_pos_wait',
  1229. 'match',
  1230. 'max',
  1231. 'md5',
  1232. 'microsecond',
  1233. 'mid',
  1234. 'min',
  1235. 'minute',
  1236. 'mod',
  1237. 'month',
  1238. 'monthname',
  1239. 'nextval',
  1240. 'now',
  1241. 'nullif',
  1242. 'oct',
  1243. 'octet_length',
  1244. 'old_password',
  1245. 'ord',
  1246. 'password',
  1247. 'period_add',
  1248. 'period_diff',
  1249. 'pi',
  1250. 'position',
  1251. 'pow',
  1252. 'power',
  1253. 'quarter',
  1254. 'quote',
  1255. 'radians',
  1256. 'rand',
  1257. 'release_lock',
  1258. 'repeat',
  1259. 'replace',
  1260. 'reverse',
  1261. 'right',
  1262. 'round',
  1263. 'row_count',
  1264. 'rpad',
  1265. 'rtrim',
  1266. 'sec_to_time',
  1267. 'second',
  1268. 'session_user',
  1269. 'sha',
  1270. 'sha1',
  1271. 'sign',
  1272. 'soundex',
  1273. 'space',
  1274. 'sqrt',
  1275. 'std',
  1276. 'stddev',
  1277. 'stddev_pop',
  1278. 'stddev_samp',
  1279. 'strcmp',
  1280. 'str_to_date',
  1281. 'subdate',
  1282. 'substring',
  1283. 'substring_index',
  1284. 'subtime',
  1285. 'sum',
  1286. 'sysdate',
  1287. 'system_user',
  1288. 'tan',
  1289. 'time',
  1290. 'timediff',
  1291. 'timestamp',
  1292. 'timestampadd',
  1293. 'timestampdiff',
  1294. 'time_format',
  1295. 'time_to_sec',
  1296. 'to_days',
  1297. 'trim',
  1298. 'truncate',
  1299. 'ucase',
  1300. 'uncompress',
  1301. 'uncompressed_length',
  1302. 'unhex',
  1303. 'unix_timestamp',
  1304. 'upper',
  1305. 'user',
  1306. 'utc_date',
  1307. 'utc_time',
  1308. 'utc_timestamp',
  1309. 'uuid',
  1310. 'var_pop',
  1311. 'var_samp',
  1312. 'variance',
  1313. 'version',
  1314. 'week',
  1315. 'weekday',
  1316. 'weekofyear',
  1317. 'year',
  1318. 'yearweek');
  1319. /* includes functions */
  1320. $this->reserved = array(
  1321. 'abs',
  1322. 'acos',
  1323. 'adddate',
  1324. 'addtime',
  1325. 'aes_encrypt',
  1326. 'aes_decrypt',
  1327. 'against',
  1328. 'ascii',
  1329. 'asin',
  1330. 'atan',
  1331. 'avg',
  1332. 'benchmark',
  1333. 'bin',
  1334. 'bit_and',
  1335. 'bit_or',
  1336. 'bitcount',
  1337. 'bitlength',
  1338. 'cast',
  1339. 'ceiling',
  1340. 'char',
  1341. 'char_length',
  1342. 'character_length',
  1343. 'charset',
  1344. 'coalesce',
  1345. 'coercibility',
  1346. 'collation',
  1347. 'compress',
  1348. 'concat',
  1349. 'concat_ws',
  1350. 'conection_id',
  1351. 'conv',
  1352. 'convert',
  1353. 'convert_tz',
  1354. 'cos',
  1355. 'cot',
  1356. 'count',
  1357. 'crc32',
  1358. 'curdate',
  1359. 'current_user',
  1360. 'currval',
  1361. 'curtime',
  1362. 'database',
  1363. 'date_add',
  1364. 'date_diff',
  1365. 'date_format',
  1366. 'date_sub',
  1367. 'day',
  1368. 'dayname',
  1369. 'dayofmonth',
  1370. 'dayofweek',
  1371. 'dayofyear',
  1372. 'decode',
  1373. 'default',
  1374. 'degrees',
  1375. 'des_decrypt',
  1376. 'des_encrypt',
  1377. 'elt',
  1378. 'encode',
  1379. 'encrypt',
  1380. 'exp',
  1381. 'export_set',
  1382. 'extract',
  1383. 'field',
  1384. 'find_in_set',
  1385. 'floor',
  1386. 'format',
  1387. 'found_rows',
  1388. 'from_days',
  1389. 'from_unixtime',
  1390. 'get_format',
  1391. 'get_lock',
  1392. 'group_concat',
  1393. 'greatest',
  1394. 'hex',
  1395. 'hour',
  1396. 'if',
  1397. 'ifnull',
  1398. 'in',
  1399. 'inet_aton',
  1400. 'inet_ntoa',
  1401. 'insert',
  1402. 'instr',
  1403. 'interval',
  1404. 'is_free_lock',
  1405. 'is_used_lock',
  1406. 'last_day',
  1407. 'last_insert_id',
  1408. 'lcase',
  1409. 'least',
  1410. 'left',
  1411. 'length',
  1412. 'ln',
  1413. 'load_file',
  1414. 'localtime',
  1415. 'localtimestamp',
  1416. 'locate',
  1417. 'log',
  1418. 'log2',
  1419. 'log10',
  1420. 'lower',
  1421. 'lpad',
  1422. 'ltrim',
  1423. 'make_set',
  1424. 'makedate',
  1425. 'maketime',
  1426. 'master_pos_wait',
  1427. 'match',
  1428. 'max',
  1429. 'md5',
  1430. 'microsecond',
  1431. 'mid',
  1432. 'min',
  1433. 'minute',
  1434. 'mod',
  1435. 'month',
  1436. 'monthname',
  1437. 'nextval',
  1438. 'now',
  1439. 'nullif',
  1440. 'oct',
  1441. 'octet_length',
  1442. 'old_password',
  1443. 'ord',
  1444. 'password',
  1445. 'period_add',
  1446. 'period_diff',
  1447. 'pi',
  1448. 'position',
  1449. 'pow',
  1450. 'power',
  1451. 'quarter',
  1452. 'quote',
  1453. 'radians',
  1454. 'rand',
  1455. 'release_lock',
  1456. 'repeat',
  1457. 'replace',
  1458. 'reverse',
  1459. 'right',
  1460. 'round',
  1461. 'row_count',
  1462. 'rpad',
  1463. 'rtrim',
  1464. 'sec_to_time',
  1465. 'second',
  1466. 'session_user',
  1467. 'sha',
  1468. 'sha1',
  1469. 'sign',
  1470. 'soundex',
  1471. 'space',
  1472. 'sqrt',
  1473. 'std',
  1474. 'stddev',
  1475. 'stddev_pop',
  1476. 'stddev_samp',
  1477. 'strcmp',
  1478. 'str_to_date',
  1479. 'subdate',
  1480. 'substring',
  1481. 'substring_index',
  1482. 'subtime',
  1483. 'sum',
  1484. 'sysdate',
  1485. 'system_user',
  1486. 'tan',
  1487. 'time',
  1488. 'timediff',
  1489. 'timestamp',
  1490. 'timestampadd',
  1491. 'timestampdiff',
  1492. 'time_format',
  1493. 'time_to_sec',
  1494. 'to_days',
  1495. 'trim',
  1496. 'truncate',
  1497. 'ucase',
  1498. 'uncompress',
  1499. 'uncompressed_length',
  1500. 'unhex',
  1501. 'unix_timestamp',
  1502. 'upper',
  1503. 'user',
  1504. 'utc_date',
  1505. 'utc_time',
  1506. 'utc_timestamp',
  1507. 'uuid',
  1508. 'var_pop',
  1509. 'var_samp',
  1510. 'variance',
  1511. 'version',
  1512. 'week',
  1513. 'weekday',
  1514. 'weekofyear',
  1515. 'year',
  1516. 'yearweek',
  1517. 'add',
  1518. 'all',
  1519. 'alter',
  1520. 'analyze',
  1521. 'and',
  1522. 'as',
  1523. 'asc',
  1524. 'asensitive',
  1525. 'auto_increment',
  1526. 'bdb',
  1527. 'before',
  1528. 'berkeleydb',
  1529. 'between',
  1530. 'bigint',
  1531. 'binary',
  1532. 'blob',
  1533. 'both',
  1534. 'by',
  1535. 'call',
  1536. 'cascade',
  1537. 'case',
  1538. 'change',
  1539. 'char',
  1540. 'character',
  1541. 'check',
  1542. 'collate',
  1543. 'column',
  1544. 'columns',
  1545. 'condition',
  1546. 'connection',
  1547. 'constraint',
  1548. 'continue',
  1549. 'create',
  1550. 'cross',
  1551. 'current_date',
  1552. 'current_time',
  1553. 'current_timestamp',
  1554. 'cursor',
  1555. 'database',
  1556. 'databases',
  1557. 'day_hour',
  1558. 'day_microsecond',
  1559. 'day_minute',
  1560. 'day_second',
  1561. 'dec',
  1562. 'decimal',
  1563. 'declare',
  1564. 'default',
  1565. 'delayed',
  1566. 'delete',
  1567. 'desc',
  1568. 'describe',
  1569. 'deterministic',
  1570. 'distinct',
  1571. 'distinctrow',
  1572. 'div',
  1573. 'double',
  1574. 'drop',
  1575. 'else',
  1576. 'elseif',
  1577. 'enclosed',
  1578. 'escaped',
  1579. 'exists',
  1580. 'exit',
  1581. 'explain',
  1582. 'false',
  1583. 'fetch',
  1584. 'fields',
  1585. 'float',
  1586. 'for',
  1587. 'force',
  1588. 'foreign',
  1589. 'found',
  1590. 'frac_second',
  1591. 'from',
  1592. 'fulltext',
  1593. 'grant',
  1594. 'group',
  1595. 'having',
  1596. 'high_priority',
  1597. 'hour_microsecond',
  1598. 'hour_minute',
  1599. 'hour_second',
  1600. 'if',
  1601. 'ignore',
  1602. 'in',
  1603. 'index',
  1604. 'infile',
  1605. 'inner',
  1606. 'innodb',
  1607. 'inout',
  1608. 'insensitive',
  1609. 'insert',
  1610. 'int',
  1611. 'integer',
  1612. 'interval',
  1613. 'into',
  1614. 'io_thread',
  1615. 'is',
  1616. 'iterate',
  1617. 'join',
  1618. 'key',
  1619. 'keys',
  1620. 'kill',
  1621. 'leading',
  1622. 'leave',
  1623. 'left',
  1624. 'like',
  1625. 'limit',
  1626. 'lines',
  1627. 'load',
  1628. 'localtime',
  1629. 'localtimestamp',
  1630. 'lock',
  1631. 'long',
  1632. 'longblob',
  1633. 'longtext',
  1634. 'loop',
  1635. 'low_priority',
  1636. 'master_server_id',
  1637. 'match',
  1638. 'mediumblob',
  1639. 'mediumint',
  1640. 'mediumtext',
  1641. 'middleint',
  1642. 'minute_microsecond',
  1643. 'minute_second',
  1644. 'mod',
  1645. 'natural',
  1646. 'not',
  1647. 'no_write_to_binlog',
  1648. 'null',
  1649. 'numeric',
  1650. 'on',
  1651. 'optimize',
  1652. 'option',
  1653. 'optionally',
  1654. 'or',
  1655. 'order',
  1656. 'out',
  1657. 'outer',
  1658. 'outfile',
  1659. 'precision',
  1660. 'primary',
  1661. 'privileges',
  1662. 'procedure',
  1663. 'purge',
  1664. 'read',
  1665. 'real',
  1666. 'references',
  1667. 'regexp',
  1668. 'rename',
  1669. 'repeat',
  1670. 'replace',
  1671. 'require',
  1672. 'restrict',
  1673. 'return',
  1674. 'revoke',
  1675. 'right',
  1676. 'rlike',
  1677. 'second_microsecond',
  1678. 'select',
  1679. 'sensitive',
  1680. 'separator',
  1681. 'set',
  1682. 'show',
  1683. 'smallint',
  1684. 'some',
  1685. 'soname',
  1686. 'spatial',
  1687. 'specific',
  1688. 'sql',
  1689. 'sqlexception',
  1690. 'sqlstate',
  1691. 'sqlwarning',
  1692. 'sql_big_result',
  1693. 'sql_calc_found_rows',
  1694. 'sql_small_result',
  1695. 'sql_tsi_day',
  1696. 'sql_tsi_frac_second',
  1697. 'sql_tsi_hour',
  1698. 'sql_tsi_minute',
  1699. 'sql_tsi_month',
  1700. 'sql_tsi_quarter',
  1701. 'sql_tsi_second',
  1702. 'sql_tsi_week',
  1703. 'sql_tsi_year',
  1704. 'ssl',
  1705. 'starting',
  1706. 'straight_join',
  1707. 'striped',
  1708. 'table',
  1709. 'tables',
  1710. 'terminated',
  1711. 'then',
  1712. 'timestampadd',
  1713. 'timestampdiff',
  1714. 'tinyblob',
  1715. 'tinyint',
  1716. 'tinytext',
  1717. 'to',
  1718. 'trailing',
  1719. 'true',
  1720. 'undo',
  1721. 'union',
  1722. 'unique',
  1723. 'unlock',
  1724. 'unsigned',
  1725. 'update',
  1726. 'usage',
  1727. 'use',
  1728. 'user_resources',
  1729. 'using',
  1730. 'utc_date',
  1731. 'utc_time',
  1732. 'utc_timestamp',
  1733. 'values',
  1734. 'varbinary',
  1735. 'varchar',
  1736. 'varcharacter',
  1737. 'varying',
  1738. 'when',
  1739. 'where',
  1740. 'while',
  1741. 'with',
  1742. 'write',
  1743. 'xor',
  1744. 'year_month',
  1745. 'zerofill'
  1746. );
  1747. for($i=0;$i<count($this->reserved);++$i) {
  1748. $this->reserved[$i]=strtoupper($this->reserved[$i]);
  1749. if(!empty($this->functions[$i])) $this->functions[$i] = strtoupper($this->functions[$i]);
  1750. }
  1751. }
  1752. } // END CLASS
  1753. define('HAVE_PHP_SQL_PARSER',1);
  1754. }