ElementParser.php
5.94 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
class Q_Db_Adapter_Pdo_ElementParser
{
/**
* 设定Map元素
*
* @var Array
*/
private $mapElementsConfig = array(
'Insert',
'Update',
'Select',
'Delete',
'Command'
);
/**
* 创建一个临时文件夹
*
* @var unknown_type
*/
private $tmp = 'tmp';
/**
* map 文件
*
* @var String
*/
private $_sqlMapXmlFile;
private $_resourceFiles;
/**
* 替换列表
*
* @var Array
*/
private $replaceMap = array();
/**
* 元素Map
*
* @var Array
*/
private $elementMap = array();
/**
* Cache
*
* @var Q_Db_Cache
*/
private $cache;
private $sqlStr;
/**
* 替换参数验证
*
* @var String
*/
private $replaceFilters = '/(?=select\s|insert\s|delete\s|update\s)/i';
/**
* 初始化
*
* @param String $dbname
*/
public function __construct($sqlMapXmlFile)
{
if (!is_string($sqlMapXmlFile) || empty($sqlMapXmlFile)) {
throw new Q_Db_Exception('ElementParser :SQL Map find');
}
$this->_sqlMapXmlFile = $sqlMapXmlFile;
$this->parseResource();
}
/**
* 获取元素
*
*/
public function element($sqlId)
{
return $this->elementMap[$sqlId];
}
/**
* 分析元素
*
*/
private function parseResource()
{
$xmlFile = new Q_Utils_Io_File($this->_sqlMapXmlFile);
if (!$xmlFile->exists()) {
throw new Q_Db_Exception($this->_sqlMapXmlFile . ' not find');
} else {
$this->resourceFiles = "file://" . $xmlFile->absPath();
$lastModified = $xmlFile->lastModified();
/**
* APC Cache
*/
if (Q_Cache_Backend::isEnabled()) {
$apcEntry = Q_Cache_Backend::load($this->resourceFiles);
if ($apcEntry != null && $lastModified == $apcEntry['lastModified']) {
$this->elementMap = $apcEntry['elementMap'];
return;
}
} else {
$cachePath = (defined('Q_CACHE_FILE_PATH') && Q_CACHE_FILE_PATH != '') ? Q_CACHE_FILE_PATH : $this->tmp;
$tmpFile = new Q_Utils_Io_File($cachePath . DIRECTORY_SEPARATOR . md5($this->resourceFiles) . '.php');
/**
* 文件Cache
*/
if ($tmpFile->exists() == true) {
$entry = json_decode($tmpFile->reader()->read(), true);
if (!empty($entry) && $lastModified == $entry['lastModified']) {
$this->elementMap = $entry['elementMap'];
return;
}
}
}
$resourceDocument = new DOMDocument();
$resourceDocument->load($xmlFile->absPath());
$resourceNode = new Q_Utils_Xml_XmlNode();
$resourceNode->loadDOM($resourceDocument);
$sqlMapNodeList = $resourceNode->namedChildren("sqlMap");
$sqlMapNode = $sqlMapNodeList[0];
$namespace = trim($sqlMapNode->attr("namespace"));
$queryNodeList = $sqlMapNode->children();
foreach ($queryNodeList as $queryNode) {
if ($queryNode->type() == XML_ELEMENT_NODE) {
$tagName = strToLower($queryNode->name());
$queryId = trim($queryNode->attr("id"));
if (in_array(ucwords($tagName), $this->mapElementsConfig) && $queryId != '' && $namespace != '') {
$this->elementMap[$namespace . '.' . $queryId] = $queryNode->value();
}
}
}
/** 存入 Cache **/
if (Q_Cache_Backend::isEnabled()) {
$valArray = array(
'elementMap' => $this->elementMap,
'lastModified' => $lastModified
);
Q_Cache_Backend::load($this->resourceFiles, $valArray);
} else {
if ($tmpFile->exists() == true) {
$valArray = array(
'elementMap' => $this->elementMap,
'lastModified' => $lastModified
);
$tmpFile->writer()->write(json_encode($valArray));
}
}
}
}
/**
* 获取所有对象
*
* @return Array
*/
public function elementMap()
{
return $this->elementMap;
}
/**
* 设置替换列表
*
* @param array $replaceMap 替换列表(关联数组)
*/
public function setReplaceMap(array $replaceMap)
{
if (is_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;
}
}
/**
* 获取Sql
*
* @return String
*/
public function sql($sqlId)
{
if (stristr($sqlId, '.') === FALSE || empty($sqlId)) {
throw new Q_Db_Exception(' sqlId Incorrect format ');
}
if (!is_array($this->replaceMap)) {
throw new Q_Db_Exception(' replaceMap Incorrect format ');
}
$this->sqlStr = strtr($this->element($sqlId), $this->replaceMap);
return $this->sqlStr;
}
/**
* 获取map标签元素配置
*
* @return Array
*/
public function getMapElementsConfig()
{
return $this->mapElementsConfig;
}
}