db.php 9.7 KB

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