SqlQueryWhereBuilder.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 sql_filter_comparisonFieldIsNull($fieldName) {
  26. $this->_log[] = array('comparisonFieldIsNull', $fieldName);
  27. }
  28. public function getQueryWhere($tablePrefix = '') {
  29. $sqlWhereRaw = $this->parseQueryWhere();
  30. $sqlTablePrefix = ($tablePrefix)? "`{$tablePrefix}`." : '';
  31. return str_replace('{tablePrefix}', $sqlTablePrefix, $sqlWhereRaw);
  32. }
  33. public function parseQueryWhere() {
  34. $this->_raw = "";
  35. $this->_usedFields = array();
  36. $sqlBlocksStack = array();
  37. $sqlValuesStack = array();
  38. $arr = array(); array_push($sqlValuesStack, $arr);// empty array to start
  39. foreach ($this->_log as $log) {
  40. switch ($log) {
  41. case 'open_block_and':
  42. case 'open_block_or':
  43. case 'open_block_not': {
  44. $blockType = substr($log, 11);
  45. array_push($sqlBlocksStack, $blockType);
  46. $arr = array(); array_push($sqlValuesStack, $arr);
  47. //echo "L.".__LINE__.":sqlBlocksStack:" . json_encode($sqlBlocksStack) . "\n";
  48. //echo "L.".__LINE__.":sqlValuesStack:" . json_encode($sqlValuesStack) . "\n\n";
  49. }
  50. break;
  51. case 'close_block_and':
  52. case 'close_block_or': {
  53. $blockType = substr($log, 12);
  54. if (empty($sqlBlocksStack)) throw new Exception("parse sql query failed - blocks stack is empty");
  55. $stackBlockType = array_pop($sqlBlocksStack);
  56. if ($blockType != $stackBlockType) throw new Exception("parse sql query failed - expected stop '{$blockType}', given '{$stackBlockType}'");
  57. // parse to string and add to parent value stack
  58. $stackValue = array_pop($sqlValuesStack);
  59. if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
  60. if (empty($stackValue)) throw new Exception("parse sql query failed - stack value is empty");
  61. $sqlFromStack = " ( " . implode(" {$blockType} ", $stackValue) . " ) ";
  62. $parentStackValue = array_pop($sqlValuesStack);
  63. if (!is_array($parentStackValue)) throw new Exception("parse sql query failed - parent stack value is not array");
  64. array_push($parentStackValue, $sqlFromStack);
  65. array_push($sqlValuesStack, $parentStackValue);
  66. }
  67. break;
  68. case 'close_block_not': {
  69. $blockType = substr($log, 12);
  70. if (empty($sqlBlocksStack)) throw new Exception("parse sql query failed - blocks stack is empty");
  71. $stackBlockType = array_pop($sqlBlocksStack);
  72. if ($blockType != $stackBlockType) throw new Exception("parse sql query failed - expected stop '{$blockType}', given '{$stackBlockType}'");
  73. // parse to string and add to parent value stack
  74. $stackValue = array_pop($sqlValuesStack);
  75. if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
  76. if (empty($stackValue)) throw new Exception("parse sql query failed - stack value is empty");
  77. if (1 != count($stackValue)) throw new Exception("parse sql query failed - stack value count is not equal to 1");
  78. $stackValue = reset($stackValue);
  79. $sqlFromStack = " ! ({$stackValue}) ";
  80. $parentStackValue = array_pop($sqlValuesStack);
  81. if (!is_array($parentStackValue)) throw new Exception("parse sql query failed - parent stack value is not array");
  82. array_push($parentStackValue, $sqlFromStack);
  83. array_push($sqlValuesStack, $parentStackValue);
  84. }
  85. break;
  86. default: {
  87. if (is_array($log) && 4 == count($log) && 'comparisonFieldToValue' == $log[0]) {
  88. $sqlFieldName = $log[1];
  89. $this->_usedFields[$sqlFieldName] = true;
  90. $sqlFromStack = "{tablePrefix}`{$sqlFieldName}` {$log[2]} '{$log[3]}'";
  91. $stackValue = array_pop($sqlValuesStack);
  92. if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
  93. array_push($stackValue, $sqlFromStack);
  94. array_push($sqlValuesStack, $stackValue);
  95. //echo "L.".__LINE__.":sqlBlocksStack:" . json_encode($sqlBlocksStack) . "\n";
  96. //echo "L.".__LINE__.":sqlValuesStack:" . json_encode($sqlValuesStack) . "\n\n";
  97. } else if (is_array($log) && 2 == count($log) && 'comparisonFieldIsNull' == $log[0]) {
  98. $sqlFieldName = $log[1];
  99. $sqlFromStack = "{tablePrefix}`{$sqlFieldName}` is null";
  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. } else {
  105. throw new Exception("parse sql query failed - unknown '" . json_encode($log) . "'");
  106. }
  107. }
  108. }
  109. }
  110. // TODO: parse log
  111. //echo "L.".__LINE__.":sqlBlocksStack:" . json_encode($sqlBlocksStack) . "\n";
  112. //echo "L.".__LINE__.":sqlValuesStack:" . json_encode($sqlValuesStack) . "\n--------\n";
  113. if (!empty($sqlBlocksStack)) throw new Exception("parse sql query failed - blocks stack is not empty");
  114. if (1 !== count($sqlValuesStack)) throw new Exception("parse sql query failed - values stack is empty");
  115. $this->_raw = implode("\n", $sqlValuesStack[0]);
  116. return $this->_raw;
  117. }
  118. public function getUsedFields() {
  119. return array_keys($this->_usedFields);
  120. }
  121. public function isAllowedBlockType($blockType) {
  122. if ('and' == $blockType || 'or' == $blockType) {
  123. return true;
  124. } else if ('not' == $blockType) {
  125. return true;
  126. }
  127. return false;
  128. }
  129. public function splitQueryToWords($q) {
  130. $searchWords = explode(' ', $q);
  131. $sqlSearchWords = array();
  132. if (!empty($searchWords)) {
  133. foreach ($searchWords as $word) {
  134. if (!empty($word)) {
  135. $sqlSearchWords[] = $word;
  136. }
  137. }
  138. }
  139. return $sqlSearchWords;
  140. }
  141. }