ElementParser.class.php
6.79 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
class Util_Dao_SqlMap_ElementParser {
/**
* 设定Map元素
*
* @var Array
*/
private $mapElementsConfig = array ('Insert', 'Select','Update', 'Delete', 'Command', 'Read_call','Write_call' );
/**
* 创建一个临时文件夹
*
* @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 Util_Dao_Cache
*/
private $cache;
private $sqlStr;
/**
* 读过滤
*
* @var array
*/
private $readFilters = array('Select','Read_call','Command');
/**
* 替换参数验证
*
* @var String
*/
private $replaceFilters = '/\bdelete\b|\bupdate\b|\binsert\b/i';
/**
* 初始化
*
* @param String $dbname
*/
public function __construct($sqlMapXmlFile) {
if (! is_string ( $sqlMapXmlFile ) || empty ( $sqlMapXmlFile )) {
throw new Util_Dao_Exception ( 'ElementParser :SQL Map find' );
}
$this->_sqlMapXmlFile = $sqlMapXmlFile;
$this->parseResource ();
}
/**
* 获取元素
*
*/
public function element($sqlId) {
return $this->elementMap [$sqlId]['sql'];
}
public function elementReadTag($sqlId) {
return $this->elementMap [$sqlId]['readTag'];
}
/**
* 分析元素
*
*/
private function parseResource() {
$xmlFile = new Util_Common_Io_File ( $this->_sqlMapXmlFile );
if (! $xmlFile->exists ()) {
throw new Util_Dao_Exception ( $this->_sqlMapXmlFile . ' not find' );
} else {
$this->resourceFiles = "file://" . $xmlFile->absPath ();
$lastModified = $xmlFile->lastModified ();
//echo $this->tmp . DIRECTORY_SEPARATOR . md5 ( $this->resourceFiles ) . '.php';
$tmpFile = new Util_Common_Io_File ( $this->tmp . DIRECTORY_SEPARATOR . md5 ( $this->resourceFiles ) . '.php' );
/**
* APC Cache
*/
if (Util_Cache_Adapter_Apc::isEnabled ()) {
$apcEntry = Util_Cache_Adapter_Apc::get ( $this->resourceFiles );
if ($apcEntry != null && $lastModified == $apcEntry ['lastModified']) {
$this->elementMap = $apcEntry ['elementMap'];
return;
}
} else {
/**
* 文件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 Util_Common_Xml_XmlNode ( );
$resourceNode->loadDOM ( $resourceDocument );
$sqlMapNodeList = $resourceNode->namedChildren ( "sqlMap" );
$namespace = "";
$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]['sql'] = $queryNode->value ();
$this->elementMap [$namespace . '.' . $queryId]['readTag'] = in_array(ucwords($tagName), $this->readFilters);
}
}
}
//var_dump($this->elementMap);
/** 存入 Cache **/
if (Util_Cache_Adapter_Apc::isEnabled ()) {
$valArray = array ('elementMap' => $this->elementMap, 'lastModified' => $lastModified );
Util_Cache_Adapter_Apc::set ( $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($replaceMap) {
if (is_array ( $replaceMap )) {
$newMap = array ();
foreach ( $replaceMap as $key => $value ) {
if (is_array ( $value )) {
throw new Util_Dao_Exception ( ' replaceMap value Should not be array ' );
}
if (preg_match ( $this->replaceFilters, $value ) == 1) {
throw new Util_Dao_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 Util_Dao_Exception ( ' sqlId Incorrect format ' );
}
if (! is_array ( $this->replaceMap )) {
throw new Util_Dao_Exception ( ' replaceMap Incorrect format ' );
}
$this->sqlStr = strtr ( $this->element ( $sqlId ), $this->replaceMap );
$this->strictParse ();
return $this->sqlStr;
}
/**
* 获取map标签元素配置
*
* @return Array
*/
public function getMapElementsConfig(){
return $this->mapElementsConfig;
}
/**
* 获取是否调用写存储
* @return boolean
*/
public function getWriteCallState() {
return $this->writeCallState;
}
/**
* 验证SQL是否合法 : 和 ? 不能同时出现在Sql中
*
*/
private function strictParse() {
if (empty ( $this->sqlStr )) {
return;
}
$sqlLength = strlen ( $this->sqlStr );
$lastQuote = null;
$positionalParams = array ();
$namedParams = array ();
for($i = 0; $i < $sqlLength; $i ++) {
$char = $this->sqlStr {$i};
if ($char == "'" || $char == "\"") {
if ($lastQuote != null) {
if ($lastQuote == $char) {
$lastQuote = null;
} else {
continue; //跳出本次循环 条件正确时继续
}
} else {
$lastQuote = $char;
}
} else {
if (! $lastQuote) {
if ($char == "?") {
$positionalParams [$i] = "?";
} else if ($char == ":") {
$nameStart = $i;
$name = "";
while ( true ) {
$nameStart ++;
if ($nameStart > $sqlLength - 1) {
break;
}
$nameChar = $this->sqlStr {$nameStart};
$ord = ord ( $nameChar ); //48-57 97-122 65-90 95
if ($ord == 95 || ($ord >= 48 && $ord <= 57) || ($ord >= 97 && $ord <= 122) || ($ord >= 65 && $ord <= 90)) {
$name .= $nameChar;
} else {
break;
}
}
if ($name != "") {
$namedParams [$i] = $name;
}
}
}
}
}
if (! empty ( $namedParams ) && ! empty ( $positionalParams )) {
throw new Util_Dao_Exception ( ' ( ? and : ) placeholders can only be either named or positional params ' );
}
}
}