| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TSimpleDynamicParser 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: TSimpleDynamicParser.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
10 |
* @package System.Data.SqlMap.Configuration
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* TSimpleDynamicParser finds place holders $name$ in the sql text and replaces
|
|
|
15 |
* it with a TSimpleDynamicParser::DYNAMIC_TOKEN.
|
|
|
16 |
*
|
|
|
17 |
* @author Wei Zhuo <weizho[at]gmail[dot]com>
|
|
|
18 |
* @version $Id: TSimpleDynamicParser.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
19 |
* @package System.Data.SqlMap.Configuration
|
|
|
20 |
* @since 3.1
|
|
|
21 |
*/
|
|
|
22 |
class TSimpleDynamicParser
|
|
|
23 |
{
|
|
|
24 |
const PARAMETER_TOKEN_REGEXP = '/\$([^\$]+)\$/';
|
|
|
25 |
const DYNAMIC_TOKEN = '`!`';
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Parse the sql text for dynamic place holders of the form $name$.
|
|
|
29 |
* @param string Sql text.
|
|
|
30 |
* @return array name value pairs 'sql' and 'parameters'.
|
|
|
31 |
*/
|
|
|
32 |
public function parse($sqlText)
|
|
|
33 |
{
|
|
|
34 |
$matches = array();
|
|
|
35 |
$mappings = array();
|
|
|
36 |
preg_match_all(self::PARAMETER_TOKEN_REGEXP, $sqlText, $matches);
|
|
|
37 |
for($i = 0, $k=count($matches[1]); $i<$k; $i++)
|
|
|
38 |
{
|
|
|
39 |
$mappings[] = $matches[1][$i];
|
|
|
40 |
$sqlText = str_replace($matches[0][$i], self::DYNAMIC_TOKEN, $sqlText);
|
|
|
41 |
}
|
|
|
42 |
return array('sql'=>$sqlText, 'parameters'=>$mappings);
|
|
|
43 |
}
|
|
|
44 |
}
|
|
|
45 |
|