API CHANGE: Auto-increment of ID column now DB-specific

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@72921 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Geoff Munn 2009-03-11 21:48:59 +00:00
parent 3201b4fd79
commit cac97f9d78
3 changed files with 14 additions and 9 deletions

View File

@ -182,13 +182,14 @@ class DB {
* @param string $table The name of the table
* @param string $fieldSchema A list of the fields to create, in the same form as DataObject::$db
* @param string $indexSchema A list of indexes to create. The keys of the array are the names of the index.
* @param boolean $hasAutoIncPK A flag indicating that the primary key on this table is an autoincrement type
* The values of the array can be one of:
* - true: Create a single column index on the field named the same as the index.
* - array('fields' => array('A','B','C'), 'type' => 'index/unique/fulltext'): This gives you full
* control over the index.
*/
static function requireTable($table, $fieldSchema = null, $indexSchema = null) {
return DB::$globalConn->requireTable($table, $fieldSchema, $indexSchema);
static function requireTable($table, $fieldSchema = null, $indexSchema = null, $hasAutoIncPK=true) {
return DB::$globalConn->requireTable($table, $fieldSchema, $indexSchema, $hasAutoIncPK);
}
/**

View File

@ -779,7 +779,7 @@ class DataObject extends ViewableData implements DataObjectInterface,i18nEntityP
if((!isset($this->record['ID']) || !$this->record['ID']) && isset($ancestry[0])) {
$baseTable = $ancestry[0];
DB::query("INSERT INTO \"{$baseTable}\" (\"Created\") VALUES (NOW())");
DB::query("INSERT INTO \"{$baseTable}\" (\"Created\") VALUES (" . DB::getConn()->now() . ")");
$this->record['ID'] = DB::getGeneratedID($baseTable);
$this->changed['ID'] = 2;
@ -806,15 +806,16 @@ class DataObject extends ViewableData implements DataObjectInterface,i18nEntityP
if(!$fieldObj instanceof CompositeDBField) {
$fieldObj->setValue($this->record[$fieldName], $this->record);
}
$fieldObj->writeToManipulation($manipulation[$class]);
if($class != $baseTable || $fieldName!='ID')
$fieldObj->writeToManipulation($manipulation[$class]);
}
}
// Add the class name to the base object
if($idx == 0) {
$manipulation[$class]['fields']["LastEdited"] = "now()";
$manipulation[$class]['fields']["LastEdited"] = DB::getConn()->now();
if($dbCommand == 'insert') {
$manipulation[$class]['fields']["Created"] = "now()";
$manipulation[$class]['fields']["Created"] = DB::getConn()->now();
//echo "<li>$this->class - " .get_class($this);
$manipulation[$class]['fields']["ClassName"] = "'$this->class'";
}
@ -2577,7 +2578,8 @@ class DataObject extends ViewableData implements DataObjectInterface,i18nEntityP
$indexes = $this->databaseIndexes();
if($fields) {
DB::requireTable($this->class, $fields, $indexes);
$hasAutoIncPK = ($this->class == ClassInfo::baseDataClass($this->class));
DB::requireTable($this->class, $fields, $indexes, $hasAutoIncPK);
} else {
DB::dontRequireTable($this->class);
}

View File

@ -228,7 +228,7 @@ abstract class Database extends Object {
* - array('fields' => array('A','B','C'), 'type' => 'index/unique/fulltext'): This gives you full
* control over the index.
*/
function requireTable($table, $fieldSchema = null, $indexSchema = null) {
function requireTable($table, $fieldSchema = null, $indexSchema = null, $hasAutoIncPK=true) {
if(!isset($this->tableList[strtolower($table)])) {
$this->transCreateTable($table);
Database::alteration_message("Table $table: created","created");
@ -237,7 +237,7 @@ abstract class Database extends Object {
}
//DB ABSTRACTION: we need to convert this to a db-specific version:
$this->requireField($table, 'ID', DB::getConn()->IdColumn());
$this->requireField($table, 'ID', DB::getConn()->IdColumn(false, $hasAutoIncPK));
// Create custom fields
if($fieldSchema) {
@ -453,6 +453,7 @@ abstract class Database extends Object {
*/
function manipulate($manipulation) {
foreach($manipulation as $table => $writeInfo) {
if(isset($writeInfo['fields']) && $writeInfo['fields']) {
$fieldList = $columnList = $valueList = array();
foreach($writeInfo['fields'] as $fieldName => $fieldVal) {
@ -485,6 +486,7 @@ abstract class Database extends Object {
$columnList[] = "\"ID\"";
$valueList[] = (int)$writeInfo['id'];
}
$columnList = implode(", ", $columnList);
$valueList = implode(", ", $valueList);
$sql = "insert into \"$table\" ($columnList) VALUES ($valueList)";