pkrenn: getGeneratedID added, fixed class PDOQuery (merged from branches/gsoc)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@41707 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2007-09-14 01:35:54 +00:00
parent c144bf1aef
commit 08b2874a41
4 changed files with 48 additions and 16 deletions

View File

@ -94,8 +94,8 @@ class DB {
* Get the autogenerated ID from the previous INSERT query.
* @return int
*/
static function getGeneratedID() {
return DB::$globalConn->getGeneratedID();
static function getGeneratedID($table) {
return DB::$globalConn->getGeneratedID($table);
}
/**

View File

@ -37,7 +37,7 @@ abstract class Database extends Object {
* Get the autogenerated ID from the previous INSERT query.
* @return int
*/
abstract function getGeneratedID();
abstract function getGeneratedID($table);
/**
* Check if the connection to the database is active.

View File

@ -99,7 +99,7 @@ class MySQLDatabase extends Database {
return new MySQLQuery($this, $handle);
}
public function getGeneratedID() {
public function getGeneratedID($table) {
return mysql_insert_id($this->dbConn);
}

View File

@ -153,7 +153,6 @@ class PDODatabase extends Database {
$stmt = $dbConn->prepare($sql);
$handle = $stmt->execute(); // Execute and save the return value (true or false)
$result = $stmt->fetchAll(); // Get the result itself
if(isset($_REQUEST['showqueries'])) {
$duration = microtime(true) - $starttime;
@ -164,16 +163,21 @@ class PDODatabase extends Database {
$error = $stmt->errorInfo();
$this->databaseError("Couldn't run query: $sql | " . $error[2], $errorLevel);
}
return new PDOQuery($result);
return new PDOQuery($stmt);
}
/**
* Get the ID for the next new record for the table.
* Get the autogenerated ID from the previous INSERT query.
* Simulate mysql_insert_id by fetching the highest ID as there is no other reliable method across databases.
* @return int
*/
public function getGeneratedID() {
public function getGeneratedID($table) {
$stmt = $dbConn->prepare("SELECT MAX(ID) FROM :table");
$stmt->bindParam(":table", $table);
$handle = $stmt->execute();
$result = $stmt->fetchColumn();
return $handle ? $result : 0;
}
@ -512,30 +516,58 @@ class PDODatabase extends Database {
* A result-set from a database query (array).
*/
class PDOQuery extends Query {
private $result;
/**
* The object that holds the result set.
* @var $stmt
*/
private $stmt;
/**
* Hook the result-set given into a Query class, suitable for use by sapphire.
* @param array $result The array of all returned values.
* @param PDO object $stmt The object of all returned values.
*/
public function __construct(PDODatabase $result) {
$this->result = $result;
public function __construct(PDODatabase $stmt) {
$this->stmt = $stmt;
parent::__construct();
}
/**
* Free the result-set given into a Query class.
*/
public function __destroy() {
$this->result = null;
$this->stmt = null;
}
/**
* Determine if a given element is part of the result set.
* @param string string $row The element to search for.
*/
public function seek($row) {
return in_array($row, $this->result);
return in_array($row, $this->stmt->fetchAll());
}
/**
* Return the number of results.
*/
public function numRecords() {
return count($this->result);
$value = $this->stmt->fetchAll();
return count($value);
}
public function nextRecord() {
/**
*
*/
public function nextRecord() {
$record = $this->stmt->fetch(PDO::FETCH_ASSOC);
if (count($record)) {
return $record;
} else {
return false;
}
}
}