Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<com:TContent ID="body">
2
 
3
<h1>Statement-type Element Attributes</h1>
4
<p>The six statement-type elements take various attributes. See
5
<a href="?page=Manual.MappedStatements">Mapped Statements</a> for a
6
table itemizing which attributes each
7
element-type accepts. The individual attributes are described in the sections
8
that follow.</p>
9
 
10
<h2><tt>id</tt> attribute</h2>
11
<p>
12
The required <tt>id</tt> attribute provides a name for this statement, which must
13
be unique within this <tt>&lt;SqlMap&gt;</tt>.</p>
14
 
15
<h2><tt>parameterMap</tt> attribute</h2>
16
<p>A Parameter Map defines an ordered list of values that match up with the "?"
17
placeholders of a standard, parameterized query statement.
18
The following example shows a <tt>&lt;parameterMap&gt;</tt> and a corresponding
19
<tt>&lt;statement&gt;</tt>.
20
 
21
<com:TTextHighlighter Language="xml" CssClass="source">
22
<parameterMap id="insert-product-param" class="Product">
23
  <parameter property="id"/>
24
  <parameter property="description"/>
25
</parameterMap>
26
 
27
<statement id="insertProduct" parameterMap="insert-product-param">
28
  insert into PRODUCT (PRD_ID, PRD_DESCRIPTION) values (?,?);
29
</statement>
30
</com:TTextHighlighter>
31
 
32
In the above example, the Parameter Map describes two parameters that
33
will match, in order, two placeholders in the SQL statement. The first "?"
34
is replaced by the value of the <tt>id</tt> property. The second is replaced with
35
the <tt>description</tt> property.</p>
36
 
37
<p>SQLMap also supports named, inline parameters, which most developers seem to
38
prefer. However, Parameter Maps are useful when the SQL must be kept in a
39
standard form or when extra information needs to be provided. See
40
<a href="?page=Manual.ParameterMap">Parameter Maps</a> for futher details.</p>
41
 
42
<h2><tt>parameterClass</tt> attribute</h2>
43
<p>If a <tt>parameterMap</tt> attribute is not specified, you may specify a
44
<tt>parameterClass</tt> instead and use <a href="?page=Manual.InlineParameterMaps">inline parameters</a>.
45
The value of the <tt>parameterClass</tt> attribute
46
can be any existing PHP class name. The following example shows a
47
statement using a PHP class named <tt>Product</tt> in <tt>parameterClass</tt>
48
attribute.</p>
49
 
50
<com:TTextHighlighter Language="xml" CssClass="source">
51
<statement id="statementName" parameterClass="Product">
52
  insert into PRODUCT values (#id#, #description#, #price#)
53
</statement>
54
</com:TTextHighlighter>
55
 
56
<h2><tt>resultMap</tt> attribute</h2>
57
<p>A Result Map lets you control how data is extracted from the result of a
58
query, and how the columns are mapped to object properties.
59
The following example shows a <tt>&lt;resultMap&gt;</tt> element and a
60
corresponding <tt>&lt;statement&gt;</tt> element.</p>
61
 
62
<com:TTextHighlighter Language="xml" CssClass="source">
63
<resultMap id="select-product-result" class="product">
64
  <result property="id" column="PRD_ID"/>
65
  <result property="description" column="PRD_DESCRIPTION"/>
66
</resultMap>
67
 
68
<statement id="selectProduct" resultMap="select-product-result">
69
  select * from PRODUCT
70
</statement>
71
</com:TTextHighlighter>
72
 
73
<p>In the above example, the result of the SQL query will be mapped to
74
an instance of the <tt>Product</tt> class using the "select-product-result"
75
<tt>&lt;resultMap&gt;</tt>. The <tt>&lt;resultMap&gt;</tt> says to
76
populate the <tt>id</tt> property
77
from the <tt>PRD_ID</tt> column, and to populate the <tt>description</tt> property
78
from the <tt>PRD_DESCRIPTION</tt> column.</p>
79
 
80
<div class="tip"><b class="tip">Tip:</b>
81
In the above example, note that using "<tt> select * </tt>" is supported. If
82
you want all the columns, you don't need to map them all individually. (Though
83
many developers consider it a good practice to always specify the columns
84
expected.)
85
</div>
86
 
87
<p>See <a href="?page=Manual.ResultMaps">Result Maps</a> for futher details.</p>
88
 
89
<h2><tt>resultClass</tt> attribute</h2>
90
<p>If a <tt>resultMap</tt> is not specified, you may specify a <tt>resultClass</tt>
91
instead. The value of the <tt>resultClass</tt> attribute can be the name of a PHP
92
class or primitives like <tt>integer</tt>, <tt>string</tt>, or <tt>array</tt>. The class
93
specified will be automatically mapped to the columns in the result, based on
94
the result metadata. The following example shows a <tt>&lt;statement&gt;</tt> element
95
with a <tt>resultClass</tt> attribute.</p>
96
 
97
<com:TTextHighlighter Language="xml" CssClass="source">
98
<statement id="SelectPerson" parameterClass="int" resultClass="Person">
99
  SELECT
100
  PER_ID as Id,
101
  PER_FIRST_NAME as FirstName,
