SqlQueryWhereBuilder.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. case 'GreaterThen': $sqlCompSign = '>'; break;
  22. case 'LessThen': $sqlCompSign = '<'; break;
  23. case 'GreaterThenOrEqualTo': $sqlCompSign = '>='; break;
  24. case 'LessThenOrEqualTo': $sqlCompSign = '<='; break;
  25. default: throw new Exception("Unsupported comparison sign");
  26. }
  27. $this->_log[] = array('comparisonFieldToValue', $fieldName, $sqlCompSign, $value);
  28. }
  29. public function addComparisonFieldFunToValue($fieldFun, $fieldName, $comparisonSign, $value) {
  30. $sqlCompSign = "";
  31. switch ($comparisonSign) {
  32. case '=': $sqlCompSign = '='; break;
  33. case 'like': $sqlCompSign = 'like'; break;
  34. case 'GreaterThen': $sqlCompSign = '>'; break;
  35. case 'LessThen': $sqlCompSign = '<'; break;
  36. case 'GreaterThenOrEqualTo': $sqlCompSign = '>='; break;
  37. case 'LessThenOrEqualTo': $sqlCompSign = '<='; break;
  38. default: throw new Exception("Unsupported comparison sign");
  39. }
  40. $this->_log[] = array('comparisonFieldFunToValue', $fieldFun, $fieldName, $sqlCompSign, $value);
  41. }
  42. public function sql_filter_comparisonFieldIsNull($fieldName) {
  43. $this->_log[] = array('comparisonFieldIsNull', $fieldName);
  44. }
  45. public function getQueryWhere($tablePrefix = '') {
  46. $sqlWhereRaw = $this->parseQueryWhere();
  47. $sqlTablePrefix = ($tablePrefix)? "`{$tablePrefix}`." : '';
  48. return str_replace('{tablePrefix}', $sqlTablePrefix, $sqlWhereRaw);
  49. }
  50. public function filterRawArray($item) {
  51. $lowerItem = array(); foreach ((array)$item as $fieldName => $value) $lowerItem[strtolower($fieldName)] = $value;
  52. DBG::_('DBG_DS_OGC', '>2', "\$lowerItem", $lowerItem, __CLASS__, __FUNCTION__, __LINE__);
  53. $sqlBlocksStack = array();
  54. $sqlValuesStack = array();
  55. $arr = array(); array_push($sqlValuesStack, $arr);// empty array to start
  56. foreach ($this->_log as $idxLog => $log) {
  57. switch ($log) {
  58. case 'open_block_and':
  59. case 'open_block_or':
  60. case 'open_block_not': {
  61. $blockType = substr($log, 11);// 'and', 'or', 'not'
  62. array_push($sqlBlocksStack, $blockType);
  63. $arr = array(); array_push($sqlValuesStack, $arr);
  64. DBG::_('DBG_DS_OGC', '>2', "\nlog loop({$idxLog}) '{$log}' \$sqlBlocksStack", $sqlBlocksStack, __CLASS__, __FUNCTION__, __LINE__);
  65. DBG::_('DBG_DS_OGC', '>2', "\nlog loop({$idxLog}) '{$log}' \$sqlValuesStack", $sqlValuesStack, __CLASS__, __FUNCTION__, __LINE__);
  66. }
  67. break;
  68. case 'close_block_and':
  69. case 'close_block_or': {
  70. $blockType = substr($log, 12);// 'and', 'or'
  71. if (empty($sqlBlocksStack)) throw new Exception("parse sql query failed - blocks stack is empty");
  72. $stackBlockType = array_pop($sqlBlocksStack);
  73. if ($blockType != $stackBlockType) throw new Exception("parse sql query failed - expected stop '{$blockType}', given '{$stackBlockType}'");
  74. // parse to string and add to parent value stack
  75. $stackValue = array_pop($sqlValuesStack);
  76. if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
  77. if (empty($stackValue)) throw new Exception("parse sql query failed - stack value is empty");
  78. $sqlFromStack = false;
  79. foreach ($stackValue as $bool) {
  80. if (!is_scalar($bool) && ($bool == true or $bool == false)) throw new Exception("parse query failed - expected bool values in block or");
  81. if ($bool) $sqlFromStack = true;
  82. }
  83. $parentStackValue = array_pop($sqlValuesStack);
  84. if (!is_array($parentStackValue)) throw new Exception("parse sql query failed - parent stack value is not array");
  85. array_push($parentStackValue, $sqlFromStack);
  86. array_push($sqlValuesStack, $parentStackValue);
  87. DBG::_('DBG_DS_OGC', '>2', "\nlog loop({$idxLog}) '{$log}' \$sqlBlocksStack", $sqlBlocksStack, __CLASS__, __FUNCTION__, __LINE__);
  88. DBG::_('DBG_DS_OGC', '>2', "\nlog loop({$idxLog}) '{$log}' \$sqlValuesStack", $sqlValuesStack, __CLASS__, __FUNCTION__, __LINE__);
  89. }
  90. break;
  91. case 'close_block_not': {
  92. $blockType = substr($log, 12);// 'not'
  93. if (empty($sqlBlocksStack)) throw new Exception("parse sql query failed - blocks stack is empty");
  94. $stackBlockType = array_pop($sqlBlocksStack);
  95. if ($blockType != $stackBlockType) throw new Exception("parse sql query failed - expected stop '{$blockType}', given '{$stackBlockType}'");
  96. // parse to string and add to parent value stack
  97. $stackValue = array_pop($sqlValuesStack);
  98. if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
  99. if (empty($stackValue)) throw new Exception("parse sql query failed - stack value is empty");
  100. if (1 != count($stackValue)) throw new Exception("parse sql query failed - stack value count is not equal to 1");
  101. $stackValue = reset($stackValue);
  102. $sqlFromStack = " ! ({$stackValue}) ";
  103. $parentStackValue = array_pop($sqlValuesStack);
  104. if (!is_array($parentStackValue)) throw new Exception("parse sql query failed - parent stack value is not array");
  105. array_push($parentStackValue, $sqlFromStack);
  106. array_push($sqlValuesStack, $parentStackValue);
  107. DBG::_('DBG_DS_OGC', '>2', "\nlog loop({$idxLog}) '{$log}' \$sqlBlocksStack", $sqlBlocksStack, __CLASS__, __FUNCTION__, __LINE__);
  108. DBG::_('DBG_DS_OGC', '>2', "\nlog loop({$idxLog}) '{$log}' \$sqlValuesStack", $sqlValuesStack, __CLASS__, __FUNCTION__, __LINE__);
  109. }
  110. break;
  111. default: {
  112. if (is_array($log) && 4 == count($log) && 'comparisonFieldToValue' == $log[0]) {
  113. $fieldName = strtolower($log[1]);
  114. if (!array_key_exists($fieldName, $lowerItem)) throw new Exception("field '{$fieldName}' not defined");
  115. switch ($log[2]) {
  116. case '=': {
  117. $sqlFromStack = ($lowerItem[$fieldName] == $log[3]);
  118. DBG::_('DBG_DS_OGC', '>2', "\nlog loop({$idxLog}) '{$log[0]}' comparison({$lowerItem[$fieldName]} == {$log[3]}) \$sqlFromStack", $sqlFromStack, __CLASS__, __FUNCTION__, __LINE__);
  119. break;
  120. }
  121. default: throw new Exception("compaison sign '{$log[2]}' not defined");
  122. }
  123. // $this->_usedFields[$sqlFieldName] = true;
  124. // $sqlFromStack = "{tablePrefix}`{$sqlFieldName}` {$log[2]} '{$log[3]}'";
  125. $stackValue = array_pop($sqlValuesStack);
  126. if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
  127. array_push($stackValue, $sqlFromStack);
  128. array_push($sqlValuesStack, $stackValue);
  129. } else if (is_array($log) && 5 == count($log) && 'comparisonFieldFunToValue' == $log[0]) {
  130. $sqlFieldFunName = $log[1];
  131. if (strtolower($sqlFieldFunName) != 'geometrytype') throw new Exception("Unsupported db function {$sqlFieldName}");
  132. $sqlFieldName = $log[2];
  133. // $this->_usedFields[$sqlFieldName] = true;
  134. $sqlFromStack = "{$sqlFieldFunName}({tablePrefix}`{$sqlFieldName}`) {$log[3]} '{$log[4]}'";
  135. $stackValue = array_pop($sqlValuesStack);
  136. if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
  137. array_push($stackValue, $sqlFromStack);
  138. array_push($sqlValuesStack, $stackValue);
  139. } else if (is_array($log) && 2 == count($log) && 'comparisonFieldIsNull' == $log[0]) {
  140. $sqlFieldName = $log[1];
  141. $sqlFromStack = "{tablePrefix}`{$sqlFieldName}` is null";
  142. $stackValue = array_pop($sqlValuesStack);
  143. if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
  144. array_push($stackValue, $sqlFromStack);
  145. array_push($sqlValuesStack, $stackValue);
  146. } else {
  147. throw new Exception("parse sql query failed - unknown '" . json_encode($log) . "'");
  148. }
  149. DBG::_('DBG_DS_OGC', '>2', "\nlog loop({$idxLog}) '{$log}' \$sqlBlocksStack", $sqlBlocksStack, __CLASS__, __FUNCTION__, __LINE__);
  150. DBG::_('DBG_DS_OGC', '>2', "\nlog loop({$idxLog}) '{$log}' \$sqlValuesStack", $sqlValuesStack, __CLASS__, __FUNCTION__, __LINE__);
  151. }
  152. }
  153. }
  154. if (!empty($sqlBlocksStack)) throw new Exception("parse sql query failed - blocks stack is not empty");
  155. if (1 !== count($sqlValuesStack)) throw new Exception("parse sql query failed - values stack is empty");
  156. if (1 !== count($sqlValuesStack[0])) throw new Exception("parse sql query failed - values stack is empty");
  157. $bool = $sqlValuesStack[0][0];
  158. if (!is_scalar($bool) && ($bool == true or $bool == false)) throw new Exception("parse query failed - expected bool in stack value");
  159. DBG::_('DBG_DS_OGC', '>2', "\nOK return ".($bool ? 'true' : 'false')." for item:", $item, __CLASS__, __FUNCTION__, __LINE__);
  160. return $bool;
  161. }
  162. public function parseQueryWhere() {
  163. $this->_raw = "";
  164. $this->_usedFields = array();
  165. $sqlBlocksStack = array();
  166. $sqlValuesStack = array();
  167. $arr = array(); array_push($sqlValuesStack, $arr);// empty array to start
  168. foreach ($this->_log as $log) {
  169. switch ($log) {
  170. case 'open_block_and':
  171. case 'open_block_or':
  172. case 'open_block_not': {
  173. $blockType = substr($log, 11);
  174. array_push($sqlBlocksStack, $blockType);
  175. $arr = array(); array_push($sqlValuesStack, $arr);
  176. //echo "L.".__LINE__.":sqlBlocksStack:" . json_encode($sqlBlocksStack) . "\n";
  177. //echo "L.".__LINE__.":sqlValuesStack:" . json_encode($sqlValuesStack) . "\n\n";
  178. }
  179. break;
  180. case 'close_block_and':
  181. case 'close_block_or': {
  182. $blockType = substr($log, 12);
  183. if (empty($sqlBlocksStack)) throw new Exception("parse sql query failed - blocks stack is empty");
  184. $stackBlockType = array_pop($sqlBlocksStack);
  185. if ($blockType != $stackBlockType) throw new Exception("parse sql query failed - expected stop '{$blockType}', given '{$stackBlockType}'");
  186. // parse to string and add to parent value stack
  187. $stackValue = array_pop($sqlValuesStack);
  188. if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
  189. if (empty($stackValue)) throw new Exception("parse sql query failed - stack value is empty");
  190. $sqlFromStack = " ( " . implode(" {$blockType} ", $stackValue) . " ) ";
  191. $parentStackValue = array_pop($sqlValuesStack);
  192. if (!is_array($parentStackValue)) throw new Exception("parse sql query failed - parent stack value is not array");
  193. array_push($parentStackValue, $sqlFromStack);
  194. array_push($sqlValuesStack, $parentStackValue);
  195. }
  196. break;
  197. case 'close_block_not': {
  198. $blockType = substr($log, 12);
  199. if (empty($sqlBlocksStack)) throw new Exception("parse sql query failed - blocks stack is empty");
  200. $stackBlockType = array_pop($sqlBlocksStack);
  201. if ($blockType != $stackBlockType) throw new Exception("parse sql query failed - expected stop '{$blockType}', given '{$stackBlockType}'");
  202. // parse to string and add to parent value stack
  203. $stackValue = array_pop($sqlValuesStack);
  204. if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
  205. if (empty($stackValue)) throw new Exception("parse sql query failed - stack value is empty");
  206. if (1 != count($stackValue)) throw new Exception("parse sql query failed - stack value count is not equal to 1");
  207. $stackValue = reset($stackValue);
  208. $sqlFromStack = " ! ({$stackValue}) ";
  209. $parentStackValue = array_pop($sqlValuesStack);
  210. if (!is_array($parentStackValue)) throw new Exception("parse sql query failed - parent stack value is not array");
  211. array_push($parentStackValue, $sqlFromStack);
  212. array_push($sqlValuesStack, $parentStackValue);
  213. }
  214. break;
  215. default: {
  216. if (is_array($log) && 4 == count($log) && 'comparisonFieldToValue' == $log[0]) {
  217. $sqlFieldName = $log[1];
  218. $this->_usedFields[$sqlFieldName] = true;
  219. $sqlFromStack = "{tablePrefix}`{$sqlFieldName}` {$log[2]} '{$log[3]}'";
  220. $stackValue = array_pop($sqlValuesStack);
  221. if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
  222. array_push($stackValue, $sqlFromStack);
  223. array_push($sqlValuesStack, $stackValue);
  224. //echo "L.".__LINE__.":sqlBlocksStack:" . json_encode($sqlBlocksStack) . "\n";
  225. //echo "L.".__LINE__.":sqlValuesStack:" . json_encode($sqlValuesStack) . "\n\n";
  226. } else if (is_array($log) && 5 == count($log) && 'comparisonFieldFunToValue' == $log[0]) {
  227. $sqlFieldFunName = $log[1];
  228. if (strtolower($sqlFieldFunName) != 'geometrytype') throw new Exception("Unsupported db function {$sqlFieldName}");
  229. $sqlFieldName = $log[2];
  230. $this->_usedFields[$sqlFieldName] = true;
  231. $sqlFromStack = "{$sqlFieldFunName}({tablePrefix}`{$sqlFieldName}`) {$log[3]} '{$log[4]}'";
  232. $stackValue = array_pop($sqlValuesStack);
  233. if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
  234. array_push($stackValue, $sqlFromStack);
  235. array_push($sqlValuesStack, $stackValue);
  236. } else if (is_array($log) && 2 == count($log) && 'comparisonFieldIsNull' == $log[0]) {
  237. $sqlFieldName = $log[1];
  238. $sqlFromStack = "{tablePrefix}`{$sqlFieldName}` is null";
  239. $stackValue = array_pop($sqlValuesStack);
  240. if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
  241. array_push($stackValue, $sqlFromStack);
  242. array_push($sqlValuesStack, $stackValue);
  243. } else {
  244. throw new Exception("parse sql query failed - unknown '" . json_encode($log) . "'");
  245. }
  246. }
  247. }
  248. }
  249. // TODO: parse log
  250. //echo "L.".__LINE__.":sqlBlocksStack:" . json_encode($sqlBlocksStack) . "\n";
  251. //echo "L.".__LINE__.":sqlValuesStack:" . json_encode($sqlValuesStack) . "\n--------\n";
  252. if (!empty($sqlBlocksStack)) throw new Exception("parse sql query failed - blocks stack is not empty");
  253. if (1 !== count($sqlValuesStack)) throw new Exception("parse sql query failed - values stack is empty");
  254. $this->_raw = implode("\n", $sqlValuesStack[0]);
  255. return $this->_raw;
  256. }
  257. public function getUsedFields() {
  258. return array_keys($this->_usedFields);
  259. }
  260. public function isAllowedBlockType($blockType) {
  261. if ('and' == $blockType || 'or' == $blockType) {
  262. return true;
  263. } else if ('not' == $blockType) {
  264. return true;
  265. }
  266. return false;
  267. }
  268. public function splitQueryToWords($q) {
  269. $searchWords = explode(' ', $q);
  270. $sqlSearchWords = array();
  271. if (!empty($searchWords)) {
  272. foreach ($searchWords as $word) {
  273. if (!empty($word)) {
  274. $sqlSearchWords[] = $word;
  275. }
  276. }
  277. }
  278. return $sqlSearchWords;
  279. }
  280. }