diff --git a/system/db/query.php b/system/db/query.php index 70b919b3..3e28f20d 100644 --- a/system/db/query.php +++ b/system/db/query.php @@ -1,5 +1,9 @@ connection = (is_null($connection)) ? \System\Config::get('db.default') : $connection; + $this->connection = (is_null($connection)) ? Config::get('db.default') : $connection; $this->from = 'FROM '.$this->wrap($this->table = $table); } @@ -116,10 +120,6 @@ public function distinct() public function select() { $this->select = ($this->distinct) ? 'SELECT DISTINCT ' : 'SELECT '; - - // --------------------------------------------------- - // Wrap all of the columns in keyword identifiers. - // --------------------------------------------------- $this->select .= implode(', ', array_map(array($this, 'wrap'), func_get_args())); return $this; @@ -326,7 +326,7 @@ public function or_where_not_null($column) */ public function order_by($column, $direction) { - $this->orderings[] = $this->wrap($column).' '.\System\Str::upper($direction); + $this->orderings[] = $this->wrap($column).' '.Str::upper($direction); return $this; } @@ -387,7 +387,7 @@ public function get() call_user_func_array(array($this, 'select'), (count(func_get_args()) > 0) ? func_get_args() : array('*')); } - return \System\DB::query(Query\Compiler::select($this), $this->bindings, $this->connection); + return DB::query(Query\Compiler::select($this), $this->bindings, $this->connection); } /** @@ -400,10 +400,7 @@ public function get() private function aggregate($aggregator, $column) { $this->select = 'SELECT '.$aggregator.'('.$this->wrap($column).') AS '.$this->wrap('aggregate'); - - $results = \System\DB::query(Query\Compiler::select($this), $this->bindings); - - return $results[0]->aggregate; + return $this->first()->aggregate; } /** @@ -414,7 +411,7 @@ private function aggregate($aggregator, $column) */ public function insert($values) { - return \System\DB::query(Query\Compiler::insert($this, $values), array_values($values), $this->connection); + return DB::query(Query\Compiler::insert($this, $values), array_values($values), $this->connection); } /** @@ -427,28 +424,25 @@ public function insert_get_id($values) { $sql = Query\Compiler::insert($this, $values); - // --------------------------------------------------- - // Postgres. - // --------------------------------------------------- - if (\System\DB::connection($this->connection)->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'pgsql') + // --------------------------------------------------------- + // Use the RETURNING clause on Postgres instead of PDO. + // The Postgres PDO ID method is slightly cumbersome. + // --------------------------------------------------------- + if (DB::driver($this->connection) == 'pgsql') { - $sql .= ' RETURNING '.$this->wrap('id'); + $query = DB::connection($this->connection)->prepare($sql.' RETURNING '.$this->wrap('id')); - $query = \System\DB::connection($this->connection)->prepare($sql); $query->execute(array_values($values)); - $result = $query->fetch(\PDO::FETCH_ASSOC); + return $query->fetch(\PDO::FETCH_CLASS, 'stdClass')->id; + } - return $result['id']; - } - // --------------------------------------------------- - // MySQL and SQLite. - // --------------------------------------------------- - else - { - \System\DB::query($sql, array_values($values), $this->connection); - return \System\DB::connection($this->connection)->lastInsertId(); - } + // --------------------------------------------------------- + // Use the PDO ID method for MySQL and SQLite. + // --------------------------------------------------------- + DB::query($sql, array_values($values), $this->connection); + + return DB::connection($this->connection)->lastInsertId(); } /** @@ -459,7 +453,7 @@ public function insert_get_id($values) */ public function update($values) { - return \System\DB::query(Query\Compiler::update($this, $values), array_merge(array_values($values), $this->bindings), $this->connection); + return DB::query(Query\Compiler::update($this, $values), array_merge(array_values($values), $this->bindings), $this->connection); } /** @@ -475,27 +469,18 @@ public function delete($id = null) $this->where('id', '=', $id); } - return \System\DB::query(Query\Compiler::delete($this), $this->bindings, $this->connection); + return DB::query(Query\Compiler::delete($this), $this->bindings, $this->connection); } /** * Wrap a value in keyword identifiers. * * @param string $value - * @param string $wrap * @return string */ - public function wrap($value, $wrap = '"') + public function wrap($value) { - // --------------------------------------------------- - // If the application is using MySQL, we need to use - // a non-standard keyword identifier. - // --------------------------------------------------- - if (\System\DB::connection($this->connection)->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'mysql') - { - $wrap = '`'; - } - + $wrap = (DB::driver($this->connection) == 'mysql') ? '`' : '"'; return implode('.', array_map(function($segment) use ($wrap) {return ($segment != '*') ? $wrap.$segment.$wrap : $segment;}, explode('.', $value))); } @@ -515,17 +500,12 @@ public function parameterize($values) */ public function __call($method, $parameters) { - // --------------------------------------------------- - // Handle any of the aggregate functions. - // --------------------------------------------------- if (in_array($method, array('count', 'min', 'max', 'avg', 'sum'))) { - return ($method == 'count') ? $this->aggregate(\System\Str::upper($method), '*') : $this->aggregate(\System\Str::upper($method), $parameters[0]); - } - else - { - throw new \Exception("Method [$method] is not defined on the Query class."); + return ($method == 'count') ? $this->aggregate(Str::upper($method), '*') : $this->aggregate(Str::upper($method), $parameters[0]); } + + throw new \Exception("Method [$method] is not defined on the Query class."); } } \ No newline at end of file