Blame | Letzte Änderung | Log anzeigen | RSS feed
<com:TContent ID="body"><h1>The Big Picture</h1><p>SQLMap is a simple but complete framework that makes it easy for you to mapyour objects to your SQL statements or stored procedures. The goal of theSQLMap framework is to obtain 80% of data access functionality using only20% of the code.</p><h1>What does it do?</h1><p>Developers often create maps between objects within an application. Onedefinition of a Mapper is an "object that sets up communication between twoindependent objects." A Data Mapper is a "layer of mappers that moves databetween objects and a database while keeping them independent of each otherand the mapper itself." [Patterns of Enterprise Architecture, ISBN0-321-12742-0].</p><p>You provide the database and the objects; SQLMap provides the mapping layerthat goes between the two.</p><h1>How does it work?</h1><p>Your programming platform already provides a capable library for accessingdatabases, whether through SQL statements or stored procedures. But developersfind several things are still hard to do well when using "stock" PHPfunction including:</p><p>Separating SQL code from programming code Passing input parameters to thelibrary classes and extracting the output Separating data access classes frombusiness logic classes Caching often-used data until it changes Managingtransactions and many more -- by using XML documents to create a mappingbetween a plain-old object and a SQL statement or a stored procedure. The"plain-old object" can be any PHP object.</p><p class="tip"><b class="tip">Tip:</b>The object does not need to be part of a special object hierarchy or implementa special interface. (Which is why we call them "plain-old" objects.)Whatever you are already using should work just fine.</p><img src=<%~ diagram.png %> alt="SQLMap DataMapper work flow" id="fig:diagram.png" class="figure"/><div class="caption"><b>Figure 1:</b> SQLMap DataMapper work flow</div><p>Here's a high level description of the work flow shown in the figure above:Provide a parameter, either as an object or aprimitive type. The parameter can be used to set runtime values in your SQLstatement or stored procedure. If a runtime value is not needed, the parametercan be omitted.</p><p>Execute the mapping by passing the parameter and the name you gave thestatement or procedure in your XML descriptor. This step is where the magichappens. The framework will prepare the SQL statement or stored procedure, setany runtime values using your parameter, execute the procedure or statement,and return the result.</p><p>In the case of an update, the number of rows affected is returned. In the caseof a query, a single object, or a collection of objects is returned. Like theparameter, the result object, or collection of objects, can be a plain-oldobject or a primitive type.</p><p>So, what does all this look like in your source code? Here's how you mightcode the insert of a "lineItem" object into your database.</p><com:TTextHighlighter Language="php" CssClass="source">TMapper::instance()->insert("InsertLineItem", $lineItem);</com:TTextHighlighter><p>If your database is generating the primary keys, the generated key can bereturned from the same method call, like this:</p><com:TTextHighlighter Language="php" CssClass="source">$myKey = TMapper::instance()->insert("InsertLineItem", $lineItem);</com:TTextHighlighter><p>The following example shows an XML descriptor for "InsertLineItem".<com:TTextHighlighter Language="xml" CssClass="source"><insert id="InsertLineItem" parameterClass="LineItem">INSERT INTO [LinesItem](Order_Id, LineItem_LineNum, Item_Id, LineItem_Quantity, LineItem_UnitPrice)VALUES(#Order.Id#, #LineNumber#, #Item.Id#, #Quantity#, #Item.ListPrice#)<selectKey type="post" resultClass="int" property="Id" >select @@IDENTITY as value</selectKey></insert></com:TTextHighlighter></p><p>The <tt><selectKey></tt> stanza returns an auto-generated key from a SQL Serverdatabase (for example). If you need to select multiple rows, SQLMap can returna list of objects, each mapped to a row in the result set:<com:TTextHighlighter Language="php" CssClass="source">$productList = Mapper::instance()->queryForList("selectProduct",$categoryKey);</com:TTextHighlighter>Or just one, if that's all you need:<com:TTextHighlighter Language="php" CssClass="source">$product = Mapper::instance()->queryForObject("selectProduct",$categoryKey);</com:TTextHighlighter></p><p>Of course, there's more, but this is SQLMap from 10,000 meters. (For a longer,gentler introduction, see the <a href="?page=Tutorial.TestFirst">Tutorial</a>.)The <a href=" ?page=Manual.DataMapperConfiguration">Data Map definition</a> files describeswhere the statement for "InsertLineItem" wouldbe defined. The <a href="?page=Manual.Installing">Installation and Setup</a> section describesthe "bootstrap" configuration file that exposes SQLMap to your application.</p><h1>Is SQLMap the best choice for my project?</h1><p>SQLMap is a Data Mapping tool. Its role is to map the columns of a databasequery (including a stored procedure) to the properties of an object. If yourapplication is based on business objects (including array or lists ofobjects), then SQLMap can be a good choice. SQLMap is an even better choicewhen your application is layered, so that that the business layer is distinctfrom the user-interface layer.</p><p>Under these circumstances, another good choice would be an Object/RelationalMapping tool (OR/M tool), like [...]. Other products in this category are[...] and [...] . An OR/M tool generates all or most of the SQL for you,either beforehand or at runtime. These products are called OR/M tools becausethey try to map an object graph to a relational schema.</p><p>SQLMap is not an OR/M tool. SQLMap helps you map objects to stored proceduresor SQL statements. The underlying schema is irrelevant. An OR/M tool is greatif you can map your objects to tables. But they are not so great if yourobjects are stored as a relational view rather than as a table. If you canwrite a statement or procedure that exposes the columns for your object,regardless of how they are stored, SQLMap can do the rest.</p><p>So, how do you decide whether to OR/M or to DataMap? As always, the bestadvice is to implement a representative part of your project using eitherapproach, and then decide. But, in general, OR/M is a good thing when you<ul><li>Have complete control over your database implementation.</li><li>Do not have a Database Administrator or SQL guru on the team.</li><li>Need to model the problem domain outside the database as an object graph.</li></ul>Likewise, the best time to use a Data Mapper, like SQLMap, is when:<ul><li>You do not have complete control over the database implementation, or want tocontinue to access a legacy database as it is being refactored.</li><li>You have database administrators or SQL gurus on the team.</li><li>The database is being used to model the problem domain, and the application'sprimary role is to help the client use the database model.</li></ul></p><p>In the end, you have to decide what's best for your project. If a OR/M toolworks better for you, that's great! If your next project has different needs,then we hope you give SQLMap another look. If SQLMap works for you now:Excellent!</p></com:TContent>