/framework/Data/SqlMap/Configuration/TSqlMapStatement.php

https://bitbucket.org/volatileeight/prado · PHP · 444 lines · 206 code · 46 blank · 192 comment · 19 complexity · 92272ca396e9abce2d11ffbc00415bce MD5 · raw file

  1. <?php
  2. /**
  3. * TSqlMapStatement, TSqlMapInsert, TSqlMapUpdate, TSqlMapDelete,
  4. * TSqlMapSelect and TSqlMapSelectKey classes file.
  5. *
  6. * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
  7. * @link http://www.pradosoft.com/
  8. * @copyright Copyright &copy; 2005-2014 PradoSoft
  9. * @license http://www.pradosoft.com/license/
  10. * @package System.Data.SqlMap.Configuration
  11. */
  12. /**
  13. * TSqlMapStatement class corresponds to <statement> element.
  14. *
  15. * Mapped Statements can hold any SQL statement and can use Parameter Maps
  16. * and Result Maps for input and output.
  17. *
  18. * The <statement> element is a general "catch all" element that can be used
  19. * for any type of SQL statement. Generally it is a good idea to use one of the
  20. * more specific statement-type elements. The more specific elements provided
  21. * better error-checking and even more functionality. (For example, the insert
  22. * statement can return a database-generated key.)
  23. *
  24. * @author Wei Zhuo <weizho[at]gmail[dot]com>
  25. * @package System.Data.SqlMap.Configuration
  26. * @since 3.1
  27. */
  28. class TSqlMapStatement extends TComponent
  29. {
  30. private $_parameterMapName;
  31. private $_parameterMap;
  32. private $_parameterClassName;
  33. private $_resultMapName;
  34. private $_resultMap;
  35. private $_resultClassName;
  36. private $_cacheModelName;
  37. private $_SQL;
  38. private $_listClass;
  39. private $_typeHandler;
  40. private $_extendStatement;
  41. private $_cache;
  42. private $_ID;
  43. /**
  44. * @return string name for this statement, unique to each sql map manager.
  45. */
  46. public function getID()
  47. {
  48. return $this->_ID;
  49. }
  50. /**
  51. * @param string name for this statement, which must be unique for each sql map manager.
  52. */
  53. public function setID($value)
  54. {
  55. $this->_ID=$value;
  56. }
  57. /**
  58. * @return string name of a parameter map.
  59. */
  60. public function getParameterMap()
  61. {
  62. return $this->_parameterMapName;
  63. }
  64. /**
  65. * A Parameter Map defines an ordered list of values that match up with
  66. * the "?" placeholders of a standard, parameterized query statement.
  67. * @param string parameter map name.
  68. */
  69. public function setParameterMap($value)
  70. {
  71. $this->_parameterMapName = $value;
  72. }
  73. /**
  74. * @return string parameter class name.
  75. */
  76. public function getParameterClass()
  77. {
  78. return $this->_parameterClassName;
  79. }
  80. /**
  81. * If a {@link ParameterMap setParameterMap()} property is not specified,
  82. * you may specify a ParameterClass instead and use inline parameters.
  83. * The value of the parameterClass attribute can be any existing PHP class name.
  84. * @param string parameter class name.
  85. */
  86. public function setParameterClass($value)
  87. {
  88. $this->_parameterClassName = $value;
  89. }
  90. /**
  91. * @return string result map name.
  92. */
  93. public function getResultMap()
  94. {
  95. return $this->_resultMapName;
  96. }
  97. /**
  98. * A Result Map lets you control how data is extracted from the result of a
  99. * query, and how the columns are mapped to object properties.
  100. * @param string result map name.
  101. */
  102. public function setResultMap($value)
  103. {
  104. $this->_resultMapName = $value;
  105. }
  106. /**
  107. * @return string result class name.
  108. */
  109. public function getResultClass()
  110. {
  111. return $this->_resultClassName;
  112. }
  113. /**
  114. * If a {@link ResultMap setResultMap()} is not specified, you may specify a
  115. * ResultClass instead. The value of the ResultClass property can be the
  116. * name of a PHP class or primitives like integer, string, or array. The
  117. * class specified will be automatically mapped to the columns in the
  118. * result, based on the result metadata.
  119. * @param string result class name.
  120. */
  121. public function setResultClass($value)
  122. {
  123. $this->_resultClassName = $value;
  124. }
  125. /**
  126. * @return string cache mode name.
  127. */
  128. public function getCacheModel()
  129. {
  130. return $this->_cacheModelName;
  131. }
  132. /**
  133. * @param string cache mode name.
  134. */
  135. public function setCacheModel($value)
  136. {
  137. $this->_cacheModelName = $value;
  138. }
  139. /**
  140. * @return TSqlMapCacheModel cache implementation instance for this statement.
  141. */
  142. public function getCache()
  143. {
  144. return $this->_cache;
  145. }
  146. /**
  147. * @param TSqlMapCacheModel cache implementation instance for this statement.
  148. */
  149. public function setCache($value)
  150. {
  151. $this->_cache = $value;
  152. }
  153. /**
  154. * @return TStaticSql sql text container.
  155. */
  156. public function getSqlText()
  157. {
  158. return $this->_SQL;
  159. }
  160. /**
  161. * @param TStaticSql sql text container.
  162. */
  163. public function setSqlText($value)
  164. {
  165. $this->_SQL = $value;
  166. }
  167. /**
  168. * @return string name of a PHP class that implements ArrayAccess.
  169. */
  170. public function getListClass()
  171. {
  172. return $this->_listClass;
  173. }
  174. /**
  175. * An ArrayAccess class can be specified to handle the type of objects in the collection.
  176. * @param string name of a PHP class that implements ArrayAccess.
  177. */
  178. public function setListClass($value)
  179. {
  180. $this->_listClass = $value;
  181. }
  182. /**
  183. * @return string another statement element name.
  184. */
  185. public function getExtends()
  186. {
  187. return $this->_extendStatement;
  188. }
  189. /**
  190. * @param string name of another statement element to extend.
  191. */
  192. public function setExtends($value)
  193. {
  194. $this->_extendStatement = $value;
  195. }
  196. /**
  197. * @return TResultMap the result map corresponding to the
  198. * {@link ResultMap getResultMap()} property.
  199. */
  200. public function resultMap()
  201. {
  202. return $this->_resultMap;
  203. }
  204. /**
  205. * @return TParameterMap the parameter map corresponding to the
  206. * {@link ParameterMap getParameterMap()} property.
  207. */
  208. public function parameterMap()
  209. {
  210. return $this->_parameterMap;
  211. }
  212. /**
  213. * @param TInlineParameterMap parameter extracted from the sql text.
  214. */
  215. public function setInlineParameterMap($map)
  216. {
  217. $this->_parameterMap = $map;
  218. }
  219. /**
  220. * @param TSqlMapManager initialize the statement, sets the result and parameter maps.
  221. */
  222. public function initialize($manager)
  223. {
  224. if(strlen($this->_resultMapName) > 0)
  225. $this->_resultMap = $manager->getResultMap($this->_resultMapName);
  226. if(strlen($this->_parameterMapName) > 0)
  227. $this->_parameterMap = $manager->getParameterMap($this->_parameterMapName);
  228. }
  229. /**
  230. * @param TSqlMapTypeHandlerRegistry type handler registry
  231. * @return ArrayAccess new instance of list class.
  232. */
  233. public function createInstanceOfListClass($registry)
  234. {
  235. if(strlen($type = $this->getListClass()) > 0)
  236. return $this->createInstanceOf($registry,$type);
  237. return array();
  238. }
  239. /**
  240. * Create a new instance of a given type.
  241. * @param TSqlMapTypeHandlerRegistry type handler registry
  242. * @param string result class name.
  243. * @param array result data.
  244. * @return mixed result object.
  245. */
  246. protected function createInstanceOf($registry,$type,$row=null)
  247. {
  248. $handler = $registry->getTypeHandler($type);
  249. if($handler!==null)
  250. return $handler->createNewInstance($row);
  251. else
  252. return $registry->createInstanceOf($type);
  253. }
  254. /**
  255. * Create a new instance of result class.
  256. * @param TSqlMapTypeHandlerRegistry type handler registry
  257. * @param array result data.
  258. * @return mixed result object.
  259. */
  260. public function createInstanceOfResultClass($registry,$row)
  261. {
  262. if(strlen($type= $this->getResultClass()) > 0)
  263. return $this->createInstanceOf($registry,$type,$row);
  264. }
  265. public function __sleep()
  266. {
  267. $cn = __CLASS__;
  268. $exprops = array("\0$cn\0_resultMap");
  269. if (!$this->_parameterMapName) $exprops[] = "\0$cn\0_parameterMapName";
  270. if (!$this->_parameterMap) $exprops[] = "\0$cn\0_parameterMap";
  271. if (!$this->_parameterClassName) $exprops[] = "\0$cn\0_parameterClassName";
  272. if (!$this->_resultMapName) $exprops[] = "\0$cn\0_resultMapName";
  273. if (!$this->_resultMap) $exprops[] = "\0$cn\0_resultMap";
  274. if (!$this->_resultClassName) $exprops[] = "\0$cn\0_resultClassName";
  275. if (!$this->_cacheModelName) $exprops[] = "\0$cn\0_cacheModelName";
  276. if (!$this->_SQL) $exprops[] = "\0$cn\0_SQL";
  277. if (!$this->_listClass) $exprops[] = "\0$cn\0_listClass";
  278. if (!$this->_typeHandler) $exprops[] = "\0$cn\0_typeHandler";
  279. if (!$this->_extendStatement) $exprops[] = "\0$cn\0_extendStatement";
  280. if (!$this->_cache) $exprops[] = "\0$cn\0_cache";
  281. return array_diff(parent::__sleep(),$exprops);
  282. }
  283. }
  284. /**
  285. * TSqlMapSelect class file.
  286. *
  287. * @author Wei Zhuo <weizho[at]gmail[dot]com>
  288. * @package System.Data.SqlMap.Statements
  289. * @since 3.1
  290. */
  291. class TSqlMapSelect extends TSqlMapStatement
  292. {
  293. private $_generate;
  294. public function getGenerate(){ return $this->_generate; }
  295. public function setGenerate($value){ $this->_generate = $value; }
  296. }
  297. /**
  298. * TSqlMapInsert class corresponds to the <insert> element.
  299. *
  300. * The <insert> element allows <selectKey> child elements that can be used
  301. * to generate a key to be used for the insert command.
  302. *
  303. * @author Wei Zhuo <weizho[at]gmail[dot]com>
  304. * @package System.Data.SqlMap.Configuration
  305. * @since 3.1
  306. */
  307. class TSqlMapInsert extends TSqlMapStatement
  308. {
  309. private $_selectKey=null;
  310. /**
  311. * @return TSqlMapSelectKey select key element.
  312. */
  313. public function getSelectKey()
  314. {
  315. return $this->_selectKey;
  316. }
  317. /**
  318. * @param TSqlMapSelectKey select key.
  319. */
  320. public function setSelectKey($value)
  321. {
  322. $this->_selectKey = $value;
  323. }
  324. }
  325. /**
  326. * TSqlMapUpdate class corresponds to <update> element.
  327. *
  328. * @author Wei Zhuo <weizho[at]gmail[dot]com>
  329. * @package System.Data.SqlMap.Configuration
  330. * @since 3.1
  331. */
  332. class TSqlMapUpdate extends TSqlMapStatement
  333. {
  334. }
  335. /**
  336. * TSqlMapDelete class corresponds to the <delete> element.
  337. *
  338. * @author Wei Zhuo <weizho[at]gmail[dot]com>
  339. * @package System.Data.SqlMap.Configuration
  340. * @since 3.1
  341. */
  342. class TSqlMapDelete extends TSqlMapUpdate
  343. {
  344. }
  345. /**
  346. * TSqlMapSelect corresponds to the <selectKey> element.
  347. *
  348. * @author Wei Zhuo <weizho[at]gmail[dot]com>
  349. * @package System.Data.SqlMap.Configuration
  350. * @since 3.1
  351. */
  352. class TSqlMapSelectKey extends TSqlMapStatement
  353. {
  354. private $_type = 'post';
  355. private $_property;
  356. /**
  357. * @return string select generated key type, 'post' or 'pre'.
  358. */
  359. public function getType()
  360. {
  361. return $this->_type;
  362. }
  363. /**
  364. * @param string select generated key type, 'post' or 'pre'.
  365. */
  366. public function setType($value)
  367. {
  368. $this->_type = strtolower($value) == 'post' ? 'post' : 'pre';
  369. }
  370. /**
  371. * @return string property name for the generated key.
  372. */
  373. public function getProperty()
  374. {
  375. return $this->_property;
  376. }
  377. /**
  378. * @param string property name for the generated key.
  379. */
  380. public function setProperty($value)
  381. {
  382. $this->_property = $value;
  383. }
  384. /**
  385. * @throws TSqlMapConfigurationException extends is unsupported.
  386. */
  387. public function setExtends($value)
  388. {
  389. throw new TSqlMapConfigurationException('sqlmap_can_not_extend_select_key');
  390. }
  391. /**
  392. * @return boolean true if key is generated after insert command, false otherwise.
  393. */
  394. public function getIsAfter()
  395. {
  396. return $this->_type == 'post';
  397. }
  398. }