1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-21 09:23:57 +01: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
* 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
*
* @param string from
* @param string to
* @return void
@ -446,6 +448,7 @@ class dibi
/**
* Remove substitution pair
*
* @param string from
* @return void
*/
@ -458,6 +461,7 @@ class dibi
/**
* Process substitutions in string
*
* @param string
* @return string
*/

View File

@ -118,7 +118,7 @@ class DibiOdbcDriver extends DibiDriver
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
{
private
$insertId = FALSE,
$affectedRows = FALSE;
public
@ -84,7 +83,7 @@ class DibiPostgreDriver extends DibiDriver
public function nativeQuery($sql)
{
$this->insertId = $this->affectedRows = FALSE;
$this->affectedRows = FALSE;
$res = @pg_query($this->getConnection(), $sql);
@ -92,9 +91,6 @@ class DibiPostgreDriver extends DibiDriver
return FALSE;
} elseif (is_resource($res)) {
$this->insertId = pg_last_oid($res);
if ($this->insertId < 0) $this->insertId = FALSE;
$this->affectedRows = pg_affected_rows($res);
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)
{
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
*
* @param array connect configuration
* @throws DibiException
*/
@ -63,6 +64,7 @@ abstract class DibiDriver
/**
* Connects to a database
*
* @throws DibiException
* @return resource
*/
@ -72,6 +74,7 @@ abstract class DibiDriver
/**
* Gets the configuration descriptor
*
* @see DibiDriver::__construct
* @return array
*/
@ -84,6 +87,7 @@ abstract class DibiDriver
/**
* Returns the connection resource
*
* @return resource
*/
final public function getConnection()
@ -166,6 +170,7 @@ abstract class DibiDriver
/**
* Executes the SQL query
*
* @param string SQL statement.
* @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
*
* @return int number of rows or FALSE on error
*/
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
* @return int|bool int on success or FALSE on failure
*
* @return int|FALSE int on success or FALSE on failure
*/
abstract public function insertId();
@ -212,6 +219,7 @@ abstract class DibiDriver
/**
* Returns last error
*
* @return array with items 'message' and 'code'
*/
abstract public function errorInfo();
@ -220,6 +228,7 @@ abstract class DibiDriver
/**
* Escapes the string
*
* @param string unescaped string
* @param bool quote string?
* @return string escaped and optionally quoted string
@ -230,6 +239,7 @@ abstract class DibiDriver
/**
* Delimites identifier (table's or column's name, etc.)
*
* @param string identifier
* @return string delimited identifier
*/
@ -248,6 +258,7 @@ abstract class DibiDriver
/**
* Experimental - injects LIMIT/OFFSET to the SQL query
*
* @param string &$sql The SQL query that will be modified.
* @param int $limit
* @param int $offset

View File

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

View File

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

View File

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