Browse Source

Gui xml working parse basic ogc:Filter to php

Piotr Labudda 10 years ago
parent
commit
744bb5f7b9
2 changed files with 130 additions and 12 deletions
  1. 15 12
      SE/schema/gui/testConvertGuiXmlToFilters.xsl
  2. 115 0
      SE/se-lib/SqlQueryWhereBuilder.php

+ 15 - 12
SE/schema/gui/testConvertGuiXmlToFilters.xsl

@@ -161,6 +161,8 @@ sf_Problemy
 
 	<xsl:template match="ui_table:filters">
 <xsl:value-of select="'&lt;?php'" />
+require_once dirname(__FILE__) . '/../../se-lib/bootstrap.php';
+Lib::loadClass('SqlQueryWhereBuilder');
 $filters = array();
 // DBG: template match="ui_table:filters"
 // DBG: element name = '<xsl:value-of select="local-name()" />'
@@ -208,8 +210,8 @@ print_r($filters);
 -->
 	<xsl:template match="ui_table:filter">
 // DBG: template match="ui_table:filter"
-$filters['<xsl:value-of select="ui_table:name" />']['label']="<xsl:value-of select="ui_table:label" />";
-$filters['<xsl:value-of select="ui_table:name" />']['icon']="<xsl:value-of select="ui_table:icon" />";
+$filters['<xsl:value-of select="ui_table:name" />']['label'] = "<xsl:value-of select="ui_table:label" />";
+$filters['<xsl:value-of select="ui_table:name" />']['icon'] = "<xsl:value-of select="ui_table:icon" />";
 $filterButtons = array();
 <xsl:apply-templates select="ui_table:filter_buttons/ui_table:filter_button"/>
 $filters['<xsl:value-of select="ui_table:name" />']['buttons'] = $filterButtons;
@@ -218,11 +220,12 @@ $filters['<xsl:value-of select="ui_table:name" />']['buttons'] = $filterButtons;
 	<xsl:template match="ui_table:filter_button">
 // DBG: template match="ui_table:filter_button"
 $filterButtons['<xsl:value-of select="ui_table:value" />'] = array();
-$filterButtons['<xsl:value-of select="ui_table:value" />']['label']="<xsl:value-of select="ui_table:name" />";
+$filterButtons['<xsl:value-of select="ui_table:value" />']['label'] = "<xsl:value-of select="ui_table:name" />";
 <!-- TODO: sql filter -->
-$filterButtons['<xsl:value-of select="ui_table:value" />']['sqlFilter']=&lt;&lt;&lt;END_OF_SQL_FILTER
+$sqlFilter = new SqlQueryWhereBuilder();
 <xsl:apply-templates select="ogc:Filter"/>
-END_OF_SQL_FILTER;
+$filterButtons['<xsl:value-of select="ui_table:value" />']['sqlFilter'] = $sqlFilter;
+$filterButtons['<xsl:value-of select="ui_table:value" />']['sqlFilterRaw'] = $sqlFilter->getQueryWhere();
 	</xsl:template>
 
 	<xsl:template match="ogc:Filter">
@@ -232,16 +235,16 @@ END_OF_SQL_FILTER;
 
 	<xsl:template match="ogc:Or">
 // DBG: template match="ogc:Or"
-		or (
-<xsl:apply-templates/>
-		)
+$sqlFilter->openBlock('or');
+		<xsl:apply-templates/>
+$sqlFilter->closeBlock('or');
 	</xsl:template>
 
 	<xsl:template match="ogc:And">
 // DBG: template match="ogc:And"
-		and (
-<xsl:apply-templates/>
-		)
+$sqlFilter->openBlock('and');
+		<xsl:apply-templates/>
+$sqlFilter->closeBlock('and');
 	</xsl:template>
 
 <!--
@@ -253,7 +256,7 @@ END_OF_SQL_FILTER;
 <!-- TODO: sql variable for table prefix (t) -->
 	<xsl:template match="ogc:PropertyIsEqualTo">
 // DBG: template match="ogc:PropertyIsEqualTo"
-			t.<xsl:value-of select="ogc:PropertyName" />='<xsl:value-of select="ogc:Literal" />'
+		$sqlFilter->addComparisonFieldToValue('<xsl:value-of select="ogc:PropertyName" />', '=', '<xsl:value-of select="ogc:Literal" />');
 	</xsl:template>
 
 </xsl:stylesheet>

+ 115 - 0
SE/se-lib/SqlQueryWhereBuilder.php

@@ -0,0 +1,115 @@
+<?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;
+	}
+
+}