summaryrefslogtreecommitdiff
path: root/docs/sqlmap/latex/ch4.tex
blob: a4cbc937b4968a497d39419c60d0696247084102 (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
\chapter{Parameter Maps and Inline Parameters}\label{section:3.4}
Most SQL statements are useful because we can pass them values at runtime.
Someone wants a database record with the ID 42, and we need to merge that ID
number into a select statement. A list of one or more parameters are passed at
runtime, and each placeholder is replaced in turn. This is simple but labor
intensive, since developers spend a lot of time counting symbols to make sure
everything is in sync.

\begin{mybox}{Note:}
Preceding sections briefly touched on inline parameters, which automatically
map properties to named parameters. Many iBATIS developers prefer this
approach. But others prefer to stick to the standard, anonymous approach to
SQL parameters by using parameter maps. Sometimes people need to retain the
purity of the SQL statements; other times they need the detailed specification
offered by parameter maps due to database or provider-specific information
that needs to be used.
\end{mybox}

\section{Parameter Map}
A Parameter Map defines an ordered list of values that match up with the
placeholders of a parameterized query statement. While the attributes
specified by the map still need to be in the correct order, each parameter is
named. You can populate the underlying class in any order, and the Parameter
Map ensures each value is passed in the correct order.

Parameter Maps can be provided as an external element and \emph{inline}.
Example~\ref{example:3.16} shows an external Parameter Map.

\begin{example}\label{example:3.16}
An external Parameter Map
\begin{verbatim}
<parameterMap id="parameterMapIdentifier"
  [extends="[sqlMapNamespace.]parameterMapId"]>
  <parameter
    property ="propertyName"
    [column="columnName"]
    [dbType="databaseType"]
    [type="propertyCLRType"]
    [nullValue="nullValueReplacement"]
    [size="columnSize"]
    [precision="columnPrecision"]
    [scale="columnScale"]
    [typeHandler="class.name"]
  <parameter ... ... />
  <parameter ... ... />
</parameterMap>
\end{verbatim}
\end{example}

In Example~\ref{example:3.16}, the parts in [brackets] are optional. The
\tt{parameterMap} element only requires the id attribute.
Example~\ref{example:3.17} shows a typical \tt{<parameterMap>}.

\begin{example}\label{example:3.17}
A typical \tt{<parameterMap>} element
\begin{verbatim}
<parameterMap id="insert-product-param" class="Product">
  <parameter property="description" />
  <parameter property="id"/>
</parameterMap>

<statement id="insertProduct" parameterMap="insert-product-param">
  insert into PRODUCT (PRD_DESCRIPTION, PRD_ID) values (?,?);
</statement>
\end{verbatim}
\end{example}

\begin{mybox}{Note:}
Parameter Map names are always local to the Data Map definition file where
they are defined. You can refer to a Parameter Map in another Data Map
definition file by prefixing the \tt{id} of the Parameter Map with the
namespace of the Data Map (set in the \tt{<sqlMap>} root element). If the
Parameter Map in Example~\ref{example:3.17} were in a Data Map named
``Product'', it could be referenced from another file using
``Product.insert-product-param''.
\end{mybox}

\subsection{\tt{<parameterMap>} attributes} The \tt{<parameterMap>} element
accepts two attributes: \tt{id} (required) and \tt{extends} (optional).

\subsubsection{\tt{id} attribute} The required \tt{id} attribute provides a
unique identifier for the \tt{<parameterMap>} within this Data Map.

\subsubsection{\tt{extends} attribute}
The optional \tt{extends} attribute can be set to the name of another
\tt{parameterMap} upon which to base this \tt{parameterMap}. All properties of
the super \tt{parameterMap} will be included as part of this
\tt{parameterMap}, and values from the super \tt{parameterMap} are set before
any values specified by this \tt{parameterMap}. The effect is similar to
extending a class.

\section{\tt{<parameter>} Elements}
The \tt{<parameterMap>} element holds one or more parameter child elements
that map object properties to placeholders in a SQL statement. The sections
that follow describe each of the attributes.

\subsection{\tt{property} attribute}
The \tt{property} attribute of \tt{<parameter>} is the name of a property of
the parameter object. It may also be the name of an entry in an array. The
name can be used more than once depending on the number of times it is needed
in the statement. (In an update, you might set a column that is also part of
the where clause.)

\subsection{\tt{direction} attribute}
The \tt{direction} attribute may be used to indicate a stored procedure
parameter's direction.

\subsection{\tt{column} attribute}
The \tt{column} attribute is used to define to the name of a parameter used by
a stored procedure.

\begin{table}[!h]\centering\label{table:3.3}
\caption{Parameter \tt{direction} attribute values }
\begin{tabular}{|l|l|}
  \hline
  % after \\: \hline or \cline{col1-col2} \cline{col3-col4} ...
  \textbf{Value} & \textbf{Description}\\
  \hline
  Input & input-only \\
  \hline
  Output & output-only \\
  \hline
  InputOutput & bidirectional \\
  \hline
\end{tabular}
\end{table}

\subsection{\tt{dbType} attribute}
The \tt{dbType} attribute is used to explicitly specify the database column
type of the parameter to be set by this property. This attribute is normally
only required if the column is nullable. Although, another reason to use the
\tt{dbType} attribute is to explicitly specify date types. Most SQL databases
have more than one \tt{datetime} type. Usually, a database has at least three
different types (DATE, DATETIME, TIMESTAMP). In order for the value to map
correctly, you might need to specify the column's \tt{dbType}.

\begin{mybox}{Note:}
Most providers only need the \tt{dbType} specified for nullable columns. In
this case, you only need to specify the type for the columns that are
nullable.
\end{mybox}

\subsection{\tt{type} attribute}
The \tt{type} attribute is used to specify the type of the parameter's
property. This attribute is useful when passing \tt{InputOutput} and
\tt{Output} parameters into stored procedures. The framework uses the
specified type to properly handle and set the parameter object's properties
with the procedure's output values after execution.

%If the attribute type is not set and the framework cannot otherwise determine
%the type, the type is assumed to be an StdObject. Section~\ref{section:6}
%details the types and available aliases that have pre-built support in the
%framework.

\subsection{\tt{nullValue} attribute}\label{section:nullValueParameter}
The \tt{nullValue} attribute can be set to any valid value (based on property
type). The \tt{nullValue} attribute is used to specify an outgoing null value
replacement. What this means is that when the value is detected in the object
property, a NULL will be written to the database (the opposite behavior of an
inbound null value replacement). This allows you to use a magic null number in
your application for types that do not support null values (such as int,
double, float). When these types of properties contain a matching null value
(for example, say, $-9999$), a NULL will be written to the database instead of
the value.


\begin{mybox}{Tip:}
For round-trip transparency of null values, you must also specify database
columns null value replacements in your Result Map (see
Chapter~\ref{section:3.5}).
\end{mybox}


\subsection{\tt{size} attribute}
The \tt{size} attribute sets the maximum size of the data within the column.

\subsection{\tt{precision} attribute}
The \tt{precision} attribute is used to set the maximum number of digits used
to represent the property value.

\subsection{\tt{scale} attribute}
The \tt{scale} attribute sets the number of decimal places used to resolve the
property value.

\subsection{\tt{typeHandler} attribute}
The \tt{typeHandler} attribute allows the use of a Custom Type Handler (see
the Custom Type Handler section). This allows you to extend the DataMapper's
capabilities in handling types that are specific to your database provider,
are not handled by your database provider, or just happen to be a part of your
application design. You can create custom type handlers to deal with storing
and retrieving booleans from your database for example.

\section{Inline Parameter Maps}\label{section:3.4.3}
If you prefer to use inline parameters instead of parameter maps, you can add
extra type information inline too. The inline parameter map syntax lets you
embed the property name, the property type, the column type, and a null value
replacement into a parametrized SQL statement. The next four examples shows
statements written with inline parameters.

\begin{example}\label{example:3.18}
 A \tt{<statement>} using inline parameters
\begin{verbatim}
<statement id="insertProduct" parameterClass="Product">
  insert into PRODUCT (PRD_ID, PRD_DESCRIPTION)
  values (#id#, #description#)
</statement>
\end{verbatim}
\end{example}

The following example shows how \tt{dbTypes} can be declared inline.

\begin{example}\label{example:3.19}
 A \tt{<statement>} using an inline parameter map with a type
\begin{verbatim}
<statement id="insertProduct" parameterClass="Product">
  insert into PRODUCT (PRD_ID, PRD_DESCRIPTION)
  values (#id, dbType=int#, #description, dbType=VarChar#)
</statement>
\end{verbatim}
\end{example}

The next example shows how \tt{dbTypes} and null value replacements can also
be declared inline.

\begin{example}\label{example:3.20}
A \tt{<statement>} using an inline parameter map with a null value replacement
\begin{verbatim}
<statement id="insertProduct" parameterClass="Product">
  insert into PRODUCT (PRD_ID, PRD_DESCRIPTION)
  values (#id, dbType=int, nullValue=-999999#, #description, dbType=VarChar#)
</statement>
\end{verbatim}
\end{example}

\begin{example}\label{example:3.21}
A more complete example.
\begin{verbatim}
<update id="UpdateAccountViaInlineParameters" parameterClass="Account">
 update Accounts set
 Account_FirstName = #FirstName#,
 Account_LastName = #LastName#,
 Account_Email = #EmailAddress,type=string,dbType=Varchar,nullValue=no_email@provided.com#
 where
 Account_ID = #Id#
</update>
\end{verbatim}
\end{example}

\begin{mybox}{Note:}
Inline parameter maps are handy for small jobs, but when there are a lot of
type descriptors and null value replacements in a complex statement, an
industrial-strength, external \tt{parameterMap} can be easier.
\end{mybox}

\section{Standard Type Parameters}
In practice, you will find that many statements take a single parameter, often
an \tt{integer} or a \tt{string}. Rather than wrap a single value in another
object, you can use the standard library object (string, integer, et cetera)
as the parameter directly. Example~\ref{example:3.22} shows a statement using
a standard type parameter.

\begin{example}\label{example:3.22}
A \tt{<statement>} using standard type parameters
\begin{verbatim}
<statement id="getProduct" parameterClass="System.Int32">
  select * from PRODUCT where PRD_ID = #value#
</statement>
\end{verbatim}
\end{example}

Assuming \tt{PRD\_ID} is a numeric type, when a call is made to this Mapped
Statement, a standard integer can be passed in. The \tt{\#value\#} parameter
will be replaced with the value of the integer. The name \tt{value} is simply
a placeholder, you can use another moniker of your choice. Result Maps support
primitive types as results as well.

For your convenience, the following PHP primitive types are supported.
\begin{itemize}
  \item \tt{string}
  \item \tt{float} or \tt{double}
  \item \tt{integer} or \tt{int}
  \item \tt{bool} or \tt{boolean}
\end{itemize}

\section{Array Type Parameters}
You can also pass in a array as a parameter object. This would usually be a an
associative array. Example~\ref{example:3.23} shows a \tt{<statement>} using
an array for a \tt{parameterClass}.


\begin{example}\label{example:3.23}
A \tt{<statement>} using an array for a \tt{parameterClass}
\begin{verbatim}
<statement id="getProduct" parameterClass="array">
  select * from PRODUCT
  where PRD_CAT_ID = #catId#
  and PRD_CODE = #code#
</statement>
\end{verbatim}
\end{example}

In Example~\ref{example:3.23}, notice that the SQL in this Mapped Statement
looks like any other. There is no difference in how the inline parameters are
used. If an associative array is passed, it must contain keys named \tt{catId}
and \tt{code}. The values referenced by those keys must be of the appropriate
type for the column, just as they would be if passed from a properties object.