DB.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Provides a shortcut to get Database related objects for [making queries](../database/query).
  4. *
  5. * Shortcut | Returned Object
  6. * -------------|---------------
  7. * [`DB::query()`](#query) | [Database_Query]
  8. * [`DB::insert()`](#insert) | [Database_Query_Builder_Insert]
  9. * [`DB::select()`](#select),<br />[`DB::select_array()`](#select_array) | [Database_Query_Builder_Select]
  10. * [`DB::update()`](#update) | [Database_Query_Builder_Update]
  11. * [`DB::delete()`](#delete) | [Database_Query_Builder_Delete]
  12. * [`DB::expr()`](#expr) | [Database_Expression]
  13. *
  14. * You pass the same parameters to these functions as you pass to the objects they return.
  15. *
  16. * @package Kohana/Database
  17. * @category Base
  18. * @author Kohana Team
  19. * @copyright (c) Kohana Team
  20. * @license https://koseven.ga/LICENSE.md
  21. */
  22. class Kohana_DB {
  23. /**
  24. * Create a new [Database_Query] of the given type.
  25. *
  26. * // Create a new SELECT query
  27. * $query = DB::query(Database::SELECT, 'SELECT * FROM users');
  28. *
  29. * // Create a new DELETE query
  30. * $query = DB::query(Database::DELETE, 'DELETE FROM users WHERE id = 5');
  31. *
  32. * Specifying the type changes the returned result. When using
  33. * `Database::SELECT`, a [Database_Query_Result] will be returned.
  34. * `Database::INSERT` queries will return the insert id and number of rows.
  35. * For all other queries, the number of affected rows is returned.
  36. *
  37. * @param integer $type type: Database::SELECT, Database::UPDATE, etc
  38. * @param string $sql SQL statement
  39. * @return Database_Query
  40. */
  41. public static function query($type, $sql)
  42. {
  43. return new Database_Query($type, $sql);
  44. }
  45. /**
  46. * Create a new [Database_Query_Builder_Select]. Each argument will be
  47. * treated as a column. To generate a `foo AS bar` alias, use an array.
  48. *
  49. * // SELECT id, username
  50. * $query = DB::select('id', 'username');
  51. *
  52. * // SELECT id AS user_id
  53. * $query = DB::select(array('id', 'user_id'));
  54. *
  55. * @param mixed $columns column name or array($column, $alias) or object
  56. * @return Database_Query_Builder_Select
  57. */
  58. public static function select($columns = NULL)
  59. {
  60. return new Database_Query_Builder_Select(func_get_args());
  61. }
  62. /**
  63. * Create a new [Database_Query_Builder_Select] from an array of columns.
  64. *
  65. * // SELECT id, username
  66. * $query = DB::select_array(array('id', 'username'));
  67. *
  68. * @param array $columns columns to select
  69. * @return Database_Query_Builder_Select
  70. */
  71. public static function select_array(array $columns = NULL)
  72. {
  73. return new Database_Query_Builder_Select($columns);
  74. }
  75. /**
  76. * Create a new [Database_Query_Builder_Insert].
  77. *
  78. * // INSERT INTO users (id, username)
  79. * $query = DB::insert('users', array('id', 'username'));
  80. *
  81. * @param string $table table to insert into
  82. * @param array $columns list of column names or array($column, $alias) or object
  83. * @return Database_Query_Builder_Insert
  84. */
  85. public static function insert($table = NULL, array $columns = NULL)
  86. {
  87. return new Database_Query_Builder_Insert($table, $columns);
  88. }
  89. /**
  90. * Create a new [Database_Query_Builder_Update].
  91. *
  92. * // UPDATE users
  93. * $query = DB::update('users');
  94. *
  95. * @param string $table table to update
  96. * @return Database_Query_Builder_Update
  97. */
  98. public static function update($table = NULL)
  99. {
  100. return new Database_Query_Builder_Update($table);
  101. }
  102. /**
  103. * Create a new [Database_Query_Builder_Delete].
  104. *
  105. * // DELETE FROM users
  106. * $query = DB::delete('users');
  107. *
  108. * @param string $table table to delete from
  109. * @return Database_Query_Builder_Delete
  110. */
  111. public static function delete($table = NULL)
  112. {
  113. return new Database_Query_Builder_Delete($table);
  114. }
  115. /**
  116. * Create a new [Database_Expression] which is not escaped. An expression
  117. * is the only way to use SQL functions within query builders.
  118. *
  119. * $expression = DB::expr('COUNT(users.id)');
  120. * $query = DB::update('users')->set(array('login_count' => DB::expr('login_count + 1')))->where('id', '=', $id);
  121. * $users = ORM::factory('user')->where(DB::expr("BINARY `hash`"), '=', $hash)->find();
  122. *
  123. * @param string $string expression
  124. * @param array parameters
  125. * @return Database_Expression
  126. */
  127. public static function expr($string, $parameters = [])
  128. {
  129. return new Database_Expression($string, $parameters);
  130. }
  131. } // End DB