\chapter{Working with Data Maps}\label{section:3}
\section{Introduction}
If you want to know how to configure and install SQLMap, see the Developer
Guide section~\ref{section:4} . But if you want to know how SQLMap really
works, continue from here.
The Data Map definition file is where the interesting stuff happens. Here, you
define how your application interacts with your database. As mentioned, the
Data Map definition is an XML descriptor file. By using a service routine
provided by SQLMap, the XML descriptors are rendered into a client object (or
Mapper). To access your Data Maps, your application calls the client object
and passes in the name of the statement you need.
The real work of using SQLMap is not so much in the application code, but in
the XML descriptors that SQLMap renders. Instead of monkeying with application
source code, you monkey with XML descriptors instead. The benefit is that the
XML descriptors are much better suited to the task of mapping your object
properties to database entities. At least, that's our own experience with our
own applications. Of course, your mileage may vary.
\section{What's in a Data Map definition file, anyway?}
If you read the Tutorial, you've already seen some simple Data Map examples,
like the one shown in Example~\ref{example:2.1}.
\begin{example}\label{example:3.1}
A simple Data Map (PHP)
\begin{verbatim}
INSERT INTO [LinesItem]
(Order_Id, LineItem_LineNum, Item_Id, LineItem_Quantity, LineItem_UnitPrice)
VALUES
(#Order.Id#, #LineNumber#, #Item.Id#, #Quantity#, #Item.ListPrice#)
\end{verbatim}
\end{example}
This map takes some properties from a \tt{LineItem} instance and merges the
values into the SQL statement. The value-add is that our SQL in separated from
our program code, and we can pass our \tt{LineItem} instance directly to a
library method:
\begin{verbatim}
TMapper::instance()->insert("InsertLineItem",$lineItem);
\end{verbatim}
No fuss, no muss. Likewise, see Example\ref{example:3.2} for a simple select
statement.
\begin{mybox}{Info:}
\textbf{A Quick Glance at Inline Parameters}
Say we have a mapped statement element that looks like this:
\begin{verbatim}
insert into Products (Product_Id, Product_Description)
values (#Id#, #Description#);
\end{verbatim}
The inline parameters here are \tt{\#Id\#} and \tt{\#Description\#}. Let's
also say that we have an object with the properties \tt{Id} and
\tt{Description}. If we set the object properties to $5$ and ``dog'',
respectively, and passed the object to the mapped statement, we'd end up with
a runtime query that looked like this:
\begin{verbatim}
insert into Products (Product_Id, Product_Description) values (5, 'dog');
\end{verbatim}
For more about inline parameters, see Chapter~\ref{section:3.4}.
\end{mybox}
But, what if you wanted some ice cream with that pie? And maybe a cherry on
top? What if we wanted to cache the result of the select? Or, what if we
didn't want to use SQL aliasing or named parameters. (Say, because we were
using pre-existing SQL that we didn't want to touch.)
Example~\ref{example:3.2} shows a Data Map that specifies a cache, and uses a
\tt{} and a \tt{} to keep our SQL pristine.
\begin{example}\label{example:3.2}
A Data Map definition file with some bells and whistles
\begin{verbatim}
\end{verbatim}
\end{example}
In Example~\ref{example:3.2}, \tt{} maps the SQL ``?'' to the
product \tt{Id} property. The \tt{} maps the columns to our object
properties. The \tt{} keeps the result of the last one thousand of
these queries in active memory for up to 24 hours.
Example~\ref{example:3.2} is longer and more complex than
Example~\ref{example:3.1}, but considering what you get in return, it seems
like a fair trade. (A bargain even.)
Many agile developers would start with something like
Example~\ref{example:3.1} and add features like caching later. If you changed
the Data Map from Example~\ref{example:3.1} to Example~\ref{example:3.2}, you
would not have to touch your application source code at all. You can start
simple and add complexity only when it is needed.
A single Data Map definition file can contain as many Cache Models, Type
Aliases, Result Maps, Parameter Maps, and Mapped Statements (including stored
procedures), as you like. Everything is loaded into the same configuration, so
you can define elements in one Data Map and then use them in another. Use
discretion and organize the statements and maps appropriately for your
application by finding some logical way to group them.
\section{Mapped Statements}
Mapped Statements can hold any SQL statement and can use Parameter Maps and
Result Maps for input and output. (A stored procedure is a specialized form of
a statement. See section~\ref{section:3.3.1} and \ref{section:3.3.2} for more
information.)
If the case is simple, the Mapped Statement can reference the parameter and
result classes directly. Mapped Statements support caching through reference
to a Cache Model element. The following example shows the syntax for a
statement element.
\begin{example}\label{example:3.3}
Statement element syntax
\begin{verbatim}
select * from Products where Product_Id = [?|#propertyName#]
order by [$simpleDynamic$]
\end{verbatim}
\end{example}
In Example~\ref{example:3.3}, the [bracketed] parts are optional, and some
options are mutually exclusive. It is perfectly legal to have a Mapped
Statement as simple as shown by Example~\ref{example:3.4}.
\begin{example}\label{example:3.4}
A simplistic Mapped Statement
\begin{verbatim}
insert into Products (Product_Id, Product_Description) values (1, "Shih Tzu")
\end{verbatim}
\end{example}
Example~\ref{example:3.4} is obviously unlikely, unless you are running a
test. But it does shows that you can use SQLMap to execute arbitrary SQL
statements. More likely, you will use the object mapping features with
Parameter Maps (Chapter~\ref{section:3.4}) and Result Maps
(Chapter~\ref{section:3.5}) since that's where the magic happens.
\subsection{Statement Types}\label{section:3.3.1}
The \tt{} element is a general ``catch all'' element that can be
used for any type of SQL statement. Generally it is a good idea to use one of
the more specific statement-type elements. The more specific elements provided
better error-checking and even more functionality. (For example, the insert
statement can return a database-generated key.) Table~\ref{table:3.1}
summarizes the statement-type elements and their supported attributes and
features. The various attributes used by statement-type elements are covered
in Section~\ref{section:3.3.4}.
\begin{table}[!hpt]
\caption{The six statement-type elements } \label{table:3.1}
\centering
\begin{tabular}{|l|l|l|l|}
\hline
\textbf{Statement Element} &
\textbf{Attribute} &
\textbf{Child Elements} &
\textbf{Methods} \\
\hline
% after \\: \hline or \cline{col1-col2} \cline{col3-col4} ...
\tt{} &
\begin{minipage}{0.17\textwidth}
\vspace{3mm}
id \\
parameterClass \\
resultClass \\
listClass \\
parameterMap \\
resultMap\\
cacheModel
\vspace{3mm}
\end{minipage}
&
\begin{minipage}{0.22\textwidth}
None
\end{minipage}
&
\begin{minipage}{0.2\textwidth}
Insert \\ Update \\ Delete \\ All query methods
\end{minipage}
\\
\hline
\tt{} &
\begin{minipage}{0.17\textwidth}
\vspace{3mm}
id \\
parameterClass \\
parameterMap
\vspace{3mm}
\end{minipage}
&
\begin{minipage}{0.22\textwidth}
\tt{}\\
\tt{}
\end{minipage}
&
\begin{minipage}{0.2\textwidth}
Insert \\ Update \\ Delete
\end{minipage}
\\
\hline
\tt{} &
\begin{minipage}{0.17\textwidth}
\vspace{3mm}
id \\
parameterClass \\
parameterMap \\
extends
\vspace{3mm}
\end{minipage}
&
\begin{minipage}{0.22\textwidth}
\tt{}
\end{minipage}
&
\begin{minipage}{0.2\textwidth}
Insert \\ Update \\ Delete
\end{minipage}
\\
\hline
\tt{} &
\begin{minipage}{0.17\textwidth}
\vspace{3mm}
id \\
parameterClass \\
parameterMap \\
extends
\vspace{3mm}
\end{minipage}
&
\begin{minipage}{0.22\textwidth}
\tt{}
\end{minipage}
&
\begin{minipage}{0.2\textwidth}
Insert \\ Update \\ Delete
\end{minipage}
\\
\hline
\tt{