| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TSqlMapXmlConfigBuilder, TSqlMapXmlConfiguration, TSqlMapXmlMappingConfiguration classes 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: TSqlMapXmlConfiguration.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
10 |
* @package System.Data.SqlMap.Configuration
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
Prado::using('System.Data.SqlMap.Configuration.TSqlMapStatement');
|
|
|
14 |
|
|
|
15 |
/**
|
|
|
16 |
* TSqlMapXmlConfig class file.
|
|
|
17 |
*
|
|
|
18 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
19 |
* @version $Id: TSqlMapXmlConfiguration.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
20 |
* @package System.Data.SqlMap.Configuration
|
|
|
21 |
*/
|
|
|
22 |
abstract class TSqlMapXmlConfigBuilder
|
|
|
23 |
{
|
|
|
24 |
/**
|
|
|
25 |
* Create an instance of an object give by the attribute named 'class' in the
|
|
|
26 |
* node and set the properties on the object given by attribute names and values.
|
|
|
27 |
* @param SimpleXmlNode property node
|
|
|
28 |
* @return Object new instance of class with class name given by 'class' attribute value.
|
|
|
29 |
*/
|
|
|
30 |
protected function createObjectFromNode($node)
|
|
|
31 |
{
|
|
|
32 |
if(isset($node['class']))
|
|
|
33 |
{
|
|
|
34 |
$obj = Prado::createComponent((string)$node['class']);
|
|
|
35 |
$this->setObjectPropFromNode($obj,$node,array('class'));
|
|
|
36 |
return $obj;
|
|
|
37 |
}
|
|
|
38 |
throw new TSqlMapConfigurationException(
|
|
|
39 |
'sqlmap_node_class_undef', $node, $this->getConfigFile());
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* For each attributes (excluding attribute named in $except) set the
|
|
|
44 |
* property of the $obj given by the name of the attribute with the value
|
|
|
45 |
* of the attribute.
|
|
|
46 |
* @param Object object instance
|
|
|
47 |
* @param SimpleXmlNode property node
|
|
|
48 |
* @param array exception property name
|
|
|
49 |
*/
|
|
|
50 |
protected function setObjectPropFromNode($obj,$node,$except=array())
|
|
|
51 |
{
|
|
|
52 |
foreach($node->attributes() as $name=>$value)
|
|
|
53 |
{
|
|
|
54 |
if(!in_array($name,$except))
|
|
|
55 |
{
|
|
|
56 |
if($obj->canSetProperty($name))
|
|
|
57 |
$obj->{$name} = (string)$value;
|
|
|
58 |
else
|
|
|
59 |
throw new TSqlMapConfigurationException(
|
|
|
60 |
'sqlmap_invalid_property', $name, get_class($obj),
|
|
|
61 |
$node, $this->getConfigFile());
|
|
|
62 |
}
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Gets the filename relative to the basefile.
|
|
|
68 |
* @param string base filename
|
|
|
69 |
* @param string relative filename
|
|
|
70 |
* @return string absolute filename.
|
|
|
71 |
*/
|
|
|
72 |
protected function getAbsoluteFilePath($basefile,$resource)
|
|
|
73 |
{
|
|
|
74 |
$basedir = dirname($basefile);
|
|
|
75 |
$file = realpath($basedir.DIRECTORY_SEPARATOR.$resource);
|
|
|
76 |
if(!is_string($file) || !is_file($file))
|
|
|
77 |
$file = realpath($resource);
|
|
|
78 |
if(is_string($file) && is_file($file))
|
|
|
79 |
return $file;
|
|
|
80 |
else
|
|
|
81 |
throw new TSqlMapConfigurationException(
|
|
|
82 |
'sqlmap_unable_to_find_resource', $resource);
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Load document using simple xml.
|
|
|
87 |
* @param string filename.
|
|
|
88 |
* @return SimpleXmlElement xml document.
|
|
|
89 |
*/
|
|
|
90 |
protected function loadXmlDocument($filename,TSqlMapXmlConfiguration $config)
|
|
|
91 |
{
|
|
|
92 |
if(!is_file($filename))
|
|
|
93 |
throw new TSqlMapConfigurationException(
|
|
|
94 |
'sqlmap_unable_to_find_config', $filename);
|
|
|
95 |
return simplexml_load_string($config->replaceProperties(file_get_contents($filename)));
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Get element node by ID value (try for attribute name ID as case insensitive).
|
|
|
100 |
* @param SimpleXmlDocument $document
|
|
|
101 |
* @param string tag name.
|
|
|
102 |
* @param string id value.
|
|
|
103 |
* @return SimpleXmlElement node if found, null otherwise.
|
|
|
104 |
*/
|
|
|
105 |
protected function getElementByIdValue($document, $tag, $value)
|
|
|
106 |
{
|
|
|
107 |
//hack to allow upper case and lower case attribute names.
|
|
|
108 |
foreach(array('id','ID','Id', 'iD') as $id)
|
|
|
109 |
{
|
|
|
110 |
$xpath = "//{$tag}[@{$id}='{$value}']";
|
|
|
111 |
foreach($document->xpath($xpath) as $node)
|
|
|
112 |
return $node;
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* @return string configuration file.
|
|
|
118 |
*/
|
|
|
119 |
protected abstract function getConfigFile();
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* TSqlMapXmlConfig class.
|
|
|
124 |
*
|
|
|
125 |
* Configures the TSqlMapManager using xml configuration file.
|
|
|
126 |
*
|
|
|
127 |
* @author Wei Zhuo <weizho[at]gmail[dot]com>
|
|
|
128 |
* @version $Id: TSqlMapXmlConfiguration.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
129 |
* @package System.Data.SqlMap.Configuration
|
|
|
130 |
* @since 3.1
|
|
|
131 |
*/
|
|
|
132 |
class TSqlMapXmlConfiguration extends TSqlMapXmlConfigBuilder
|
|
|
133 |
{
|
|
|
134 |
/**
|
|
|
135 |
* @var TSqlMapManager manager
|
|
|
136 |
*/
|
|
|
137 |
private $_manager;
|
|
|
138 |
/**
|
|
|
139 |
* @var string configuration file.
|
|
|
140 |
*/
|
|
|
141 |
private $_configFile;
|
|
|
142 |
/**
|
|
|
143 |
* @var array global properties.
|
|
|
144 |
*/
|
|
|
145 |
private $_properties=array();
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* @param TSqlMapManager manager instance.
|
|
|
149 |
*/
|
|
|
150 |
public function __construct($manager)
|
|
|
151 |
{
|
|
|
152 |
$this->_manager=$manager;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
public function getManager()
|
|
|
156 |
{
|
|
|
157 |
return $this->_manager;
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
protected function getConfigFile()
|
|
|
161 |
{
|
|
|
162 |
return $this->_configFile;
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
/**
|
|
|
166 |
* Configure the TSqlMapManager using the given xml file.
|
|
|
167 |
* @param string SqlMap configuration xml file.
|
|
|
168 |
*/
|
|
|
169 |
public function configure($filename=null)
|
|
|
170 |
{
|
|
|
171 |
$this->_configFile=$filename;
|
|
|
172 |
$document = $this->loadXmlDocument($filename,$this);
|
|
|
173 |
|
|
|
174 |
foreach($document->xpath('//property') as $property)
|
|
|
175 |
$this->loadGlobalProperty($property);
|
|
|
176 |
|
|
|
177 |
foreach($document->xpath('//typeHandler') as $handler)
|
|
|
178 |
$this->loadTypeHandler($handler);
|
|
|
179 |
|
|
|
180 |
foreach($document->xpath('//connection[last()]') as $conn)
|
|
|
181 |
$this->loadDatabaseConnection($conn);
|
|
|
182 |
|
|
|
183 |
//try to load configuration in the current config file.
|
|
|
184 |
$mapping = new TSqlMapXmlMappingConfiguration($this);
|
|
|
185 |
$mapping->configure($filename);
|
|
|
186 |
|
|
|
187 |
foreach($document->xpath('//sqlMap') as $sqlmap)
|
|
|
188 |
$this->loadSqlMappingFiles($sqlmap);
|
|
|
189 |
|
|
|
190 |
$this->resolveResultMapping();
|
|
|
191 |
$this->attachCacheModels();
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
/**
|
|
|
195 |
* Load global replacement property.
|
|
|
196 |
* @param SimpleXmlElement property node.
|
|
|
197 |
*/
|
|
|
198 |
protected function loadGlobalProperty($node)
|
|
|
199 |
{
|
|
|
200 |
$this->_properties[(string)$node['name']] = (string)$node['value'];
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
/**
|
|
|
204 |
* Load the type handler configurations.
|
|
|
205 |
* @param SimpleXmlElement type handler node
|
|
|
206 |
*/
|
|
|
207 |
protected function loadTypeHandler($node)
|
|
|
208 |
{
|
|
|
209 |
$handler = $this->createObjectFromNode($node);
|
|
|
210 |
$this->_manager->getTypeHandlers()->registerTypeHandler($handler);
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
/**
|
|
|
214 |
* Load the database connection tag.
|
|
|
215 |
* @param SimpleXmlElement connection node.
|
|
|
216 |
*/
|
|
|
217 |
protected function loadDatabaseConnection($node)
|
|
|
218 |
{
|
|
|
219 |
$conn = $this->createObjectFromNode($node);
|
|
|
220 |
$this->_manager->setDbConnection($conn);
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
/**
|
|
|
224 |
* Load SqlMap mapping configuration.
|
|
|
225 |
* @param unknown_type $node
|
|
|
226 |
*/
|
|
|
227 |
protected function loadSqlMappingFiles($node)
|
|
|
228 |
{
|
|
|
229 |
if(strlen($resource = (string)$node['resource']) > 0)
|
|
|
230 |
{
|
|
|
231 |
$mapping = new TSqlMapXmlMappingConfiguration($this);
|
|
|
232 |
$filename = $this->getAbsoluteFilePath($this->_configFile, $resource);
|
|
|
233 |
$mapping->configure($filename);
|
|
|
234 |
}
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
/**
|
|
|
238 |
* Resolve nest result mappings.
|
|
|
239 |
*/
|
|
|
240 |
protected function resolveResultMapping()
|
|
|
241 |
{
|
|
|
242 |
$maps = $this->_manager->getResultMaps();
|
|
|
243 |
foreach($maps as $entry)
|
|
|
244 |
{
|
|
|
245 |
foreach($entry->getColumns() as $item)
|
|
|
246 |
{
|
|
|
247 |
$resultMap = $item->getResultMapping();
|
|
|
248 |
if(strlen($resultMap) > 0)
|
|
|
249 |
{
|
|
|
250 |
if($maps->contains($resultMap))
|
|
|
251 |
$item->setNestedResultMap($maps[$resultMap]);
|
|
|
252 |
else
|
|
|
253 |
throw new TSqlMapConfigurationException(
|
|
|
254 |
'sqlmap_unable_to_find_result_mapping',
|
|
|
255 |
$resultMap, $this->_configFile, $entry->getID());
|
|
|
256 |
}
|
|
|
257 |
}
|
|
|
258 |
if(!is_null($entry->getDiscriminator()))
|
|
|
259 |
$entry->getDiscriminator()->initialize($this->_manager);
|
|
|
260 |
}
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
/**
|
|
|
264 |
* Set the cache for each statement having a cache model property.
|
|
|
265 |
*/
|
|
|
266 |
protected function attachCacheModels()
|
|
|
267 |
{
|
|
|
268 |
foreach($this->_manager->getMappedStatements() as $mappedStatement)
|
|
|
269 |
{
|
|
|
270 |
if(strlen($model = $mappedStatement->getStatement()->getCacheModel()) > 0)
|
|
|
271 |
{
|
|
|
272 |
$cache = $this->_manager->getCacheModel($model);
|
|
|
273 |
$mappedStatement->getStatement()->setCache($cache);
|
|
|
274 |
}
|
|
|
275 |
}
|
|
|
276 |
}
|
|
|
277 |
|
|
|
278 |
/**
|
|
|
279 |
* Replace the place holders ${name} in text with properties the
|
|
|
280 |
* corresponding global property value.
|
|
|
281 |
* @param string original string.
|
|
|
282 |
* @return string string with global property replacement.
|
|
|
283 |
*/
|
|
|
284 |
public function replaceProperties($string)
|
|
|
285 |
{
|
|
|
286 |
foreach($this->_properties as $find => $replace)
|
|
|
287 |
$string = str_replace('${'.$find.'}', $replace, $string);
|
|
|
288 |
return $string;
|
|
|
289 |
}
|
|
|
290 |
}
|
|
|
291 |
|
|
|
292 |
/**
|
|
|
293 |
* Loads the statements, result maps, parameters maps from xml configuration.
|
|
|
294 |
*
|
|
|
295 |
* description
|
|
|
296 |
*
|
|
|
297 |
* @author Wei Zhuo <weizho[at]gmail[dot]com>
|
|
|
298 |
* @version $Id: TSqlMapXmlConfiguration.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
299 |
* @package System.Data.SqlMap.Configuration
|
|
|
300 |
* @since 3.1
|
|
|
301 |
*/
|
|
|
302 |
class TSqlMapXmlMappingConfiguration extends TSqlMapXmlConfigBuilder
|
|
|
303 |
{
|
|
|
304 |
private $_xmlConfig;
|
|
|
305 |
private $_configFile;
|
|
|
306 |
private $_manager;
|
|
|
307 |
|
|
|
308 |
private $_document;
|
|
|
309 |
|
|
|
310 |
private $_FlushOnExecuteStatements=array();
|
|
|
311 |
|
|
|
312 |
/**
|
|
|
313 |
* Regular expressions for escaping simple/inline parameter symbols
|
|
|
314 |
*/
|
|
|
315 |
const SIMPLE_MARK='$';
|
|
|
316 |
const INLINE_SYMBOL='#';
|
|
|
317 |
const ESCAPED_SIMPLE_MARK_REGEXP='/\$\$/';
|
|
|
318 |
const ESCAPED_INLINE_SYMBOL_REGEXP='/\#\#/';
|
|
|
319 |
const SIMPLE_PLACEHOLDER='`!!`';
|
|
|
320 |
const INLINE_PLACEHOLDER='`!!!`';
|
|
|
321 |
|
|
|
322 |
/**
|
|
|
323 |
* @param TSqlMapXmlConfiguration parent xml configuration.
|
|
|
324 |
*/
|
|
|
325 |
public function __construct(TSqlMapXmlConfiguration $xmlConfig)
|
|
|
326 |
{
|
|
|
327 |
$this->_xmlConfig=$xmlConfig;
|
|
|
328 |
$this->_manager=$xmlConfig->getManager();
|
|
|
329 |
}
|
|
|
330 |
|
|
|
331 |
protected function getConfigFile()
|
|
|
332 |
{
|
|
|
333 |
return $this->_configFile;
|
|
|
334 |
}
|
|
|
335 |
|
|
|
336 |
/**
|
|
|
337 |
* Configure an XML mapping.
|
|
|
338 |
* @param string xml mapping filename.
|
|
|
339 |
*/
|
|
|
340 |
public function configure($filename)
|
|
|
341 |
{
|
|
|
342 |
$this->_configFile=$filename;
|
|
|
343 |
$document = $this->loadXmlDocument($filename,$this->_xmlConfig);
|
|
|
344 |
$this->_document=$document;
|
|
|
345 |
|
|
|
346 |
foreach($document->xpath('//resultMap') as $node)
|
|
|
347 |
$this->loadResultMap($node);
|
|
|
348 |
|
|
|
349 |
foreach($document->xpath('//parameterMap') as $node)
|
|
|
350 |
$this->loadParameterMap($node);
|
|
|
351 |
|
|
|
352 |
foreach($document->xpath('//statement') as $node)
|
|
|
353 |
$this->loadStatementTag($node);
|
|
|
354 |
|
|
|
355 |
foreach($document->xpath('//select') as $node)
|
|
|
356 |
$this->loadSelectTag($node);
|
|
|
357 |
|
|
|
358 |
foreach($document->xpath('//insert') as $node)
|
|
|
359 |
$this->loadInsertTag($node);
|
|
|
360 |
|
|
|
361 |
foreach($document->xpath('//update') as $node)
|
|
|
362 |
$this->loadUpdateTag($node);
|
|
|
363 |
|
|
|
364 |
foreach($document->xpath('//delete') as $node)
|
|
|
365 |
$this->loadDeleteTag($node);
|
|
|
366 |
|
|
|
367 |
foreach($document->xpath('//procedure') as $node)
|
|
|
368 |
$this->loadProcedureTag($node);
|
|
|
369 |
|
|
|
370 |
foreach($document->xpath('//cacheModel') as $node)
|
|
|
371 |
$this->loadCacheModel($node);
|
|
|
372 |
|
|
|
373 |
$this->registerCacheTriggers();
|
|
|
374 |
}
|
|
|
375 |
|
|
|
376 |
/**
|
|
|
377 |
* Load the result maps.
|
|
|
378 |
* @param SimpleXmlElement result map node.
|
|
|
379 |
*/
|
|
|
380 |
protected function loadResultMap($node)
|
|
|
381 |
{
|
|
|
382 |
$resultMap = $this->createResultMap($node);
|
|
|
383 |
|
|
|
384 |
//find extended result map.
|
|
|
385 |
if(strlen($extendMap = $resultMap->getExtends()) > 0)
|
|
|
386 |
{
|
|
|
387 |
if(!$this->_manager->getResultMaps()->contains($extendMap))
|
|
|
388 |
{
|
|
|
389 |
$extendNode=$this->getElementByIdValue($this->_document,'resultMap',$extendMap);
|
|
|
390 |
if($extendNode!==null)
|
|
|
391 |
$this->loadResultMap($extendNode);
|
|
|
392 |
}
|
|
|
393 |
|
|
|
394 |
if(!$this->_manager->getResultMaps()->contains($extendMap))
|
|
|
395 |
throw new TSqlMapConfigurationException(
|
|
|
396 |
'sqlmap_unable_to_find_parent_result_map', $node, $this->_configFile, $extendMap);
|
|
|
397 |
|
|
|
398 |
$superMap = $this->_manager->getResultMap($extendMap);
|
|
|
399 |
$resultMap->getColumns()->mergeWith($superMap->getColumns());
|
|
|
400 |
}
|
|
|
401 |
|
|
|
402 |
//add the result map
|
|
|
403 |
if(!$this->_manager->getResultMaps()->contains($resultMap->getID()))
|
|
|
404 |
$this->_manager->addResultMap($resultMap);
|
|
|
405 |
}
|
|
|
406 |
|
|
|
407 |
/**
|
|
|
408 |
* Create a new result map and its associated result properties,
|
|
|
409 |
* disciminiator and sub maps.
|
|
|
410 |
* @param SimpleXmlElement result map node
|
|
|
411 |
* @return TResultMap SqlMap result mapping.
|
|
|
412 |
*/
|
|
|
413 |
protected function createResultMap($node)
|
|
|
414 |
{
|
|
|
415 |
$resultMap = new TResultMap();
|
|
|
416 |
$this->setObjectPropFromNode($resultMap,$node);
|
|
|
417 |
|
|
|
418 |
//result nodes
|
|
|
419 |
foreach($node->result as $result)
|
|
|
420 |
{
|
|
|
421 |
$property = new TResultProperty($resultMap);
|
|
|
422 |
$this->setObjectPropFromNode($property,$result);
|
|
|
423 |
$resultMap->addResultProperty($property);
|
|
|
424 |
}
|
|
|
425 |
|
|
|
426 |
//create the discriminator
|
|
|
427 |
$discriminator = null;
|
|
|
428 |
if(isset($node->discriminator))
|
|
|
429 |
{
|
|
|
430 |
$discriminator = new TDiscriminator();
|
|
|
431 |
$this->setObjectPropFromNode($discriminator, $node->discriminator);
|
|
|
432 |
$discriminator->initMapping($resultMap);
|
|
|
433 |
}
|
|
|
434 |
|
|
|
435 |
foreach($node->xpath('subMap') as $subMapNode)
|
|
|
436 |
{
|
|
|
437 |
if(is_null($discriminator))
|
|
|
438 |
throw new TSqlMapConfigurationException(
|
|
|
439 |
'sqlmap_undefined_discriminator', $node, $this->_configFile,$subMapNode);
|
|
|
440 |
$subMap = new TSubMap;
|
|
|
441 |
$this->setObjectPropFromNode($subMap,$subMapNode);
|
|
|
442 |
$discriminator->addSubMap($subMap);
|
|
|
443 |
}
|
|
|
444 |
|
|
|
445 |
if(!is_null($discriminator))
|
|
|
446 |
$resultMap->setDiscriminator($discriminator);
|
|
|
447 |
|
|
|
448 |
return $resultMap;
|
|
|
449 |
}
|
|
|
450 |
|
|
|
451 |
/**
|
|
|
452 |
* Load parameter map from xml.
|
|
|
453 |
*
|
|
|
454 |
* @param SimpleXmlElement parameter map node.
|
|
|
455 |
*/
|
|
|
456 |
protected function loadParameterMap($node)
|
|
|
457 |
{
|
|
|
458 |
$parameterMap = $this->createParameterMap($node);
|
|
|
459 |
|
|
|
460 |
if(strlen($extendMap = $parameterMap->getExtends()) > 0)
|
|
|
461 |
{
|
|
|
462 |
if(!$this->_manager->getParameterMaps()->contains($extendMap))
|
|
|
463 |
{
|
|
|
464 |
$extendNode=$this->getElementByIdValue($this->_document,'parameterMap',$extendMap);
|
|
|
465 |
if($extendNode!==null)
|
|
|
466 |
$this->loadParameterMap($extendNode);
|
|
|
467 |
}
|
|
|
468 |
|
|
|
469 |
if(!$this->_manager->getParameterMaps()->contains($extendMap))
|
|
|
470 |
throw new TSqlMapConfigurationException(
|
|
|
471 |
'sqlmap_unable_to_find_parent_parameter_map', $node, $this->_configFile,$extendMap);
|
|
|
472 |
$superMap = $this->_manager->getParameterMap($extendMap);
|
|
|
473 |
$index = 0;
|
|
|
474 |
foreach($superMap->getPropertyNames() as $propertyName)
|
|
|
475 |
$parameterMap->insertProperty($index++,$superMap->getProperty($propertyName));
|
|
|
476 |
}
|
|
|
477 |
$this->_manager->addParameterMap($parameterMap);
|
|
|
478 |
}
|
|
|
479 |
|
|
|
480 |
/**
|
|
|
481 |
* Create a new parameter map from xml node.
|
|
|
482 |
* @param SimpleXmlElement parameter map node.
|
|
|
483 |
* @return TParameterMap new parameter mapping.
|
|
|
484 |
*/
|
|
|
485 |
protected function createParameterMap($node)
|
|
|
486 |
{
|
|
|
487 |
$parameterMap = new TParameterMap();
|
|
|
488 |
$this->setObjectPropFromNode($parameterMap,$node);
|
|
|
489 |
foreach($node->parameter as $parameter)
|
|
|
490 |
{
|
|
|
491 |
$property = new TParameterProperty();
|
|
|
492 |
$this->setObjectPropFromNode($property,$parameter);
|
|
|
493 |
$parameterMap->addProperty($property);
|
|
|
494 |
}
|
|
|
495 |
return $parameterMap;
|
|
|
496 |
}
|
|
|
497 |
|
|
|
498 |
/**
|
|
|
499 |
* Load statement mapping from xml configuration file.
|
|
|
500 |
* @param SimpleXmlElement statement node.
|
|
|
501 |
*/
|
|
|
502 |
protected function loadStatementTag($node)
|
|
|
503 |
{
|
|
|
504 |
$statement = new TSqlMapStatement();
|
|
|
505 |
$this->setObjectPropFromNode($statement,$node);
|
|
|
506 |
$this->processSqlStatement($statement, $node);
|
|
|
507 |
$mappedStatement = new TMappedStatement($this->_manager, $statement);
|
|
|
508 |
$this->_manager->addMappedStatement($mappedStatement);
|
|
|
509 |
}
|
|
|
510 |
|
|
|
511 |
/**
|
|
|
512 |
* Load extended SQL statements if application. Replaces global properties
|
|
|
513 |
* in the sql text. Extracts inline parameter maps.
|
|
|
514 |
* @param TSqlMapStatement mapped statement.
|
|
|
515 |
* @param SimpleXmlElement statement node.
|
|
|
516 |
*/
|
|
|
517 |
protected function processSqlStatement($statement, $node)
|
|
|
518 |
{
|
|
|
519 |
$commandText = (string)$node;
|
|
|
520 |
if(strlen($extend = $statement->getExtends()) > 0)
|
|
|
521 |
{
|
|
|
522 |
$superNode = $this->getElementByIdValue($this->_document,'*',$extend);
|
|
|
523 |
if($superNode!==null)
|
|
|
524 |
$commandText = (string)$superNode . $commandText;
|
|
|
525 |
else
|
|
|
526 |
throw new TSqlMapConfigurationException(
|
|
|
527 |
'sqlmap_unable_to_find_parent_sql', $extend, $this->_configFile,$node);
|
|
|
528 |
}
|
|
|
529 |
//$commandText = $this->_xmlConfig->replaceProperties($commandText);
|
|
|
530 |
$statement->initialize($this->_manager);
|
|
|
531 |
$this->applyInlineParameterMap($statement, $commandText, $node);
|
|
|
532 |
}
|
|
|
533 |
|
|
|
534 |
/**
|
|
|
535 |
* Extract inline parameter maps.
|
|
|
536 |
* @param TSqlMapStatement statement object.
|
|
|
537 |
* @param string sql text
|
|
|
538 |
* @param SimpleXmlElement statement node.
|
|
|
539 |
*/
|
|
|
540 |
protected function applyInlineParameterMap($statement, $sqlStatement, $node)
|
|
|
541 |
{
|
|
|
542 |
$scope['file'] = $this->_configFile;
|
|
|
543 |
$scope['node'] = $node;
|
|
|
544 |
|
|
|
545 |
$sqlStatement=preg_replace(self::ESCAPED_INLINE_SYMBOL_REGEXP,self::INLINE_PLACEHOLDER,$sqlStatement);
|
|
|
546 |
if($statement->parameterMap() === null)
|
|
|
547 |
{
|
|
|
548 |
// Build a Parametermap with the inline parameters.
|
|
|
549 |
// if they exist. Then delete inline infos from sqltext.
|
|
|
550 |
$parameterParser = new TInlineParameterMapParser;
|
|
|
551 |
$sqlText = $parameterParser->parse($sqlStatement, $scope);
|
|
|
552 |
if(count($sqlText['parameters']) > 0)
|
|
|
553 |
{
|
|
|
554 |
$map = new TParameterMap();
|
|
|
555 |
$map->setID($statement->getID().'-InLineParameterMap');
|
|
|
556 |
$statement->setInlineParameterMap($map);
|
|
|
557 |
foreach($sqlText['parameters'] as $property)
|
|
|
558 |
$map->addProperty($property);
|
|
|
559 |
}
|
|
|
560 |
$sqlStatement = $sqlText['sql'];
|
|
|
561 |
}
|
|
|
562 |
$sqlStatement=preg_replace('/'.self::INLINE_PLACEHOLDER.'/',self::INLINE_SYMBOL,$sqlStatement);
|
|
|
563 |
|
|
|
564 |
$this->prepareSql($statement, $sqlStatement, $node);
|
|
|
565 |
}
|
|
|
566 |
|
|
|
567 |
/**
|
|
|
568 |
* Prepare the sql text (may extend to dynamic sql).
|
|
|
569 |
* @param TSqlMapStatement mapped statement.
|
|
|
570 |
* @param string sql text.
|
|
|
571 |
* @param SimpleXmlElement statement node.
|
|
|
572 |
* @todo Extend to dynamic sql.
|
|
|
573 |
*/
|
|
|
574 |
protected function prepareSql($statement,$sqlStatement, $node)
|
|
|
575 |
{
|
|
|
576 |
$simpleDynamic = new TSimpleDynamicParser;
|
|
|
577 |
$sqlStatement=preg_replace(self::ESCAPED_SIMPLE_MARK_REGEXP,self::SIMPLE_PLACEHOLDER,$sqlStatement);
|
|
|
578 |
$dynamics = $simpleDynamic->parse($sqlStatement);
|
|
|
579 |
if(count($dynamics['parameters']) > 0)
|
|
|
580 |
{
|
|
|
581 |
$sql = new TSimpleDynamicSql($dynamics['parameters']);
|
|
|
582 |
$sqlStatement = $dynamics['sql'];
|
|
|
583 |
}
|
|
|
584 |
else
|
|
|
585 |
$sql = new TStaticSql();
|
|
|
586 |
$sqlStatement=preg_replace('/'.self::SIMPLE_PLACEHOLDER.'/',self::SIMPLE_MARK,$sqlStatement);
|
|
|
587 |
$sql->buildPreparedStatement($statement, $sqlStatement);
|
|
|
588 |
$statement->setSqlText($sql);
|
|
|
589 |
}
|
|
|
590 |
|
|
|
591 |
/**
|
|
|
592 |
* Load select statement from xml mapping.
|
|
|
593 |
* @param SimpleXmlElement select node.
|
|
|
594 |
*/
|
|
|
595 |
protected function loadSelectTag($node)
|
|
|
596 |
{
|
|
|
597 |
$select = new TSqlMapSelect;
|
|
|
598 |
$this->setObjectPropFromNode($select,$node);
|
|
|
599 |
$this->processSqlStatement($select,$node);
|
|
|
600 |
$mappedStatement = new TMappedStatement($this->_manager, $select);
|
|
|
601 |
if(strlen($select->getCacheModel()) > 0)
|
|
|
602 |
$mappedStatement = new TCachingStatement($mappedStatement);
|
|
|
603 |
|
|
|
604 |
$this->_manager->addMappedStatement($mappedStatement);
|
|
|
605 |
}
|
|
|
606 |
|
|
|
607 |
/**
|
|
|
608 |
* Load insert statement from xml mapping.
|
|
|
609 |
* @param SimpleXmlElement insert node.
|
|
|
610 |
*/
|
|
|
611 |
protected function loadInsertTag($node)
|
|
|
612 |
{
|
|
|
613 |
$insert = $this->createInsertStatement($node);
|
|
|
614 |
$this->processSqlStatement($insert, $node);
|
|
|
615 |
$mappedStatement = new TInsertMappedStatement($this->_manager, $insert);
|
|
|
616 |
$this->_manager->addMappedStatement($mappedStatement);
|
|
|
617 |
}
|
|
|
618 |
|
|
|
619 |
/**
|
|
|
620 |
* Create new insert statement from xml node.
|
|
|
621 |
* @param SimpleXmlElement insert node.
|
|
|
622 |
* @return TSqlMapInsert insert statement.
|
|
|
623 |
*/
|
|
|
624 |
protected function createInsertStatement($node)
|
|
|
625 |
{
|
|
|
626 |
$insert = new TSqlMapInsert;
|
|
|
627 |
$this->setObjectPropFromNode($insert,$node);
|
|
|
628 |
if(isset($node->selectKey))
|
|
|
629 |
$this->loadSelectKeyTag($insert,$node->selectKey);
|
|
|
630 |
return $insert;
|
|
|
631 |
}
|
|
|
632 |
|
|
|
633 |
/**
|
|
|
634 |
* Load the selectKey statement from xml mapping.
|
|
|
635 |
* @param SimpleXmlElement selectkey node
|
|
|
636 |
*/
|
|
|
637 |
protected function loadSelectKeyTag($insert, $node)
|
|
|
638 |
{
|
|
|
639 |
$selectKey = new TSqlMapSelectKey;
|
|
|
640 |
$this->setObjectPropFromNode($selectKey,$node);
|
|
|
641 |
$selectKey->setID($insert->getID());
|
|
|
642 |
$selectKey->setID($insert->getID().'.SelectKey');
|
|
|
643 |
$this->processSqlStatement($selectKey,$node);
|
|
|
644 |
$insert->setSelectKey($selectKey);
|
|
|
645 |
$mappedStatement = new TMappedStatement($this->_manager, $selectKey);
|
|
|
646 |
$this->_manager->addMappedStatement($mappedStatement);
|
|
|
647 |
}
|
|
|
648 |
|
|
|
649 |
/**
|
|
|
650 |
* Load update statement from xml mapping.
|
|
|
651 |
* @param SimpleXmlElement update node.
|
|
|
652 |
*/
|
|
|
653 |
protected function loadUpdateTag($node)
|
|
|
654 |
{
|
|
|
655 |
$update = new TSqlMapUpdate;
|
|
|
656 |
$this->setObjectPropFromNode($update,$node);
|
|
|
657 |
$this->processSqlStatement($update, $node);
|
|
|
658 |
$mappedStatement = new TUpdateMappedStatement($this->_manager, $update);
|
|
|
659 |
$this->_manager->addMappedStatement($mappedStatement);
|
|
|
660 |
}
|
|
|
661 |
|
|
|
662 |
/**
|
|
|
663 |
* Load delete statement from xml mapping.
|
|
|
664 |
* @param SimpleXmlElement delete node.
|
|
|
665 |
*/
|
|
|
666 |
protected function loadDeleteTag($node)
|
|
|
667 |
{
|
|
|
668 |
$delete = new TSqlMapDelete;
|
|
|
669 |
$this->setObjectPropFromNode($delete,$node);
|
|
|
670 |
$this->processSqlStatement($delete, $node);
|
|
|
671 |
$mappedStatement = new TDeleteMappedStatement($this->_manager, $delete);
|
|
|
672 |
$this->_manager->addMappedStatement($mappedStatement);
|
|
|
673 |
}
|
|
|
674 |
|
|
|
675 |
/**
|
|
|
676 |
* Load procedure statement from xml mapping.
|
|
|
677 |
* @todo Implement loading procedure
|
|
|
678 |
* @param SimpleXmlElement procedure node
|
|
|
679 |
*/
|
|
|
680 |
protected function loadProcedureTag($node)
|
|
|
681 |
{
|
|
|
682 |
//var_dump('todo: add load procedure');
|
|
|
683 |
}
|
|
|
684 |
|
|
|
685 |
/**
|
|
|
686 |
* Load cache models from xml mapping.
|
|
|
687 |
* @param SimpleXmlElement cache node.
|
|
|
688 |
*/
|
|
|
689 |
protected function loadCacheModel($node)
|
|
|
690 |
{
|
|
|
691 |
$cacheModel = new TSqlMapCacheModel;
|
|
|
692 |
$properties = array('id','implementation');
|
|
|
693 |
foreach($node->attributes() as $name=>$value)
|
|
|
694 |
{
|
|
|
695 |
if(in_array(strtolower($name), $properties))
|
|
|
696 |
$cacheModel->{'set'.$name}((string)$value);
|
|
|
697 |
}
|
|
|
698 |
$cache = Prado::createComponent($cacheModel->getImplementationClass());
|
|
|
699 |
$this->setObjectPropFromNode($cache,$node,$properties);
|
|
|
700 |
$cacheModel->initialize($cache);
|
|
|
701 |
$this->_manager->addCacheModel($cacheModel);
|
|
|
702 |
foreach($node->xpath('flushOnExecute') as $flush)
|
|
|
703 |
$this->loadFlushOnCache($cacheModel,$node,$flush);
|
|
|
704 |
}
|
|
|
705 |
|
|
|
706 |
/**
|
|
|
707 |
* Load the flush on cache properties.
|
|
|
708 |
* @param TSqlMapCacheModel cache model
|
|
|
709 |
* @param SimpleXmlElement parent node.
|
|
|
710 |
* @param SimpleXmlElement flush node.
|
|
|
711 |
*/
|
|
|
712 |
protected function loadFlushOnCache($cacheModel,$parent,$node)
|
|
|
713 |
{
|
|
|
714 |
$id = $cacheModel->getID();
|
|
|
715 |
if(!isset($this->_FlushOnExecuteStatements[$id]))
|
|
|
716 |
$this->_FlushOnExecuteStatements[$id] = array();
|
|
|
717 |
foreach($node->attributes() as $name=>$value)
|
|
|
718 |
{
|
|
|
719 |
if(strtolower($name)==='statement')
|
|
|
720 |
$this->_FlushOnExecuteStatements[$id][] = (string)$value;
|
|
|
721 |
}
|
|
|
722 |
}
|
|
|
723 |
|
|
|
724 |
/**
|
|
|
725 |
* Attach CacheModel to statement and register trigger statements for cache models
|
|
|
726 |
*/
|
|
|
727 |
protected function registerCacheTriggers()
|
|
|
728 |
{
|
|
|
729 |
foreach($this->_FlushOnExecuteStatements as $cacheID => $statementIDs)
|
|
|
730 |
{
|
|
|
731 |
$cacheModel = $this->_manager->getCacheModel($cacheID);
|
|
|
732 |
foreach($statementIDs as $statementID)
|
|
|
733 |
{
|
|
|
734 |
$statement = $this->_manager->getMappedStatement($statementID);
|
|
|
735 |
$cacheModel->registerTriggerStatement($statement);
|
|
|
736 |
}
|
|
|
737 |
}
|
|
|
738 |
}
|
|
|
739 |
}
|
|
|
740 |
|