more 2.0 changes
This commit is contained in:
@@ -30,19 +30,60 @@ class Connection {
|
||||
*/
|
||||
public $queries = array();
|
||||
|
||||
/**
|
||||
* The database connector instance.
|
||||
*
|
||||
* @var Connector
|
||||
*/
|
||||
protected $connector;
|
||||
|
||||
/**
|
||||
* Create a new Connection instance.
|
||||
*
|
||||
* @param string $name
|
||||
* @param object $config
|
||||
* @param array $config
|
||||
* @param Connector $connector
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($name, $config, $connector)
|
||||
public function __construct($name, $config, Connector $connector)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->config = $config;
|
||||
$this->pdo = $connector->connect($this->config);
|
||||
$this->connector = $connector;
|
||||
}
|
||||
|
||||
/**
|
||||
* Establish the PDO connection for the connection instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function connect()
|
||||
{
|
||||
$this->pdo = $this->connector->connect($this->config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a PDO connection has been established for the connection.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function connected()
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,26 +110,38 @@ class Connection {
|
||||
*
|
||||
* @param string $sql
|
||||
* @param array $bindings
|
||||
* @return array
|
||||
* @return mixed
|
||||
*/
|
||||
public function query($sql, $bindings = array())
|
||||
{
|
||||
$this->queries[] = $sql;
|
||||
if ( ! $this->connected()) $this->connect();
|
||||
|
||||
$query = $this->pdo->prepare($sql);
|
||||
$this->queries[] = compact('sql', 'bindings');
|
||||
|
||||
$result = $query->execute($bindings);
|
||||
return $this->execute($this->pdo->prepare($sql), $bindings);
|
||||
}
|
||||
|
||||
if (strpos(strtoupper($sql), 'SELECT') === 0)
|
||||
/**
|
||||
* Execute a prepared PDO statement and return the appropriate results.
|
||||
*
|
||||
* @param PDOStatement $statement
|
||||
* @param array $results
|
||||
* @return mixed
|
||||
*/
|
||||
protected function execute(\PDOStatement $statement, $bindings)
|
||||
{
|
||||
$result = $statement->execute($bindings);
|
||||
|
||||
if (strpos(strtoupper($statement->queryString), 'SELECT') === 0)
|
||||
{
|
||||
return $query->fetchAll(\PDO::FETCH_CLASS, 'stdClass');
|
||||
return $statement->fetchAll(\PDO::FETCH_CLASS, 'stdClass');
|
||||
}
|
||||
elseif (strpos(strtoupper($sql), 'UPDATE') === 0 or strpos(strtoupper($sql), 'DELETE') === 0)
|
||||
elseif (strpos(strtoupper($statement->queryString), 'INSERT') === 0)
|
||||
{
|
||||
return $query->rowCount();
|
||||
return $result;
|
||||
}
|
||||
|
||||
return $result;
|
||||
return $statement->rowCount();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,19 +152,7 @@ class Connection {
|
||||
*/
|
||||
public function table($table)
|
||||
{
|
||||
return new Query($table, $this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the keyword identifier wrapper for the connection.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function wrapper()
|
||||
{
|
||||
if (array_key_exists('wrap', $this->config) and $this->config['wrap'] === false) return '';
|
||||
|
||||
return ($this->driver() == 'mysql') ? '`' : '"';
|
||||
return Query\Factory::make($table, $this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -121,6 +162,8 @@ class Connection {
|
||||
*/
|
||||
public function driver()
|
||||
{
|
||||
if ( ! $this->connected()) $this->connect();
|
||||
|
||||
return $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
|
||||
}
|
||||
|
||||
@@ -134,4 +177,11 @@ class Connection {
|
||||
return (array_key_exists('prefix', $this->config)) ? $this->config['prefix'] : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the keyword identifier wrapper for the connection.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function wrapper() { return '"'; }
|
||||
|
||||
}
|
||||
30
laravel/db/connection/factory.php
Normal file
30
laravel/db/connection/factory.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php namespace Laravel\DB\Connection;
|
||||
|
||||
use Laravel\DB\Connector;
|
||||
use Laravel\DB\Connection;
|
||||
|
||||
class Factory {
|
||||
|
||||
/**
|
||||
* Get a connnection instance.
|
||||
*
|
||||
* The connection instance created depends on the driver being used.
|
||||
*
|
||||
* @param string $connection
|
||||
* @param object $config
|
||||
* @param Connector $connector
|
||||
* @return Connection
|
||||
*/
|
||||
public static function make($connection, $config, Connector $connector)
|
||||
{
|
||||
switch ($config['driver'])
|
||||
{
|
||||
case 'mysql':
|
||||
return new MySQL($connection, $config, $connector);
|
||||
|
||||
default:
|
||||
return new Connection($connection, $config, $connector);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
16
laravel/db/connection/mysql.php
Normal file
16
laravel/db/connection/mysql.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php namespace Laravel\DB\Connection;
|
||||
|
||||
use Laravel\DB\Connection;
|
||||
|
||||
class MySQL extends Connection {
|
||||
|
||||
/**
|
||||
* Get the keyword identifier wrapper for the connection.
|
||||
*
|
||||
* MySQL uses a non-standard wrapper
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function wrapper() { return '`'; }
|
||||
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
<?php namespace Laravel\DB;
|
||||
|
||||
use Laravel\Config;
|
||||
|
||||
class Connector {
|
||||
abstract class Connector {
|
||||
|
||||
/**
|
||||
* The PDO connection options.
|
||||
@@ -10,99 +8,19 @@ class Connector {
|
||||
* @var array
|
||||
*/
|
||||
public $options = array(
|
||||
\PDO::ATTR_CASE => \PDO::CASE_LOWER,
|
||||
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
|
||||
\PDO::ATTR_ORACLE_NULLS => \PDO::NULL_NATURAL,
|
||||
\PDO::ATTR_STRINGIFY_FETCHES => false,
|
||||
\PDO::ATTR_EMULATE_PREPARES => false,
|
||||
\PDO::ATTR_CASE => \PDO::CASE_LOWER,
|
||||
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
|
||||
\PDO::ATTR_ORACLE_NULLS => \PDO::NULL_NATURAL,
|
||||
\PDO::ATTR_STRINGIFY_FETCHES => false,
|
||||
\PDO::ATTR_EMULATE_PREPARES => false,
|
||||
);
|
||||
|
||||
/**
|
||||
* Establish a PDO database connection.
|
||||
*
|
||||
* @param object $connection
|
||||
* @param array $config
|
||||
* @return PDO
|
||||
*/
|
||||
public function connect($config)
|
||||
{
|
||||
switch ($config->driver)
|
||||
{
|
||||
case 'sqlite':
|
||||
return $this->connect_to_sqlite($config);
|
||||
|
||||
case 'mysql':
|
||||
case 'pgsql':
|
||||
return $this->connect_to_server($config);
|
||||
|
||||
default:
|
||||
return $this->connect_to_generic($config);
|
||||
}
|
||||
|
||||
throw new \Exception('Database driver '.$config->driver.' is not supported.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Establish a PDO connection to a SQLite database.
|
||||
*
|
||||
* SQLite database paths can be specified either relative to the application/db
|
||||
* directory, or as an absolute path to any location on the file system. In-memory
|
||||
* databases are also supported.
|
||||
*
|
||||
* @param object $config
|
||||
* @return PDO
|
||||
*/
|
||||
private function connect_to_sqlite($config)
|
||||
{
|
||||
if ($config->database == ':memory:')
|
||||
{
|
||||
return new \PDO('sqlite::memory:', null, null, $this->options);
|
||||
}
|
||||
elseif (file_exists($path = DATABASE_PATH.$config->database.'.sqlite'))
|
||||
{
|
||||
return new \PDO('sqlite:'.$path, null, null, $this->options);
|
||||
}
|
||||
elseif (file_exists($config->database))
|
||||
{
|
||||
return new \PDO('sqlite:'.$config->database, null, null, $this->options);
|
||||
}
|
||||
|
||||
throw new \Exception("SQLite database [".$config->database."] could not be found.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to a MySQL or PostgreSQL database server.
|
||||
*
|
||||
* @param object $config
|
||||
* @return PDO
|
||||
*/
|
||||
private function connect_to_server($config)
|
||||
{
|
||||
$dsn = $config->driver.':host='.$config->host.';dbname='.$config->database;
|
||||
|
||||
if (isset($config->port))
|
||||
{
|
||||
$dsn .= ';port='.$config->port;
|
||||
}
|
||||
|
||||
$connection = new \PDO($dsn, $config->username, $config->password, $this->options);
|
||||
|
||||
if (isset($config->charset))
|
||||
{
|
||||
$connection->prepare("SET NAMES '".$config->charset."'")->execute();
|
||||
}
|
||||
|
||||
return $connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to a generic data source.
|
||||
*
|
||||
* @param object $config
|
||||
* @return PDO
|
||||
*/
|
||||
private function connect_to_generic($config)
|
||||
{
|
||||
return new \PDO($config->driver.':'.$config->dsn, $config->username, $config->password, $this->options);
|
||||
}
|
||||
abstract public function connect($config);
|
||||
|
||||
}
|
||||
29
laravel/db/connector/factory.php
Normal file
29
laravel/db/connector/factory.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php namespace Laravel\DB\Connector;
|
||||
|
||||
class Factory {
|
||||
|
||||
/**
|
||||
* Create a new database connector instance for a given driver.
|
||||
*
|
||||
* @param string $driver
|
||||
* @return Connector
|
||||
*/
|
||||
public static function make($driver)
|
||||
{
|
||||
switch ($driver)
|
||||
{
|
||||
case 'sqlite':
|
||||
return new SQLite;
|
||||
|
||||
case 'mysql':
|
||||
return new MySQL;
|
||||
|
||||
case 'postgres':
|
||||
return new Postgres;
|
||||
|
||||
default:
|
||||
return new Generic;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
18
laravel/db/connector/generic.php
Normal file
18
laravel/db/connector/generic.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php namespace Laravel\DB\Connector;
|
||||
|
||||
use Laravel\DB\Connector;
|
||||
|
||||
class Generic extends Connector {
|
||||
|
||||
/**
|
||||
* Establish a PDO database connection.
|
||||
*
|
||||
* @param array $config
|
||||
* @return PDO
|
||||
*/
|
||||
public function connect($config)
|
||||
{
|
||||
return new \PDO($config['driver'].':'.$config['dsn'], $config['username'], $config['password'], $this->options);
|
||||
}
|
||||
|
||||
}
|
||||
32
laravel/db/connector/mysql.php
Normal file
32
laravel/db/connector/mysql.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php namespace Laravel\DB\Connector;
|
||||
|
||||
use Laravel\DB\Connector;
|
||||
|
||||
class MySQL extends Connector {
|
||||
|
||||
/**
|
||||
* Establish a PDO database connection.
|
||||
*
|
||||
* @param array $config
|
||||
* @return PDO
|
||||
*/
|
||||
public function connect($config)
|
||||
{
|
||||
$dsn = $config['driver'].':host='.$config['host'].';dbname='.$config['database'];
|
||||
|
||||
if (isset($config['port']))
|
||||
{
|
||||
$dsn .= ';port='.$config['port'];
|
||||
}
|
||||
|
||||
$connection = new \PDO($dsn, $config['username'], $config['password'], $this->options);
|
||||
|
||||
if (isset($config['charset']))
|
||||
{
|
||||
$connection->prepare("SET NAMES '".$config['charset']."'")->execute();
|
||||
}
|
||||
|
||||
return $connection;
|
||||
}
|
||||
|
||||
}
|
||||
32
laravel/db/connector/postgres.php
Normal file
32
laravel/db/connector/postgres.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php namespace Laravel\DB\Connector;
|
||||
|
||||
use Laravel\DB\Connector;
|
||||
|
||||
class MySQL extends Connector {
|
||||
|
||||
/**
|
||||
* Establish a PDO database connection.
|
||||
*
|
||||
* @param array $config
|
||||
* @return PDO
|
||||
*/
|
||||
public function connect($config)
|
||||
{
|
||||
$dsn = $config['driver'].':host='.$config['host'].';dbname='.$config['database'];
|
||||
|
||||
if (isset($config['port']))
|
||||
{
|
||||
$dsn .= ';port='.$config['port'];
|
||||
}
|
||||
|
||||
$connection = new \PDO($dsn, $config['username'], $config['password'], $this->options);
|
||||
|
||||
if (isset($config['charset']))
|
||||
{
|
||||
$connection->prepare("SET NAMES '".$config['charset']."'")->execute();
|
||||
}
|
||||
|
||||
return $connection;
|
||||
}
|
||||
|
||||
}
|
||||
31
laravel/db/connector/sqlite.php
Normal file
31
laravel/db/connector/sqlite.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php namespace Laravel\DB\Connector;
|
||||
|
||||
use Laravel\DB\Connector;
|
||||
|
||||
class SQLite extends Connector {
|
||||
|
||||
/**
|
||||
* Establish a PDO database connection.
|
||||
*
|
||||
* @param array $config
|
||||
* @return PDO
|
||||
*/
|
||||
public function connect($config)
|
||||
{
|
||||
if ($config['database'] == ':memory:')
|
||||
{
|
||||
return new \PDO('sqlite::memory:', null, null, $this->options);
|
||||
}
|
||||
elseif (file_exists($path = DATABASE_PATH.$config['database'].'.sqlite'))
|
||||
{
|
||||
return new \PDO('sqlite:'.$path, null, null, $this->options);
|
||||
}
|
||||
elseif (file_exists($config['database']))
|
||||
{
|
||||
return new \PDO('sqlite:'.$config['database'], null, null, $this->options);
|
||||
}
|
||||
|
||||
throw new \Exception("SQLite database [".$config['database']."] could not be found.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
<?php namespace System\DB\Eloquent;
|
||||
<?php namespace Laravel\DB\Eloquent;
|
||||
|
||||
class Hydrator {
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
<?php namespace System\DB\Eloquent;
|
||||
<?php namespace Laravel\DB\Eloquent;
|
||||
|
||||
use System\DB;
|
||||
use System\Str;
|
||||
use System\Config;
|
||||
use System\Inflector;
|
||||
use System\Paginator;
|
||||
use Laravel\DB;
|
||||
use Laravel\Str;
|
||||
use Laravel\Config;
|
||||
use Laravel\Inflector;
|
||||
use Laravel\Paginator;
|
||||
|
||||
abstract class Model {
|
||||
|
||||
|
||||
@@ -83,7 +83,7 @@ class Query {
|
||||
* @param Connection $connection
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($table, $connection)
|
||||
public function __construct($table, Connection $connection)
|
||||
{
|
||||
$this->table = $table;
|
||||
$this->connection = $connection;
|
||||
@@ -97,7 +97,7 @@ class Query {
|
||||
* @param Connection $connection
|
||||
* @return Query
|
||||
*/
|
||||
public static function table($table, $connection)
|
||||
public static function table($table, Connection $connection)
|
||||
{
|
||||
return new static($table, $connection);
|
||||
}
|
||||
@@ -579,18 +579,7 @@ class Query {
|
||||
*/
|
||||
public function insert_get_id($values)
|
||||
{
|
||||
$sql = $this->compile_insert($values);
|
||||
|
||||
if ($this->connection->driver() == 'pgsql')
|
||||
{
|
||||
$query = $this->connection->pdo->prepare($sql.' RETURNING '.$this->wrap('id'));
|
||||
|
||||
$query->execute(array_values($values));
|
||||
|
||||
return $query->fetch(\PDO::FETCH_CLASS, 'stdClass')->id;
|
||||
}
|
||||
|
||||
$this->connection->query($sql, array_values($values));
|
||||
$this->connection->query($this->compile_insert($values), array_values($values));
|
||||
|
||||
return $this->connection->pdo->lastInsertId();
|
||||
}
|
||||
|
||||
27
laravel/db/query/factory.php
Normal file
27
laravel/db/query/factory.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php namespace Laravel\DB\Query;
|
||||
|
||||
use Laravel\DB\Query;
|
||||
use Laravel\DB\Connection;
|
||||
|
||||
class Factory {
|
||||
|
||||
/**
|
||||
* Create a new query instance for a given driver.
|
||||
*
|
||||
* @param string $table
|
||||
* @param Connection $connection
|
||||
* @return Query
|
||||
*/
|
||||
public static function make($table, Connection $connection)
|
||||
{
|
||||
switch ($connection->driver())
|
||||
{
|
||||
case 'postgres':
|
||||
return new Postgres($table, $connection);
|
||||
|
||||
default:
|
||||
return new Query($table, $connection);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
24
laravel/db/query/postgres.php
Normal file
24
laravel/db/query/postgres.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php namespace Laravel\DB\Query;
|
||||
|
||||
use Laravel\DB\Query;
|
||||
|
||||
class Postgres extends Query {
|
||||
|
||||
/**
|
||||
* Execute an INSERT statement and get the insert ID.
|
||||
*
|
||||
* @param array $values
|
||||
* @return int
|
||||
*/
|
||||
public function insert_get_id($values)
|
||||
{
|
||||
$sql = $this->compile_insert($values);
|
||||
|
||||
$query = $this->connection->pdo->prepare($sql.' RETURNING '.$this->wrap('id'));
|
||||
|
||||
$query->execute(array_values($values));
|
||||
|
||||
return $query->fetch(\PDO::FETCH_CLASS, 'stdClass')->id;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user