PageRenderTime 1082ms CodeModel.GetById 45ms RepoModel.GetById 6ms app.codeStats 0ms

/db/lib/db.php

https://github.com/plank/Seed-Framework
PHP | 265 lines | 127 code | 60 blank | 78 comment | 18 complexity | c24970d78448ad7a44ef91d9aa16e30a MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * part of the seed framework
  4. *
  5. * @author mateo murphy
  6. * @copyright mateo murphy
  7. * @license The MIT License
  8. * @package model
  9. */
  10. seed_include('library/iterator');
  11. seed_include('library/logger');
  12. /**
  13. * The date format used for database queries
  14. */
  15. define('SQL_DATE_TIME_FORMAT', 'Y-m-d H:i:s');
  16. define('SQL_DATE_FORMAT', 'Y-m-d H:i:s'); // deprecated, but we need to keep for now
  17. define('SQL_TIME_FORMAT', 'H:i:s');
  18. /**
  19. * Serves as a factory for creating db objects as well as a registry of created db objects
  20. */
  21. class DB {
  22. var $last_query;
  23. /**
  24. * Singleton function
  25. *
  26. * @param string $key The key of the db object to get
  27. * @return DB
  28. */
  29. function & get_db($key = 'default') {
  30. return DB::_db_storage($key);
  31. }
  32. /**
  33. * Creates a new db subclass of the desired type, using the passed connection settings
  34. *
  35. * @param string $type
  36. * @param string $host
  37. * @param string $user
  38. * @param string $pass
  39. * @param string $database
  40. * @return DB
  41. */
  42. function & factory($type, $host = DB_HOST, $user = DB_USER, $pass = DB_PASS, $database = DB_NAME) {
  43. $class_name = ucfirst($type).'DB';
  44. if (!class_exists($class_name)) {
  45. $file_name = dirname(__FILE__).'/adapters/'.$type.'.php';
  46. if (!file_exists($file_name)) {
  47. trigger_error("No adapter file for DB type $type", E_USER_ERROR);
  48. }
  49. require_once($file_name);
  50. }
  51. if (!class_exists($class_name)) {
  52. trigger_error("No driver for DB type $type", E_USER_ERROR);
  53. $db = false;
  54. } else {
  55. $db = new $class_name($host, $user, $pass, $database);
  56. }
  57. return $db;
  58. }
  59. /**
  60. * Registers a new db with the give connection parameters
  61. *
  62. * @param string $key
  63. * @param string $type
  64. * @param string $host
  65. * @param string $user
  66. * @param string $pass
  67. * @param string $database
  68. * @return DB
  69. */
  70. function register($key = 'default', $type = 'mysql', $host = DB_HOST, $user = DB_USER, $pass = DB_PASS, $database = DB_NAME) {
  71. $db = & DB::factory($type, $host, $user, $pass, $database);
  72. return DB::_db_storage($key, $db);
  73. }
  74. /**
  75. * Static storage for the database objects
  76. *
  77. * @param string $key
  78. * @param DB $db
  79. * @return DB
  80. */
  81. function & _db_storage($key = 'default', $db = null) {
  82. static $db_storage;
  83. if (!isset($db_storage) || $key === false) {
  84. $db_storage = array();
  85. }
  86. if (isset($db)) {
  87. $db_storage[$key] = $db;
  88. }
  89. return $db_storage[$key];
  90. }
  91. /**
  92. * Creates and executes an insert query
  93. */
  94. function insert_query($table_name, $data = null, $unescaped_data = null) {
  95. $fields = array();
  96. $values = array();
  97. if (isset($data)) {
  98. foreach($data as $field => $value) {
  99. $fields[$field] = "`".$field."`";
  100. $values[$field] = "'".$this->escape($value)."'";
  101. }
  102. }
  103. if (isset($unescaped_data)) {
  104. foreach($unescaped_data as $field => $value) {
  105. $fields[$field] = "`".$field."`";
  106. $values[$field] = $value;
  107. }
  108. }
  109. if (!count($values)) {
  110. trigger_error("No data in update_query", E_USER_WARNING);
  111. return false;
  112. }
  113. $sql = "INSERT INTO ".$this->escape_identifier($table_name)." (".implode(', ', $fields).") VALUES (".implode(', ', $values).")";
  114. return $this->query($sql);
  115. }
  116. /**
  117. * Creates and executes an update query
  118. */
  119. function update_query($table_name, $condition, $data = null, $unescaped_data = null) {
  120. $values = array();
  121. if (isset($data)) {
  122. foreach($data as $field => $value) {
  123. $values[$field] = "`".$field."` = '".$this->escape($value)."'";
  124. }
  125. }
  126. if (isset($unescaped_data)) {
  127. foreach($unescaped_data as $field => $value) {
  128. $values[$field] = "`".$field."` = ".$value;
  129. }
  130. }
  131. if (!count($values)) {
  132. trigger_error("No data in update_query", E_USER_WARNING);
  133. return false;
  134. }
  135. $sql = "UPDATE ".$this->escape_identifier($table_name)." SET ".implode(', ',$values)." WHERE ".$condition;
  136. return $this->query($sql);
  137. }
  138. function escape($string) {
  139. return "'".$string."'";
  140. }
  141. function escape_identifier($string) {
  142. return '"'.$string.'"';
  143. }
  144. // Abstract methods
  145. /**
  146. * Executes a given query, then returns the result resource
  147. *
  148. * @param string $sql
  149. * @return resource
  150. */
  151. function query($sql) {
  152. trigger_error('Abstract method', E_USER_ERROR);
  153. }
  154. function explain($sql) {
  155. trigger_error('Abstract method', E_USER_ERROR);
  156. }
  157. function query_iterator($sql) {
  158. trigger_error('Abstract method', E_USER_ERROR);
  159. }
  160. /**
  161. * Executes a query and returns a single value
  162. *
  163. * @param string $sql
  164. * @return string
  165. */
  166. function query_value($sql) {
  167. trigger_error('Abstract method', E_USER_ERROR);
  168. }
  169. /**
  170. * Executes a query and returns the result as an array.
  171. *
  172. * @param string $sql
  173. * @return array
  174. */
  175. function query_array($sql, $primary_key = null) {
  176. trigger_error('Abstract method', E_USER_ERROR);
  177. }
  178. function query_single($sql) {
  179. trigger_error('Abstract method', E_USER_ERROR);
  180. }
  181. /**
  182. * Returns the last insert id
  183. *
  184. * @return int
  185. */
  186. function insert_id() {
  187. trigger_error('Abstract method', E_USER_ERROR);
  188. }
  189. function limit_offset($limit = 0, $offset = 0) {
  190. }
  191. function sanitize_sql($sql) {
  192. if (!is_array($sql)) return $sql;
  193. $string = array_shift($sql);
  194. for($x = 0; $x < count($sql); $x++) {
  195. if (!is_numeric($sql[$x])) {
  196. $sql[$x] = "'".$this->escape($sql[$x])."'";
  197. } elseif (is_integer($sql[$x])) {
  198. $sql[$x] = intval($sql[$x]);
  199. } elseif (is_float($sql[$x])) {
  200. $sql[$x] = floatval($sql[$x]);
  201. }
  202. }
  203. return vsprintf(str_replace('?', '%s', $string), $sql);
  204. }
  205. }