| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TInlineParameterMapParser class file.
|
|
|
4 |
*
|
|
|
5 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TInlineParameterMapParser.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
10 |
* @package System.Data.SqlMap.Configuration
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* TInlineParameterMapParser class.
|
|
|
15 |
*
|
|
|
16 |
* The inline parameter map syntax lets you embed the property name,
|
|
|
17 |
* the property type, the column type, and a null value replacement into a
|
|
|
18 |
* parametrized SQL statement.
|
|
|
19 |
*
|
|
|
20 |
* @author Wei Zhuo <weizho[at]gmail[dot]com>
|
|
|
21 |
* @version $Id: TInlineParameterMapParser.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
22 |
* @package System.Data.SqlMap.Configuration
|
|
|
23 |
* @since 3.1
|
|
|
24 |
*/
|
|
|
25 |
class TInlineParameterMapParser
|
|
|
26 |
{
|
|
|
27 |
/**
|
|
|
28 |
* Regular expression for parsing inline parameter maps.
|
|
|
29 |
*/
|
|
|
30 |
const PARAMETER_TOKEN_REGEXP = '/#([^#]+)#/';
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Parse the sql text for inline parameters.
|
|
|
34 |
* @param string sql text
|
|
|
35 |
* @param array file and node details for exception message.
|
|
|
36 |
* @return array 'sql' and 'parameters' name value pairs.
|
|
|
37 |
*/
|
|
|
38 |
public function parse($sqlText, $scope)
|
|
|
39 |
{
|
|
|
40 |
$matches = array();
|
|
|
41 |
$mappings = array();
|
|
|
42 |
preg_match_all(self::PARAMETER_TOKEN_REGEXP, $sqlText, $matches);
|
|
|
43 |
|
|
|
44 |
for($i = 0, $k=count($matches[1]); $i<$k; $i++)
|
|
|
45 |
{
|
|
|
46 |
$mappings[] = $this->parseMapping($matches[1][$i], $scope);
|
|
|
47 |
$sqlText = str_replace($matches[0][$i], '?', $sqlText);
|
|
|
48 |
}
|
|
|
49 |
return array('sql'=>$sqlText, 'parameters'=>$mappings);
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Parse inline parameter with syntax as
|
|
|
54 |
* #propertyName,type=string,dbype=Varchar,nullValue=N/A,handler=string#
|
|
|
55 |
* @param string parameter token
|
|
|
56 |
* @param array file and node details for exception message.
|
|
|
57 |
*/
|
|
|
58 |
protected function parseMapping($token, $scope)
|
|
|
59 |
{
|
|
|
60 |
$mapping = new TParameterProperty;
|
|
|
61 |
$properties = explode(',', $token);
|
|
|
62 |
$mapping->setProperty(trim(array_shift($properties)));
|
|
|
63 |
foreach($properties as $property)
|
|
|
64 |
{
|
|
|
65 |
$prop = explode('=',$property);
|
|
|
66 |
$name = trim($prop[0]); $value=trim($prop[1]);
|
|
|
67 |
if($mapping->canSetProperty($name))
|
|
|
68 |
$mapping->{'set'.$name}($value);
|
|
|
69 |
else
|
|
|
70 |
{
|
|
|
71 |
throw new TSqlMapUndefinedException(
|
|
|
72 |
'sqlmap_undefined_property_inline_map',
|
|
|
73 |
$name, $scope['file'], $scope['node'], $token);
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
return $mapping;
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
|