3 * TSqlMapGateway class file.
5 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6 * @link http://www.pradosoft.com/
7 * @copyright Copyright © 2005-2014 PradoSoft
8 * @license http://www.pradosoft.com/license/
9 * @package System.Data.SqlMap
12 Prado::using('System.Data.SqlMap.TSqlMapManager');
15 * DataMapper client, a fascade to provide access the rest of the DataMapper
16 * framework. It provides three core functions:
18 * # execute an update query (including insert and delete).
19 * # execute a select query for a single object
20 * # execute a select query for a list of objects
22 * This class should be instantiated from a TSqlMapManager instance.
24 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
25 * @package System.Data.SqlMap
28 class TSqlMapGateway extends TComponent
31 * @var TSqlMapManager manager
35 public function __construct($manager)
37 $this->_manager=$manager;
41 * @return TSqlMapManager sqlmap manager.
43 public function getSqlMapManager()
45 return $this->_manager;
49 * @return TDbConnection database connection.
51 public function getDbConnection()
53 return $this->getSqlMapManager()->getDbConnection();
57 * Executes a Sql SELECT statement that returns that returns data
58 * to populate a single object instance.
60 * The parameter object is generally used to supply the input
61 * data for the WHERE clause parameter(s) of the SELECT statement.
63 * @param string The name of the sql statement to execute.
64 * @param mixed The object used to set the parameters in the SQL.
65 * @param mixed An object of the type to be returned.
66 * @return object A single result object populated with the result set data.
68 public function queryForObject($statementName, $parameter=null, $result=null)
70 $statement = $this->getSqlMapManager()->getMappedStatement($statementName);
71 return $statement->executeQueryForObject($this->getDbConnection(), $parameter, $result);
75 * Executes a Sql SELECT statement that returns data to populate a number
78 * The parameter object is generally used to supply the input
79 * data for the WHERE clause parameter(s) of the SELECT statement.
81 * @param string The name of the sql statement to execute.
82 * @param mixed The object used to set the parameters in the SQL.
83 * @param TList An Ilist object used to hold the objects,
84 * pass in null if want to return a list instead.
85 * @param int The number of rows to skip over.
86 * @param int The maximum number of rows to return.
87 * @return TList A List of result objects.
89 public function queryForList($statementName, $parameter=null, $result=null, $skip=-1, $max=-1)
91 $statement = $this->getSqlMapManager()->getMappedStatement($statementName);
92 return $statement->executeQueryForList($this->getDbConnection(),$parameter, $result, $skip, $max);
96 * Runs a query for list with a custom object that gets a chance to deal
97 * with each row as it is processed.
99 * Example: $sqlmap->queryWithRowDelegate('getAccounts', array($this, 'rowHandler'));
101 * @param string The name of the sql statement to execute.
102 * @param callback Row delegate handler, a valid callback required.
103 * @param mixed The object used to set the parameters in the SQL.
104 * @param TList An Ilist object used to hold the objects,
105 * pass in null if want to return a list instead.
106 * @param int The number of rows to skip over.
107 * @param int The maximum number of rows to return.
108 * @return TList A List of result objects.
110 public function queryWithRowDelegate($statementName, $delegate, $parameter=null, $result=null, $skip=-1, $max=-1)
112 $statement = $this->getSqlMapManager()->getMappedStatement($statementName);
113 return $statement->executeQueryForList($this->getDbConnection(), $parameter, $result, $skip, $max, $delegate);
117 * Executes the SQL and retuns a subset of the results in a dynamic
118 * TPagedList that can be used to automatically scroll through results
119 * from a database table.
120 * @param string The name of the sql statement to execute.
121 * @param mixed The object used to set the parameters in the SQL.
122 * @param integer The maximum number of objects to store in each page.
123 * @param integer The number of the page to initially load into the list.
124 * @return TPagedList A PaginatedList of beans containing the rows.
126 public function queryForPagedList($statementName, $parameter=null, $pageSize=10, $page=0)
128 $statement = $this->getSqlMapManager()->getMappedStatement($statementName);
129 return new TSqlMapPagedList($statement, $parameter, $pageSize, null, $page);
133 * Executes the SQL and retuns a subset of the results in a dynamic
134 * TPagedList that can be used to automatically scroll through results
135 * from a database table.
137 * Runs paged list query with row delegate
138 * Example: $sqlmap->queryForPagedListWithRowDelegate('getAccounts', array($this, 'rowHandler'));
140 * @param string The name of the sql statement to execute.
141 * @param callback Row delegate handler, a valid callback required.
142 * @param mixed The object used to set the parameters in the SQL.
143 * @param integer The maximum number of objects to store in each page.
144 * @param integer The number of the page to initially load into the list.
145 * @return TPagedList A PaginatedList of beans containing the rows.
147 public function queryForPagedListWithRowDelegate($statementName,$delegate, $parameter=null, $pageSize=10, $page=0)
149 $statement = $this->getSqlMapManager()->getMappedStatement($statementName);
150 return new TSqlMapPagedList($statement, $parameter, $pageSize, $delegate,$page);
155 * Executes the SQL and retuns all rows selected in a map that is keyed on
156 * the property named in the keyProperty parameter. The value at each key
157 * will be the value of the property specified in the valueProperty
158 * parameter. If valueProperty is null, the entire result object will be
160 * @param string The name of the sql statement to execute.
161 * @param mixed The object used to set the parameters in the SQL.
162 * @param string The property of the result object to be used as the key.
163 * @param string The property of the result object to be used as the value.
164 * @return TMap Array object containing the rows keyed by keyProperty.
166 public function queryForMap($statementName, $parameter=null, $keyProperty=null, $valueProperty=null, $skip=-1, $max=-1)
168 $statement = $this->getSqlMapManager()->getMappedStatement($statementName);
169 return $statement->executeQueryForMap($this->getDbConnection(), $parameter, $keyProperty, $valueProperty, $skip, $max);
173 * Runs a query with a custom object that gets a chance to deal
174 * with each row as it is processed.
176 * Example: $sqlmap->queryForMapWithRowDelegate('getAccounts', array($this, 'rowHandler'));
178 * @param string The name of the sql statement to execute.
179 * @param callback Row delegate handler, a valid callback required.
180 * @param mixed The object used to set the parameters in the SQL.
181 * @param string The property of the result object to be used as the key.
182 * @param string The property of the result object to be used as the value.
183 * @return TMap Array object containing the rows keyed by keyProperty.
185 public function queryForMapWithRowDelegate($statementName, $delegate, $parameter=null, $keyProperty=null, $valueProperty=null, $skip=-1, $max=-1)
187 $statement = $this->getSqlMapManager()->getMappedStatement($statementName);
188 return $statement->executeQueryForMap($this->getDbConnection(), $parameter, $keyProperty, $valueProperty, $skip, $max, $delegate);
192 * Executes a Sql INSERT statement.
194 * Insert is a bit different from other update methods, as it provides
195 * facilities for returning the primary key of the newly inserted row
196 * (rather than the effected rows),
198 * The parameter object is generally used to supply the input data for the
201 * @param string The name of the statement to execute.
202 * @param string The parameter object.
203 * @return mixed The primary key of the newly inserted row.
204 * This might be automatically generated by the RDBMS,
205 * or selected from a sequence table or other source.
207 public function insert($statementName, $parameter=null)
209 $statement = $this->getSqlMapManager()->getMappedStatement($statementName);
210 return $statement->executeInsert($this->getDbConnection(), $parameter);
214 * Executes a Sql UPDATE statement.
216 * Update can also be used for any other update statement type, such as
217 * inserts and deletes. Update returns the number of rows effected.
219 * The parameter object is generally used to supply the input data for the
220 * UPDATE values as well as the WHERE clause parameter(s).
222 * @param string The name of the statement to execute.
223 * @param mixed The parameter object.
224 * @return integer The number of rows effected.
226 public function update($statementName, $parameter=null)
228 $statement = $this->getSqlMapManager()->getMappedStatement($statementName);
229 return $statement->executeUpdate($this->getDbConnection(), $parameter);
233 * Executes a Sql DELETE statement. Delete returns the number of rows effected.
234 * @param string The name of the statement to execute.
235 * @param mixed The parameter object.
236 * @return integer The number of rows effected.
238 public function delete($statementName, $parameter=null)
240 return $this->update($statementName, $parameter);
244 * Flushes all cached objects that belong to this SqlMap
246 public function flushCaches()
248 $this->getSqlMapManager()->flushCacheModels();
252 * @param TSqlMapTypeHandler new type handler.
254 public function registerTypeHandler($typeHandler)
256 $this->getSqlMapManager()->getTypeHandlers()->registerTypeHandler($typeHandler);