CodeParser.php
1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/**
* Class Q_Db_Adapter_Pdo_CodeParser
*/
class Q_Db_Adapter_Pdo_CodeParser
{
/**
* 替换列表
* @var array
*/
private $replaceMap = array();
/**
* 替换参数验证
* @var string
*/
private $replaceFilters = '/(?=select\s|insert\s|delete\s|update\s)/i';
/**
* 设置替换列表
* @param array $replaceMap 替换列表(关联数组)
* @throws Q_Db_Exception
*/
function setReplaceMap(array $replaceMap)
{
$newMap = array();
foreach ($replaceMap as $key => $value) {
if (is_array($value)) {
throw new Q_Db_Exception('replaceMap value Should not be array ');
}
if (preg_match($this->replaceFilters, $value) == 1) {
throw new Q_Db_Exception('replaceMap should not contain select、insert、delete、update ');
}
$newMap["#" . $key . "#"] = $value;
}
$this->replaceMap = $newMap;
return $this;
}
/**
* 获取Sql
* @param $sql
* @return string
* @throws Q_Db_Exception
*/
function sql($sql)
{
return strtr($sql, $this->replaceMap);
}
}