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

170 lines
3.4 KiB
PHP
Raw Normal View History

<?php namespace Laravel\Database;
2011-08-01 17:58:20 -05:00
class Connection {
/**
* The connection name.
*
* @var string
*/
public $name;
/**
* The connection configuration.
*
* @var array
*/
public $config;
/**
* The PDO connection.
*
* @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-19 20:12:39 -05:00
/**
* The database connector instance.
*
* @var Connector
*/
2011-08-19 21:39:38 -05:00
private $connector;
2011-08-19 20:12:39 -05:00
2011-08-01 17:58:20 -05:00
/**
* Create a new Connection instance.
*
2011-08-01 19:57:48 -05:00
* @param string $name
2011-08-19 20:12:39 -05:00
* @param array $config
2011-08-01 19:57:48 -05:00
* @param Connector $connector
2011-08-01 17:58:20 -05:00
* @return void
*/
2011-08-19 20:12:39 -05:00
public function __construct($name, $config, Connector $connector)
2011-08-01 17:58:20 -05:00
{
$this->name = $name;
$this->config = $config;
2011-08-19 20:12:39 -05:00
$this->connector = $connector;
}
/**
* Establish the PDO connection for the connection instance.
*
* @return void
*/
2011-08-19 21:39:38 -05:00
public function connect()
2011-08-19 20:12:39 -05:00
{
$this->pdo = $this->connector->connect($this->config);
}
/**
* Determine if a PDO connection has been established for the connection.
*
* @return bool
*/
2011-08-19 21:39:38 -05:00
public function connected()
2011-08-19 20:12:39 -05:00
{
return ! is_null($this->pdo);
}
/**
* Execute a SQL query against the connection and return a scalar result.
*
* @param string $sql
* @param array $bindings
* @return mixed
*/
public function scalar($sql, $bindings = array())
{
$result = (array) $this->first($sql, $bindings);
return (strpos(strtolower($sql), 'select count') === 0) ? (int) reset($result) : (float) reset($result);
2011-08-01 17:58:20 -05:00
}
/**
* Execute a SQL query against the connection and return the first result.
*
* @param string $sql
* @param array $bindings
* @return object
*/
public function first($sql, $bindings = array())
{
return (count($results = $this->query($sql, $bindings)) > 0) ? $results[0] : null;
}
/**
* 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.
*
* @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())
{
2011-08-19 20:12:39 -05:00
if ( ! $this->connected()) $this->connect();
$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-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
*/
2011-08-19 21:39:38 -05:00
private 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
{
2011-08-19 20:12:39 -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.
*
* @param string $table
* @return Query
*/
public function table($table)
{
2011-08-22 21:56:42 -05:00
return Query\Factory::make($table, $this, Query\Compiler\Factory::make($this));
2011-08-01 17:58:20 -05:00
}
/**
* Get the driver name for the database connection.
*
* @return string
*/
public function driver()
{
2011-08-19 20:12:39 -05:00
if ( ! $this->connected()) $this->connect();
2011-08-01 17:58:20 -05:00
return $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
}
}