SqlQueryWhereBuilder.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. class SqlQueryWhereBuilder {
  3. private $_log = array();
  4. private $_usedFields = array();
  5. private $_raw = "";
  6. public function __construct() {
  7. }
  8. public function openBlock($blockType) {
  9. if (!$this->isAllowedBlockType($blockType)) throw new Exception("unsupported block type");
  10. $this->_log[] = "open_block_{$blockType}";
  11. }
  12. public function closeBlock($blockType) {
  13. if (!$this->isAllowedBlockType($blockType)) throw new Exception("unsupported block type");
  14. $this->_log[] = "close_block_{$blockType}";
  15. }
  16. public function addComparisonFieldToValue($fieldName, $comparisonSign, $value) {
  17. $sqlCompSign = "";
  18. switch ($comparisonSign) {
  19. case '=': $sqlCompSign = '='; break;
  20. case 'like': $sqlCompSign = 'like'; break;
  21. default: throw new Exception("Unsupported comparison sign");
  22. }
  23. $this->_log[] = array('comparisonFieldToValue', $fieldName, $sqlCompSign, $value);
  24. }
  25. public function addComparisonFieldFunToValue($fieldFun, $fieldName, $comparisonSign, $value) {
  26. $sqlCompSign = "";
  27. switch ($comparisonSign) {
  28. case '=': $sqlCompSign = '='; break;
  29. case 'like': $sqlCompSign = 'like'; break;
  30. default: throw new Exception("Unsupported comparison sign");
  31. }
  32. $this->_log[] = array('comparisonFieldFunToValue', $fieldFun, $fieldName, $sqlCompSign, $value);
  33. }
  34. public function sql_filter_comparisonFieldIsNull($fieldName) {
  35. $this->_log[] = array('comparisonFieldIsNull', $fieldName);
  36. }
  37. public function getQueryWhere($tablePrefix = '') {
  38. $sqlWhereRaw = $this->parseQueryWhere();
  39. $sqlTablePrefix = ($tablePrefix)? "`{$tablePrefix}`." : '';
  40. return str_replace('{tablePrefix}', $sqlTablePrefix, $sqlWhereRaw);
  41. }
  42. public function parseQueryWhere() {
  43. $this->_raw = "";
  44. $this->_usedFields = array();
  45. $sqlBlocksStack = array();
  46. $sqlValuesStack = array();
  47. $arr = array(); array_push($sqlValuesStack, $arr);// empty array to start
  48. foreach ($this->_log as $log) {
  49. switch ($log) {
  50. case 'open_block_and':
  51. case 'open_block_or':
  52. case 'open_block_not': {
  53. $blockType = substr($log, 11);
  54. array_push($sqlBlocksStack, $blockType);
  55. $arr = array(); array_push($sqlValuesStack, $arr);
  56. //echo "L.".__LINE__.":sqlBlocksStack:" . json_encode($sqlBlocksStack) . "\n";
  57. //echo "L.".__LINE__.":sqlValuesStack:" . json_encode($sqlValuesStack) . "\n\n";
  58. }
  59. break;
  60. case 'close_block_and':
  61. case 'close_block_or': {
  62. $blockType = substr($log, 12);
  63. if (empty($sqlBlocksStack)) throw new Exception("parse sql query failed - blocks stack is empty");
  64. $stackBlockType = array_pop($sqlBlocksStack);
  65. if ($blockType != $stackBlockType) throw new Exception("parse sql query failed - expected stop '{$blockType}', given '{$stackBlockType}'");
  66. // parse to string and add to parent value stack
  67. $stackValue = array_pop($sqlValuesStack);
  68. if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
  69. if (empty($stackValue)) throw new Exception("parse sql query failed - stack value is empty");
  70. $sqlFromStack = " ( " . implode(" {$blockType} ", $stackValue) . " ) ";
  71. $parentStackValue = array_pop($sqlValuesStack);
  72. if (!is_array($parentStackValue)) throw new Exception("parse sql query failed - parent stack value is not array");
  73. array_push($parentStackValue, $sqlFromStack);
  74. array_push($sqlValuesStack, $parentStackValue);
  75. }
  76. break;
  77. case 'close_block_not': {
  78. $blockType = substr($log, 12);
  79. if (empty($sqlBlocksStack)) throw new Exception("parse sql query failed - blocks stack is empty");
  80. $stackBlockType = array_pop($sqlBlocksStack);
  81. if ($blockType != $stackBlockType) throw new Exception("parse sql query failed - expected stop '{$blockType}', given '{$stackBlockType}'");
  82. // parse to string and add to parent value stack
  83. $stackValue = array_pop($sqlValuesStack);
  84. if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
  85. if (empty($stackValue)) throw new Exception("parse sql query failed - stack value is empty");
  86. if (1 != count($stackValue)) throw new Exception("parse sql query failed - stack value count is not equal to 1");
  87. $stackValue = reset($stackValue);
  88. $sqlFromStack = " ! ({$stackValue}) ";
  89. $parentStackValue = array_pop($sqlValuesStack);
  90. if (!is_array($parentStackValue)) throw new Exception("parse sql query failed - parent stack value is not array");
  91. array_push($parentStackValue, $sqlFromStack);
  92. array_push($sqlValuesStack, $parentStackValue);
  93. }
  94. break;
  95. default: {
  96. if (is_array($log) && 4 == count($log) && 'comparisonFieldToValue' == $log[0]) {
  97. $sqlFieldName = $log[1];
  98. $this->_usedFields[$sqlFieldName] = true;
  99. $sqlFromStack = "{tablePrefix}`{$sqlFieldName}` {$log[2]} '{$log[3]}'";
  100. $stackValue = array_pop($sqlValuesStack);
  101. if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
  102. array_push($stackValue, $sqlFromStack);
  103. array_push($sqlValuesStack, $stackValue);
  104. //echo "L.".__LINE__.":sqlBlocksStack:" . json_encode($sqlBlocksStack) . "\n";
  105. //echo "L.".__LINE__.":sqlValuesStack:" . json_encode($sqlValuesStack) . "\n\n";
  106. } else if (is_array($log) && 5 == count($log) && 'comparisonFieldFunToValue' == $log[0]) {
  107. $sqlFieldFunName = $log[1];
  108. if (strtolower($sqlFieldFunName) != 'geometrytype') throw new Exception("Unsupported db function {$sqlFieldName}");
  109. $sqlFieldName = $log[2];
  110. $this->_usedFields[$sqlFieldName] = true;
  111. $sqlFromStack = "{$sqlFieldFunName}({tablePrefix}`{$sqlFieldName}`) {$log[3]} '{$log[4]}'";
  112. $stackValue = array_pop($sqlValuesStack);
  113. if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
  114. array_push($stackValue, $sqlFromStack);
  115. array_push($sqlValuesStack, $stackValue);
  116. } else if (is_array($log) && 2 == count($log) && 'comparisonFieldIsNull' == $log[0]) {
  117. $sqlFieldName = $log[1];
  118. $sqlFromStack = "{tablePrefix}`{$sqlFieldName}` is null";
  119. $stackValue = array_pop($sqlValuesStack);
  120. if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
  121. array_push($stackValue, $sqlFromStack);
  122. array_push($sqlValuesStack, $stackValue);
  123. } else {
  124. throw new Exception("parse sql query failed - unknown '" . json_encode($log) . "'");
  125. }
  126. }
  127. }
  128. }
  129. // TODO: parse log
  130. //echo "L.".__LINE__.":sqlBlocksStack:" . json_encode($sqlBlocksStack) . "\n";
  131. //echo "L.".__LINE__.":sqlValuesStack:" . json_encode($sqlValuesStack) . "\n--------\n";
  132. if (!empty($sqlBlocksStack)) throw new Exception("parse sql query failed - blocks stack is not empty");
  133. if (1 !== count($sqlValuesStack)) throw new Exception("parse sql query failed - values stack is empty");
  134. $this->_raw = implode("\n", $sqlValuesStack[0]);
  135. return $this->_raw;
  136. }
  137. public function getUsedFields() {
  138. return array_keys($this->_usedFields);
  139. }
  140. public function isAllowedBlockType($blockType) {
  141. if ('and' == $blockType || 'or' == $blockType) {
  142. return true;
  143. } else if ('not' == $blockType) {
  144. return true;
  145. }
  146. return false;
  147. }
  148. public function splitQueryToWords($q) {
  149. $searchWords = explode(' ', $q);
  150. $sqlSearchWords = array();
  151. if (!empty($searchWords)) {
  152. foreach ($searchWords as $word) {
  153. if (!empty($word)) {
  154. $sqlSearchWords[] = $word;
  155. }
  156. }
  157. }
  158. return $sqlSearchWords;
  159. }
  160. }