SqlQueryWhereBuilder.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. $blockType = substr($log, 11);
  41. array_push($sqlBlocksStack, $blockType);
  42. $arr = array(); array_push($sqlValuesStack, $arr);
  43. //echo "L.".__LINE__.":sqlBlocksStack:" . json_encode($sqlBlocksStack) . "\n";
  44. //echo "L.".__LINE__.":sqlValuesStack:" . json_encode($sqlValuesStack) . "\n\n";
  45. }
  46. break;
  47. case 'close_block_and':
  48. case 'close_block_or': {
  49. $blockType = substr($log, 12);
  50. if (empty($sqlBlocksStack)) throw new Exception("parse sql query failed - blocks stack is empty");
  51. $stackBlockType = array_pop($sqlBlocksStack);
  52. if ($blockType != $stackBlockType) throw new Exception("parse sql query failed - expected stop '{$blockType}', given '{$stackBlockType}'");
  53. // parse to string and add to parent value stack
  54. $stackValue = array_pop($sqlValuesStack);
  55. if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
  56. if (empty($stackValue)) throw new Exception("parse sql query failed - stack value is empty");
  57. $sqlFromStack = " ( " . implode(" {$blockType} ", $stackValue) . " ) ";
  58. $parentStackValue = array_pop($sqlValuesStack);
  59. if (!is_array($parentStackValue)) throw new Exception("parse sql query failed - parent stack value is not array");
  60. array_push($parentStackValue, $sqlFromStack);
  61. array_push($sqlValuesStack, $parentStackValue);
  62. }
  63. break;
  64. default: {
  65. if (is_array($log) && 4 == count($log) && 'comparisonFieldToValue' == $log[0]) {
  66. $sqlFieldName = $log[1];
  67. $this->_usedFields[$sqlFieldName] = true;
  68. $sqlFromStack = "{tablePrefix}`{$sqlFieldName}` {$log[2]} '{$log[3]}'";
  69. $stackValue = array_pop($sqlValuesStack);
  70. if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
  71. array_push($stackValue, $sqlFromStack);
  72. array_push($sqlValuesStack, $stackValue);
  73. //echo "L.".__LINE__.":sqlBlocksStack:" . json_encode($sqlBlocksStack) . "\n";
  74. //echo "L.".__LINE__.":sqlValuesStack:" . json_encode($sqlValuesStack) . "\n\n";
  75. } else {
  76. throw new Exception("parse sql query failed - unknown '" . json_encode($log) . "'");
  77. }
  78. }
  79. }
  80. }
  81. // TODO: parse log
  82. //echo "L.".__LINE__.":sqlBlocksStack:" . json_encode($sqlBlocksStack) . "\n";
  83. //echo "L.".__LINE__.":sqlValuesStack:" . json_encode($sqlValuesStack) . "\n--------\n";
  84. if (!empty($sqlBlocksStack)) throw new Exception("parse sql query failed - blocks stack is not empty");
  85. if (1 !== count($sqlValuesStack)) throw new Exception("parse sql query failed - values stack is empty");
  86. $this->_raw = implode("\n", $sqlValuesStack[0]);
  87. return $this->_raw;
  88. }
  89. public function getUsedFields() {
  90. return array_keys($this->_usedFields);
  91. }
  92. public function isAllowedBlockType($blockType) {
  93. if ('and' == $blockType || 'or' == $blockType) {
  94. return true;
  95. } else if ('not' == $blockType) {
  96. return false;// TODO: allow not operator: expect only one children, if more -> use only last
  97. }
  98. return false;
  99. }
  100. public function splitQueryToWords($q) {
  101. $searchWords = explode(' ', $q);
  102. $sqlSearchWords = array();
  103. if (!empty($searchWords)) {
  104. foreach ($searchWords as $word) {
  105. if (!empty($word)) {
  106. $sqlSearchWords[] = $word;
  107. }
  108. }
  109. }
  110. return $sqlSearchWords;
  111. }
  112. }