<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">\chapter{Installation and Setup}\label{section:4.3}
\section{Introduction}
This section explains how to install, configure, and use the SQLMap DataMapper
with your PHP application.

\section{Installing the DataMapper for PHP}
There are two steps to using SQLMap DataMapper with your application for the
first time.
\begin{itemize}
  \item Setup the distribution
  \item Add XML documents
\end{itemize}

\subsection{Setup the Distribution}

The official site for SQLMap DataMapper for PHP is http://... . The DataMapper
is availabe as a source distribution in the form of a ZIP archive. To download
the distributions, follow the link to the Downloads area on the web site, and
select the the source distribution for the SQLMap PHP DataMapper release. You
can extract the distribution using a utility like WinZip or the extractor
built into newer versions of Windows.

Under the distribution's source folder are eight folders that make up the
SQLMap PHP DataMapper distribution, as shown in Table 4.1.

\begin{table}[!hpt]
\caption{Folders found in the SQLMap PHP DataMapper source distribution}
\label{table:4.1}
 \centering
\begin{tabular}{|l|l|}
\hline
 \textbf{Folder name} &amp; \textbf{Description} \\
 \hline
\end{tabular}
\end{table}

\subsection{Add XML file items}
After unpacking the source distribution, you will need to add two types of XML
files to your Web application, or library project (and Test project if you
have one). These files are:

\begin{description}
  \item[SqlMap.xml] --
  A Data Map file that contains your SQL queries. Your project will contain one
  or more of these files with names such as Account.xml or Product.xml.

  \item[SqlMap.config] --
    The DataMapper configuration file that is used to specify the locations of your
    SqlMap.xml files. It is also used to define other DataMapper
   configuration options such as caching. You will need to include one SqlMap.config
   file for each data source that your project has.
\end{description}

As expected, the \tt{SqlMap.config} file must be placed where the DataMapper
can find them at runtime.

\section{Configuring the DataMapper for PHP}
The SQLMap PHP DataMapper is configured using a central XML descriptor file,
usually named \tt{SqlMap.config}, which provides the details for your data
source, data maps, and other features like caching, and transactions. At
runtime, your application code will call a class method provided by the SQLMap
library to read and parse your \tt{SqlMap.config} file. After parsing the
configuration file, a DataMapper client will be returned by SQLMap for your
application to use.

\subsection{DataMapper clients}
Currently, the SQLMap PHP DataMapper framework revolves around the
\tt{TSqlMapper} class, which acts as a facade to the DataMapper framework API.
You can create a DataMapper client by instantiating an object of the
\tt{TSqlMapper} class. An instance of the \tt{TSqlMapper} class (your
DataMapper client) is created by reading a single configuration file. Each
configuration file can specify one database or data source. You can of couse
use multiple DataMapper clients in your application. Just create another
configuration file and pass the name of that file when the DataMapper client
is created. The configuration files might use a different account with the
same database, or reference different databases on different servers. You can
read from one client and write to another, if that's what you need to do. See
Section~\ref{section:4.4.1} for more details on building a \tt{TSqlMapper}
instance, but first, let's take a look at the DataMapper configuration file.

\section{DataMapper Configuration File (SqlMap.config)}
A sample configuration file for a PHP web application is shown in
Example~\ref{example:4.1}. Not all configuration elements are required. The
following sections describe the elements of this \tt{SqlMap.config} file in
more detail.

\begin{example}\label{example:4.1}
Sample SqlMap.Config for a PHP Web Application.
\begin{verbatim}
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;sqlMapConfig&gt;

    &lt;provider class="TAdodbProvider" &gt;
        &lt;datasource ConnectionString="mysql://user:pass@localhost/test1" /&gt;
    &lt;/provider&gt;

    &lt;sqlMaps&gt;
        &lt;sqlMap name="Account" resource="maps/Account.xml"/&gt;
        &lt;sqlMap name="Order" resource="maps/Order.xml"/&gt;
        &lt;sqlMap name="Category" resource="maps/Category.xml"/&gt;
        &lt;sqlMap name="LineItem" resource="maps/LineItem.xml"/&gt;
    &lt;/sqlMaps&gt;

&lt;/sqlMapConfig&gt;
\end{verbatim}
\end{example}

\section{DataMapper Configuration Elements}
Sometimes the values we use in an XML configuration file occur in more than
one element. Often, there are values that change when we move the application
from one server to another. To help you manage configuration values, you can
specify a standard properties file (with name=value entries) as part of a
DataMapper configuration. Each named value in the properties file becomes a
shell variable that can be used in the DataMapper configuration file and your
Data Map definition files (see Chapter~\ref{section:3}).

\subsection{\tt{&lt;properties&gt;} attributes}
The \tt{&lt;properties&gt;} element can accept one \tt{resource} attribute to
specify the location of the properties file.

\begin{table}[!hpt]
\caption{Attributes of the \tt{&lt;properties&gt;} element} \label{table:4.3}
\centering
\begin{tabular}{|l|l|}
 \hline
 \textbf{Attribute} &amp; \textbf{Description} \\
 \hline
 \tt{resource} &amp;
  \begin{minipage}{0.7\textwidth}\vspace{2mm}
    Specify the properties file to be loaded from the directory relative to
    the current file.
    \vspace{-3mm}\begin{verbatim}
    resource="properties.config"
    \end{verbatim}\vspace{-5mm}
  \end{minipage}
  \\
  \hline
