]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Data/SqlMap/TSqlMapGateway.php
8ce09ee44d9b2fbacb16ba80f77028cae6ac90ba
[bacula/bacula] / gui / baculum / framework / Data / SqlMap / TSqlMapGateway.php
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 &copy; 2005-2014 PradoSoft
8  * @license http://www.pradosoft.com/license/
9  * @package System.Data.SqlMap
10  */
11
12 Prado::using('System.Data.SqlMap.TSqlMapManager');
13
14 /**
15  * DataMapper client, a fascade to provide access the rest of the DataMapper
16  * framework. It provides three core functions:
17  *
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
21  *
22  * This class should be instantiated from a TSqlMapManager instance.
23  *
24  * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
25  * @package System.Data.SqlMap
26  * @since 3.1
27  */
28 class TSqlMapGateway extends TComponent
29 {
30         /**
31          * @var TSqlMapManager manager
32          */
33         private $_manager;
34
35         public function __construct($manager)
36         {
37                 $this->_manager=$manager;
38         }
39
40         /**
41          * @return TSqlMapManager sqlmap manager.
42          */
43         public function getSqlMapManager()
44         {
45                 return $this->_manager;
46         }
47
48         /**
49          * @return TDbConnection database connection.
50          */
51         public function getDbConnection()
52         {
53                 return $this->getSqlMapManager()->getDbConnection();
54         }
55
56         /**
57          * Executes a Sql SELECT statement that returns that returns data
58          * to populate a single object instance.
59          *
60          * The parameter object is generally used to supply the input
61          * data for the WHERE clause parameter(s) of the SELECT statement.
62          *
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.
67          */
68         public function queryForObject($statementName, $parameter=null, $result=null)
69         {
70                 $statement = $this->getSqlMapManager()->getMappedStatement($statementName);
71                 return $statement->executeQueryForObject($this->getDbConnection(), $parameter, $result);
72         }
73
74         /**
75          * Executes a Sql SELECT statement that returns data to populate a number
76          * of result objects.
77          *
78          * The parameter object is generally used to supply the input
79          * data for the WHERE clause parameter(s) of the SELECT statement.
80          *
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.
88          */
89         public function queryForList($statementName, $parameter=null, $result=null, $skip=-1, $max=-1)
90         {
91                 $statement = $this->getSqlMapManager()->getMappedStatement($statementName);
92                 return $statement->executeQueryForList($this->getDbConnection(),$parameter, $result, $skip, $max);
93         }
94
95         /**
96          * Runs a query for list with a custom object that gets a chance to deal
97          * with each row as it is processed.
98          *
99          * Example: $sqlmap->queryWithRowDelegate('getAccounts', array($this, 'rowHandler'));
100          *
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.
109          */
110         public function queryWithRowDelegate($statementName, $delegate, $parameter=null, $result=null, $skip=-1, $max=-1)
111         {
112                 $statement = $this->getSqlMapManager()->getMappedStatement($statementName);
113                 return $statement->executeQueryForList($this->getDbConnection(), $parameter, $result, $skip, $max, $delegate);
114         }
115
116         /**
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.
125          */
126         public function queryForPagedList($statementName, $parameter=null, $pageSize=10, $page=0)
127         {
128                 $statement = $this->getSqlMapManager()->getMappedStatement($statementName);
129                 return new TSqlMapPagedList($statement, $parameter, $pageSize, null, $page);
130         }
131
132         /**
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.
136          *
137          * Runs paged list query with row delegate
138          * Example: $sqlmap->queryForPagedListWithRowDelegate('getAccounts', array($this, 'rowHandler'));
139          *
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.
146          */
147         public function queryForPagedListWithRowDelegate($statementName,$delegate, $parameter=null, $pageSize=10, $page=0)
148         {
149                 $statement = $this->getSqlMapManager()->getMappedStatement($statementName);
150                 return new TSqlMapPagedList($statement, $parameter, $pageSize, $delegate,$page);
151         }
152
153
154         /**
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
159          * entered.
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.
165          */
166         public function queryForMap($statementName, $parameter=null, $keyProperty=null, $valueProperty=null, $skip=-1, $max=-1)
167         {
168                 $statement = $this->getSqlMapManager()->getMappedStatement($statementName);
169                 return $statement->executeQueryForMap($this->getDbConnection(), $parameter, $keyProperty, $valueProperty, $skip, $max);
170         }
171
172         /**
173          * Runs a query with a custom object that gets a chance to deal
174          * with each row as it is processed.
175          *
176          * Example: $sqlmap->queryForMapWithRowDelegate('getAccounts', array($this, 'rowHandler'));
177          *
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.
184          */
185         public function queryForMapWithRowDelegate($statementName, $delegate, $parameter=null, $keyProperty=null, $valueProperty=null, $skip=-1, $max=-1)
186         {
187                 $statement = $this->getSqlMapManager()->getMappedStatement($statementName);
188                 return $statement->executeQueryForMap($this->getDbConnection(), $parameter, $keyProperty, $valueProperty, $skip, $max, $delegate);
189         }
190
191         /**
192          * Executes a Sql INSERT statement.
193          *
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),
197          *
198          * The parameter object is generally used to supply the input data for the
199          * INSERT values.
200          *
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.
206          */
207         public function insert($statementName, $parameter=null)
208         {
209                 $statement = $this->getSqlMapManager()->getMappedStatement($statementName);
210                 return $statement->executeInsert($this->getDbConnection(), $parameter);
211         }
212
213         /**
214          * Executes a Sql UPDATE statement.
215          *
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.
218          *
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).
221          *
222          * @param string The name of the statement to execute.
223          * @param mixed The parameter object.
224          * @return integer The number of rows effected.
225          */
226         public function update($statementName, $parameter=null)
227         {
228                 $statement = $this->getSqlMapManager()->getMappedStatement($statementName);
229                 return $statement->executeUpdate($this->getDbConnection(), $parameter);
230         }
231
232         /**
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.
237          */
238         public function delete($statementName, $parameter=null)
239         {
240                 return $this->update($statementName, $parameter);
241         }
242
243         /**
244          * Flushes all cached objects that belong to this SqlMap
245          */
246         public function flushCaches()
247         {
248                 $this->getSqlMapManager()->flushCacheModels();
249         }
250
251         /**
252          * @param TSqlMapTypeHandler new type handler.
253          */
254         public function registerTypeHandler($typeHandler)
255         {
256                 $this->getSqlMapManager()->getTypeHandlers()->registerTypeHandler($typeHandler);
257         }
258 }
259