PageRenderTime 28ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/system/components/dbtools/branch/activeRecord/activeRecord.php

https://github.com/melechi/Simple-Core
PHP | 398 lines | 347 code | 32 blank | 19 comment | 30 complexity | ea94985ce5b869e2ad2e2bd09cd694dc MD5 | raw file
  1. <?php
  2. class dbtools_activeRecord_activeRecord extends overloader implements SeekableIterator
  3. {
  4. private $database ='';
  5. private $name ='';
  6. private $prefix ='';
  7. private $columns =array();
  8. private $columnNames =array();
  9. private $unprefixedColumnNames =array();
  10. private $idColumn =false;
  11. private $_records =null;
  12. private $__pointer =0;
  13. private $_gotLength =false;
  14. // public $length =0;
  15. // private $row=array();
  16. public function __construct(dbtools_activeRecord $parent)
  17. {
  18. parent::__construct($parent);
  19. $this->_records=new dbtools_activeRecord_recordSet($this);
  20. }
  21. public function __get($theVar=null)
  22. {
  23. //Check if defined.
  24. if ($theVar=='length')
  25. {
  26. if ($this->_gotLength)
  27. {
  28. return $this->length;
  29. }
  30. else
  31. {
  32. return $this->updateLength();
  33. }
  34. }
  35. else
  36. {
  37. return parent::__get($theVar);
  38. }
  39. }
  40. public function current()
  41. {
  42. return ($this->length)?$this->_records[$this->_pointer]:null;
  43. }
  44. public function key()
  45. {
  46. return $this->_pointer;
  47. }
  48. public function next()
  49. {
  50. ++$this->_pointer;
  51. if ($this->valid())
  52. {
  53. return $this->current();
  54. }
  55. return null;
  56. }
  57. public function back()
  58. {
  59. if ($this->_pointer!=0)--$this->_pointer;
  60. return $this->current();
  61. }
  62. public function rewind()
  63. {
  64. $this->_pointer=0;
  65. return $this->current();
  66. }
  67. public function valid()
  68. {
  69. return $this->_pointer<count($this->_records);
  70. }
  71. public function seek($position)
  72. {
  73. if (isset($this->_records[$position]))
  74. {
  75. $this->_pointer=$position;
  76. return $this->current();
  77. }
  78. return null;
  79. }
  80. private function fillNewRecordValues($values=array())
  81. {
  82. foreach ($values as $key=>&$val)
  83. {
  84. if (!in_array($key,$this->columnNames) && in_array($key,$this->unprefixedColumnNames))
  85. {
  86. $tmp=$val;
  87. unset($values[$key]);
  88. $values[$this->prefix.'_'.$key]=$tmp;
  89. }
  90. }
  91. foreach ($this->columns as $name=>&$def)
  92. {
  93. if (!isset($values[$name]))
  94. {
  95. $values[$name]=null;
  96. }
  97. }
  98. return $values;
  99. }
  100. public function newRecord($values=array())
  101. {
  102. if (count($values))
  103. {
  104. return new dbtools_activeRecord_record($this,$this->fillNewRecordValues($values),true);
  105. }
  106. else
  107. {
  108. return new dbtools_activeRecord_record($this,$this->fillNewRecordValues(),true);
  109. }
  110. }
  111. public function setName($name)
  112. {
  113. $this->name=$name;
  114. return $this;
  115. }
  116. public function getName()
  117. {
  118. return $this->name;
  119. }
  120. public function setDatabase($database)
  121. {
  122. $this->database=$database;
  123. return $this;
  124. }
  125. public function getDatabase()
  126. {
  127. return $this->database;
  128. }
  129. public function setPrefix($prefix)
  130. {
  131. $this->prefix=$prefix;
  132. return $this;
  133. }
  134. public function getPrefix()
  135. {
  136. return $this->prefix;
  137. }
  138. public function registerColumn($name,$type,$length=0,$primaryKey=false,$id=false)
  139. {
  140. $this->columns[$name]=array
  141. (
  142. 'name' =>$name,
  143. 'type' =>$type,
  144. 'length' =>$length,
  145. 'primaryKey'=>$primaryKey,
  146. 'id' =>$id
  147. );
  148. array_push($this->columnNames,$name);
  149. array_push($this->unprefixedColumnNames,str_replace($this->prefix.'_','',$name));
  150. if ($id)
  151. {
  152. $this->idColumn=$this->columns[$name];
  153. }
  154. return $this;
  155. }
  156. public function id($id)
  157. {
  158. if (!$this->idColumn)
  159. {
  160. return new dbtools_activeRecord_recordSet($this);
  161. }
  162. else
  163. {
  164. $result=array();
  165. if (!is_array($id))
  166. {
  167. if (isset($this->_records[$id]))
  168. {
  169. return $this->_records[$id];
  170. }
  171. else
  172. {
  173. $this->component->database->c('core')
  174. ->query('SELECT * FROM [PREFIX]'.$this->name.' WHERE '.$this->idColumn['name'].'='.$id.' LIMIT 1;');
  175. $thisResult=$this->component->database->result();
  176. if (count($thisResult))
  177. {
  178. array_push($result,$thisResult);
  179. }
  180. else
  181. {
  182. array_push($result,$thisResult);
  183. }
  184. }
  185. }
  186. else
  187. {
  188. $this->component->database->c('core');
  189. for ($i=0,$j=count($id); $i<$j; $i++)
  190. {
  191. if ($this->_records->is_set($id[$i]))
  192. {
  193. array_push($result,$this->_records->get($id[$i]));
  194. }
  195. else
  196. {
  197. $this->component->database->query('SELECT * FROM [PREFIX]'.$this->name.' WHERE '.$this->idColumn['name'].'='.$id[$i].' LIMIT 1;');
  198. $thisResult=$this->component->database->result();
  199. if (count($thisResult))
  200. {
  201. array_push($result,$thisResult);
  202. }
  203. else
  204. {
  205. array_push($result,$thisResult);
  206. }
  207. }
  208. }
  209. }
  210. $recordSet=new dbtools_activeRecord_recordSet($this);
  211. for ($i=0,$j=count($result); $i<$j; $i++)
  212. {
  213. // if (is_null($result[$i]))
  214. // {
  215. // array_push($return,null);
  216. // continue;
  217. // }
  218. if (!$this->isRecord($result[$i]))
  219. {
  220. $thisRecord=new dbtools_activeRecord_record($this,$result[$i]);
  221. $this->_records->set($result[$i][$this->idColumn['name']],$thisRecord);
  222. $recordSet->push($thisRecord);
  223. }
  224. else
  225. {
  226. $recordSet->push($result[$i]);
  227. }
  228. }
  229. return $recordSet;
  230. }
  231. }
  232. // public function select($keys='*',$whereKey=false,$whereOperator=false,$value=false)
  233. // {
  234. // $query=new dbtools_activeRecord_selection($this);
  235. //
  236. // }
  237. public function quickSelect()
  238. {
  239. $numArgs=func_num_args();
  240. if (!$numArgs)
  241. {
  242. $this->component->database->query('SELECT * FROM [PREFIX]'.$this->name.';');
  243. $result=$this->component->database->result();
  244. $recordSet=new dbtools_activeRecord_recordSet($this);
  245. for ($i=0,$j=count($result); $i<$j; $i++)
  246. {
  247. if (!$this->isRecord($result[$i]))
  248. {
  249. $thisRecord=new dbtools_activeRecord_record($this,$result[$i]);
  250. $this->_records->set($result[$i][$this->idColumn['name']],$thisRecord);
  251. $recordSet->push($thisRecord);
  252. }
  253. else
  254. {
  255. $recordSet->push($result[$i]);
  256. }
  257. }
  258. }
  259. else
  260. {
  261. $selection=$this->newSelection();
  262. $args=func_get_args();
  263. for ($i=0; $i<$numArgs; $i++)
  264. {
  265. $selection->filter($args[$i]);
  266. }
  267. $recordSet=$selection->execute();
  268. }
  269. return $recordSet;
  270. }
  271. public function commit(dbtools_activeRecord_record $record)
  272. {
  273. if (!$record->isNewRecord())
  274. {
  275. $update=array();
  276. $query='UPDATE [PREFIX]'.$this->name.' SET ';
  277. foreach ($record->getValues() as $key=>$val)
  278. {
  279. if ($key==$this->idColumn['name'])continue;
  280. $update[].="$key='$val'";
  281. }
  282. $query.=@implode(', ',$update).' WHERE '.$this->idColumn['name'].'='.$record->{$this->idColumn['name']}.';';
  283. if ($this->component->database->c('core')->query($query))
  284. {
  285. return true;
  286. }
  287. else
  288. {
  289. //Error
  290. }
  291. }
  292. else
  293. {
  294. $values=$record->getValues();
  295. $query='INSERT INTO [PREFIX]'.$this->name.' ('
  296. .implode(array_keys($values),',')
  297. .')VALUES('
  298. .'"'.implode(array_values($values),'","').'");';
  299. if ($this->component->database->c('core')->query($query))
  300. {
  301. $id=$this->component->database->lastInsertID();
  302. $record->{$this->idColumn['name']}=$id;
  303. $this->_records[$id]=$record;
  304. return true;
  305. }
  306. else
  307. {
  308. //Error
  309. }
  310. }
  311. }
  312. public function delete(dbtools_activeRecord_record $record)
  313. {
  314. if (!$record->isNewRecord())
  315. {
  316. $this->component->database->c('core')->query('DELETE FROM [PREFIX]'.$this->name.' WHERE '.$this->idColumn['name'].'='.$record->{$this->idColumn['name']}.' LIMIT 1;');
  317. unset($this->_records[$record->{$this->idColumn['name']}]);
  318. }
  319. return true;
  320. }
  321. // public function forget(dbtools_activeRecord_record $record)
  322. // {
  323. // unset($record);
  324. // }
  325. public function isRecord($object)
  326. {
  327. return ($object instanceof dbtools_activeRecord_record);
  328. }
  329. public function getIDColumn()
  330. {
  331. return $this->idColumn;
  332. }
  333. public function getColumnNames($prefixed=true,$both=false)
  334. {
  335. if ($prefixed)
  336. {
  337. if (!$both)
  338. {
  339. return $this->columnNames;
  340. }
  341. else
  342. {
  343. return array_merge($this->columnNames,$this->unprefixedColumnNames);
  344. }
  345. }
  346. else
  347. {
  348. return $this->unprefixedColumnNames;
  349. }
  350. }
  351. public function getCachedRecords()
  352. {
  353. return $this->_records;
  354. }
  355. private function updateLength()
  356. {
  357. $this->component->database->c('core')->query('SELECT COUNT(*) as length FROM [PREFIX]'.$this->name.' LIMIT 1;');
  358. $this->length=$this->component->database->result('length');
  359. return $this->length;
  360. }
  361. public function newSelection()
  362. {
  363. return new dbtools_activeRecord_selection($this);
  364. }
  365. }
  366. ?>