PageRenderTime 99ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/oc-includes/osclass/classes/DAO.php

https://code.google.com/
PHP | 265 lines | 165 code | 33 blank | 67 comment | 15 complexity | efeb4848bd92fa9730bd01a6e336a3b5 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php if ( ! defined('ABS_PATH')) exit('ABS_PATH is not loaded. Direct access is not allowed.');
  2. /*
  3. * OSCLass – software for creating and publishing online classified
  4. * advertising platforms
  5. *
  6. * Copyright (C) 2010 OSCLASS
  7. *
  8. * This program is free software: you can redistribute it and/or
  9. * modify it under the terms of the GNU Affero General Public License
  10. * as published by the Free Software Foundation, either version 3 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. define('DB_FUNC_NOW', 'NOW()');
  22. define('DB_CONST_TRUE', 'TRUE');
  23. define('DB_CONST_FALSE', 'FALSE');
  24. define('DB_CONST_NULL', 'NULL');
  25. define('DB_CUSTOM_COND', 'DB_CUSTOM_COND');
  26. /**
  27. * This is a simple DAO implementation just to use it
  28. * on the OSClass project.
  29. *
  30. * @author OSClass
  31. */
  32. abstract class DAO {
  33. protected $conn ;
  34. protected $metadata_conn ;
  35. /**
  36. * Make a new instance of the DAO from its name.
  37. */
  38. public static function load($entityName)
  39. {
  40. if(class_exists($entityName)) {
  41. return new $entityName;
  42. } else {
  43. return null;
  44. }
  45. }
  46. public function __construct() {
  47. $this->conn = getConnection(osc_db_host(), osc_db_user(), osc_db_password(), osc_db_name(), DEBUG_LEVEL) ;
  48. }
  49. public function createMetadataConnection() {
  50. $this->metadata_conn = getConnection(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, DEBUG_LEVEL) ;
  51. }
  52. public function getConnection() {
  53. return $this->conn ;
  54. }
  55. public function getMetadataConnection() {
  56. return $this->metadata_conn ;
  57. }
  58. /*
  59. * @return Array returns 0 if 'asc' or 1 if 'desc'
  60. */
  61. public static function getAllowedTypesForSorting() {
  62. return ( array (0 => 'asc', 1 => 'desc') ) ;
  63. }
  64. /**
  65. * Formats a value before being inserted in DB.
  66. */
  67. public function formatValue($value) {
  68. if(is_null($value)) return DB_CONST_NULL;
  69. else $value = trim($value);
  70. switch($value) {
  71. case DB_FUNC_NOW:
  72. case DB_CONST_TRUE:
  73. case DB_CONST_FALSE:
  74. case DB_CONST_NULL:
  75. break;
  76. default:
  77. $value = '\'' . addslashes($value) . '\'' ;
  78. break;
  79. }
  80. return $value;
  81. }
  82. /**
  83. * @return the number of rows mathing the conditions passed by parameter.
  84. */
  85. public function exists($conditions) {
  86. $where = array();
  87. foreach($conditions as $key => $value) {
  88. if($key == DB_CUSTOM_COND)
  89. $where[] = $value;
  90. else
  91. $where[] = $key . ' = ' . $this->formatValue($value);
  92. }
  93. $where = implode(' AND ', $where);
  94. return $this->conn->osc_dbFetchValue("SELECT COUNT(*) FROM %s WHERE " . $where, $this->getTableName());
  95. }
  96. /**
  97. * @return the number of rows mathing the conditions passed by parameter.
  98. */
  99. public function findByConditions($conditions) {
  100. $where = array();
  101. foreach($conditions as $key => $value) {
  102. if($key == DB_CUSTOM_COND)
  103. $where[] = $value;
  104. else
  105. $where[] = $key . ' = ' . $this->formatValue($value);
  106. }
  107. $where = implode(' AND ', $where);
  108. return $this->conn->osc_dbFetchResult("SELECT * FROM %s WHERE " . $where, $this->getTableName());
  109. }
  110. /**
  111. * @return a row with the same id passed by parameter.
  112. */
  113. public function findByPrimaryKey($pk) {
  114. return $this->conn->osc_dbFetchResult("SELECT * FROM %s WHERE %s = '%s'",
  115. $this->getTableName(), $this->getPrimaryKey(), $pk
  116. );
  117. }
  118. /**
  119. * @return only for metadata.
  120. */
  121. public function findByPrimaryKeyInMetadataDB($pk) {
  122. return $this->metadata_conn->osc_dbFetchResult("SELECT * FROM %s WHERE s_site like '%%%s%%'",
  123. $this->getTableName(), $pk
  124. );
  125. }
  126. /**
  127. * Deletes a row with the id passed by parameter.
  128. */
  129. public function deleteByPrimaryKey($id) {
  130. return $this->delete(array('pk_i_id' => $id));
  131. }
  132. /**
  133. * Deletes the rows matching the conditions passed by parameter.
  134. */
  135. public function delete($conditions) {
  136. $where = array();
  137. foreach($conditions as $key => $value) {
  138. if($key == DB_CUSTOM_COND)
  139. $where[] = $value;
  140. else
  141. $where[] = $key . ' = ' . $this->formatValue($value);
  142. }
  143. $where = implode(' AND ', $where);
  144. $this->conn->osc_dbExec('DELETE FROM %s WHERE ' . $where, $this->getTableName());
  145. return $this->conn->get_affected_rows();
  146. }
  147. /**
  148. * @return int the number of rows in the table represented by this object.
  149. */
  150. public function count() {
  151. $result = $this->conn->osc_dbFetchResult('SELECT COUNT(*) AS count FROM %s', $this->getTableName());
  152. return $result['count'];
  153. }
  154. /**
  155. * @return array with all the rows in this table.
  156. */
  157. public function listAll() {
  158. return $this->conn->osc_dbFetchResults('SELECT * FROM %s', $this->getTableName());
  159. }
  160. /**
  161. * Updates rows in the table when matching the conditions in the second parameter.
  162. */
  163. public function update($fields, $conditions = null) {
  164. foreach($fields as $key => &$value)
  165. $value = $key . ' = ' . $this->formatValue($value);
  166. unset($value);
  167. $set = implode(', ', $fields);
  168. $where = '';
  169. if(!is_null($conditions)) {
  170. foreach($conditions as $key => &$value) {
  171. if($key != DB_CUSTOM_COND)
  172. $value = $key . ' = ' . $this->formatValue($value);
  173. }
  174. unset($value);
  175. $where = ' WHERE ' . implode(' AND ', $conditions);
  176. }
  177. $sql = 'UPDATE ' . $this->getTableName() . ' SET ' . $set . $where;
  178. if(defined('DEBUG')) {
  179. trigger_error($sql) ;
  180. }
  181. $this->conn->osc_dbExec($sql) ;
  182. return $this->conn->get_affected_rows();
  183. }
  184. public function insert($fields, $aFieldsDescription = null) {
  185. $columns = implode(', ', array_keys($fields));
  186. foreach($fields as &$value)
  187. $value = $this->formatValue($value);
  188. unset($value);
  189. $values = implode(', ', $fields);
  190. $sql = 'INSERT INTO ' . $this->getTableName() . ' (' . $columns . ') VALUES (' . $values . ')';
  191. return $this->conn->osc_dbExec($sql);
  192. }
  193. public function listWhere() {
  194. $argv = func_get_args();
  195. $sql = null;
  196. switch(func_num_args()) {
  197. case 0: return array(); break;
  198. case 1: $sql = $argv[0]; break;
  199. default:
  200. $args = func_get_args();
  201. $format = array_shift($args);
  202. $sql = vsprintf($format, $args);
  203. break;
  204. }
  205. return $this->conn->osc_dbFetchResults('SELECT * FROM %s WHERE %s', $this->getTableName(), $sql);
  206. }
  207. public function listWhereCount() {
  208. $argv = func_get_args();
  209. $sql = null;
  210. switch(func_num_args()) {
  211. case 0: return array(); break;
  212. case 1: $sql = $argv[0]; break;
  213. default:
  214. $args = func_get_args();
  215. $format = array_shift($args);
  216. $sql = vsprintf($format, $args);
  217. break;
  218. }
  219. return $this->conn->osc_dbFetchResults('SELECT COUNT(*) as count FROM %s WHERE %s', $this->getTableName(), $sql);
  220. }
  221. /**
  222. * @return string with the name of the primary key represented by this class.
  223. */
  224. public function getPrimaryKey() {
  225. return 'pk_i_id';
  226. }
  227. /**
  228. * @return string with the name of the table represented by this class.
  229. */
  230. abstract public function getTableName();
  231. }
  232. ?>