| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TSqlMapGateway class file.
|
|
|
4 |
*
|
|
|
5 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TSqlMapGateway.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
10 |
* @package System.Data.SqlMap
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
Prado::using('System.Data.SqlMap.TSqlMapManager');
|
|
|
14 |
|
|
|
15 |
/**
|
|
|
16 |
* DataMapper client, a fascade to provide access the rest of the DataMapper
|
|
|
17 |
* framework. It provides three core functions:
|
|
|
18 |
*
|
|
|
19 |
* # execute an update query (including insert and delete).
|
|
|
20 |
* # execute a select query for a single object
|
|
|
21 |
* # execute a select query for a list of objects
|
|
|
22 |
*
|
|
|
23 |
* This class should be instantiated from a TSqlMapManager instance.
|
|
|
24 |
*
|
|
|
25 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
26 |
* @version $Id: TSqlMapGateway.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
27 |
* @package System.Data.SqlMap
|
|
|
28 |
* @since 3.1
|
|
|
29 |
*/
|
|
|
30 |
class TSqlMapGateway extends TComponent
|
|
|
31 |
{
|
|
|
32 |
/**
|
|
|
33 |
* @var TSqlMapManager manager
|
|
|
34 |
*/
|
|
|
35 |
private $_manager;
|
|
|
36 |
|
|
|
37 |
public function __construct($manager)
|
|
|
38 |
{
|
|
|
39 |
$this->_manager=$manager;
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* @return TSqlMapManager sqlmap manager.
|
|
|
44 |
*/
|
|
|
45 |
public function getSqlMapManager()
|
|
|
46 |
{
|
|
|
47 |
return $this->_manager;
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* @return TDbConnection database connection.
|
|
|
52 |
*/
|
|
|
53 |
public function getDbConnection()
|
|
|
54 |
{
|
|
|
55 |
return $this->getSqlMapManager()->getDbConnection();
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Executes a Sql SELECT statement that returns that returns data
|
|
|
60 |
* to populate a single object instance.
|
|
|
61 |
*
|
|
|
62 |
* The parameter object is generally used to supply the input
|
|
|
63 |
* data for the WHERE clause parameter(s) of the SELECT statement.
|
|
|
64 |
*
|
|
|
65 |
* @param string The name of the sql statement to execute.
|
|
|
66 |
* @param mixed The object used to set the parameters in the SQL.
|
|
|
67 |
* @param mixed An object of the type to be returned.
|
|
|
68 |
* @return object A single result object populated with the result set data.
|
|
|
69 |
*/
|
|
|
70 |
public function queryForObject($statementName, $parameter=null, $result=null)
|
|
|
71 |
{
|
|
|
72 |
$statement = $this->getSqlMapManager()->getMappedStatement($statementName);
|
|
|
73 |
return $statement->executeQueryForObject($this->getDbConnection(), $parameter, $result);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Executes a Sql SELECT statement that returns data to populate a number
|
|
|
78 |
* of result objects.
|
|
|
79 |
*
|
|
|
80 |
* The parameter object is generally used to supply the input
|
|
|
81 |
* data for the WHERE clause parameter(s) of the SELECT statement.
|
|
|
82 |
*
|
|
|
83 |
* @param string The name of the sql statement to execute.
|
|
|
84 |
* @param mixed The object used to set the parameters in the SQL.
|
|
|
85 |
* @param TList An Ilist object used to hold the objects,
|
|
|
86 |
* pass in null if want to return a list instead.
|
|
|
87 |
* @param int The number of rows to skip over.
|
|
|
88 |
* @param int The maximum number of rows to return.
|
|
|
89 |
* @return TList A List of result objects.
|
|
|
90 |
*/
|
|
|
91 |
public function queryForList($statementName, $parameter=null, $result=null, $skip=-1, $max=-1)
|
|
|
92 |
{
|
|
|
93 |
$statement = $this->getSqlMapManager()->getMappedStatement($statementName);
|
|
|
94 |
return $statement->executeQueryForList($this->getDbConnection(),$parameter, $result, $skip, $max);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Runs a query for list with a custom object that gets a chance to deal
|
|
|
99 |
* with each row as it is processed.
|
|
|
100 |
*
|
|
|
101 |
* Example: $sqlmap->queryWithRowDelegate('getAccounts', array($this, 'rowHandler'));
|
|
|
102 |
*
|
|
|
103 |
* @param string The name of the sql statement to execute.
|
|
|
104 |
* @param callback Row delegate handler, a valid callback required.
|
|
|
105 |
* @param mixed The object used to set the parameters in the SQL.
|
|
|
106 |
* @param TList An Ilist object used to hold the objects,
|
|
|
107 |
* pass in null if want to return a list instead.
|
|
|
108 |
* @param int The number of rows to skip over.
|
|
|
109 |
* @param int The maximum number of rows to return.
|
|
|
110 |
* @return TList A List of result objects.
|
|
|
111 |
*/
|
|
|
112 |
public function queryWithRowDelegate($statementName, $delegate, $parameter=null, $result=null, $skip=-1, $max=-1)
|
|
|
113 |
{
|
|
|
114 |
$statement = $this->getSqlMapManager()->getMappedStatement($statementName);
|
|
|
115 |
return $statement->executeQueryForList($this->getDbConnection(), $parameter, $result, $skip, $max, $delegate);
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* Executes the SQL and retuns a subset of the results in a dynamic
|
|
|
120 |
* TPagedList that can be used to automatically scroll through results
|
|
|
121 |
* from a database table.
|
|
|
122 |
* @param string The name of the sql statement to execute.
|
|
|
123 |
* @param mixed The object used to set the parameters in the SQL.
|
|
|
124 |
* @param integer The maximum number of objects to store in each page.
|
|
|
125 |
* @param integer The number of the page to initially load into the list.
|
|
|
126 |
* @return TPagedList A PaginatedList of beans containing the rows.
|
|
|
127 |
*/
|
|
|
128 |
public function queryForPagedList($statementName, $parameter=null, $pageSize=10, $page=0)
|
|
|
129 |
{
|
|
|
130 |
$statement = $this->getSqlMapManager()->getMappedStatement($statementName);
|
|
|
131 |
return new TSqlMapPagedList($statement, $parameter, $pageSize, null, $page);
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
/**
|
|
|
135 |
* Executes the SQL and retuns a subset of the results in a dynamic
|
|
|
136 |
* TPagedList that can be used to automatically scroll through results
|
|
|
137 |
* from a database table.
|
|
|
138 |
*
|
|
|
139 |
* Runs paged list query with row delegate
|
|
|
140 |
* Example: $sqlmap->queryForPagedListWithRowDelegate('getAccounts', array($this, 'rowHandler'));
|
|
|
141 |
*
|
|
|
142 |
* @param string The name of the sql statement to execute.
|
|
|
143 |
* @param callback Row delegate handler, a valid callback required.
|
|
|
144 |
* @param mixed The object used to set the parameters in the SQL.
|
|
|
145 |
* @param integer The maximum number of objects to store in each page.
|
|
|
146 |
* @param integer The number of the page to initially load into the list.
|
|
|
147 |
* @return TPagedList A PaginatedList of beans containing the rows.
|
|
|
148 |
*/
|
|
|
149 |
public function queryForPagedListWithRowDelegate($statementName,$delegate, $parameter=null, $pageSize=10, $page=0)
|
|
|
150 |
{
|
|
|
151 |
$statement = $this->getSqlMapManager()->getMappedStatement($statementName);
|
|
|
152 |
return new TSqlMapPagedList($statement, $parameter, $pageSize, $delegate,$page);
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
|
|
|
156 |
/**
|
|
|
157 |
* Executes the SQL and retuns all rows selected in a map that is keyed on
|
|
|
158 |
* the property named in the keyProperty parameter. The value at each key
|
|
|
159 |
* will be the value of the property specified in the valueProperty
|
|
|
160 |
* parameter. If valueProperty is null, the entire result object will be
|
|
|
161 |
* entered.
|
|
|
162 |
* @param string The name of the sql statement to execute.
|
|
|
163 |
* @param mixed The object used to set the parameters in the SQL.
|
|
|
164 |
* @param string The property of the result object to be used as the key.
|
|
|
165 |
* @param string The property of the result object to be used as the value.
|
|
|
166 |
* @return TMap Array object containing the rows keyed by keyProperty.
|
|
|
167 |
*/
|
|
|
168 |
public function queryForMap($statementName, $parameter=null, $keyProperty=null, $valueProperty=null, $skip=-1, $max=-1)
|
|
|
169 |
{
|
|
|
170 |
$statement = $this->getSqlMapManager()->getMappedStatement($statementName);
|
|
|
171 |
return $statement->executeQueryForMap($this->getDbConnection(), $parameter, $keyProperty, $valueProperty, $skip, $max);
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
/**
|
|
|
175 |
* Runs a query with a custom object that gets a chance to deal
|
|
|
176 |
* with each row as it is processed.
|
|
|
177 |
*
|
|
|
178 |
* Example: $sqlmap->queryForMapWithRowDelegate('getAccounts', array($this, 'rowHandler'));
|
|
|
179 |
*
|
|
|
180 |
* @param string The name of the sql statement to execute.
|
|
|
181 |
* @param callback Row delegate handler, a valid callback required.
|
|
|
182 |
* @param mixed The object used to set the parameters in the SQL.
|
|
|
183 |
* @param string The property of the result object to be used as the key.
|
|
|
184 |
* @param string The property of the result object to be used as the value.
|
|
|
185 |
* @return TMap Array object containing the rows keyed by keyProperty.
|
|
|
186 |
*/
|
|
|
187 |
public function queryForMapWithRowDelegate($statementName, $delegate, $parameter=null, $keyProperty=null, $valueProperty=null, $skip=-1, $max=-1)
|
|
|
188 |
{
|
|
|
189 |
$statement = $this->getSqlMapManager()->getMappedStatement($statementName);
|
|
|
190 |
return $statement->executeQueryForMap($this->getDbConnection(), $parameter, $keyProperty, $valueProperty, $skip, $max, $delegate);
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
/**
|
|
|
194 |
* Executes a Sql INSERT statement.
|
|
|
195 |
*
|
|
|
196 |
* Insert is a bit different from other update methods, as it provides
|
|
|
197 |
* facilities for returning the primary key of the newly inserted row
|
|
|
198 |
* (rather than the effected rows),
|
|
|
199 |
*
|
|
|
200 |
* The parameter object is generally used to supply the input data for the
|
|
|
201 |
* INSERT values.
|
|
|
202 |
*
|
|
|
203 |
* @param string The name of the statement to execute.
|
|
|
204 |
* @param string The parameter object.
|
|
|
205 |
* @return mixed The primary key of the newly inserted row.
|
|
|
206 |
* This might be automatically generated by the RDBMS,
|
|
|
207 |
* or selected from a sequence table or other source.
|
|
|
208 |
*/
|
|
|
209 |
public function insert($statementName, $parameter=null)
|
|
|
210 |
{
|
|
|
211 |
$statement = $this->getSqlMapManager()->getMappedStatement($statementName);
|
|
|
212 |
return $statement->executeInsert($this->getDbConnection(), $parameter);
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
/**
|
|
|
216 |
* Executes a Sql UPDATE statement.
|
|
|
217 |
*
|
|
|
218 |
* Update can also be used for any other update statement type, such as
|
|
|
219 |
* inserts and deletes. Update returns the number of rows effected.
|
|
|
220 |
*
|
|
|
221 |
* The parameter object is generally used to supply the input data for the
|
|
|
222 |
* UPDATE values as well as the WHERE clause parameter(s).
|
|
|
223 |
*
|
|
|
224 |
* @param string The name of the statement to execute.
|
|
|
225 |
* @param mixed The parameter object.
|
|
|
226 |
* @return integer The number of rows effected.
|
|
|
227 |
*/
|
|
|
228 |
public function update($statementName, $parameter=null)
|
|
|
229 |
{
|
|
|
230 |
$statement = $this->getSqlMapManager()->getMappedStatement($statementName);
|
|
|
231 |
return $statement->executeUpdate($this->getDbConnection(), $parameter);
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
/**
|
|
|
235 |
* Executes a Sql DELETE statement. Delete returns the number of rows effected.
|
|
|
236 |
* @param string The name of the statement to execute.
|
|
|
237 |
* @param mixed The parameter object.
|
|
|
238 |
* @return integer The number of rows effected.
|
|
|
239 |
*/
|
|
|
240 |
public function delete($statementName, $parameter=null)
|
|
|
241 |
{
|
|
|
242 |
return $this->update($statementName, $parameter);
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
/**
|
|
|
246 |
* Flushes all cached objects that belong to this SqlMap
|
|
|
247 |
*/
|
|
|
248 |
public function flushCaches()
|
|
|
249 |
{
|
|
|
250 |
$this->getSqlMapManager()->flushCacheModels();
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
/**
|
|
|
254 |
* @param TSqlMapTypeHandler new type handler.
|
|
|
255 |
*/
|
|
|
256 |
public function registerTypeHandler($typeHandler)
|
|
|
257 |
{
|
|
|
258 |
$this->getSqlMapManager()->getTypeHandlers()->registerTypeHandler($typeHandler);
|
|
|
259 |
}
|
|
|
260 |
}
|
|
|
261 |
|