pdo = $pdo; $this->config = $config; } /** * Execute a SQL query against the connection and return a single column result. * * * // Get the total number of rows on a table * $count = DB::connection()->only('select count(*) from users'); * * // Get the sum of payment amounts from a table * $sum = DB::connection()->only('select sum(amount) from payments') * * * @param string $sql * @param array $bindings * @return mixed */ public function only($sql, $bindings = array()) { $result = (array) $this->first($sql, $bindings); return reset($result); } /** * Execute a SQL query against the connection and return the first result. * * * // Execute a query against the database connection * $user = DB::connection()->first('select * from users'); * * // Execute a query with bound parameters * $user = DB::connection()->first('select * from users where id = ?', array($id)); * * * @param string $sql * @param array $bindings * @return object */ public function first($sql, $bindings = array()) { if (count($results = $this->query($sql, $bindings)) > 0) return $results[0]; } /** * Execute a SQL query against the connection. * * The method returns the following based on query type: * * SELECT -> Array of stdClasses * UPDATE -> Number of rows affected. * DELETE -> Number of Rows affected. * ELSE -> Boolean true / false depending on success. * * * // Execute a query against the database connection * $users = DB::connection()->query('select * from users'); * * // Execute a query with bound parameters * $user = DB::connection()->query('select * from users where id = ?', array($id)); * * * @param string $sql * @param array $bindings * @return mixed */ public function query($sql, $bindings = array()) { $this->queries[] = compact('sql', 'bindings'); return $this->execute($this->pdo->prepare(trim($sql)), $bindings); } /** * Execute a prepared PDO statement and return the appropriate results. * * @param PDOStatement $statement * @param array $results * @return mixed */ protected function execute(PDOStatement $statement, $bindings) { $result = $statement->execute($bindings); if (strpos(strtoupper($statement->queryString), 'SELECT') === 0) { return $statement->fetchAll(PDO::FETCH_CLASS, 'stdClass'); } elseif (strpos(strtoupper($statement->queryString), 'INSERT') === 0) { return $result; } return $statement->rowCount(); } /** * Begin a fluent query against a table. * * @param string $table * @return Query */ public function table($table) { return new Query($this, $this->grammar(), $table); } /** * Create a new query grammar for the connection. * * @return Grammars\Grammar */ protected function grammar() { if (isset($this->grammar)) return $this->grammar; switch (isset($this->config['grammar']) ? $this->config['grammar'] : $this->driver()) { case 'mysql': return $this->grammar = new Grammars\MySQL; default: return $this->grammar = new Grammars\Grammar; } } /** * Get the driver name for the database connection. * * @return string */ public function driver() { return $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME); } /** * Magic Method for dynamically beginning queries on database tables. */ public function __call($method, $parameters) { return $this->table($method); } }