| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- class SqlQueryWhereBuilder {
- private $_log = array();
- public function __construct() {
- }
- public function openBlock($blockType) {
- if ('and' == $blockType) {
- } else if ('or' == $blockType) {
- } else {
- throw new Exception("unsupported block type");
- }
- $this->_log[] = "open_block_{$blockType}";
- }
- public function closeBlock($blockType) {
- if ('and' == $blockType) {
- } else if ('or' == $blockType) {
- } else {
- throw new Exception("unsupported block type");
- }
- $this->_log[] = "close_block_{$blockType}";
- }
- public function addComparisonFieldToValue($fieldName, $comparisonSign, $value) {
- $sqlCompSign = "";
- switch ($comparisonSign) {
- case '=': $sqlCompSign = '='; break;
- default: throw new Exception("Unsupported comparison sign");
- }
- $this->_log[] = array('comparisonFieldToValue', $fieldName, $sqlCompSign, $value);
- }
- public function getQueryWhere($tablePrefix = '') {
- $sqlWhere = "";
- $sqlBlocksStack = array();
- $sqlValuesStack = array();
- $sqlTablePrefix = ($tablePrefix)? "`{$tablePrefix}`." : '';
- $arr = array(); array_push($sqlValuesStack, $arr);// empty array to start
- foreach ($this->_log as $log) {
- switch ($log) {
- case 'open_block_and': {
- array_push($sqlBlocksStack, 'and');
- $arr = array(); array_push($sqlValuesStack, $arr);
- }
- break;
- case 'close_block_and': {
- if (empty($sqlBlocksStack)) throw new Exception("parse sql query failed - blocks stack is empty");
- $blockType = array_pop($sqlBlocksStack);
- if ('and' != $blockType) throw new Exception("parse sql query failed - expected stop 'and', given '{$blockType}'");
- // parse to string and add to parent value stack
- $stackValue = array_pop($sqlValuesStack);
- if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
- if (empty($stackValue)) throw new Exception("parse sql query failed - stack value is empty");
- $sqlFromStack = " ( " . implode(" and ", $stackValue) . " ) ";
- $parentStackValue = array_pop($sqlValuesStack);
- if (!is_array($parentStackValue)) throw new Exception("parse sql query failed - parent stack value is not array");
- array_push($parentStackValue, $sqlFromStack);
- array_push($sqlValuesStack, $parentStackValue);
- }
- break;
- case 'open_block_or': {
- array_push($sqlBlocksStack, 'or');
- $arr = array(); array_push($sqlValuesStack, $arr);
- //echo "L.".__LINE__.":sqlBlocksStack:" . json_encode($sqlBlocksStack) . "\n";
- //echo "L.".__LINE__.":sqlValuesStack:" . json_encode($sqlValuesStack) . "\n\n";
- }
- break;
- case 'close_block_or': {
- if (empty($sqlBlocksStack)) throw new Exception("parse sql query failed - blocks stack is empty");
- $blockType = array_pop($sqlBlocksStack);
- if ('or' != $blockType) throw new Exception("parse sql query failed - expected stop 'or', given '{$blockType}'");
- // parse to string and add to parent value stack
- $stackValue = array_pop($sqlValuesStack);
- if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
- if (empty($stackValue)) throw new Exception("parse sql query failed - stack value is empty");
- $sqlFromStack = " ( " . implode(" or ", $stackValue) . " ) ";
- $parentStackValue = array_pop($sqlValuesStack);
- if (!is_array($parentStackValue)) throw new Exception("parse sql query failed - parent stack value is not array");
- array_push($parentStackValue, $sqlFromStack);
- array_push($sqlValuesStack, $parentStackValue);
- //echo "L.".__LINE__.":sqlBlocksStack:" . json_encode($sqlBlocksStack) . "\n";
- //echo "L.".__LINE__.":sqlValuesStack:" . json_encode($sqlValuesStack) . "\n\n";
- }
- break;
- default: {
- if (is_array($log) && 4 == count($log) && 'comparisonFieldToValue' == $log[0]) {
- $sqlFromStack = "{$sqlTablePrefix}`{$log[1]}` {$log[2]} '{$log[3]}'";
- $stackValue = array_pop($sqlValuesStack);
- if (!is_array($stackValue)) throw new Exception("parse sql query failed - stack value is not array");
- array_push($stackValue, $sqlFromStack);
- array_push($sqlValuesStack, $stackValue);
- //echo "L.".__LINE__.":sqlBlocksStack:" . json_encode($sqlBlocksStack) . "\n";
- //echo "L.".__LINE__.":sqlValuesStack:" . json_encode($sqlValuesStack) . "\n\n";
- } else {
- throw new Exception("parse sql query failed - unknown '" . json_encode($log) . "'");
- }
- }
- }
- }
- // TODO: parse log
- //echo "L.".__LINE__.":sqlBlocksStack:" . json_encode($sqlBlocksStack) . "\n";
- //echo "L.".__LINE__.":sqlValuesStack:" . json_encode($sqlValuesStack) . "\n--------\n";
- if (!empty($sqlBlocksStack)) throw new Exception("parse sql query failed - blocks stack is not empty");
- if (1 !== count($sqlValuesStack)) throw new Exception("parse sql query failed - values stack is empty");
- $sqlWhere = implode("\n", $sqlValuesStack[0]);
- return $sqlWhere;
- }
- }
|