Source for file DAO.php

Documentation is available at DAO.php

  1. <?
  2. /**
  3. * Data Access Object which inserts, updates and deletes thru every possible model
  4. *
  5. * @author Hasin Hayder
  6. * @since 13th April, 2006
  7. * @package Zephyr
  8. * @version Beta 2.0
  9. * @copyright LGPL
  10. */
  11.  
  12. class DAO
  13. {
  14. /**
  15. * @var object
  16. * @access private
  17. * @since PR 1.0
  18. */
  19. private $model;
  20. /**
  21. * @var string
  22. * @access private
  23. * @since PR 1.0
  24. */
  25. private $dbinfo;
  26. /**
  27. * @var string
  28. * @access private
  29. * @since PR 1.0
  30. */
  31. private $dbtype;
  32.  
  33. /**
  34. * PHP5 Constructor
  35. * function constructor
  36. * load the model class and set it internally for reflection
  37. *
  38. * @param object $model
  39. * @return data access object
  40. * @access public
  41. * @since PR 1.0
  42. */
  43. public function __construct(&$model=null, $dbinfo_class_name="dbinfo")
  44. {
  45. $model_name = get_class($model);
  46. //include_once("data_models/{$model_name}.php");
  47. $this->model = &$model;
  48. //set the datasource class name
  49. $this->dbinfo = $dbinfo_class_name;
  50. $pm = new packagemanager();
  51. include_once("../packages/{$pm->get_base_path()}/helper/{$dbinfo_class_name}.class.php");
  52. $__dbinfo = new $dbinfo_class_name(); //dbinfo();
  53. $this->dbtype = $__dbinfo->get_dbtype();
  54.  
  55. }
  56.  
  57. /**
  58. * function insert()
  59. * this reflects the available properties in the class and iterates thru them
  60. * then creates the insert query
  61. * and then execute it via DataBaseConnector
  62. *
  63. * @param void
  64. * @return string
  65. * @access public
  66. * @since PR 1.0
  67. */
  68. public function insert(){
  69.  
  70. //get the reflection class
  71. $cls = new ReflectionClass($this->model);
  72. $props = $cls->getProperties();
  73. while ($i < count($props))
  74. {
  75. $prop_name = $props[(int) $i]->getName();
  76. if ($this->dbtype!="sqlite")
  77. $field_name .= "`".$prop_name. "`, " ;
  78. else
  79. $field_name .= "".$prop_name. ", " ;
  80. $field_value .= "'". addslashes($this->model->$prop_name) ."'". ", ";
  81. $i++;
  82. }
  83. $field_name = substr($field_name, 0, -2);
  84. $field_value = substr($field_value, 0 , -2);
  85. if ($this->dbtype=="sqlite")
  86. $insert_query = "insert into ".get_class($this->model)." ({$field_name}) values ({$field_value})";
  87. else
  88. $insert_query = "insert into `".get_class($this->model)."` ({$field_name}) values ({$field_value})";
  89. try{
  90. $result = DatabaseConnector::new_instance($this->dbinfo)->execute($insert_query);
  91. }
  92. catch (Exception $db_exception)
  93. {
  94. if ($db_exception->getCode()==DB_CONNECTION_FAILURE) die("Cannot connect to database. Please check your dsn");
  95. $error_message = DatabaseConnector::new_instance($this->dbinfo)->ErrorMsg();
  96. $lg->log($error_message, "DAO.php : Try");
  97. }
  98. if ($result===false)
  99. {
  100. $error_message = DatabaseConnector::new_instance($this->dbinfo)->ErrorMsg();
  101. throw new exception("Cannot insert data, there is an error. The error message is <font color='red'>{$error_message}</font>");
  102. }
  103. $this->flush(); //flush the model to avoid scope of data repeating
  104. return $insert_query;
  105.  
  106. }
  107.  
  108. /**
  109. * function update()
  110. * this reflects the available properties in the class and iterates thru them
  111. * then creates the update query, only non-empty values
  112. * and then execute it via DataBaseConnector
  113. *
  114. * @param string $clause
  115. * @return string
  116. * @access public
  117. * @since PR 1.0
  118. */
  119. public function update($clause){
  120.  
  121. //get the reflection class
  122. $cls = new ReflectionClass($this->model);
  123. $props = $cls->getProperties();
  124. while ($i < count($props))
  125. {
  126. $prop_name = $props[(int) $i]->getName();
  127. $field_name = $prop_name ;
  128. $field_value = $this->model->$prop_name;
  129. if (!empty($field_value))
  130. {
  131. $update_string .= " {$field_name} = '{$field_value}',";
  132. }
  133. $i++;
  134. }
  135. $update_string = substr($update_string, 0 , -1);
  136. $update_query .= "update ".get_class($this->model)." set {$update_string} where {$clause}";
  137. try{
  138. $result=DatabaseConnector::new_instance($this->dbinfo)->execute($update_query);
  139. }
  140. catch (Exception $db_exception)
  141. {
  142. if ($db_exception->getCode()==DB_CONNECTION_FAILURE) die("Cannot connect to database. Please check your dsn");
  143. }
  144. if ($result===false)
  145. {
  146. $error_message = DatabaseConnector::new_instance($this->dbinfo)->ErrorMsg();
  147. throw new exception("Cannot update data, there is an error. The error message is <font color='red'>{$error_message}</font>");
  148.  
  149. }
  150.  
  151. // flush the model to avoid scope of data repeating
  152. $this->flush();
  153. return $update_query;
  154.  
  155. }
  156.  
  157. /**
  158. * just makes every variable of the model to empty
  159. *
  160. * @return void
  161. * @access private
  162. * @since PR 1.0
  163. */
  164. private function flush()
  165. {
  166. while ($i < count($props))
  167. {
  168. $prop_name = $props[(int) $i]->getName();
  169. $field_name = $prop_name ;
  170. $this->model->$prop_name = ""; //flushed
  171. $i++;
  172. }
  173. }
  174.  
  175. /**
  176. * function delete()
  177. * just delete the record based on clause
  178. *
  179. * @param string $clause
  180. * @return void
  181. * @access public
  182. * @since PR 1.0
  183. */
  184.  
  185. public function delete($clause)
  186. {
  187. $delete_query = "delete from ".get_class($this->model)." where {$clause}";
  188. try{
  189. $result = DatabaseConnector::new_instance($this->dbinfo)->execute($delete_query);
  190. }
  191. catch (Exception $db_exception)
  192. {
  193. if ($db_exception->getCode()==DB_CONNECTION_FAILURE) die("Cannot connect to database. Please check your dsn");
  194. }
  195. return $delete_query;
  196. }
  197.  
  198. /**
  199. * function selectBySQL()
  200. * this function executes a query and return teh whole rowset in a array
  201. * for best performance use LIMIT keyword
  202. *
  203. * @param string $query
  204. * @return array
  205. * @access public
  206. * @since PR 1.0
  207. */
  208. public function selectBySQL($query)
  209. {
  210. $data_store = array();
  211. $select_query = $query;
  212. try{
  213. $result = DatabaseConnector::new_instance($this->dbinfo)->execute($select_query);
  214. }
  215. catch (Exception $db_exception)
  216. {
  217. if ($db_exception->getCode()==DB_CONNECTION_FAILURE) die("Cannot connect to database. Please check your dsn");
  218. }
  219. if ($result===false)
  220. {
  221. $error_message = DatabaseConnector::new_instance($this->dbinfo)->ErrorMsg();
  222. throw new exception("Cannot load data, there is an error. The error message is <font color='red'>{$error_message}</font>");
  223. }
  224. //populate the array with data
  225. while ($row = $result->fetchRow())
  226. {
  227. $data_store[] = $row;
  228. }
  229. return $data_store; //return the array
  230.  
  231. }
  232.  
  233. /**
  234. * just executes a query
  235. * @since Beta2
  236. * @access public
  237. * @param string
  238. * @return void
  239. */
  240. public function execute($query)
  241. {
  242. DatabaseConnector::new_instance($this->dbinfo)->execute($query);
  243. }
  244.  
  245. /**
  246. * this function call any aggregate function and return teh result
  247. *
  248. * @since preview release 6.00
  249. * @param string function name
  250. * @param array column names
  251. * @param table name
  252. * @param string where clause
  253. * @return string
  254. */
  255. public function aggregator($function, $columns, $table, $condition=null)
  256. {
  257. $where_clause = empty($condition)? null: "where {$clause}";
  258. $my_columns = implode(",", $columns);
  259. $query = "select {$function}({$my_columns}) from {$table} {$where_clause}";
  260. $result = $this->selectBySQL($query);
  261. return $result[0][0];
  262. }
  263.  
  264. }
  265. ?>

Documentation generated on Sat, 15 Apr 2006 22:10:27 +0600 by phpDocumentor 1.3.0RC3