102
  PER_LAST_NAME as LastName,
103
  PER_BIRTH_DATE as BirthDate,
104
  PER_WEIGHT_KG as WeightInKilograms,
105
  PER_HEIGHT_M as HeightInMeters
106
  FROM PERSON
107
  WHERE PER_ID = #value#
108
</statement>
109
</com:TTextHighlighter>
110
 
111
<p>In the above example, the <tt>Person</tt> class has properties including:
112
<tt>Id</tt>, <tt>FirstName</tt>, <tt>LastName</tt>, <tt>BirthDate</tt>,
113
<tt>WeightInKilograms</tt>, and <tt>HeightInMeters</tt>. Each of these corresponds
114
with the column aliases described by the SQL select statement using the "as"
115
keyword, a standard SQL feature. When executed, a <tt>Person</tt> object is
116
instantiated and populated by matching the object property names to the column
117
names from the query.</p>
118
 
119
<p>Using SQL aliases to map columns to properties saves defining a
120
<tt>&lt;resultMap&gt;</tt> element, but there are limitations. There is no way to
121
specify the types of the output columns (if needed), there is no way to
122
automatically load related data such as complex properties.You can overcome
123
these limitations with an explicit <a href="?page=Manual.ResultMaps">Result Map</a>.</p>
124
 
125
<h2><tt>listClass</tt> attribute</h2>
126
<p>In addition to providing the ability to return an <tt>TList</tt> of objects, the
127
DataMapper supports the use of custom collection: a class that implements
128
<tt>ArrayAccess</tt>. The following is an example of a TList (it implements
129
ArrayAccess) class that can be used with the DataMapper.</p>
130
 
131
<com:TTextHighlighter Language="php" CssClass="source">
132
class AccountCollection extends TList
133
{
134
    public function addRange($accounts)
135
    {
136
        foreach($accounts as $account)
137
            $this->add($account);
138
    }
139
 
140
    public function copyTo(TList $array)
141
    {
142
        $array->copyFrom($this);
143
    }
144
}
145
</com:TTextHighlighter>
146
 
147
<p>An <tt>ArrayAccess</tt> class can be specified for a select statement through the
148
<tt>listClass</tt> attribute. The value of the <tt>listClass</tt> attribute is the
149
full name of a PHP class that implements <tt>ArrayAccess</tt>. The statement
150
should also indicate the <tt>resultClass</tt> so that the DataMapper knows how to
151
handle the type of objects in the collection. The <tt>resultClass</tt> specified
152
will be automatically mapped to the columns in the result, based on the result
153
metadata. The following example shows a <tt>&lt;statement&gt;</tt> element with a
154
<tt>listClass</tt> attribute.</p>
155
 
156
<com:TTextHighlighter Language="xml" CssClass="source">
157
<statement id="GetAllAccounts"
158
 listClass="AccountCollection"
159
 resultClass="Account">
160
   select
161
   Account_ID as Id,
162
   Account_FirstName as FirstName,
163
   Account_LastName as LastName,
164
   Account_Email as EmailAddress
165
   from Accounts
166
   order by Account_LastName, Account_FirstName
167
</statement>
168
</com:TTextHighlighter>
169
 
170
<h2><tt>cacheModel</tt> attribute</h2>
171
<p>If you want to cache the result of a query, you can specify a Cache Model as
172
part of the <tt>&lt;statement&gt;</tt> element. The following example shows a
173
<tt>&lt;cacheModel&gt;</tt> element and a corresponding <tt>&lt;statement&gt;</tt>.</p>
174
 
175
<com:TTextHighlighter Language="xml" CssClass="source">
176
<cacheModel id="product-cache" implementation="LRU">
177
  <flushInterval hours="24"/>
178
  <flushOnExecute statement="insertProduct"/>
179
  <flushOnExecute statement="updateProduct"/>
180
  <flushOnExecute statement="deleteProduct"/>
181
  <property name="size" value="1000" />
182
</cacheModel>
183
 
184
<statement id="selectProductList" parameterClass="int" cacheModel="product-cache">
185
  select * from PRODUCT where PRD_CAT_ID = #value#
186
</statement>
187
</com:TTextHighlighter>
188
 
189
<p>In the above example, a cache is defined for products that uses a
190
Least Recently Used [LRU] type and flushes every 24 hours or whenever
191
associated update statements are executed. See
192
<a href="?page=Manual.CacheModels">Cache Models</a> for futher details</p>
193
 
194
<h2><tt>extends</tt> attribute</h2>
195
<p>When writing Sql, you often encounter duplicate fragments of SQL. SQLMap
196
offers a simple yet powerful attribute to reuse them.</p>
197
 
198
<com:TTextHighlighter Language="xml" CssClass="source">
199
<select id="GetAllAccounts"
200
  resultMap="indexed-account-result">
201
select
202
  Account_ID,
203
  Account_FirstName,
204
  Account_LastName,
205
  Account_Email
206
from Accounts
207
</select>
208
 
209
<select id="GetAllAccountsOrderByName"
210
  extends="GetAllAccounts"
211
  resultMap="indexed-account-result">
212
    order by Account_FirstName
213
</select>
214
</com:TTextHighlighter>
215
 
216
</com:TContent>