summaryrefslogtreecommitdiff
path: root/lib/prado/framework/Data/SqlMap/Configuration/TInlineParameterMapParser.php
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2016-02-24 23:18:07 +0100
committeremkael <emkael@tlen.pl>2016-02-24 23:18:07 +0100
commit6f7fdef0f500cd4bb540affd3bc1482243f337c1 (patch)
tree4853eecd0769a903e6130c1896e1d070848150dd /lib/prado/framework/Data/SqlMap/Configuration/TInlineParameterMapParser.php
parent61f2ea48a4e11cb5fb941b3783e19c9e9ef38a45 (diff)
* Prado 3.3.0
Diffstat (limited to 'lib/prado/framework/Data/SqlMap/Configuration/TInlineParameterMapParser.php')
-rw-r--r--lib/prado/framework/Data/SqlMap/Configuration/TInlineParameterMapParser.php77
1 files changed, 77 insertions, 0 deletions
diff --git a/lib/prado/framework/Data/SqlMap/Configuration/TInlineParameterMapParser.php b/lib/prado/framework/Data/SqlMap/Configuration/TInlineParameterMapParser.php
new file mode 100644
index 0000000..dfe14f8
--- /dev/null
+++ b/lib/prado/framework/Data/SqlMap/Configuration/TInlineParameterMapParser.php
@@ -0,0 +1,77 @@
+<?php
+/**
+ * TInlineParameterMapParser class file.
+ *
+ * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
+ * @link https://github.com/pradosoft/prado
+ * @copyright Copyright &copy; 2005-2015 The PRADO Group
+ * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
+ * @package System.Data.SqlMap.Configuration
+ */
+
+/**
+ * TInlineParameterMapParser class.
+ *
+ * The inline parameter map syntax lets you embed the property name,
+ * the property type, the column type, and a null value replacement into a
+ * parametrized SQL statement.
+ *
+ * @author Wei Zhuo <weizho[at]gmail[dot]com>
+ * @package System.Data.SqlMap.Configuration
+ * @since 3.1
+ */
+class TInlineParameterMapParser
+{
+ /**
+ * Regular expression for parsing inline parameter maps.
+ */
+ const PARAMETER_TOKEN_REGEXP = '/#([^#]+)#/';
+
+ /**
+ * Parse the sql text for inline parameters.
+ * @param string sql text
+ * @param array file and node details for exception message.
+ * @return array 'sql' and 'parameters' name value pairs.
+ */
+ public function parse($sqlText, $scope)
+ {
+ $matches = array();
+ $mappings = array();
+ preg_match_all(self::PARAMETER_TOKEN_REGEXP, $sqlText, $matches);
+
+ for($i = 0, $k=count($matches[1]); $i<$k; $i++)
+ {
+ $mappings[] = $this->parseMapping($matches[1][$i], $scope);
+ $sqlText = str_replace($matches[0][$i], '?', $sqlText);
+ }
+ return array('sql'=>$sqlText, 'parameters'=>$mappings);
+ }
+
+ /**
+ * Parse inline parameter with syntax as
+ * #propertyName,type=string,dbype=Varchar,nullValue=N/A,handler=string#
+ * @param string parameter token
+ * @param array file and node details for exception message.
+ */
+ protected function parseMapping($token, $scope)
+ {
+ $mapping = new TParameterProperty;
+ $properties = explode(',', $token);
+ $mapping->setProperty(trim(array_shift($properties)));
+ foreach($properties as $property)
+ {
+ $prop = explode('=',$property);
+ $name = trim($prop[0]); $value=trim($prop[1]);
+ if($mapping->canSetProperty($name))
+ $mapping->{'set'.$name}($value);
+ else
+ {
+ throw new TSqlMapUndefinedException(
+ 'sqlmap_undefined_property_inline_map',
+ $name, $scope['file'], $scope['node'], $token);
+ }
+ }
+ return $mapping;
+ }
+}
+