\end{tabular}
\end{table}

For example, if the ``properties.config'' file contains

\begin{verbatim}
&lt;?xml version="1.0" encoding="utf-8" ?&gt;
&lt;settings&gt;
  &lt;add key="username" value="albert" /&gt;
&lt;/settings&gt;
\end{verbatim}

then all elements in the DataMapper configuration can use the variable
\tt{\${username}} to insert the value ``albert''. For example:

\begin{verbatim}
&lt;provider ConnectionString="mysql://${username}:..."
\end{verbatim}

Properties are handy during building, testing, and deployment by making it
easy to reconfigure your application for multiple environments.

\subsection{\tt{&lt;property&gt;} element and attributes}
You can also specify more than one properties file or add property keys and
values directly into your \tt{SqlMap.config} file by using \tt{&lt;property&gt;}
elements. For example:

\begin{verbatim}
&lt;properties&gt;
 &lt;property resource="myProperties.config"/&gt;
 &lt;property resource="anotherProperties.config"/&gt;
 &lt;property key="host" value="ibatis.com" /&gt;
&lt;/properties&gt;
\end{verbatim}

\begin{table}[!hpt]
\caption{Attributes of the \tt{&lt;property&gt;} element} \label{table:4.3}
\centering
\begin{tabular}{|l|l|}
 \hline
 \textbf{Attribute} &amp; \textbf{Description} \\
 \hline
 \tt{resource} &amp;
 \begin{minipage}{0.7\textwidth}\vspace{2mm}
    Specify the properties file to be loaded from the directory relative to
    the current file.
    \vspace{-3mm}\begin{verbatim}
    resource="properties.config"
    \end{verbatim}\vspace{-5mm}
  \end{minipage}
  \\
  \hline
%
 \tt{key} &amp;
  \begin{minipage}{0.7\textwidth}\vspace{2mm}
   Defines a property key (variable) name
    \vspace{-3mm}\begin{verbatim}
    key="username"
    \end{verbatim}\vspace{-5mm}
  \end{minipage}
  \\
  \hline
%
 \tt{value} &amp;
  \begin{minipage}{0.7\textwidth}\vspace{2mm}
   Defines a value that will be used by the DataMapper in place of the
   the specified property key/variable
    \vspace{-3mm}\begin{verbatim}
    value="mydbuser"
    \end{verbatim}\vspace{-5mm}
  \end{minipage}
  \\
  \hline
\end{tabular}
\end{table}

\subsection{The \tt{&lt;typeHandler&gt;} Element}
The \tt{&lt;typeHandler&gt;} element allows for the configuration and use of a
Custom Type Handler (see the Custom Type Handler section). This extends 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.

\begin{verbatim}
  &lt;typeHandler type="date" callback="TDateTimeHandler"/&gt;
\end{verbatim}

The \tt{&lt;typeHandler&gt;} element has three attributes:
\begin{table}[!hpt]
\caption{Attributes of the \tt{&lt;typeHandler&gt;} element} \label{table:4.5}
\centering
\begin{tabular}{|l|l|}
 \hline
 \textbf{Attribute} &amp; \textbf{Description} \\
 \hline
%
 \tt{type} &amp;
 \begin{minipage}{0.7\textwidth}\vspace{2mm}
    Refers to the name of the type to handle
    \vspace{-3mm}\begin{verbatim}
    type="date"
    \end{verbatim}\vspace{-5mm}
  \end{minipage}
  \\
  \hline
%
 \tt{dbType} &amp;
 \begin{minipage}{0.7\textwidth}\vspace{2mm}
    Indicates the provider dbType to handle
    \vspace{-3mm}\begin{verbatim}
    dbType="Varchar2"
    \end{verbatim}\vspace{-5mm}
  \end{minipage}
  \\
  \hline
%
 \tt{callback} &amp;
 \begin{minipage}{0.7\textwidth}\vspace{2mm}
   The custom type handler class name
    \vspace{-3mm}\begin{verbatim}
    callback="TDateTimeHandler"
    \end{verbatim}\vspace{-5mm}
  \end{minipage}
  \\
  \hline
\end{tabular}
\end{table}


\subsection{The \tt{&lt;provider&gt;} element and attribute}
The \tt{&lt;provider&gt;} element encloses a \tt{&lt;datasource&gt;} that configure the
database system for use by the framework.
\begin{table}[!hpt]
\caption{Attributes of the \tt{&lt;provider&gt;} element} \label{table:4.3}
\centering
\begin{tabular}{|l|l|}
 \hline
 \textbf{Attribute} &amp; \textbf{Description} \\
 \hline
 \tt{class} &amp;
 \begin{minipage}{0.7\textwidth}\vspace{2mm}
    The database provider class that extends
    \tt{TDatabaseProvider}.
    \vspace{-3mm}\begin{verbatim}
    class="TAdodbProvider"
    \end{verbatim}\vspace{-5mm}
  \end{minipage}
  \\
  \hline
  \end{tabular}
