1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-06 06:07:39 +02:00

* support for sequence name in dibi::insertId() & DibiPostgreDriver::insertId()

* implemented DibiPostgreDriver::insertId()
* implemented DibiPostgreDriver::delimite()
This commit is contained in:
David Grudl
2007-08-28 23:17:34 +00:00
parent d5e6cedddb
commit 53874f22d4
7 changed files with 53 additions and 14 deletions

View File

@@ -289,11 +289,12 @@ class dibi
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query
* Monostate for DibiDriver::insertId() * Monostate for DibiDriver::insertId()
* *
* @return int|bool int on success or FALSE on failure * @param string optional sequence name for DibiPostgreDriver
* @return int|FALSE int on success or FALSE on failure
*/ */
public static function insertId() public static function insertId($sequence=NULL)
{ {
return self::getConnection()->insertId(); return self::getConnection()->insertId($sequence);
} }
@@ -433,6 +434,7 @@ class dibi
/** /**
* Create a new substitution pair for indentifiers * Create a new substitution pair for indentifiers
*
* @param string from * @param string from
* @param string to * @param string to
* @return void * @return void
@@ -446,6 +448,7 @@ class dibi
/** /**
* Remove substitution pair * Remove substitution pair
*
* @param string from * @param string from
* @return void * @return void
*/ */
@@ -458,6 +461,7 @@ class dibi
/** /**
* Process substitutions in string * Process substitutions in string
*
* @param string * @param string
* @return string * @return string
*/ */

View File

@@ -118,7 +118,7 @@ class DibiOdbcDriver extends DibiDriver
public function insertId() public function insertId()
{ {
return FALSE; throw new DibiException(__METHOD__ . ' is not implemented');
} }

View File

