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
|
<com:TContent ID="body">
<h1>Statement-type Element Attributes</h1>
<p>The six statement-type elements take various attributes. See
<a href="?page=Manual.MappedStatements">Mapped Statements</a> for a
table itemizing which attributes each
element-type accepts. The individual attributes are described in the sections
that follow.</p>
<h2><tt>id</tt> attribute</h2>
<p>
The required <tt>id</tt> attribute provides a name for this statement, which must
be unique within this <tt><SqlMap></tt>.</p>
<h2><tt>parameterMap</tt> attribute</h2>
<p>A Parameter Map defines an ordered list of values that match up with the "?"
placeholders of a standard, parameterized query statement.
The following example shows a <tt><parameterMap></tt> and a corresponding
<tt><statement></tt>.
<com:TTextHighlighter Language="xml" CssClass="source">
<parameterMap id="insert-product-param" class="Product">
<parameter property="id"/>
<parameter property="description"/>
</parameterMap>
<statement id="insertProduct" parameterMap="insert-product-param">
insert into PRODUCT (PRD_ID, PRD_DESCRIPTION) values (?,?);
</statement>
</com:TTextHighlighter>
In the above example, the Parameter Map describes two parameters that
will match, in order, two placeholders in the SQL statement. The first "?"
is replaced by the value of the <tt>id</tt> property. The second is replaced with
the <tt>description</tt> property.</p>
<p>SQLMap also supports named, inline parameters, which most developers seem to
prefer. However, Parameter Maps are useful when the SQL must be kept in a
standard form or when extra information needs to be provided. See
<a href="?page=Manual.ParameterMap">Parameter Maps</a> for futher details.</p>
<h2><tt>parameterClass</tt> attribute</h2>
<p>If a <tt>parameterMap</tt> attribute is not specified, you may specify a
<tt>parameterClass</tt> instead and use <a href="?page=Manual.InlineParameterMaps">inline parameters</a>.
The value of the <tt>parameterClass</tt> attribute
can be any existing PHP class name. The following example shows a
statement using a PHP class named <tt>Product</tt> in <tt>parameterClass</tt>
attribute.</p>
<com:TTextHighlighter Language="xml" CssClass="source">
<statement id="statementName" parameterClass="Product">
insert into PRODUCT values (#id#, #description#, #price#)
</statement>
</com:TTextHighlighter>
<h2><tt>resultMap</tt> attribute</h2>
<p>A Result Map lets you control how data is extracted from the result of a
query, and how the columns are mapped to object properties.
The following example shows a <tt><resultMap></tt> element and a
corresponding <tt><statement></tt> element.</p>
<com:TTextHighlighter Language="xml" CssClass="source">
<resultMap id="select-product-result" class="product">
<result property="id" column="PRD_ID"/>
<result property="description" column="PRD_DESCRIPTION"/>
</resultMap>
<statement id="selectProduct" resultMap="select-product-result">
select * from PRODUCT
</statement>
</com:TTextHighlighter>
<p>In the above example, the result of the SQL query will be mapped to
an instance of the <tt>Product</tt> class using the "select-product-result"
<tt><resultMap></tt>. The <tt><resultMap></tt> says to
populate the <tt>id</tt> property
from the <tt>PRD_ID</tt> column, and to populate the <tt>description</tt> property
from the <tt>PRD_DESCRIPTION</tt> column.</p>
<div class="tip"><b class="tip">Tip:</b>
In the above example, note that using "<tt> select * </tt>" is supported. If
you want all the columns, you don't need to map them all individually. (Though
many developers consider it a good practice to always specify the columns
expected.)
</div>
<p>See <a href="?page=Manual.ResultMaps">Result Maps</a> for futher details.</p>
<h2><tt>resultClass</tt> attribute</h2>
<p>If a <tt>resultMap</tt> is not specified, you may specify a <tt>resultClass</tt>
instead. The value of the <tt>resultClass</tt> attribute can be the name of a PHP
class or primitives like <tt>integer</tt>, <tt>string</tt>, or <tt>array</tt>. The class
specified will be automatically mapped to the columns in the result, based on
the result metadata. The following example shows a <tt><statement></tt> element
with a <tt>resultClass</tt> attribute.</p>
<com:TTextHighlighter Language="xml" CssClass="source">
<statement id="SelectPerson" parameterClass="int" resultClass="Person">
SELECT
PER_ID as Id,
PER_FIRST_NAME as FirstName,
PER_LAST_NAME as LastName,
PER_BIRTH_DATE as BirthDate,
PER_WEIGHT_KG as WeightInKilograms,
PER_HEIGHT_M as HeightInMeters
FROM PERSON
WHERE PER_ID = #value#
</statement>
</com:TTextHighlighter>
<p>In the above example, the <tt>Person</tt> class has properties including:
<tt>Id</tt>, <tt>FirstName</tt>, <tt>LastName</tt>, <tt>BirthDate</tt>,
<tt>WeightInKilograms</tt>, and <tt>HeightInMeters</tt>. Each of these corresponds
with the column aliases described by the SQL select statement using the "as"
keyword, a standard SQL feature. When executed, a <tt>Person</tt> object is
instantiated and populated by matching the object property names to the column
names from the query.</p>
<p>Using SQL aliases to map columns to properties saves defining a
<tt><resultMap></tt> element, but there are limitations. There is no way to
specify the types of the output columns (if needed), there is no way to
automatically load related data such as complex properties.You can overcome
these limitations with an explicit <a href="?page=Manual.ResultMaps">Result Map</a>.</p>
<h2><tt>listClass</tt> attribute</h2>
<p>In addition to providing the ability to return an <tt>TList</tt> of objects, the
DataMapper supports the use of custom collection: a class that implements
<tt>ArrayAccess</tt>. The following is an example of a TList (it implements
ArrayAccess) class that can be used with the DataMapper.</p>
<com:TTextHighlighter Language="php" CssClass="source">
class AccountCollection extends TList
{
public function addRange($accounts)
{
foreach($accounts as $account)
$this->add($account);
}
public function copyTo(TList $array)
{
$array->copyFrom($this);
}
}
</com:TTextHighlighter>
<p>An <tt>ArrayAccess</tt> class can be specified for a select statement through the
<tt>listClass</tt> attribute. The value of the <tt>listClass</tt> attribute is the
full name of a PHP class that implements <tt>ArrayAccess</tt>. The statement
should also indicate the <tt>resultClass</tt> so that the DataMapper knows how to
handle the type of objects in the collection. The <tt>resultClass</tt> specified
will be automatically mapped to the columns in the result, based on the result
metadata. The following example shows a <tt><statement></tt> element with a
<tt>listClass</tt> attribute.</p>
<com:TTextHighlighter Language="xml" CssClass="source">
<statement id="GetAllAccounts"
listClass="AccountCollection"
resultClass="Account">
select
Account_ID as Id,
Account_FirstName as FirstName,
Account_LastName as LastName,
Account_Email as EmailAddress
from Accounts
order by Account_LastName, Account_FirstName
</statement>
</com:TTextHighlighter>
<h2><tt>cacheModel</tt> attribute</h2>
<p>If you want to cache the result of a query, you can specify a Cache Model as
part of the <tt><statement></tt> element. The following example shows a
<tt><cacheModel></tt> element and a corresponding <tt><statement></tt>.</p>
<com:TTextHighlighter Language="xml" CssClass="source">
<cacheModel id="product-cache" implementation="LRU">
<flushInterval hours="24"/>
<flushOnExecute statement="insertProduct"/>
<flushOnExecute statement="updateProduct"/>
<flushOnExecute statement="deleteProduct"/>
<property name="size" value="1000" />
</cacheModel>
<statement id="selectProductList" parameterClass="int" cacheModel="product-cache">
select * from PRODUCT where PRD_CAT_ID = #value#
</statement>
</com:TTextHighlighter>
<p>In the above example, a cache is defined for products that uses a
Least Recently Used [LRU] type and flushes every 24 hours or whenever
associated update statements are executed. See
<a href="?page=Manual.CacheModels">Cache Models</a> for futher details</p>
<h2><tt>extends</tt> attribute</h2>
<p>When writing Sql, you often encounter duplicate fragments of SQL. SQLMap
offers a simple yet powerful attribute to reuse them.</p>
<com:TTextHighlighter Language="xml" CssClass="source">
<select id="GetAllAccounts"
resultMap="indexed-account-result">
select
Account_ID,
Account_FirstName,
Account_LastName,
Account_Email
from Accounts
</select>
<select id="GetAllAccountsOrderByName"
extends="GetAllAccounts"
resultMap="indexed-account-result">
order by Account_FirstName
</select>
</com:TTextHighlighter>
</com:TContent>
|