summaryrefslogtreecommitdiff
path: root/framework/Data/SqlMap/Configuration/TDiscriminator.php
blob: cbc05612bc836c6678d19c8196c87c01afe647b3 (plain)
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
<?php
/**
 * TDiscriminator and TSubMap classes file.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2012 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @version $Id$
 * @package System.Data.SqlMap.Configuration
 */

/**
 * The TDiscriminator corresponds to the <discriminator> tag within a <resultMap>.
 *
 * TDiscriminator allows inheritance logic in SqlMap result mappings.
 * SqlMap compares the data found in the discriminator column to the different
 * <submap> values using the column value's string equivalence. When the string values
 * matches a particular <submap>, SqlMap will use the <resultMap> defined by
 * {@link resultMapping TSubMap::setResultMapping()} property for loading
 * the object data.
 *
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
 * @version $Id$
 * @package System.Data.SqlMap.Configuration
 * @since 3.1
 */
class TDiscriminator extends TComponent
{
	private $_column;
	private $_type;
	private $_typeHandler=null;
	private $_columnIndex;
	private $_nullValue;
	private $_mapping;
	private $_resultMaps=array();
	private $_subMaps=array();

	/**
	 * @return string the name of the column in the result set from which the
	 * value will be used to populate the property.
	 */
	public function getColumn()
	{
		return $this->_column;
	}

	/**
	 * @param string the name of the column in the result set from which the
	 * value will be used to populate the property.
	 */
	public function setColumn($value)
	{
		$this->_column = $value;
	}

	/**
	 * @param string property type of the parameter to be set.
	 */
	public function getType()
	{
		return $this->_type;
	}

	/**
	 * The type attribute is used to explicitly specify the property type of the
	 * parameter to be set. If the attribute type is not set and the framework
	 * cannot otherwise determine the type, the type is assumed from the default
	 * value of the property.
	 * @return string property type of the parameter to be set.
	 */
	public function setType($value)
	{
		$this->_type = $value;
	}

	/**
	 * @return string custom type handler class name (may use namespace).
	 */
	public function getTypeHandler()
	{
		return $this->_typeHandler;
	}

	/**
	 * @param string custom type handler class name (may use namespace).
	 */
	public function setTypeHandler($value)
	{
		$this->_typeHandler = $value;
	}

	/**
	 * @return int index of the column in the ResultSet
	 */
	public function getColumnIndex()
	{
		return $this->_columnIndex;
	}

	/**
	 * The columnIndex attribute value is the index of the column in the
	 * ResultSet from which the value will be used to populate the object property.
	 * @param int index of the column in the ResultSet
	 */
	public function setColumnIndex($value)
	{
		$this->_columnIndex = TPropertyValue::ensureInteger($value);
	}

	/**
	 * @return mixed outgoing null value replacement.
	 */
	public function getNullValue()
	{
		return $this->_nullValue;
	}

	/**
	 * @param mixed outgoing null value replacement.
	 */
	public function setNullValue($value)
	{
		$this->_nullValue = $value;
	}

	/**
	 * @return TResultProperty result property for the discriminator column.
	 */
	public function getMapping()
	{
		return $this->_mapping;
	}

	/**
	 * @param TSubMap add new sub mapping.
	 */
	public function addSubMap($subMap)
	{
		$this->_subMaps[] = $subMap;
	}

	/**
	 * @param string database value
	 * @return TResultMap result mapping.
	 */
	public function getSubMap($value)
	{
		if(isset($this->_resultMaps[$value]))
			return $this->_resultMaps[$value];
	}

	/**
	 * Copies the discriminator properties to a new TResultProperty.
	 * @param TResultMap result map holding the discriminator.
	 */
	public function initMapping($resultMap)
	{
		$this->_mapping = new TResultProperty($resultMap);
		$this->_mapping->setColumn($this->getColumn());
		$this->_mapping->setColumnIndex($this->getColumnIndex());
		$this->_mapping->setType($this->getType());
		$this->_mapping->setTypeHandler($this->getTypeHandler());
		$this->_mapping->setNullValue($this->getNullValue());
	}

	/**
	 * Set the result maps for particular sub-mapping values.
	 * @param TSqlMapManager sql map manager instance.
	 */
	public function initialize($manager)
	{
		foreach($this->_subMaps as $subMap)
		{
			$this->_resultMaps[$subMap->getValue()] =
				$manager->getResultMap($subMap->getResultMapping());
		}
	}
}

/**
 * TSubMap class defines a submapping value and the corresponding <resultMap>
 *
 * The {@link Value setValue()} property is used for comparison with the
 * discriminator column value. When the {@link Value setValue()} matches
 * that of the discriminator column value, the corresponding {@link ResultMapping setResultMapping}
 * is used inplace of the current result map.
 *
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
 * @version $Id$
 * @package System.Data.SqlMap.Configuration
 * @since 3.1
 */
class TSubMap extends TComponent
{
	private $_value;
	private $_resultMapping;

	/**
	 * @return string value for comparison with discriminator column value.
	 */
	public function getValue()
	{
		return $this->_value;
	}

	/**
	 * @param string value for comparison with discriminator column value.
	 */
	public function setValue($value)
	{
		$this->_value = $value;
	}

	/**
	 * The result map to use when the Value matches the discriminator column value.
	 * @return string ID of a result map
	 */
	public function getResultMapping()
	{
		return $this->_resultMapping;
	}

	/**
	 * @param string ID of a result map
	 */
	public function setResultMapping($value)
	{
		$this->_resultMapping = $value;
	}
}