@@ -23,7 +23,6 @@ if (!class_exists('dibi', FALSE)) die();
class DibiPostgreDriver extends DibiDriver class DibiPostgreDriver extends DibiDriver
{ {
private private
$insertId = FALSE,
$affectedRows = FALSE; $affectedRows = FALSE;
public public
@@ -84,7 +83,7 @@ class DibiPostgreDriver extends DibiDriver
public function nativeQuery($sql) public function nativeQuery($sql)
{ {
$this->insertId = $this->affectedRows = FALSE; $this->affectedRows = FALSE;
$res = @pg_query($this->getConnection(), $sql); $res = @pg_query($this->getConnection(), $sql);
@@ -92,9 +91,6 @@ class DibiPostgreDriver extends DibiDriver
return FALSE; return FALSE;
} elseif (is_resource($res)) { } elseif (is_resource($res)) {
$this->insertId = pg_last_oid($res);
if ($this->insertId < 0) $this->insertId = FALSE;
$this->affectedRows = pg_affected_rows($res); $this->affectedRows = pg_affected_rows($res);
if ($this->affectedRows < 0) $this->affectedRows = FALSE; if ($this->affectedRows < 0) $this->affectedRows = FALSE;
@@ -114,9 +110,22 @@ class DibiPostgreDriver extends DibiDriver
public function insertId() public function insertId($sequence = NULL)
{ {
return $this->insertId; if (empty($sequence)) {
// PostgreSQL 8.1 is needed
$res = pg_query($this->getConnection(), "SELECT LASTVAL() AS seq");
} else {
$res = pg_query($this->getConnection(), "SELECT CURRVAL('$sequence') AS seq");
}
if (is_resource($res)) {
$row = pg_fetch_assoc($res);
pg_free_result($res);
return $row['seq'];
}
return FALSE;
} }
@@ -163,7 +172,8 @@ class DibiPostgreDriver extends DibiDriver
public function delimite($value) public function delimite($value)
{ {
return $value; $value = str_replace('"', '""', $value);
return '"' . str_replace('.', '"."', $value) . '"';
} }

View File

@@ -50,6 +50,7 @@ abstract class DibiDriver
/** /**
* Creates object and (optionally) connects to a database * Creates object and (optionally) connects to a database
*
* @param array connect configuration * @param array connect configuration
* @throws DibiException * @throws DibiException
*/ */
@@ -63,6 +64,7 @@ abstract class DibiDriver
/** /**
* Connects to a database * Connects to a database
*
* @throws DibiException * @throws DibiException
* @return resource * @return resource
*/ */
@@ -72,6 +74,7 @@ abstract class DibiDriver
/** /**
* Gets the configuration descriptor * Gets the configuration descriptor
*
* @see DibiDriver::__construct * @see DibiDriver::__construct
* @return array * @return array
*/ */
@@ -84,6 +87,7 @@ abstract class DibiDriver
/** /**
* Returns the connection resource * Returns the connection resource
*
* @return resource * @return resource
*/ */
final public function getConnection() final public function getConnection()
@@ -166,6 +170,7 @@ abstract class DibiDriver
/** /**
* Executes the SQL query * Executes the SQL query
*
* @param string SQL statement. * @param string SQL statement.
* @return object|bool Result set object or TRUE on success, FALSE on failure * @return object|bool Result set object or TRUE on success, FALSE on failure
*/ */
@@ -175,6 +180,7 @@ abstract class DibiDriver
/** /**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query * Gets the number of affected rows by the last INSERT, UPDATE or DELETE query
*
* @return int number of rows or FALSE on error * @return int number of rows or FALSE on error
*/ */
abstract public function affectedRows(); abstract public function affectedRows();
@@ -183,7 +189,8 @@ abstract class DibiDriver
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query
* @return int|bool int on success or FALSE on failure *
* @return int|FALSE int on success or FALSE on failure
*/ */
abstract public function insertId(); abstract public function insertId();
@@ -212,6 +219,7 @@ abstract class DibiDriver
/** /**
* Returns last error * Returns last error
*
* @return array with items 'message' and 'code' * @return array with items 'message' and 'code'
*/ */
abstract public function errorInfo(); abstract public function errorInfo();
@@ -220,6 +228,7 @@ abstract class DibiDriver
/** /**
* Escapes the string * Escapes the string
*
* @param string unescaped string * @param string unescaped string
* @param bool quote string? * @param bool quote string?
* @return string escaped and optionally quoted string * @return string escaped and optionally quoted string
@@ -230,6 +239,7 @@ abstract class DibiDriver
/** /**
* Delimites identifier (table's or column's name, etc.) * Delimites identifier (table's or column's name, etc.)
*
* @param string identifier * @param string identifier
* @return string delimited identifier * @return string delimited identifier
*/ */
@@ -248,6 +258,7 @@ abstract class DibiDriver
/** /**
* Experimental - injects LIMIT/OFFSET to the SQL query * Experimental - injects LIMIT/OFFSET to the SQL query
*
* @param string &$sql The SQL query that will be modified. * @param string &$sql The SQL query that will be modified.
* @param int $limit * @param int $limit
* @param int $offset * @param int $offset

View File

@@ -67,6 +67,7 @@ abstract class DibiResult implements IteratorAggregate, Countable
/** /**
* Moves cursor position without fetching row * Moves cursor position without fetching row
*
* @param int the 0-based cursor pos to seek to * @param int the 0-based cursor pos to seek to
* @return boolean TRUE on success, FALSE if unable to seek to specified record * @return boolean TRUE on success, FALSE if unable to seek to specified record
*/ */
@@ -76,6 +77,7 @@ abstract class DibiResult implements IteratorAggregate, Countable
/** /**
* Returns the number of rows in a result set * Returns the number of rows in a result set
*
* @return int * @return int
*/ */
abstract public function rowCount(); abstract public function rowCount();
@@ -84,6 +86,7 @@ abstract class DibiResult implements IteratorAggregate, Countable
/** /**
* Frees the resources allocated for this result set * Frees the resources allocated for this result set
*
* @return void * @return void
*/ */
abstract protected function free(); abstract protected function free();
@@ -93,6 +96,7 @@ abstract class DibiResult implements IteratorAggregate, Countable
/** /**
* Fetches the row at current position and moves the internal cursor to the next position * Fetches the row at current position and moves the internal cursor to the next position
* internal usage only * internal usage only
*
* @return array|FALSE array on success, FALSE if no next record * @return array|FALSE array on success, FALSE if no next record
*/ */
abstract protected function doFetch(); abstract protected function doFetch();
@@ -102,6 +106,7 @@ abstract class DibiResult implements IteratorAggregate, Countable
/** /**
* Fetches the row at current position, process optional type conversion * Fetches the row at current position, process optional type conversion
* and moves the internal cursor to the next position * and moves the internal cursor to the next position
*
* @return array|FALSE array on success, FALSE if no next record * @return array|FALSE array on success, FALSE if no next record
*/ */
final public function fetch() final public function fetch()
@@ -125,6 +130,7 @@ abstract class DibiResult implements IteratorAggregate, Countable
/** /**
* Like fetch(), but returns only first field * Like fetch(), but returns only first field
*
* @return mixed value on success, FALSE if no next record * @return mixed value on success, FALSE if no next record
*/ */
final function fetchSingle() final function fetchSingle()
@@ -148,6 +154,7 @@ abstract class DibiResult implements IteratorAggregate, Countable
/** /**
* Fetches all records from table. * Fetches all records from table.
*
* @return array * @return array
*/ */
final function fetchAll() final function fetchAll()
@@ -238,6 +245,7 @@ abstract class DibiResult implements IteratorAggregate, Countable
/** /**
* Fetches all records from table like $key => $value pairs * Fetches all records from table like $key => $value pairs
*
* @param string associative key * @param string associative key
* @param string value * @param string value
* @return array * @return array
@@ -283,6 +291,7 @@ abstract class DibiResult implements IteratorAggregate, Countable
/** /**
* Automatically frees the resources allocated for this result set * Automatically frees the resources allocated for this result set
*
* @return void * @return void
*/ */
public function __destruct() public function __destruct()
@@ -341,6 +350,7 @@ abstract class DibiResult implements IteratorAggregate, Countable
/** /**
* Gets an array of field names * Gets an array of field names
*
* @return array * @return array
*/ */
final public function getFields() final public function getFields()
@@ -356,6 +366,7 @@ abstract class DibiResult implements IteratorAggregate, Countable
/** /**
* Gets an array of meta informations about column * Gets an array of meta informations about column
*
* @param string column name * @param string column name
* @return array * @return array
*/ */
@@ -372,6 +383,7 @@ abstract class DibiResult implements IteratorAggregate, Countable
/** /**
* Acquires .... * Acquires ....
*
* @return void * @return void
*/ */
final protected function detectTypes() final protected function detectTypes()

View File

@@ -318,6 +318,7 @@ final class DibiTranslator
/** /**
* PREG callback for @see self::formatValue() * PREG callback for @see self::formatValue()
*
* @param array * @param array
* @return string * @return string
*/ */
@@ -395,6 +396,7 @@ final class DibiTranslator
/** /**
* Apply substitutions to indentifier and delimites it * Apply substitutions to indentifier and delimites it
*
* @param string indentifier * @param string indentifier
* @return string * @return string
*/ */

View File

@@ -41,5 +41,5 @@ FROM [customers]
WHERE WHERE
%if', isset($name), '[name] LIKE %s', $name, ' %if', isset($name), '[name] LIKE %s', $name, '
%if', $cond2, 'AND [admin]=1 %end %if', $cond2, 'AND [admin]=1 %end
%else LIMIT 10 %end' %else 1 LIMIT 10 %end'
); );