SqlQueryWhereBuilder.php 5.7 KB

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