Files
spa-api/laravel/database/connection.php

266 lines
6.1 KiB
PHP
Raw Normal View History

2012-02-16 08:46:48 -06:00
<?php namespace Laravel\Database; use PDO, PDOStatement, Laravel\Config, Laravel\Event;
2011-08-01 17:58:20 -05:00
class Connection {
/**
2011-11-02 21:27:43 -05:00
* The raw PDO connection instance.
*
2011-11-02 21:27:43 -05:00
* @var PDO
*/
2011-11-02 21:27:43 -05:00
public $pdo;
2011-09-17 23:55:36 -05:00
/**
2011-11-02 21:27:43 -05:00
* The connection configuration array.
2011-08-01 17:58:20 -05:00
*
2011-11-02 21:27:43 -05:00
* @var array
2011-08-01 17:58:20 -05:00
*/
public $config;
2011-08-01 17:58:20 -05:00
2011-08-15 22:35:32 -05:00
/**
2011-11-02 21:27:43 -05:00
* The query grammar instance for the connection.
2011-08-15 22:35:32 -05:00
*
2011-11-02 21:27:43 -05:00
* @var Grammars\Grammar
2011-08-15 22:35:32 -05:00
*/
2011-11-02 21:27:43 -05:00
protected $grammar;
2011-08-15 22:35:32 -05:00
2012-01-16 13:59:24 -06:00
/**
* All of the queries that have been executed on all connections.
*
* @var array
*/
public static $queries = array();
2011-08-01 17:58:20 -05:00
/**
* Create a new database connection instance.
2011-08-19 20:12:39 -05:00
*
* @param PDO $pdo
* @param array $config
2011-08-19 20:12:39 -05:00
* @return void
*/
public function __construct(PDO $pdo, $config)
2011-09-15 23:03:47 -05:00
{
$this->pdo = $pdo;
$this->config = $config;
2011-09-15 23:03:47 -05:00
}
2011-08-19 20:12:39 -05:00
2011-10-20 21:44:18 -05:00
/**
* Begin a fluent query against a table.
*
2012-01-16 13:59:24 -06:00
* <code>
* // Start a fluent query against the "users" table
* $query = DB::connection()->table('users');
*
* // Start a fluent query against the "users" table and get all the users
* $users = DB::connection()->table('users')->get();
* </code>
*
2011-10-20 21:44:18 -05:00
* @param string $table
* @return Query
*/
public function table($table)
{
return new Query($this, $this->grammar(), $table);
}
/**
* Create a new query grammar for the connection.
*
2012-01-16 13:59:24 -06:00
* @return Query\Grammars\Grammar
2011-10-20 21:44:18 -05:00
*/
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 Query\Grammars\MySQL($this);
2012-01-16 13:59:24 -06:00
case 'sqlsrv':
return $this->grammar = new Query\Grammars\SQLServer($this);
2011-10-20 21:44:18 -05:00
default:
return $this->grammar = new Query\Grammars\Grammar($this);
2011-10-20 21:44:18 -05:00
}
}
2011-08-19 20:12:39 -05:00
/**
2011-09-29 21:22:48 -05:00
* Execute a SQL query against the connection and return a single column result.
2011-08-19 20:12:39 -05:00
*
2011-09-27 21:10:32 -05:00
* <code>
* // Get the total number of rows on a table
2011-09-29 21:22:48 -05:00
* $count = DB::connection()->only('select count(*) from users');
2011-09-27 21:10:32 -05:00
*
* // Get the sum of payment amounts from a table
2011-09-29 21:22:48 -05:00
* $sum = DB::connection()->only('select sum(amount) from payments')
2011-09-27 21:10:32 -05:00
* </code>
*
* @param string $sql
* @param array $bindings
2011-09-29 21:22:48 -05:00
* @return mixed
2011-08-19 20:12:39 -05:00
*/
2011-09-29 21:22:48 -05:00
public function only($sql, $bindings = array())
2011-08-19 20:12:39 -05:00
{
2012-01-16 13:59:24 -06:00
$results = (array) $this->first($sql, $bindings);
2011-08-19 20:12:39 -05:00
2012-01-16 13:59:24 -06:00
return reset($results);
2011-08-01 17:58:20 -05:00
}
/**
* Execute a SQL query against the connection and return the first result.
*
2011-09-27 21:10:32 -05:00
* <code>
* // 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));
* </code>
*
2011-08-01 17:58:20 -05:00
* @param string $sql
* @param array $bindings
* @return object
*/
public function first($sql, $bindings = array())
{
2011-11-02 21:27:43 -05:00
if (count($results = $this->query($sql, $bindings)) > 0)
{
return $results[0];
}
2011-08-01 17:58:20 -05:00
}
/**
2012-01-16 13:59:24 -06:00
* Execute a SQL query and return an array of StdClass objects.
2011-08-01 17:58:20 -05:00
*
2012-01-16 13:59:24 -06:00
* @param string $sql
* @param array $bindings
* @return array
*/
public function query($sql, $bindings = array())
{
list($statement, $result) = $this->execute($sql, $bindings);
return $statement->fetchAll(PDO::FETCH_CLASS, 'stdClass');
}
/**
* Execute a SQL UPDATE query and return the affected row count.
2011-08-01 17:58:20 -05:00
*
2012-01-16 13:59:24 -06:00
* @param string $sql
* @param array $bindings
* @return int
*/
public function update($sql, $bindings = array())
{
list($statement, $result) = $this->execute($sql, $bindings);
return $statement->rowCount();
}
/**
* Execute a SQL DELETE query and return the affected row count.
2011-08-01 17:58:20 -05:00
*
2012-01-16 13:59:24 -06:00
* @param string $sql
* @param array $bindings
* @return int
*/
public function delete($sql, $bindings = array())
{
list($statement, $result) = $this->execute($sql, $bindings);
return $statement->rowCount();
}
/**
* Execute an SQL query and return the boolean result of the PDO statement.
2011-09-27 21:10:32 -05:00
*
2012-01-16 13:59:24 -06:00
* @param string $sql
* @param array $bindings
* @return bool
*/
public function statement($sql, $bindings = array())
{
list($statement, $result) = $this->execute($sql, $bindings);
return $result;
}
/**
* Execute a SQL query against the connection.
*
* The PDO statement and boolean result will be return in an array.
2011-09-27 21:10:32 -05:00
*
2011-08-01 17:58:20 -05:00
* @param string $sql
* @param array $bindings
2012-01-16 13:59:24 -06:00
* @return array
2011-08-01 17:58:20 -05:00
*/
2012-01-16 13:59:24 -06:00
protected function execute($sql, $bindings = array())
2011-08-01 17:58:20 -05:00
{
2012-02-06 21:39:30 -06:00
$bindings = (array) $bindings;
2012-01-16 13:59:24 -06:00
// Since expressions are injected into the query as strings, we need to
// remove them from the array of bindings. After we have removed them,
// we'll reset the array so there aren't gaps in the keys.
$bindings = array_values(array_filter($bindings, function($binding)
{
2012-01-16 13:59:24 -06:00
return ! $binding instanceof Expression;
}));
2012-02-06 22:09:15 -06:00
$sql = $this->grammar()->shortcut($sql, $bindings);
2011-10-11 21:27:16 -05:00
2012-01-16 13:59:24 -06:00
$statement = $this->pdo->prepare($sql);
2011-08-15 22:35:32 -05:00
2012-01-16 13:59:24 -06:00
// Every query is timed so that we can log the executinon time along
// with the query SQL and array of bindings. This should be make it
2012-02-12 21:27:07 -06:00
// convenient for the developer to profile performance.
2012-01-16 13:59:24 -06:00
$time = microtime(true);
$result = $statement->execute($bindings);
$time = number_format((microtime(true) - $time) * 1000, 2);
// Once we have execute the query, we log the SQL, bindings, and
// execution time in a static array that is accessed by all of
2012-02-12 16:34:11 -06:00
// the connections used by the application.
2012-02-16 08:46:48 -06:00
if (Config::get('database.profile'))
{
$this->log($sql, $bindings, $time);
}
2012-01-16 13:59:24 -06:00
return array($statement, $result);
2011-08-19 20:12:39 -05:00
}
2011-08-01 17:58:20 -05:00
/**
* Log the query and fire the core query event.
*
* @param string $sql
* @param array $bindings
* @param int $time
* @return void
*/
2012-02-01 08:27:46 -06:00
protected function log($sql, $bindings, $time)
{
2012-02-16 09:08:00 -06:00
Event::fire('query', array($sql, $bindings, $time));
2012-02-15 13:29:37 -06:00
static::$queries[] = compact('sql', 'bindings', 'time');
}
2011-08-01 17:58:20 -05:00
/**
* Get the driver name for the database connection.
*
* @return string
*/
public function driver()
{
return $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
2011-08-01 17:58:20 -05:00
}
/**
* Magic Method for dynamically beginning queries on database tables.
*/
public function __call($method, $parameters)
{
return $this->table($method);
}
2011-08-01 17:58:20 -05:00
}