cursor.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. /*
  3. Copyright (c) 2009-2017 F3::Factory/Bong Cosca, All rights reserved.
  4. This file is part of the Fat-Free Framework (http://fatfreeframework.com).
  5. This is free software: you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the Free Software
  7. Foundation, either version 3 of the License, or later.
  8. Fat-Free Framework is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with Fat-Free Framework. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. namespace DB;
  16. //! Simple cursor implementation
  17. abstract class Cursor extends \Magic implements \IteratorAggregate {
  18. //@{ Error messages
  19. const
  20. E_Field='Undefined field %s';
  21. //@}
  22. protected
  23. //! Query results
  24. $query=[],
  25. //! Current position
  26. $ptr=0,
  27. //! Event listeners
  28. $trigger=[];
  29. /**
  30. * Return database type
  31. * @return string
  32. **/
  33. abstract function dbtype();
  34. /**
  35. * Return field names
  36. * @return array
  37. **/
  38. abstract function fields();
  39. /**
  40. * Return fields of mapper object as an associative array
  41. * @return array
  42. * @param $obj object
  43. **/
  44. abstract function cast($obj=NULL);
  45. /**
  46. * Return records (array of mapper objects) that match criteria
  47. * @return array
  48. * @param $filter string|array
  49. * @param $options array
  50. * @param $ttl int
  51. **/
  52. abstract function find($filter=NULL,array $options=NULL,$ttl=0);
  53. /**
  54. * Count records that match criteria
  55. * @return int
  56. * @param $filter array
  57. * @param $options array
  58. * @param $ttl int
  59. **/
  60. abstract function count($filter=NULL,array $options=NULL,$ttl=0);
  61. /**
  62. * Insert new record
  63. * @return array
  64. **/
  65. abstract function insert();
  66. /**
  67. * Update current record
  68. * @return array
  69. **/
  70. abstract function update();
  71. /**
  72. * Hydrate mapper object using hive array variable
  73. * @return NULL
  74. * @param $var array|string
  75. * @param $func callback
  76. **/
  77. abstract function copyfrom($var,$func=NULL);
  78. /**
  79. * Populate hive array variable with mapper fields
  80. * @return NULL
  81. * @param $key string
  82. **/
  83. abstract function copyto($key);
  84. /**
  85. * Get cursor's equivalent external iterator
  86. * Causes a fatal error in PHP 5.3.5 if uncommented
  87. * return ArrayIterator
  88. **/
  89. abstract function getiterator();
  90. /**
  91. * Return TRUE if current cursor position is not mapped to any record
  92. * @return bool
  93. **/
  94. function dry() {
  95. return empty($this->query[$this->ptr]);
  96. }
  97. /**
  98. * Return first record (mapper object) that matches criteria
  99. * @return static|FALSE
  100. * @param $filter string|array
  101. * @param $options array
  102. * @param $ttl int
  103. **/
  104. function findone($filter=NULL,array $options=NULL,$ttl=0) {
  105. if (!$options)
  106. $options=[];
  107. // Override limit
  108. $options['limit']=1;
  109. return ($data=$this->find($filter,$options,$ttl))?$data[0]:FALSE;
  110. }
  111. /**
  112. * Return array containing subset of records matching criteria,
  113. * total number of records in superset, specified limit, number of
  114. * subsets available, and actual subset position
  115. * @return array
  116. * @param $pos int
  117. * @param $size int
  118. * @param $filter string|array
  119. * @param $options array
  120. * @param $ttl int
  121. * @param $bounce bool
  122. **/
  123. function paginate(
  124. $pos=0,$size=10,$filter=NULL,array $options=NULL,$ttl=0,$bounce=TRUE) {
  125. $total=$this->count($filter,$options,$ttl);
  126. $count=ceil($total/$size);
  127. if ($bounce)
  128. $pos=max(0,min($pos,$count-1));
  129. return [
  130. 'subset'=>($bounce || $pos<$count)?$this->find($filter,
  131. array_merge(
  132. $options?:[],
  133. ['limit'=>$size,'offset'=>$pos*$size]
  134. ),
  135. $ttl
  136. ):[],
  137. 'total'=>$total,
  138. 'limit'=>$size,
  139. 'count'=>$count,
  140. 'pos'=>$bounce?($pos<$count?$pos:0):$pos
  141. ];
  142. }
  143. /**
  144. * Map to first record that matches criteria
  145. * @return array|FALSE
  146. * @param $filter string|array
  147. * @param $options array
  148. * @param $ttl int
  149. **/
  150. function load($filter=NULL,array $options=NULL,$ttl=0) {
  151. $this->reset();
  152. return ($this->query=$this->find($filter,$options,$ttl)) &&
  153. $this->skip(0)?$this->query[$this->ptr]:FALSE;
  154. }
  155. /**
  156. * Return the count of records loaded
  157. * @return int
  158. **/
  159. function loaded() {
  160. return count($this->query);
  161. }
  162. /**
  163. * Map to first record in cursor
  164. * @return mixed
  165. **/
  166. function first() {
  167. return $this->skip(-$this->ptr);
  168. }
  169. /**
  170. * Map to last record in cursor
  171. * @return mixed
  172. **/
  173. function last() {
  174. return $this->skip(($ofs=count($this->query)-$this->ptr)?$ofs-1:0);
  175. }
  176. /**
  177. * Map to nth record relative to current cursor position
  178. * @return mixed
  179. * @param $ofs int
  180. **/
  181. function skip($ofs=1) {
  182. $this->ptr+=$ofs;
  183. return $this->ptr>-1 && $this->ptr<count($this->query)?
  184. $this->query[$this->ptr]:FALSE;
  185. }
  186. /**
  187. * Map next record
  188. * @return mixed
  189. **/
  190. function next() {
  191. return $this->skip();
  192. }
  193. /**
  194. * Map previous record
  195. * @return mixed
  196. **/
  197. function prev() {
  198. return $this->skip(-1);
  199. }
  200. /**
  201. * Return whether current iterator position is valid.
  202. */
  203. function valid() {
  204. return !$this->dry();
  205. }
  206. /**
  207. * Save mapped record
  208. * @return mixed
  209. **/
  210. function save() {
  211. return $this->query?$this->update():$this->insert();
  212. }
  213. /**
  214. * Delete current record
  215. * @return int|bool
  216. **/
  217. function erase() {
  218. $this->query=array_slice($this->query,0,$this->ptr,TRUE)+
  219. array_slice($this->query,$this->ptr,NULL,TRUE);
  220. $this->skip(0);
  221. }
  222. /**
  223. * Define onload trigger
  224. * @return callback
  225. * @param $func callback
  226. **/
  227. function onload($func) {
  228. return $this->trigger['load']=$func;
  229. }
  230. /**
  231. * Define beforeinsert trigger
  232. * @return callback
  233. * @param $func callback
  234. **/
  235. function beforeinsert($func) {
  236. return $this->trigger['beforeinsert']=$func;
  237. }
  238. /**
  239. * Define afterinsert trigger
  240. * @return callback
  241. * @param $func callback
  242. **/
  243. function afterinsert($func) {
  244. return $this->trigger['afterinsert']=$func;
  245. }
  246. /**
  247. * Define oninsert trigger
  248. * @return callback
  249. * @param $func callback
  250. **/
  251. function oninsert($func) {
  252. return $this->afterinsert($func);
  253. }
  254. /**
  255. * Define beforeupdate trigger
  256. * @return callback
  257. * @param $func callback
  258. **/
  259. function beforeupdate($func) {
  260. return $this->trigger['beforeupdate']=$func;
  261. }
  262. /**
  263. * Define afterupdate trigger
  264. * @return callback
  265. * @param $func callback
  266. **/
  267. function afterupdate($func) {
  268. return $this->trigger['afterupdate']=$func;
  269. }
  270. /**
  271. * Define onupdate trigger
  272. * @return callback
  273. * @param $func callback
  274. **/
  275. function onupdate($func) {
  276. return $this->afterupdate($func);
  277. }
  278. /**
  279. * Define beforesave trigger
  280. * @return callback
  281. * @param $func callback
  282. **/
  283. function beforesave($func) {
  284. $this->trigger['beforeinsert']=$func;
  285. $this->trigger['beforeupdate']=$func;
  286. return $func;
  287. }
  288. /**
  289. * Define aftersave trigger
  290. * @return callback
  291. * @param $func callback
  292. **/
  293. function aftersave($func) {
  294. $this->trigger['afterinsert']=$func;
  295. $this->trigger['afterupdate']=$func;
  296. return $func;
  297. }
  298. /**
  299. * Define onsave trigger
  300. * @return callback
  301. * @param $func callback
  302. **/
  303. function onsave($func) {
  304. return $this->aftersave($func);
  305. }
  306. /**
  307. * Define beforeerase trigger
  308. * @return callback
  309. * @param $func callback
  310. **/
  311. function beforeerase($func) {
  312. return $this->trigger['beforeerase']=$func;
  313. }
  314. /**
  315. * Define aftererase trigger
  316. * @return callback
  317. * @param $func callback
  318. **/
  319. function aftererase($func) {
  320. return $this->trigger['aftererase']=$func;
  321. }
  322. /**
  323. * Define onerase trigger
  324. * @return callback
  325. * @param $func callback
  326. **/
  327. function onerase($func) {
  328. return $this->aftererase($func);
  329. }
  330. /**
  331. * Reset cursor
  332. * @return NULL
  333. **/
  334. function reset() {
  335. $this->query=[];
  336. $this->ptr=0;
  337. }
  338. }