/fuel/core/classes/db.php

https://bitbucket.org/sriedel/iccrm-wip · PHP · 379 lines · 126 code · 31 blank · 222 comment · 3 complexity · dac5de8a3864d3951a77a129ce475d5b MD5 · raw file

  1. <?php
  2. /**
  3. * Database object creation helper methods.
  4. *
  5. * @package Fuel/Database
  6. * @category Base
  7. * @author Kohana Team
  8. * @copyright (c) 2009 Kohana Team
  9. * @license http://kohanaphp.com/license
  10. */
  11. namespace Fuel\Core;
  12. class DB
  13. {
  14. // Query types
  15. const SELECT = 1;
  16. const INSERT = 2;
  17. const UPDATE = 3;
  18. const DELETE = 4;
  19. public static $query_count = 0;
  20. /**
  21. * Create a new [Database_Query] of the given type.
  22. *
  23. * // Create a new SELECT query
  24. * $query = DB::query('SELECT * FROM users');
  25. *
  26. * // Create a new DELETE query
  27. * $query = DB::query('DELETE FROM users WHERE id = 5');
  28. *
  29. * Specifying the type changes the returned result. When using
  30. * `DB::SELECT`, a [Database_Query_Result] will be returned.
  31. * `DB::INSERT` queries will return the insert id and number of rows.
  32. * For all other queries, the number of affected rows is returned.
  33. *
  34. * @param integer type: DB::SELECT, DB::UPDATE, etc
  35. * @param string SQL statement
  36. * @return Database_Query
  37. */
  38. public static function query($sql, $type = null)
  39. {
  40. return new \Database_Query($sql, $type);
  41. }
  42. /*
  43. * Returns the last query
  44. *
  45. * @return string the last query
  46. */
  47. public static function last_query($db = null)
  48. {
  49. return \Database_Connection::instance($db)->last_query;
  50. }
  51. /**
  52. * Create a new [Database_Query_Builder_Select]. Each argument will be
  53. * treated as a column. To generate a `foo AS bar` alias, use an array.
  54. *
  55. * // SELECT id, username
  56. * $query = DB::select('id', 'username');
  57. *
  58. * // SELECT id AS user_id
  59. * $query = DB::select(array('id', 'user_id'));
  60. *
  61. * @param mixed column name or array($column, $alias) or object
  62. * @param ...
  63. * @return Database_Query_Builder_Select
  64. */
  65. public static function select($columns = NULL)
  66. {
  67. return new \Database_Query_Builder_Select(func_get_args());
  68. }
  69. /**
  70. * Create a new [Database_Query_Builder_Select] from an array of columns.
  71. *
  72. * // SELECT id, username
  73. * $query = DB::select_array(array('id', 'username'));
  74. *
  75. * @param array columns to select
  76. * @return Database_Query_Builder_Select
  77. */
  78. public static function select_array(array $columns = NULL)
  79. {
  80. return new \Database_Query_Builder_Select($columns);
  81. }
  82. /**
  83. * Create a new [Database_Query_Builder_Insert].
  84. *
  85. * // INSERT INTO users (id, username)
  86. * $query = DB::insert('users', array('id', 'username'));
  87. *
  88. * @param string table to insert into
  89. * @param array list of column names or array($column, $alias) or object
  90. * @return Database_Query_Builder_Insert
  91. */
  92. public static function insert($table = NULL, array $columns = NULL)
  93. {
  94. return new \Database_Query_Builder_Insert($table, $columns);
  95. }
  96. /**
  97. * Create a new [Database_Query_Builder_Update].
  98. *
  99. * // UPDATE users
  100. * $query = DB::update('users');
  101. *
  102. * @param string table to update
  103. * @return Database_Query_Builder_Update
  104. */
  105. public static function update($table = NULL)
  106. {
  107. return new \Database_Query_Builder_Update($table);
  108. }
  109. /**
  110. * Create a new [Database_Query_Builder_Delete].
  111. *
  112. * // DELETE FROM users
  113. * $query = DB::delete('users');
  114. *
  115. * @param string table to delete from
  116. * @return Database_Query_Builder_Delete
  117. */
  118. public static function delete($table = NULL)
  119. {
  120. return new \Database_Query_Builder_Delete($table);
  121. }
  122. /**
  123. * Create a new [Database_Expression] which is not escaped. An expression
  124. * is the only way to use SQL functions within query builders.
  125. *
  126. * $expression = DB::expr('COUNT(users.id)');
  127. *
  128. * @param string expression
  129. * @return Database_Expression
  130. */
  131. public static function expr($string)
  132. {
  133. return new \Database_Expression($string);
  134. }
  135. /**
  136. * Quote a value for an SQL query.
  137. *
  138. * @param string $string the string to quote
  139. * @param string $db the database connection to use
  140. * @return string the quoted value
  141. */
  142. public static function quote($string, $db = null)
  143. {
  144. if (is_array($string))
  145. {
  146. foreach ($string as $k => $s)
  147. {
  148. $string[$k] = static::quote($s, $db);
  149. }
  150. return $string;
  151. }
  152. return \Database_Connection::instance($db)->quote($string);
  153. }
  154. /**
  155. * Quotes an identifier so it is ready to use in a query.
  156. *
  157. * @param string $string the string to quote
  158. * @param string $db the database connection to use
  159. * @return string the quoted identifier
  160. */
  161. public static function quote_identifier($string, $db = null)
  162. {
  163. if (is_array($string))
  164. {
  165. foreach ($string as $k => $s)
  166. {
  167. $string[$k] = static::quote_identifier($s, $db);
  168. }
  169. return $string;
  170. }
  171. return \Database_Connection::instance($db)->quote_identifier($string);
  172. }
  173. /**
  174. * Quote a database table name and adds the table prefix if needed.
  175. *
  176. * @param string $string the string to quote
  177. * @param string $db the database connection to use
  178. * @return string the quoted identifier
  179. */
  180. public static function quote_table($string, $db = null)
  181. {
  182. if (is_array($string))
  183. {
  184. foreach ($string as $k => $s)
  185. {
  186. $string[$k] = static::quote_table($s, $db);
  187. }
  188. return $string;
  189. }
  190. return \Database_Connection::instance($db)->quote_table($string);
  191. }
  192. /**
  193. * Escapes a string to be ready for use in a sql query
  194. *
  195. * @param string $string the string to escape
  196. * @param string $db the database connection to use
  197. * @return string the escaped string
  198. */
  199. public static function escape($string, $db = null)
  200. {
  201. return \Database_Connection::instance($db)->escape($string);
  202. }
  203. /**
  204. * If a table name is given it will return the table name with the configured
  205. * prefix. If not, then just the prefix is returned
  206. *
  207. * @param string $table the table name to prefix
  208. * @param string $db the database connection to use
  209. * @return string the prefixed table name or the prefix
  210. */
  211. public static function table_prefix($table = null, $db = null)
  212. {
  213. return \Database_Connection::instance($db)->table_prefix($table);
  214. }
  215. /**
  216. * Lists all of the columns in a table. Optionally, a LIKE string can be
  217. * used to search for specific fields.
  218. *
  219. * // Get all columns from the "users" table
  220. * $columns = DB::list_columns('users');
  221. *
  222. * // Get all name-related columns
  223. * $columns = DB::list_columns('users', '%name%');
  224. *
  225. * @param string table to get columns from
  226. * @param string column to search for
  227. * @param string the database connection to use
  228. * @return array
  229. */
  230. public static function list_columns($table = null, $like = null, $db = null)
  231. {
  232. return \Database_Connection::instance($db)->list_columns($table, $like);
  233. }
  234. /**
  235. * If a table name is given it will return the table name with the configured
  236. * prefix. If not, then just the prefix is returned
  237. *
  238. * @param string $table the table name to prefix
  239. * @param string $db the database connection to use
  240. * @return string the prefixed table name or the prefix
  241. */
  242. public static function list_tables($like = null, $db = null)
  243. {
  244. return \Database_Connection::instance($db)->list_tables($like);
  245. }
  246. /**
  247. * Returns a normalized array describing the SQL data type
  248. *
  249. * DB::datatype('char');
  250. *
  251. * @param string SQL data type
  252. * @param string db connection
  253. * @return array
  254. */
  255. public static function datatype($type, $db = null)
  256. {
  257. return \Database_Connection::instance($db)->datatype($type);
  258. }
  259. /**
  260. * Count the number of records in a table.
  261. *
  262. * // Get the total number of records in the "users" table
  263. * $count = DB::count_records('users');
  264. *
  265. * @param mixed table name string or array(query, alias)
  266. * @param string db connection
  267. * @return integer
  268. */
  269. public static function count_records($table, $db = null)
  270. {
  271. return \Database_Connection::instance($db)->count_records($table);
  272. }
  273. /**
  274. * Count the number of records in the last query, without LIMIT or OFFSET applied.
  275. *
  276. * // Get the total number of records that match the last query
  277. * $count = $db->count_last_query();
  278. *
  279. * @param string db connection
  280. * @return integer
  281. */
  282. public static function count_last_query($db = null)
  283. {
  284. return \Database_Connection::instance($db)->count_last_query();
  285. }
  286. /**
  287. * Set the connection character set. This is called automatically by [static::connect].
  288. *
  289. * DB::set_charset('utf8');
  290. *
  291. * @throws Database_Exception
  292. * @param string character set name
  293. * @param string db connection
  294. * @return void
  295. */
  296. public static function set_charset($charset, $db = null)
  297. {
  298. \Database_Connection::instance($db)->set_charset($charset);
  299. }
  300. /**
  301. * Checks whether a connection is in transaction.
  302. *
  303. * DB::in_transaction();
  304. *
  305. * @param string db connection
  306. * @return bool
  307. */
  308. public static function in_transaction($db = null)
  309. {
  310. return \Database_Connection::instance($db)->in_transaction();
  311. }
  312. /**
  313. * Begins a transaction on instance
  314. *
  315. * DB::start_transaction();
  316. *
  317. * @param string db connection
  318. * @return bool
  319. */
  320. public static function start_transaction($db = null)
  321. {
  322. return \Database_Connection::instance($db)->start_transaction();
  323. }
  324. /**
  325. * Commits all pending transactional queries
  326. *
  327. * DB::commit_transaction();
  328. *
  329. * @param string db connection
  330. * @return bool
  331. */
  332. public static function commit_transaction($db = null)
  333. {
  334. return \Database_Connection::instance($db)->commit_transaction();
  335. }
  336. /**
  337. * Rollsback all pending transactional queries
  338. *
  339. * DB::rollback_transaction();
  340. *
  341. * @param string db connection
  342. * @return bool
  343. */
  344. public static function rollback_transaction($db = null)
  345. {
  346. return \Database_Connection::instance($db)->rollback_transaction();
  347. }
  348. }