\end{table}

\subsection{The \tt{&lt;datasource&gt;} element and attributes}
The \tt{&lt;datasource&gt;} element specifies the connection string.
Example~\ref{example:4.2} shows sample element MySql.

\begin{example}\label{example:4.2}
Sample \tt{&lt;provider&gt;} and \tt{&lt;datasource&gt;} elements.
\begin{verbatim}
&lt;!-- The ${properties} are defined in an external file, --&gt;
&lt;!-- but the values could also be coded inline. --&gt;

&lt;!-- Connecting to a MySQL database --&gt;
&lt;provider class="TAdodbProvider" &gt;
  &lt;datasource
    ConnectionString="mysql://${username}:${password}@${host}/${database}" /&gt;
&lt;/provider&gt;
\end{verbatim}
\end{example}

\begin{table}[!hpt]
\caption{Attributes of the \tt{&lt;datasource&gt;} element} \label{table:4.4}
\centering
\begin{tabular}{|l|l|}
 \hline
 \textbf{Attribute} &amp; \textbf{Description} \\
 \hline
%
 \tt{connectionString} &amp;
 \begin{minipage}{0.7\textwidth}\vspace{2mm}
    Data Source Name (DSN) connection string.
    \vspace{-3mm}\begin{verbatim}
    connectionString="mysql://root:pwd@localhost/mydb"
    \end{verbatim}\vspace{-5mm}
  \end{minipage}
  \\
  \hline
%
 \tt{driver} &amp;
 \begin{minipage}{0.7\textwidth}\vspace{2mm}
    Database driver name (mysql, sqlite, etc.)
    \vspace{-3mm}\begin{verbatim}
    driver="mysql"
    \end{verbatim}\vspace{-5mm}
  \end{minipage}
  \\
  \hline
%
 \tt{host} &amp;
 \begin{minipage}{0.7\textwidth}\vspace{2mm}
    DB host name/IP (and port number) in the format \tt{host[:port]}.
    \vspace{-3mm}\begin{verbatim}
    connectionString="mysql://root:pwd@localhost/mydb"
    \end{verbatim}\vspace{-5mm}
  \end{minipage}
  \\
  \hline
%
 \tt{username} &amp;
 \begin{minipage}{0.7\textwidth}\vspace{2mm}
    Database connection username.\vspace{2mm}
  \end{minipage}
  \\
  \hline
%
 \tt{password} &amp;
 \begin{minipage}{0.7\textwidth}\vspace{2mm}
    Database connection password.\vspace{2mm}
  \end{minipage}
  \\
  \hline
%
 \tt{database} &amp;
 \begin{minipage}{0.7\textwidth}\vspace{2mm}
    Database name to use in the connection.\vspace{2mm}
  \end{minipage}
  \\
  \hline
  \end{tabular}
\end{table}

\begin{mybox}{Tip:}
   Use Data Source Name (DSN) connection string or specify the
   necessary individual connection parameters.
\end{mybox}

\subsection{The \tt{&lt;sqlMap&gt;} Element}
On a daily basis, most of your work will be with the Data Maps, which are
covered by Chapter~\ref{section:3}. The Data Maps define the actual SQL
statements or stored procedures used by your application. The parameter and
result objects are also defined as part of the Data Map. As your application
grows, you may have several varieties of Data Map. To help you keep your Data
Maps organized, you can create any number of Data Map definition files and
incorporate them by reference in the DataMapper configuration. All of the
definition files used by a DataMapper instance must be listed in the
configuration file.

Example~\ref{example:4.3} shows \tt{&lt;sqlMap&gt;} elements for loading a set of
Data Map definitions. For more about Data Map definition files, see
Chapter~\ref{section:3}.

\begin{example}\label{example:4.3}
Specifying \tt{sqlMap} locations
\begin{verbatim}
&lt;!-- Relative path from the directory of the
     current file using a property variable --&gt;
&lt;sqlMap resource="${root}/Maps/Account.xml"/&gt;
&lt;sqlMap resource="${root}/Maps/Category.xml"/&gt;
&lt;sqlMap resource="${root}/Maps/Product.xml"/&gt;

&lt;!-- Full file path with a property variable --&gt;
&lt;sqlMap resource="/${projectdir}/MyApp/Maps/Account.xml"/&gt;
&lt;sqlMap resource="/${projectdir}/MyApp/Maps/Category.xml"/&gt;
&lt;sqlMap resource="/${projectdir}/MyApp/Maps/Product.xml"/&gt;
\end{verbatim}
\end{example}

\begin{mybox}{Tip:}
Since the application root directory location differs by project type
(Windows, Web, or library), it is best to use a properties variable to
indicate the relative path when using the \tt{&lt;sqlMap&gt;} \tt{resource}
attribute. Having a variable defined in a properties file makes it easy to
change the path to all your Data Mapper configuration resources in one
location (note the \tt{\$\{projectdir\}} and \tt{\$\{root\}} variables in the
example above).
\end{mybox}
</pre></body></html>