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

274 lines
6.5 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);
// The result we return depends on the type of query executed against the
// database. On SELECT clauses, we will return the result set, for update
2012-02-24 11:17:56 -06:00
// and deletes we will return the affected row count.
if (stripos($sql, 'select') === 0)
{
2012-02-24 11:35:02 -06:00
return $this->fetch($statement, Config::get('database.fetch'));
}
elseif (stripos($sql, 'update') === 0 or stripos($sql, 'delete') === 0)
{
return $statement->rowCount();
}
else
{
return $result;
}
2012-01-16 13:59:24 -06:00
}
/**
* 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 are not gaps within the keys.
$bindings = array_filter($bindings, function($binding)
{
2012-01-16 13:59:24 -06:00
return ! $binding instanceof Expression;
});
$bindings = array_values($bindings);
2012-02-06 22:09:15 -06:00
$sql = $this->grammar()->shortcut($sql, $bindings);
2011-10-11 21:27:16 -05:00
// Each database operation is wrapped in a try / catch so we can wrap
// any database exceptions in our custom exception class, which will
// set the message to include the SQL and query bindings.
try
{
$statement = $this->pdo->prepare($sql);
2011-08-15 22:35:32 -05:00
$start = microtime(true);
2012-01-16 13:59:24 -06:00
$result = $statement->execute($bindings);
}
// If an exception occurs, we'll pass it into our custom exception
// and set the message to include the SQL and query bindings so
// debugging is much easier on the developer.
catch (\Exception $exception)
{
$exception = new Exception($sql, $bindings, $exception);
2012-01-16 13:59:24 -06:00
throw $exception;
}
2012-01-16 13:59:24 -06:00
// Once we have execute the query, we log the SQL, bindings, and
// execution time in a static array that is accessed by all of
// the connections actively being used by the application.
2012-02-16 08:46:48 -06:00
if (Config::get('database.profile'))
{
$this->log($sql, $bindings, $start);
2012-02-16 08:46:48 -06:00
}
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
2012-02-24 11:35:02 -06:00
/**
* Fetch all of the rows for a given statement.
*
* @param PDOStatement $statement
* @param int $style
* @return array
*/
protected function fetch($statement, $style)
{
// If the fetch style is "class", we'll hydrate an array of PHP
// stdClass objects as generic containers for the query rows,
// otherwise we'll just use the fetch styel value.
if ($style === PDO::FETCH_CLASS)
{
return $statement->fetchAll(PDO::FETCH_CLASS, 'stdClass');
}
else
{
return $statement->fetchAll($style);
}
}
/**
* Log the query and fire the core query event.
*
* @param string $sql
* @param array $bindings
* @param int $start
* @return void
*/
protected function log($sql, $bindings, $start)
{
$time = number_format((microtime(true) - $start) * 1000, 2);
2012-02-16 16:26:39 -06:00
Event::fire('laravel.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
}