/framework/Data/SqlMap/TSqlMapGateway.php

https://bitbucket.org/volatileeight/prado · PHP · 259 lines · 75 code · 19 blank · 165 comment · 0 complexity · d5efbf172df785543f9400a457302476 MD5 · raw file

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