improve / fix postgres support.
This commit is contained in:
parent
56c6202d28
commit
691d68ba54
|
@ -89,6 +89,9 @@ protected function grammar()
|
|||
case 'sqlsrv':
|
||||
return $this->grammar = new Query\Grammars\SQLServer($this);
|
||||
|
||||
case 'pgsql':
|
||||
return $this->grammar = new Query\Grammars\Postgres($this);
|
||||
|
||||
default:
|
||||
return $this->grammar = new Query\Grammars\Grammar($this);
|
||||
}
|
||||
|
@ -190,6 +193,13 @@ public function query($sql, $bindings = array())
|
|||
{
|
||||
return $statement->rowCount();
|
||||
}
|
||||
// For insert statements that use the "returning" clause, which is allowed
|
||||
// by databsae systems such as Postgres, we need to actually return the
|
||||
// real query result so the consumer can get the ID.
|
||||
elseif (stripos($sql, 'insert') === 0 and stripos($sql, 'returning') !== false)
|
||||
{
|
||||
return $this->fetch($statement, Config::get('database.fetch'));
|
||||
}
|
||||
else
|
||||
{
|
||||
return $result;
|
||||
|
|
|
@ -2,6 +2,18 @@
|
|||
|
||||
class Postgres extends Connector {
|
||||
|
||||
/**
|
||||
* The PDO connection options.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $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,
|
||||
);
|
||||
|
||||
/**
|
||||
* Establish a PDO database connection.
|
||||
*
|
||||
|
|
|
@ -399,7 +399,7 @@ public function save()
|
|||
// then we can consider the insert successful.
|
||||
else
|
||||
{
|
||||
$id = $this->query()->insert_get_id($this->attributes, $this->sequence());
|
||||
$id = $this->query()->insert_get_id($this->attributes, $this->key());
|
||||
|
||||
$this->set_key($id);
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
use Closure;
|
||||
use Laravel\Database;
|
||||
use Laravel\Paginator;
|
||||
use Laravel\Database\Query\Grammars\Postgres;
|
||||
use Laravel\Database\Query\Grammars\SQLServer;
|
||||
|
||||
class Query {
|
||||
|
@ -751,19 +752,23 @@ public function insert($values)
|
|||
* Insert an array of values into the database table and return the ID.
|
||||
*
|
||||
* @param array $values
|
||||
* @param string $sequence
|
||||
* @param string $column
|
||||
* @return int
|
||||
*/
|
||||
public function insert_get_id($values, $sequence = null)
|
||||
public function insert_get_id($values, $column = 'id')
|
||||
{
|
||||
$sql = $this->grammar->insert($this, $values);
|
||||
$sql = $this->grammar->insert_get_id($this, $values, $column);
|
||||
|
||||
$this->connection->query($sql, array_values($values));
|
||||
$result = $this->connection->query($sql, array_values($values));
|
||||
|
||||
// Some database systems (Postgres) require a sequence name to be
|
||||
// given when retrieving the auto-incrementing ID, so we'll pass
|
||||
// the given sequence into the method just in case.
|
||||
return (int) $this->connection->pdo->lastInsertId($sequence);
|
||||
if ($this->grammar instanceof Postgres)
|
||||
{
|
||||
return (int) $result[0]->$column;
|
||||
}
|
||||
else
|
||||
{
|
||||
return (int) $this->connection->pdo->lastInsertId();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -375,6 +375,19 @@ public function insert(Query $query, $values)
|
|||
return "INSERT INTO {$table} ({$columns}) VALUES {$parameters}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile a SQL INSERT and get ID statment from a Query instance.
|
||||
*
|
||||
* @param Query $query
|
||||
* @param array $values
|
||||
* @param string $column
|
||||
* @return string
|
||||
*/
|
||||
public function insert_get_id(Query $query, $values, $column)
|
||||
{
|
||||
return $this->insert($query, $values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile a SQL UPDATE statment from a Query instance.
|
||||
*
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
<?php namespace Laravel\Database\Query\Grammars;
|
||||
|
||||
use Laravel\Database\Query;
|
||||
|
||||
class Postgres extends Grammar {
|
||||
|
||||
/**
|
||||
* Compile a SQL INSERT and get ID statment from a Query instance.
|
||||
*
|
||||
* @param Query $query
|
||||
* @param array $values
|
||||
* @param string $column
|
||||
* @return string
|
||||
*/
|
||||
public function insert_get_id(Query $query, $values, $column)
|
||||
{
|
||||
return $this->insert($query, $values)." RETURNING $column";
|
||||
}
|
||||
|
||||
}
|
|
@ -324,7 +324,7 @@ protected function type_string(Fluent $column)
|
|||
*/
|
||||
protected function type_integer(Fluent $column)
|
||||
{
|
||||
return ($column->increment) ? 'SERIAL' : 'INTEGER';
|
||||
return ($column->increment) ? 'SERIAL' : 'BIGINT';
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -136,7 +136,9 @@ public function key($type, $columns, $name)
|
|||
// the index that can be used when dropping indexes.
|
||||
if (is_null($name))
|
||||
{
|
||||
$name = $this->name.'_'.implode('_', $columns).'_'.$type;
|
||||
$name = str_replace(array('-', '.'), '_', $this->name);
|
||||
|
||||
$name = $name.'_'.implode('_', $columns).'_'.$type;
|
||||
}
|
||||
|
||||
return $this->command($type, compact('name', 'columns'));
|
||||
|
|
Loading…
Reference in New Issue