PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/parser_sql/processors/ShowProcessor.php

https://gitlab.com/staging06/myproject
PHP | 166 lines | 117 code | 11 blank | 38 comment | 7 complexity | 6f6e8382f260879247c6aecd411147ec MD5 | raw file
  1. <?php
  2. /**
  3. * ShowProcessor.php
  4. *
  5. * This file implements the processor for the SHOW statements.
  6. *
  7. * Copyright (c) 2010-2012, Justin Swanhart
  8. * with contributions by André Rothe <arothe@phosco.info, phosco@gmx.de>
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. *
  15. * * Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * * Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  22. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  23. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  24. * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  26. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  27. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  29. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  30. * DAMAGE.
  31. */
  32. require_once(dirname(__FILE__) . '/../utils/PHPSQLParserConstants.php');
  33. require_once(dirname(__FILE__) . '/../utils/ExpressionType.php');
  34. require_once(dirname(__FILE__) . '/LimitProcessor.php');
  35. require_once(dirname(__FILE__) . '/AbstractProcessor.php');
  36. /**
  37. *
  38. * This class processes the SHOW statements.
  39. *
  40. * @author arothe
  41. *
  42. */
  43. class ShowProcessor extends AbstractProcessor {
  44. private $limitProcessor;
  45. public function __construct() {
  46. $this->limitProcessor = new LimitProcessor();
  47. }
  48. public function process($tokens) {
  49. $resultList = array();
  50. $category = "";
  51. $prev = "";
  52. foreach ($tokens as $k => $token) {
  53. $upper = strtoupper(trim($token));
  54. if ($this->isWhitespaceToken($token)) {
  55. continue;
  56. }
  57. switch ($upper) {
  58. case 'FROM':
  59. $resultList[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => trim($token));
  60. if ($prev === 'INDEX' || $prev === 'COLUMNS') {
  61. continue;
  62. }
  63. $category = $upper;
  64. break;
  65. case 'CREATE':
  66. case 'DATABASE':
  67. case 'FUNCTION':
  68. case 'PROCEDURE':
  69. case 'ENGINE':
  70. case 'TABLE':
  71. case 'FOR':
  72. case 'LIKE':
  73. case 'INDEX':
  74. case 'COLUMNS':
  75. case 'PLUGIN':
  76. case 'PRIVILEGES':
  77. case 'PROCESSLIST':
  78. case 'LOGS':
  79. case 'STATUS':
  80. case 'GLOBAL':
  81. case 'SESSION':
  82. case 'FULL':
  83. case 'GRANTS':
  84. case 'INNODB':
  85. case 'STORAGE':
  86. case 'ENGINES':
  87. case 'OPEN':
  88. case 'BDB':
  89. case 'TRIGGERS':
  90. case 'VARIABLES':
  91. case 'DATABASES':
  92. case 'ERRORS':
  93. case 'TABLES':
  94. case 'WARNINGS':
  95. case 'CHARACTER':
  96. case 'SET':
  97. case 'COLLATION':
  98. $resultList[] = array('expr_type' => ExpressionType::RESERVED, 'base_expr' => trim($token));
  99. $category = $upper;
  100. break;
  101. default:
  102. switch ($prev) {
  103. case 'LIKE':
  104. $resultList[] = array('expr_type' => ExpressionType::CONSTANT, 'base_expr' => $token);
  105. break;
  106. case 'LIMIT':
  107. $limit = array_pop($resultList);
  108. $limit['sub_tree'] = $this->limitProcessor->process(array_slice($tokens, $k));
  109. $resultList[] = $limit;
  110. break;
  111. case 'FROM':
  112. case 'DATABASE':
  113. $resultList[] = array('expr_type' => ExpressionType::DATABASE, 'name' => $token,
  114. 'no_quotes' => $this->revokeQuotation($token), 'base_expr' => $token);
  115. break;
  116. case 'FOR':
  117. $resultList[] = array('expr_type' => ExpressionType::USER, 'name' => $token,
  118. 'no_quotes' => $this->revokeQuotation($token), 'base_expr' => $token);
  119. break;
  120. case 'INDEX':
  121. case 'COLUMNS':
  122. case 'TABLE':
  123. $resultList[] = array('expr_type' => ExpressionType::TABLE, 'table' => $token,
  124. 'no_quotes' => $this->revokeQuotation($token), 'base_expr' => $token);
  125. $category = "TABLENAME";
  126. break;
  127. case 'FUNCTION':
  128. if (PHPSQLParserConstants::isAggregateFunction($upper)) {
  129. $expr_type = ExpressionType::AGGREGATE_FUNCTION;
  130. } else {
  131. $expr_type = ExpressionType::SIMPLE_FUNCTION;
  132. }
  133. $resultList[] = array('expr_type' => $expr_type, 'name' => $token,
  134. 'no_quotes' => $this->revokeQuotation($token), 'base_expr' => $token);
  135. break;
  136. case 'PROCEDURE':
  137. $resultList[] = array('expr_type' => ExpressionType::PROCEDURE, 'name' => $token,
  138. 'no_quotes' => $this->revokeQuotation($token), 'base_expr' => $token);
  139. break;
  140. case 'ENGINE':
  141. $resultList[] = array('expr_type' => ExpressionType::ENGINE, 'name' => $token,
  142. 'no_quotes' => $this->revokeQuotation($token), 'base_expr' => $token);
  143. break;
  144. default:
  145. // ignore
  146. break;
  147. }
  148. break;
  149. }
  150. $prev = $category;
  151. }
  152. return $resultList;
  153. }
  154. }
  155. ?>