| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TParameterMap 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: TParameterMap.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
10 |
* @package System.Data.SqlMap.Configuration
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* TParameterMap corresponds to the <parameterMap> element.
|
|
|
15 |
*
|
|
|
16 |
* TParameterMap holds one or more parameter child elements that map object
|
|
|
17 |
* properties to placeholders in a SQL statement.
|
|
|
18 |
*
|
|
|
19 |
* A TParameterMap defines an ordered list of values that match up with the
|
|
|
20 |
* placeholders of a parameterized query statement. While the attributes
|
|
|
21 |
* specified by the map still need to be in the correct order, each parameter
|
|
|
22 |
* is named. You can populate the underlying class in any order, and the
|
|
|
23 |
* TParameterMap ensures each value is passed in the correct order.
|
|
|
24 |
*
|
|
|
25 |
* Parameter Maps can be provided as an external element and inline.
|
|
|
26 |
* The <parameterMap> element accepts two attributes: id (required) and extends (optional).
|
|
|
27 |
*
|
|
|
28 |
* @author Wei Zhuo <weizho[at]gmail[dot]com>
|
|
|
29 |
* @version $Id: TParameterMap.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
30 |
* @package System.Data.SqlMap.Configuration
|
|
|
31 |
* @since 3.1
|
|
|
32 |
*/
|
|
|
33 |
class TParameterMap extends TComponent
|
|
|
34 |
{
|
|
|
35 |
private $_extend;
|
|
|
36 |
private $_properties;
|
|
|
37 |
private $_propertyMap;
|
|
|
38 |
private $_extendMap;
|
|
|
39 |
private $_ID;
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Initialize the properties and property map collections.
|
|
|
43 |
*/
|
|
|
44 |
public function __construct()
|
|
|
45 |
{
|
|
|
46 |
$this->_properties = new TList;
|
|
|
47 |
$this->_propertyMap = new TMap;
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* @return string a unique identifier for the <parameterMap>.
|
|
|
52 |
*/
|
|
|
53 |
public function getID()
|
|
|
54 |
{
|
|
|
55 |
return $this->_ID;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* @param string a unique identifier for the <parameterMap>.
|
|
|
60 |
*/
|
|
|
61 |
public function setID($value)
|
|
|
62 |
{
|
|
|
63 |
$this->_ID=$value;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* @return TParameterProperty[] list of properties for the parameter map.
|
|
|
68 |
*/
|
|
|
69 |
public function getProperties()
|
|
|
70 |
{
|
|
|
71 |
return $this->_properties;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* @return string name of another <parameterMap> upon which to base this TParameterMap.
|
|
|
76 |
*/
|
|
|
77 |
public function getExtends()
|
|
|
78 |
{
|
|
|
79 |
return $this->_extend;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* @param string name of another <parameterMap> upon which to base this TParameterMap.
|
|
|
84 |
*/
|
|
|
85 |
public function setExtends($value)
|
|
|
86 |
{
|
|
|
87 |
$this->_extend = $value;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* @param string name of a parameter property.
|
|
|
92 |
* @return TParameterProperty parameter property.
|
|
|
93 |
* @throws TSqlMapException if index is not string nor integer.
|
|
|
94 |
*/
|
|
|
95 |
public function getProperty($index)
|
|
|
96 |
{
|
|
|
97 |
if(is_string($index))
|
|
|
98 |
return $this->_propertyMap->itemAt($index);
|
|
|
99 |
else if(is_int($index))
|
|
|
100 |
return $this->_properties->itemAt($index);
|
|
|
101 |
else
|
|
|
102 |
throw new TSqlMapException('sqlmap_index_must_be_string_or_int', $index);
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* @param TParameterProperty new parameter property
|
|
|
107 |
*/
|
|
|
108 |
public function addProperty(TParameterProperty $property)
|
|
|
109 |
{
|
|
|
110 |
$this->_propertyMap->add($property->getProperty(), $property);
|
|
|
111 |
$this->_properties->add($property);
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* @param int parameter property index
|
|
|
116 |
* @param TParameterProperty new parameter property.
|
|
|
117 |
*/
|
|
|
118 |
public function insertProperty($index, TParameterProperty $property)
|
|
|
119 |
{
|
|
|
120 |
$this->_propertyMap->add($property->getProperty(), $property);
|
|
|
121 |
$this->_properties->insertAt($index, $property);
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* @return array list of property names.
|
|
|
126 |
*/
|
|
|
127 |
public function getPropertyNames()
|
|
|
128 |
{
|
|
|
129 |
return $this->_propertyMap->getKeys();
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Get the value of a property from the the parameter object.
|
|
|
134 |
* @param TSqlMapTypeHandlerRegistry type handler registry.
|
|
|
135 |
* @param TParameterProperty parameter proproperty.
|
|
|
136 |
* @param mixed parameter object to get the value from.
|
|
|
137 |
* @return unknown
|
|
|
138 |
*/
|
|
|
139 |
public function getPropertyValue($registry, $property, $parameterValue)
|
|
|
140 |
{
|
|
|
141 |
$value = $this->getObjectValue($parameterValue,$property);
|
|
|
142 |
|
|
|
143 |
if(!is_null($handler=$this->createTypeHandler($property, $registry)))
|
|
|
144 |
$value = $handler->getParameter($value);
|
|
|
145 |
|
|
|
146 |
$value = $this->nullifyDefaultValue($property,$value);
|
|
|
147 |
|
|
|
148 |
if(!is_null($type = $property->getType()))
|
|
|
149 |
$value = $registry->convertToType($type, $value);
|
|
|
150 |
|
|
|
151 |
return $value;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
|
|
|
155 |
/**
|
|
|
156 |
* Create type handler from {@link Type setType()} or {@link TypeHandler setTypeHandler}.
|
|
|
157 |
* @param TParameterProperty parameter property
|
|
|
158 |
* @param TSqlMapTypeHandlerRegistry type handler registry
|
|
|
159 |
* @return TSqlMapTypeHandler type handler.
|
|
|
160 |
*/
|
|
|
161 |
protected function createTypeHandler($property, $registry)
|
|
|
162 |
{
|
|
|
163 |
$type=$property->getTypeHandler() ? $property->getTypeHandler() : $property->getType();
|
|
|
164 |
$handler=$registry->getTypeHandler($type);
|
|
|
165 |
if($handler===null && $property->getTypeHandler())
|
|
|
166 |
$handler = Prado::createComponent($type);
|
|
|
167 |
return $handler;
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
|
|
|
171 |
/**
|
|
|
172 |
* @param mixed object to obtain the property from.
|
|
|
173 |
* @param TParameterProperty parameter property.
|
|
|
174 |
* @return mixed property value.
|
|
|
175 |
* @throws TSqlMapException if property access is invalid.
|
|
|
176 |
*/
|
|
|
177 |
protected function getObjectValue($object,$property)
|
|
|
178 |
{
|
|
|
179 |
try
|
|
|
180 |
{
|
|
|
181 |
return TPropertyAccess::get($object, $property->getProperty());
|
|
|
182 |
}
|
|
|
183 |
catch (TInvalidPropertyException $e)
|
|
|
184 |
{
|
|
|
185 |
throw new TSqlMapException(
|
|
|
186 |
'sqlmap_unable_to_get_property_for_parameter',
|
|
|
187 |
$this->getID(), $property->getProperty(), get_class($object));
|
|
|
188 |
}
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
/**
|
|
|
192 |
* When the actual value matches the {@link NullValue TParameterProperty::setNullValue()},
|
|
|
193 |
* set the current value to null.
|
|
|
194 |
* @param TParameterProperty parameter property.
|
|
|
195 |
* @param mixed current property value
|
|
|
196 |
* @return mixed null if NullValue matches currrent value.
|
|
|
197 |
*/
|
|
|
198 |
protected function nullifyDefaultValue($property,$value)
|
|
|
199 |
{
|
|
|
200 |
if(!is_null($nullValue = $property->getNullValue()))
|
|
|
201 |
{
|
|
|
202 |
if($nullValue === $value)
|
|
|
203 |
$value = null;
|
|
|
204 |
}
|
|
|
205 |
return $value;
|
|
|
206 |
}
|
|
|
207 |
}
|