Introduced array-based syntax for specifying field types (Merged branches/dbabs into trunk)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@66403 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2008-11-23 01:01:03 +00:00
parent a3d3fb65a9
commit c7330dd231
14 changed files with 191 additions and 18 deletions

View File

@ -2107,6 +2107,7 @@ class DataObject extends ViewableData implements DataObjectInterface,i18nEntityP
}
}
//TODO: DB ABSTRACTION: IF STATEMENT:
$query->select[] = "\"$baseClass\".ID";
$query->select[] = "if(\"$baseClass\".ClassName,\"$baseClass\".ClassName,'$baseClass') AS RecordClassName";

View File

@ -296,6 +296,10 @@ abstract class Database extends Object {
$newTable = false;
Profiler::mark('requireField');
//Convert the $spec array into a database-specific string
$spec=DB::getConn()->$spec['type']($spec['parts']);
// Collations didn't come in until MySQL 4.1. Anything earlier will throw a syntax error if you try and use
// collations.
if(!$this->supportsCollations()) {

View File

@ -395,6 +395,150 @@ class MySQLDatabase extends Database {
user_error($msg, $errorLevel);
}
/**
* Return a boolean type-formatted string
*
* @params array $values Contains a tokenised list of info about this data type
* @return string
*/
public function boolean($values){
//For reference, this is what typically gets passed to this function:
//$parts=Array('datatype'=>'tinyint', 'precision'=>1, 'sign'=>'unsigned', 'null'=>'not null', 'default'=>$this->default);
//DB::requireField($this->tableName, $this->name, "tinyint(1) unsigned not null default '{$this->defaultVal}'");
return 'tinyint(1) unsigned not null default ' . (int)$values['default'];
}
/**
* Return a date type-formatted string
* For MySQL, we simply return the word 'date', no other parameters are necessary
*
* @params array $values Contains a tokenised list of info about this data type
* @return string
*/
public function date($values){
//For reference, this is what typically gets passed to this function:
//$parts=Array('datatype'=>'date');
//DB::requireField($this->tableName, $this->name, "date");
return 'date';
}
/**
* Return a decimal type-formatted string
*
* @params array $values Contains a tokenised list of info about this data type
* @return string
*/
public function decimal($values){
//For reference, this is what typically gets passed to this function:
//$parts=Array('datatype'=>'decimal', 'precision'=>"$this->wholeSize,$this->decimalSize");
//DB::requireField($this->tableName, $this->name, "decimal($this->wholeSize,$this->decimalSize)");
return 'decimal(' . (int)$values['precision'] . ')';
}
/**
* Return a enum type-formatted string
*
* @params array $values Contains a tokenised list of info about this data type
* @return string
*/
public function enum($values){
//For reference, this is what typically gets passed to this function:
//$parts=Array('datatype'=>'enum', 'enums'=>$this->enum, 'character set'=>'utf8', 'collate'=> 'utf8_general_ci', 'default'=>$this->default);
//DB::requireField($this->tableName, $this->name, "enum('" . implode("','", $this->enum) . "') character set utf8 collate utf8_general_ci default '{$this->default}'");
return 'enum(\'' . implode('\', \'', $values['enums']) . '\') character set utf8 collate utf8_general_ci default \'' . $values['default'] . '\'';
}
/**
* Return a float type-formatted string
* For MySQL, we simply return the word 'date', no other parameters are necessary
*
* @params array $values Contains a tokenised list of info about this data type
* @return string
*/
public function float($values){
//For reference, this is what typically gets passed to this function:
//$parts=Array('datatype'=>'float');
//DB::requireField($this->tableName, $this->name, "float");
return 'float';
}
/**
* Return a int type-formatted string
*
* @params array $values Contains a tokenised list of info about this data type
* @return string
*/
public function int($values){
//For reference, this is what typically gets passed to this function:
//$parts=Array('datatype'=>'int', 'precision'=>11, 'null'=>'not null', 'default'=>(int)$this->default);
//DB::requireField($this->tableName, $this->name, "int(11) not null default '{$this->defaultVal}'");
return 'int(11) not null default ' . (int)$values['default'];
}
/**
* Return a datetime type-formatted string
* For MySQL, we simply return the word 'datetime', no other parameters are necessary
*
* @params array $values Contains a tokenised list of info about this data type
* @return string
*/
public function ssdatetime($values){
//For reference, this is what typically gets passed to this function:
//$parts=Array('datatype'=>'datetime');
//DB::requireField($this->tableName, $this->name, $values);
return 'datetime';
}
/**
* Return a text type-formatted string
*
* @params array $values Contains a tokenised list of info about this data type
* @return string
*/
public function text($values){
//For reference, this is what typically gets passed to this function:
//$parts=Array('datatype'=>'mediumtext', 'character set'=>'utf8', 'collate'=>'utf8_general_ci');
//DB::requireField($this->tableName, $this->name, "mediumtext character set utf8 collate utf8_general_ci");
return 'mediumtext character set utf8 collate utf8_general_ci';
}
/**
* Return a time type-formatted string
* For MySQL, we simply return the word 'time', no other parameters are necessary
*
* @params array $values Contains a tokenised list of info about this data type
* @return string
*/
public function time($values){
//For reference, this is what typically gets passed to this function:
//$parts=Array('datatype'=>'time');
//DB::requireField($this->tableName, $this->name, "time");
return 'time';
}
/**
* Return a varchar type-formatted string
*
* @params array $values Contains a tokenised list of info about this data type
* @return string
*/
public function varchar($values){
//For reference, this is what typically gets passed to this function:
//$parts=Array('datatype'=>'varchar', 'precision'=>$this->size, 'character set'=>'utf8', 'collate'=>'utf8_general_ci');
//DB::requireField($this->tableName, $this->name, "varchar($this->size) character set utf8 collate utf8_general_ci");
return 'varchar(' . $values['precision'] . ') character set utf8 collate utf8_general_ci';
}
}
/**

View File

@ -185,7 +185,10 @@ class Versioned extends DataObjectDecorator {
// Version fields on each root table (including Stage)
if(isset($rootTable)) {
$stageTable = ($stage == $this->defaultStage) ? $table : "{$table}_$stage";
DB::requireField($stageTable, "Version", "int(11) not null default '0'");
//DB::requireField($stageTable, "Version", "int(11) not null default '0'");
$parts=Array('datatype'=>'int', 'precision'=>11, 'null'=>'not null', 'default'=>(int)0);
$values=Array('type'=>'int', 'parts'=>$parts);
DB::requireField($stageTable, 'Version', $values);
}
}
@ -431,6 +434,7 @@ class Versioned extends DataObjectDecorator {
}
// We test for equality - if one of the versions doesn't exist, this will be false
//TODO: DB Abstraction: if statement here:
$stagesAreEqual = DB::query("SELECT if(\"$table1\".Version=\"$table2\".Version,1,0) FROM \"$table1\" INNER JOIN \"$table2\" ON \"$table1\".ID = \"$table2\".ID AND \"$table1\".ID = {$this->owner->ID}")->value();
return !$stagesAreEqual;
}

View File

@ -13,7 +13,9 @@ class Boolean extends DBField {
}
function requireField() {
DB::requireField($this->tableName, $this->name, "tinyint(1) unsigned not null default '{$this->defaultVal}'");
$parts=Array('datatype'=>'tinyint', 'precision'=>1, 'sign'=>'unsigned', 'null'=>'not null', 'default'=>$this->defaultVal);
$values=Array('type'=>'boolean', 'parts'=>$parts);
DB::requireField($this->tableName, $this->name, $values);
}
function nullValue() {
@ -60,4 +62,4 @@ class Boolean extends DBField {
}
?>
?>

View File

@ -175,7 +175,9 @@ class Date extends DBField {
}
function requireField() {
DB::requireField($this->tableName, $this->name, "date");
$parts=Array('datatype'=>'date');
$values=Array('type'=>'date', 'parts'=>$parts);
DB::requireField($this->tableName, $this->name, $values);
}
function InPast() {
@ -241,4 +243,4 @@ class Date extends DBField {
}
}
?>
?>

View File

@ -25,7 +25,9 @@ class Decimal extends DBField {
}
function requireField() {
DB::requireField($this->tableName, $this->name, "decimal($this->wholeSize,$this->decimalSize)");
$parts=Array('datatype'=>'decimal', 'precision'=>"$this->wholeSize,$this->decimalSize");
$values=Array('type'=>'decimal', 'parts'=>$parts);
DB::requireField($this->tableName, $this->name, $values);
}
function saveInto($dataObject) {
@ -57,4 +59,4 @@ class Decimal extends DBField {
}
?>
?>

View File

@ -42,8 +42,10 @@ class Enum extends DBField {
}
function requireField(){
DB::requireField($this->tableName, $this->name, "enum('" . implode("','", $this->enum) . "') character set utf8 collate utf8_general_ci default '{$this->default}'");
}
$parts=Array('datatype'=>'enum', 'enums'=>$this->enum, 'character set'=>'utf8', 'collate'=> 'utf8_general_ci', 'default'=>$this->default);
$values=Array('type'=>'enum', 'parts'=>$parts);
DB::requireField($this->tableName, $this->name, $values);
}
public function scaffoldFormField($title = null, $params = null) {
@ -70,4 +72,4 @@ class Enum extends DBField {
}
}
?>
?>

View File

@ -7,7 +7,9 @@
class Float extends DBField {
function requireField() {
DB::requireField($this->tableName, $this->name, "float");
$parts=Array('datatype'=>'float');
$values=Array('type'=>'float', 'parts'=>$parts);
DB::requireField($this->tableName, $this->name, $values);
}
function Nice() {
@ -41,4 +43,4 @@ class Float extends DBField {
}
}
?>
?>

View File

@ -23,7 +23,9 @@ class Int extends DBField {
}
function requireField() {
DB::requireField($this->tableName, $this->name, "int(11) not null default '{$this->defaultVal}'");
$parts=Array('datatype'=>'int', 'precision'=>11, 'null'=>'not null', 'default'=>(int)$this->defaultVal);
$values=Array('type'=>'int', 'parts'=>$parts);
DB::requireField($this->tableName, $this->name, $values);
}
function Times() {

View File

@ -27,7 +27,9 @@ class SSDatetime extends Date {
}
function requireField() {
DB::requireField($this->tableName, $this->name, "datetime");
$parts=Array('datatype'=>'datetime');
$values=Array('type'=>'ssdatetime', 'parts'=>$parts);
DB::requireField($this->tableName, $this->name, $values);
}
function URLDatetime() {

View File

@ -10,7 +10,9 @@ class Text extends DBField {
);
function requireField() {
DB::requireField($this->tableName, $this->name, "mediumtext character set utf8 collate utf8_general_ci");
$parts=Array('datatype'=>'mediumtext', 'character set'=>'utf8', 'collate'=>'utf8_general_ci');
$values=Array('type'=>'text', 'parts'=>$parts);
DB::requireField($this->tableName, $this->name, $values, $this->default);
}
function hasValue() {

View File

@ -31,11 +31,13 @@ class Time extends DBField {
}
function requireField() {
DB::requireField($this->tableName, $this->name, "time");
$parts=Array('datatype'=>'time');
$values=Array('type'=>'time', 'parts'=>$parts);
DB::requireField($this->tableName, $this->name, $values);
}
public function scaffoldFormField($title = null, $params = null) {
return new TimeField($this->name, $title);
}
}
?>
?>

View File

@ -14,7 +14,9 @@ class Varchar extends DBField {
}
function requireField() {
DB::requireField($this->tableName, $this->name, "varchar($this->size) character set utf8 collate utf8_general_ci");
$parts=Array('datatype'=>'varchar', 'precision'=>$this->size, 'character set'=>'utf8', 'collate'=>'utf8_general_ci');
$values=Array('type'=>'varchar', 'parts'=>$parts);
DB::requireField($this->tableName, $this->name, $values);
}
function hasValue() {