PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/system/database/query.php

https://bitbucket.org/viswanath608/roar
PHP | 316 lines | 151 code | 64 blank | 101 comment | 12 complexity | b31eb12329307e8e2152563684a5bcf1 MD5 | raw file
  1. <?php namespace System\Database;
  2. /**
  3. * Nano
  4. *
  5. * Lightweight php framework
  6. *
  7. * @package nano
  8. * @author k. wilson
  9. * @link http://madebykieron.co.uk
  10. */
  11. use System\Database as DB;
  12. use System\Config;
  13. class Query {
  14. private $sql = '';
  15. private $bindings = array();
  16. private $wrapper = '`';
  17. private $table, $select, $join, $where, $group = array(), $order = array(), $limit, $offset;
  18. private $connection, $style;
  19. private function wrap($value) {
  20. // remove white space
  21. $column = trim($value);
  22. // handle aliases
  23. if(strpos(strtolower($column), ' as ') !== false) {
  24. list($col, $alias) = explode(' as ', strtolower($column));
  25. return $this->wrap($col) . ' AS ' . $this->wrap($alias);
  26. }
  27. // dont wrap function calls
  28. if(preg_match('/[a-z]+\(.*?\)/i', $column)) {
  29. return $column;
  30. }
  31. foreach(explode('.', $column) as $segment) {
  32. $sql[] = ($segment !== '*') ? $this->wrapper . $segment . $this->wrapper : $segment;
  33. }
  34. return implode('.', $sql);
  35. }
  36. private function columnizer($value) {
  37. if(is_array($value)) {
  38. foreach($value as $column) {
  39. $sql[] = $this->wrap($column);
  40. }
  41. return implode(', ', $sql);
  42. }
  43. return $this->wrap($value);
  44. }
  45. public static function table($table) {
  46. return new static($table);
  47. }
  48. public function __construct($table, $connection = null) {
  49. $this->table = $table;
  50. $this->connection = DB::connection($connection);
  51. $this->style = Config::get('database.fetch');
  52. }
  53. /*
  54. MySQL Joins
  55. Example:
  56. Db::table('users')
  57. ->join('groups', 'groups.id', '=', 'users.group')
  58. ->get(array('users.id', 'groups.name'));
  59. */
  60. public function join($table, $left, $operator, $right, $type = 'INNER') {
  61. $this->join .= ' ' . $type . ' JOIN ' . $this->wrap($table) . ' ON (' . $this->wrap($left) . ' ' . $operator . ' ' . $this->wrap($right) . ')';
  62. return $this;
  63. }
  64. public function left_join($table, $left, $operator, $right) {
  65. return $this->join($table, $left, $operator, $right, 'LEFT');
  66. }
  67. /*
  68. MySQL where clauses
  69. */
  70. public function where($column, $operator, $value) {
  71. $this->where .= (empty($this->where) ? ' WHERE ' : ' AND ') . $this->wrap($column) . ' ' . $operator . ' ?';
  72. $this->bindings[] = $value;
  73. return $this;
  74. }
  75. public function or_where($column, $operator, $value) {
  76. $this->where .= ' OR ' . $this->wrap($column) . ' ' . $operator . ' ?';
  77. $this->bindings[] = $value;
  78. return $this;
  79. }
  80. public function where_in($column, $keys) {
  81. if(count($keys)) {
  82. $instance = $this->connection->pdo;
  83. $keys = array_map(function($string) use ($instance) {
  84. return $instance->quote($string);
  85. }, $keys);
  86. $this->where .= (empty($this->where) ? ' WHERE ' : ' AND ') . $this->wrap($column) . ' IN (' . implode(',', $keys) . ')';
  87. }
  88. return $this;
  89. }
  90. public function where_is_null($column) {
  91. $this->where .= (empty($this->where) ? ' WHERE ' : ' AND ') . $this->wrap($column) . ' IS NULL';
  92. return $this;
  93. }
  94. public function or_where_is_null($column) {
  95. $this->where .= ' OR ' . $this->wrap($column) . ' IS NULL';
  96. return $this;
  97. }
  98. /*
  99. MySQL Sorting
  100. */
  101. public function take($num) {
  102. $this->limit = ' LIMIT ' . $num;
  103. return $this;
  104. }
  105. public function skip($num) {
  106. $this->offset = ' OFFSET ' . $num;
  107. return $this;
  108. }
  109. public function order_by($column, $mode = 'ASC') {
  110. $this->order[] = $this->wrap($column) . ' ' . strtoupper($mode);
  111. return $this;
  112. }
  113. public function group_by($column) {
  114. $this->group[] = $this->wrap($column);
  115. return $this;
  116. }
  117. /*
  118. Build SQL statement
  119. */
  120. public function build() {
  121. $select = empty($this->select) ? '*' : $this->columnizer($this->select);
  122. $ordering = count($this->order) ? ' ORDER BY ' . implode(', ', $this->order) : '';
  123. $grouping = count($this->group) ? ' GROUP BY ' . implode(', ', $this->group) : '';
  124. return 'SELECT ' . $select . ' FROM ' . $this->table . $this->join . $this->where . $grouping . $ordering . $this->limit . $this->offset;
  125. }
  126. /*
  127. MySQL select first col from first record
  128. Example:
  129. Query::table('visits')
  130. ->where('id', '=', 26)
  131. ->col();
  132. */
  133. public function col($column_number = 0) {
  134. $sql = $this->build();
  135. list($statement, $result) = $this->connection->execute($sql, $this->bindings);
  136. if($result) return $statement->fetchColumn($column_number);
  137. }
  138. /*
  139. MySQL select first record
  140. Example:
  141. Query::table('books')
  142. ->where('id', '=', 26)
  143. ->fetch(array('name', 'author'));
  144. */
  145. public function fetch($columns = array()) {
  146. $this->select = $columns;
  147. $sql = $this->build();
  148. list($statement, $result) = $this->connection->execute($sql, $this->bindings);
  149. if($result) return $statement->fetch($this->style);
  150. }
  151. /*
  152. MySQL get result set
  153. Example:
  154. Query::table('books')
  155. ->get(array('name', 'author'));
  156. */
  157. public function get($columns = array()) {
  158. $this->select = $columns;
  159. $sql = $this->build();
  160. list($statement, $result) = $this->connection->execute($sql, $this->bindings);
  161. if($result) return $statement->fetchAll($this->style);
  162. }
  163. /*
  164. Aggregate methods
  165. */
  166. public function count() {
  167. $this->select = 'COUNT(*)';
  168. $sql = $this->build();
  169. list($statement, $result) = $this->connection->execute($sql, $this->bindings);
  170. if($result) return $statement->fetchColumn();
  171. }
  172. /*
  173. MySQL Insert
  174. Example:
  175. Query::table('my_table')
  176. ->insert(array('id' => 1));
  177. */
  178. public function insert($data) {
  179. foreach($data as $k => $v) {
  180. $keys[] = $this->wrap($k);
  181. $tokens[] = '?';
  182. $bindings[] = $v;
  183. }
  184. $sql = 'INSERT INTO ' . $this->wrap($this->table) . ' (' . implode(', ', $keys) . ') values (' . implode(', ', $tokens) . ')';
  185. list($statement, $result) = $this->connection->execute($sql, $bindings);
  186. if($result) return $statement;
  187. }
  188. /*
  189. MySQL Insert
  190. Example:
  191. Query::table('my_table')
  192. ->insert(array('id' => 1));
  193. */
  194. public function insert_get_id($data) {
  195. if($statement = $this->insert($data)) {
  196. return $this->connection->pdo->lastInsertId();
  197. }
  198. }
  199. /*
  200. MySQL Update
  201. Example:
  202. Query::table('my_table')
  203. ->where('id', '=', 2)
  204. ->update(array('name' => 'dave'));
  205. */
  206. public function update($data) {
  207. foreach($data as $k => $v) {
  208. $update[] = $this->wrap($k) . ' = ?';
  209. $bindings[] = $v;
  210. }
  211. $sql = 'UPDATE ' . $this->wrap($this->table) . ' SET ' . implode(', ', $update) . $this->where;
  212. $bindings = array_merge($bindings, $this->bindings);
  213. list($statement, $result) = $this->connection->execute($sql, $bindings);
  214. if($result) return $statement->rowCount();
  215. }
  216. /*
  217. MySQL Delete
  218. Example:
  219. Query::table('my_table')
  220. ->where('id', '=', 1)
  221. ->delete();
  222. */
  223. public function delete() {
  224. $sql = 'DELETE FROM ' . $this->wrap($this->table) . $this->where;
  225. list($statement, $result) = $this->connection->execute($sql, $this->bindings);
  226. if($result) return $statement->rowCount();
  227. }
  228. }