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

229 lines
5.3 KiB
PHP
Raw Normal View History

2011-09-27 21:10:32 -05:00
<?php namespace Laravel\Database; use PDO, PDOStatement;
2011-08-01 17:58:20 -05:00
class Connection {
/**
* The connection configuration array.
*
* @var array
*/
protected $config;
2011-08-01 17:58:20 -05:00
/**
2011-09-17 23:55:36 -05:00
* The query grammar instance for the connection.
*
* @var Grammars\Grammar
*/
protected $grammar;
/**
* The raw PDO connection instance.
2011-08-01 17:58:20 -05:00
*
* @var PDO
*/
public $pdo;
2011-08-15 22:35:32 -05:00
/**
* All of the queries that have been executed on the connection.
*
* @var array
*/
public $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-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
{
$result = (array) $this->first($sql, $bindings);
2011-09-29 21:22:48 -05:00
return reset($result);
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-09-27 21:10:32 -05:00
if (count($results = $this->query($sql, $bindings)) > 0) return $results[0];
2011-08-01 17:58:20 -05:00
}
/**
* 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.
*
2011-09-27 21:10:32 -05:00
* <code>
* // 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));
* </code>
*
2011-08-01 17:58:20 -05:00
* @param string $sql
* @param array $bindings
2011-08-19 20:12:39 -05:00
* @return mixed
2011-08-01 17:58:20 -05:00
*/
public function query($sql, $bindings = array())
{
// First we need to remove all expressions from the bindings
// since they will be placed into the query as raw strings.
foreach ($bindings as $key => $value)
{
if ($value instanceof Expression) unset($bindings[$key]);
}
2011-10-11 21:27:16 -05:00
$sql = $this->transform($sql, $bindings);
2011-08-19 20:12:39 -05:00
$this->queries[] = compact('sql', 'bindings');
2011-08-15 22:35:32 -05:00
2011-08-23 23:16:44 -05:00
return $this->execute($this->pdo->prepare(trim($sql)), $bindings);
2011-08-19 20:12:39 -05:00
}
2011-08-01 17:58:20 -05:00
2011-10-11 21:27:16 -05:00
/**
* Transform an SQL query into an executable query.
*
* Laravel provides a convenient short-cut when writing raw queries for
* handling cumbersome "where in" statements. This method will transform
* those segments into their full SQL counterparts.
*
* @param string $sql
* @param array $bindings
* @return string
*/
protected function transform($sql, $bindings)
{
if (strpos($sql, '(...)') === false) return $sql;
for ($i = 0; $i < count($bindings); $i++)
{
// If the binding is an array, we can assume it is being used to fill
// a "where in" condition, so we will replace the next place-holder
// in the query with the correct number of parameters based on the
// number of elements in this binding.
if (is_array($bindings[$i]))
{
$parameters = implode(', ', array_fill(0, count($bindings[$i]), '?'));
$sql = preg_replace('~\(\.\.\.\)~', "({$parameters})", $sql, 1);
}
}
return $sql;
}
2011-08-19 20:12:39 -05:00
/**
* Execute a prepared PDO statement and return the appropriate results.
*
* @param PDOStatement $statement
* @param array $results
* @return mixed
*/
protected function execute(PDOStatement $statement, $bindings)
2011-08-19 20:12:39 -05:00
{
$result = $statement->execute($bindings);
2011-08-01 17:58:20 -05:00
2011-08-19 20:12:39 -05:00
if (strpos(strtoupper($statement->queryString), 'SELECT') === 0)
2011-08-01 17:58:20 -05:00
{
return $statement->fetchAll(PDO::FETCH_CLASS, 'stdClass');
2011-08-01 17:58:20 -05:00
}
2011-08-19 20:12:39 -05:00
elseif (strpos(strtoupper($statement->queryString), 'INSERT') === 0)
2011-08-01 17:58:20 -05:00
{
2011-08-19 20:12:39 -05:00
return $result;
2011-08-01 17:58:20 -05:00
}
2011-08-08 16:00:01 -05:00
2011-08-19 20:12:39 -05:00
return $statement->rowCount();
2011-08-01 17:58:20 -05:00
}
/**
* Begin a fluent query against a table.
*
2011-09-12 23:34:23 -05:00
* @param string $table
* @return Query
2011-08-01 17:58:20 -05:00
*/
public function table($table)
{
2011-09-13 23:47:38 -05:00
return new Query($this, $this->grammar(), $table);
2011-09-12 21:48:48 -05:00
}
/**
* Create a new query grammar for the connection.
2011-09-12 21:48:48 -05:00
*
* @return Grammars\Grammar
2011-09-12 21:48:48 -05:00
*/
protected function grammar()
2011-09-12 21:48:48 -05:00
{
2011-09-17 23:55:36 -05:00
if (isset($this->grammar)) return $this->grammar;
switch (isset($this->config['grammar']) ? $this->config['grammar'] : $this->driver())
2011-09-12 21:48:48 -05:00
{
case 'mysql':
2011-09-17 23:55:36 -05:00
return $this->grammar = new Grammars\MySQL;
2011-09-12 21:48:48 -05:00
default:
2011-09-17 23:55:36 -05:00
return $this->grammar = new Grammars\Grammar;
2011-09-12 21:48:48 -05:00
}
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
}