adding better comments to database classes.
This commit is contained in:
parent
2521ab3c1d
commit
b8a901c792
|
@ -1,5 +1,8 @@
|
||||||
<?php namespace Laravel\Database;
|
<?php namespace Laravel\Database;
|
||||||
|
|
||||||
|
use PDO;
|
||||||
|
use PDOStatement;
|
||||||
|
|
||||||
class Connection {
|
class Connection {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -7,14 +10,21 @@ class Connection {
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public $name;
|
protected $name;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The connection configuration.
|
* The connection configuration.
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public $config;
|
protected $config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The database connector instance.
|
||||||
|
*
|
||||||
|
* @var Connector\Connector
|
||||||
|
*/
|
||||||
|
protected $connector;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The PDO connection.
|
* The PDO connection.
|
||||||
|
@ -30,24 +40,17 @@ class Connection {
|
||||||
*/
|
*/
|
||||||
public $queries = array();
|
public $queries = array();
|
||||||
|
|
||||||
/**
|
|
||||||
* The database connector instance.
|
|
||||||
*
|
|
||||||
* @var Connector
|
|
||||||
*/
|
|
||||||
private $connector;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new Connection instance.
|
* Create a new Connection instance.
|
||||||
*
|
*
|
||||||
* @param Connector $connector
|
* @param Connector\Connector $connector
|
||||||
* @param Query\Factory $factory
|
* @param Query\Factory $factory
|
||||||
* @param Compiler\Factory $compiler
|
* @param Compiler\Factory $compiler
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param array $config
|
* @param array $config
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct(Connector $connector, Query\Factory $query, Query\Compiler\Factory $compiler, $name, $config)
|
public function __construct(Connector\Connector $connector, Query\Factory $query, Query\Compiler\Factory $compiler, $name, $config)
|
||||||
{
|
{
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
$this->query = $query;
|
$this->query = $query;
|
||||||
|
@ -79,9 +82,17 @@ public function connected()
|
||||||
/**
|
/**
|
||||||
* Execute a SQL query against the connection and return a scalar result.
|
* Execute a SQL query against the connection and return a scalar result.
|
||||||
*
|
*
|
||||||
|
* <code>
|
||||||
|
* // Get the number of rows in the "users" table
|
||||||
|
* $count = DB::connection()->scalar('select count(*) from users');
|
||||||
|
*
|
||||||
|
* // Get the sum of payments from the "bank" table
|
||||||
|
* $sum = DB::connection()->scalar('select sum(payment) from banks where bank_id = ?', array(1));
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
* @param string $sql
|
* @param string $sql
|
||||||
* @param array $bindings
|
* @param array $bindings
|
||||||
* @return mixed
|
* @return int|float
|
||||||
*/
|
*/
|
||||||
public function scalar($sql, $bindings = array())
|
public function scalar($sql, $bindings = array())
|
||||||
{
|
{
|
||||||
|
@ -93,6 +104,14 @@ public function scalar($sql, $bindings = array())
|
||||||
/**
|
/**
|
||||||
* Execute a SQL query against the connection and return the first result.
|
* Execute a SQL query against the connection and return the first result.
|
||||||
*
|
*
|
||||||
|
* <code>
|
||||||
|
* // Get the first result from the "users" table
|
||||||
|
* $user = DB::connection()->first('select * from users limit 1');
|
||||||
|
*
|
||||||
|
* // Get the first result from a specified group of users
|
||||||
|
* $user = DB::connection()->first('select * from users where group_id = ?', array(1));
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
* @param string $sql
|
* @param string $sql
|
||||||
* @param array $bindings
|
* @param array $bindings
|
||||||
* @return object
|
* @return object
|
||||||
|
@ -112,6 +131,14 @@ public function first($sql, $bindings = array())
|
||||||
* DELETE -> Number of Rows affected.
|
* DELETE -> Number of Rows affected.
|
||||||
* ELSE -> Boolean true / false depending on success.
|
* ELSE -> Boolean true / false depending on success.
|
||||||
*
|
*
|
||||||
|
* <code>
|
||||||
|
* // Execute a query against the connection
|
||||||
|
* $users = DB::connection()->query('select * from users');
|
||||||
|
*
|
||||||
|
* // Execute a query against the connection using bindings
|
||||||
|
* $users = DB::connection()->query('select * from users where group_id = ?', array(1));
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
* @param string $sql
|
* @param string $sql
|
||||||
* @param array $bindings
|
* @param array $bindings
|
||||||
* @return mixed
|
* @return mixed
|
||||||
|
@ -132,13 +159,13 @@ public function query($sql, $bindings = array())
|
||||||
* @param array $results
|
* @param array $results
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
private function execute(\PDOStatement $statement, $bindings)
|
protected function execute(PDOStatement $statement, $bindings)
|
||||||
{
|
{
|
||||||
$result = $statement->execute($bindings);
|
$result = $statement->execute($bindings);
|
||||||
|
|
||||||
if (strpos(strtoupper($statement->queryString), 'SELECT') === 0)
|
if (strpos(strtoupper($statement->queryString), 'SELECT') === 0)
|
||||||
{
|
{
|
||||||
return $statement->fetchAll(\PDO::FETCH_CLASS, 'stdClass');
|
return $statement->fetchAll(PDO::FETCH_CLASS, 'stdClass');
|
||||||
}
|
}
|
||||||
elseif (strpos(strtoupper($statement->queryString), 'INSERT') === 0)
|
elseif (strpos(strtoupper($statement->queryString), 'INSERT') === 0)
|
||||||
{
|
{
|
||||||
|
@ -151,6 +178,11 @@ private function execute(\PDOStatement $statement, $bindings)
|
||||||
/**
|
/**
|
||||||
* Begin a fluent query against a table.
|
* Begin a fluent query against a table.
|
||||||
*
|
*
|
||||||
|
* <code>
|
||||||
|
* // Begin a fluent query against the "users" table
|
||||||
|
* $query = DB::connection()->table('users');
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
* @param string $table
|
* @param string $table
|
||||||
* @return Query
|
* @return Query
|
||||||
*/
|
*/
|
||||||
|
@ -168,11 +200,16 @@ public function driver()
|
||||||
{
|
{
|
||||||
if ( ! $this->connected()) $this->connect();
|
if ( ! $this->connected()) $this->connect();
|
||||||
|
|
||||||
return $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
|
return $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Magic Method for dynamically beginning queries on database tables.
|
* Magic Method for dynamically beginning queries on database tables.
|
||||||
|
*
|
||||||
|
* <code>
|
||||||
|
* // Begin a query against the "users" table
|
||||||
|
* $query = DB::connection()->users();
|
||||||
|
* </code>
|
||||||
*/
|
*/
|
||||||
public function __call($method, $parameters)
|
public function __call($method, $parameters)
|
||||||
{
|
{
|
||||||
|
|
|
@ -7,7 +7,7 @@ class Manager {
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public $connections = array();
|
protected $connections = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The connector factory instance.
|
* The connector factory instance.
|
||||||
|
@ -51,6 +51,14 @@ public function __construct(Connector\Factory $connector, $config, $default)
|
||||||
*
|
*
|
||||||
* Note: Database connections are managed as singletons.
|
* Note: Database connections are managed as singletons.
|
||||||
*
|
*
|
||||||
|
* <code>
|
||||||
|
* // Get the default database connection
|
||||||
|
* $connection = DB::connection();
|
||||||
|
*
|
||||||
|
* // Get a database connection by name
|
||||||
|
* $connection = DB::connection('slave');
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
* @param string $connection
|
* @param string $connection
|
||||||
* @return Database\Connection
|
* @return Database\Connection
|
||||||
*/
|
*/
|
||||||
|
@ -76,7 +84,13 @@ public function connection($connection = null)
|
||||||
/**
|
/**
|
||||||
* Begin a fluent query against a table.
|
* Begin a fluent query against a table.
|
||||||
*
|
*
|
||||||
* This method primarily serves as a short-cut to the $connection->table() method.
|
* <code>
|
||||||
|
* // Begin a fluent query against the "users" table using the default connection
|
||||||
|
* $query = DB::table('users');
|
||||||
|
*
|
||||||
|
* // Begin a fluent query against the "users" table using a specified connection
|
||||||
|
* $query = DB::table('users', 'slave');
|
||||||
|
* </code>
|
||||||
*
|
*
|
||||||
* @param string $table
|
* @param string $table
|
||||||
* @param string $connection
|
* @param string $connection
|
||||||
|
@ -91,6 +105,14 @@ public function table($table, $connection = null)
|
||||||
* Magic Method for calling methods on the default database connection.
|
* Magic Method for calling methods on the default database connection.
|
||||||
*
|
*
|
||||||
* This provides a convenient API for querying or examining the default database connection.
|
* This provides a convenient API for querying or examining the default database connection.
|
||||||
|
*
|
||||||
|
* <code>
|
||||||
|
* // Perform a query against the default connection
|
||||||
|
* $results = DB::query('select * from users');
|
||||||
|
*
|
||||||
|
* // Get the name of the PDO driver being used by the default connection
|
||||||
|
* $driver = DB::driver();
|
||||||
|
* </code>
|
||||||
*/
|
*/
|
||||||
public function __call($method, $parameters)
|
public function __call($method, $parameters)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue