2013-06-21 00:32:08 +02:00
|
|
|
<?php
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
namespace SilverStripe\ORM\Connect;
|
|
|
|
|
|
|
|
use Config;
|
|
|
|
use Convert;
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Represents schema management object for MySQL
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @package framework
|
2016-06-15 06:03:16 +02:00
|
|
|
* @subpackage orm
|
2013-06-21 00:32:08 +02:00
|
|
|
*/
|
|
|
|
class MySQLSchemaManager extends DBSchemaManager {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Identifier for this schema, used for configuring schema-specific table
|
2013-06-21 00:32:08 +02:00
|
|
|
* creation options
|
2016-06-23 01:37:22 +02:00
|
|
|
*
|
|
|
|
* @skipUpgrade
|
2013-06-21 00:32:08 +02:00
|
|
|
*/
|
2016-06-23 01:37:22 +02:00
|
|
|
const ID = 'MySQLDatabase';
|
2013-06-21 00:32:08 +02:00
|
|
|
|
|
|
|
public function createTable($table, $fields = null, $indexes = null, $options = null, $advancedOptions = null) {
|
|
|
|
$fieldSchemas = $indexSchemas = "";
|
|
|
|
|
|
|
|
if (!empty($options[self::ID])) {
|
|
|
|
$addOptions = $options[self::ID];
|
|
|
|
} else {
|
|
|
|
$addOptions = "ENGINE=InnoDB";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($fields['ID'])) {
|
|
|
|
$fields['ID'] = "int(11) not null auto_increment";
|
|
|
|
}
|
|
|
|
if ($fields) {
|
|
|
|
foreach ($fields as $k => $v)
|
|
|
|
$fieldSchemas .= "\"$k\" $v,\n";
|
|
|
|
}
|
|
|
|
if ($indexes) {
|
|
|
|
foreach ($indexes as $k => $v) {
|
|
|
|
$indexSchemas .= $this->getIndexSqlDefinition($k, $v) . ",\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Switch to "CREATE TEMPORARY TABLE" for temporary tables
|
|
|
|
$temporary = empty($options['temporary'])
|
|
|
|
? ""
|
|
|
|
: "TEMPORARY";
|
|
|
|
|
|
|
|
$this->query("CREATE $temporary TABLE \"$table\" (
|
|
|
|
$fieldSchemas
|
|
|
|
$indexSchemas
|
|
|
|
primary key (ID)
|
|
|
|
) {$addOptions}");
|
|
|
|
|
|
|
|
return $table;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function alterTable($tableName, $newFields = null, $newIndexes = null, $alteredFields = null,
|
|
|
|
$alteredIndexes = null, $alteredOptions = null, $advancedOptions = null
|
|
|
|
) {
|
|
|
|
if ($this->isView($tableName)) {
|
|
|
|
$this->alterationMessage(
|
|
|
|
sprintf("Table %s not changed as it is a view", $tableName),
|
|
|
|
"changed"
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$alterList = array();
|
|
|
|
|
|
|
|
if ($newFields) {
|
|
|
|
foreach ($newFields as $k => $v) {
|
|
|
|
$alterList[] .= "ADD \"$k\" $v";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($newIndexes) {
|
|
|
|
foreach ($newIndexes as $k => $v) {
|
|
|
|
$alterList[] .= "ADD " . $this->getIndexSqlDefinition($k, $v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($alteredFields) {
|
|
|
|
foreach ($alteredFields as $k => $v) {
|
|
|
|
$alterList[] .= "CHANGE \"$k\" \"$k\" $v";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($alteredIndexes) {
|
|
|
|
foreach ($alteredIndexes as $k => $v) {
|
|
|
|
$alterList[] .= "DROP INDEX \"$k\"";
|
|
|
|
$alterList[] .= "ADD " . $this->getIndexSqlDefinition($k, $v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-07 01:50:48 +01:00
|
|
|
$dbID = self::ID;
|
|
|
|
if ($alteredOptions && isset($alteredOptions[$dbID])) {
|
2013-06-21 00:32:08 +02:00
|
|
|
$indexList = $this->indexList($tableName);
|
|
|
|
$skip = false;
|
|
|
|
foreach ($indexList as $index) {
|
|
|
|
if ($index['type'] === 'fulltext') {
|
|
|
|
$skip = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($skip) {
|
|
|
|
$this->alterationMessage(
|
|
|
|
sprintf(
|
|
|
|
"Table %s options not changed to %s due to fulltextsearch index",
|
|
|
|
$tableName,
|
2016-03-07 01:50:48 +01:00
|
|
|
$alteredOptions[$dbID]
|
2013-06-21 00:32:08 +02:00
|
|
|
),
|
|
|
|
"changed"
|
|
|
|
);
|
|
|
|
} else {
|
2016-03-07 01:50:48 +01:00
|
|
|
$this->query(sprintf("ALTER TABLE \"%s\" %s", $tableName, $alteredOptions[$dbID]));
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->alterationMessage(
|
2016-03-07 01:50:48 +01:00
|
|
|
sprintf("Table %s options changed: %s", $tableName, $alteredOptions[$dbID]),
|
2013-06-21 00:32:08 +02:00
|
|
|
"changed"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$alterations = implode(",\n", $alterList);
|
|
|
|
$this->query("ALTER TABLE \"$tableName\" $alterations");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isView($tableName) {
|
|
|
|
$info = $this->query("SHOW /*!50002 FULL*/ TABLES LIKE '$tableName'")->record();
|
|
|
|
return $info && strtoupper($info['Table_type']) == 'VIEW';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function renameTable($oldTableName, $newTableName) {
|
|
|
|
$this->query("ALTER TABLE \"$oldTableName\" RENAME \"$newTableName\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function checkAndRepairTable($tableName) {
|
2015-10-13 10:09:22 +02:00
|
|
|
// Flag to ensure we only send the warning about PDO + native mode once
|
|
|
|
static $pdo_warning_sent = false;
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// If running PDO and not in emulated mode, check table will fail
|
|
|
|
if($this->database->getConnector() instanceof PDOConnector && !PDOConnector::is_emulate_prepare()) {
|
2015-10-13 10:09:22 +02:00
|
|
|
if (!$pdo_warning_sent) {
|
|
|
|
$this->alterationMessage('CHECK TABLE command disabled for PDO in native mode', 'notice');
|
|
|
|
$pdo_warning_sent = true;
|
|
|
|
}
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Perform check
|
|
|
|
if (!$this->runTableCheckCommand("CHECK TABLE \"$tableName\"")) {
|
|
|
|
if ($this->runTableCheckCommand("CHECK TABLE \"" . strtolower($tableName) . "\"")) {
|
|
|
|
$this->alterationMessage(
|
2014-08-15 08:53:05 +02:00
|
|
|
"Table $tableName: renamed from lowercase",
|
2013-06-21 00:32:08 +02:00
|
|
|
"repaired"
|
|
|
|
);
|
|
|
|
return $this->renameTable(strtolower($tableName), $tableName);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->alterationMessage(
|
2014-08-15 08:53:05 +02:00
|
|
|
"Table $tableName: repaired",
|
2013-06-21 00:32:08 +02:00
|
|
|
"repaired"
|
|
|
|
);
|
|
|
|
return $this->runTableCheckCommand("REPAIR TABLE \"$tableName\" USE_FRM");
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function used by checkAndRepairTable.
|
|
|
|
* @param string $sql Query to run.
|
|
|
|
* @return boolean Returns if the query returns a successful result.
|
|
|
|
*/
|
|
|
|
protected function runTableCheckCommand($sql) {
|
|
|
|
$testResults = $this->query($sql);
|
|
|
|
foreach ($testResults as $testRecord) {
|
|
|
|
if (strtolower($testRecord['Msg_text']) != 'ok') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasTable($table) {
|
|
|
|
// MySQLi doesn't like parameterised queries for some queries
|
|
|
|
$sqlTable = $this->database->quoteString($table);
|
|
|
|
return (bool) ($this->query("SHOW TABLES LIKE $sqlTable")->value());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createField($tableName, $fieldName, $fieldSpec) {
|
|
|
|
$this->query("ALTER TABLE \"$tableName\" ADD \"$fieldName\" $fieldSpec");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function databaseList() {
|
|
|
|
return $this->query("SHOW DATABASES")->column();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function databaseExists($name) {
|
|
|
|
// MySQLi doesn't like parameterised queries for some queries
|
|
|
|
$sqlName = $this->database->quoteString($name);
|
|
|
|
return !!($this->query("SHOW DATABASES LIKE $sqlName")->value());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createDatabase($name) {
|
2016-06-15 06:03:16 +02:00
|
|
|
$charset = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'charset');
|
|
|
|
$collation = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'collation');
|
2015-09-05 02:50:26 +02:00
|
|
|
$this->query("CREATE DATABASE \"$name\" DEFAULT CHARACTER SET {$charset} DEFAULT COLLATE {$collation}");
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function dropDatabase($name) {
|
|
|
|
$this->query("DROP DATABASE \"$name\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the database type of the given field.
|
|
|
|
* @param string $tableName The name of the tbale the field is in.
|
|
|
|
* @param string $fieldName The name of the field to change.
|
|
|
|
* @param string $fieldSpec The new field specification
|
|
|
|
*/
|
|
|
|
public function alterField($tableName, $fieldName, $fieldSpec) {
|
|
|
|
$this->query("ALTER TABLE \"$tableName\" CHANGE \"$fieldName\" \"$fieldName\" $fieldSpec");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the database column name of the given field.
|
|
|
|
*
|
|
|
|
* @param string $tableName The name of the tbale the field is in.
|
|
|
|
* @param string $oldName The name of the field to change.
|
|
|
|
* @param string $newName The new name of the field
|
|
|
|
*/
|
|
|
|
public function renameField($tableName, $oldName, $newName) {
|
|
|
|
$fieldList = $this->fieldList($tableName);
|
|
|
|
if (array_key_exists($oldName, $fieldList)) {
|
|
|
|
$this->query("ALTER TABLE \"$tableName\" CHANGE \"$oldName\" \"$newName\" " . $fieldList[$oldName]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static $_cache_collation_info = array();
|
|
|
|
|
|
|
|
public function fieldList($table) {
|
|
|
|
$fields = $this->query("SHOW FULL FIELDS IN \"$table\"");
|
2016-06-17 06:27:45 +02:00
|
|
|
$fieldList = array();
|
2013-06-21 00:32:08 +02:00
|
|
|
foreach ($fields as $field) {
|
2016-06-17 06:27:45 +02:00
|
|
|
$fieldSpec = $field['Type'];
|
2013-06-21 00:32:08 +02:00
|
|
|
if (!$field['Null'] || $field['Null'] == 'NO') {
|
|
|
|
$fieldSpec .= ' not null';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($field['Collation'] && $field['Collation'] != 'NULL') {
|
|
|
|
// Cache collation info to cut down on database traffic
|
|
|
|
if (!isset(self::$_cache_collation_info[$field['Collation']])) {
|
|
|
|
self::$_cache_collation_info[$field['Collation']]
|
|
|
|
= $this->query("SHOW COLLATION LIKE '{$field['Collation']}'")->record();
|
|
|
|
}
|
|
|
|
$collInfo = self::$_cache_collation_info[$field['Collation']];
|
|
|
|
$fieldSpec .= " character set $collInfo[Charset] collate $field[Collation]";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($field['Default'] || $field['Default'] === "0") {
|
|
|
|
$fieldSpec .= " default " . $this->database->quoteString($field['Default']);
|
|
|
|
}
|
|
|
|
if ($field['Extra']) $fieldSpec .= " " . $field['Extra'];
|
|
|
|
|
|
|
|
$fieldList[$field['Field']] = $fieldSpec;
|
|
|
|
}
|
|
|
|
return $fieldList;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an index on a table.
|
|
|
|
*
|
|
|
|
* @param string $tableName The name of the table.
|
|
|
|
* @param string $indexName The name of the index.
|
|
|
|
* @param string $indexSpec The specification of the index, see {@link SS_Database::requireIndex()} for more
|
|
|
|
* details.
|
|
|
|
*/
|
|
|
|
public function createIndex($tableName, $indexName, $indexSpec) {
|
|
|
|
$this->query("ALTER TABLE \"$tableName\" ADD " . $this->getIndexSqlDefinition($indexName, $indexSpec));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate SQL suitable for creating this index
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string $indexName
|
|
|
|
* @param string|array $indexSpec See {@link requireTable()} for details
|
|
|
|
* @return string MySQL compatible ALTER TABLE syntax
|
|
|
|
*/
|
|
|
|
protected function getIndexSqlDefinition($indexName, $indexSpec) {
|
|
|
|
$indexSpec = $this->parseIndexSpec($indexName, $indexSpec);
|
|
|
|
if ($indexSpec['type'] == 'using') {
|
|
|
|
return "index \"$indexName\" using ({$indexSpec['value']})";
|
|
|
|
} else {
|
|
|
|
return "{$indexSpec['type']} \"$indexName\" ({$indexSpec['value']})";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function alterIndex($tableName, $indexName, $indexSpec) {
|
|
|
|
$indexSpec = $this->parseIndexSpec($indexName, $indexSpec);
|
|
|
|
$this->query("ALTER TABLE \"$tableName\" DROP INDEX \"$indexName\"");
|
|
|
|
$this->query("ALTER TABLE \"$tableName\" ADD {$indexSpec['type']} \"$indexName\" {$indexSpec['value']}");
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
protected function indexKey($table, $index, $spec) {
|
|
|
|
// MySQL simply uses the same index name as SilverStripe does internally
|
|
|
|
return $index;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function indexList($table) {
|
|
|
|
$indexes = $this->query("SHOW INDEXES IN \"$table\"");
|
|
|
|
$groupedIndexes = array();
|
|
|
|
$indexList = array();
|
|
|
|
|
|
|
|
foreach ($indexes as $index) {
|
|
|
|
$groupedIndexes[$index['Key_name']]['fields'][$index['Seq_in_index']] = $index['Column_name'];
|
|
|
|
|
|
|
|
if ($index['Index_type'] == 'FULLTEXT') {
|
|
|
|
$groupedIndexes[$index['Key_name']]['type'] = 'fulltext';
|
|
|
|
} else if (!$index['Non_unique']) {
|
|
|
|
$groupedIndexes[$index['Key_name']]['type'] = 'unique';
|
|
|
|
} else if ($index['Index_type'] == 'HASH') {
|
|
|
|
$groupedIndexes[$index['Key_name']]['type'] = 'hash';
|
|
|
|
} else if ($index['Index_type'] == 'RTREE') {
|
|
|
|
$groupedIndexes[$index['Key_name']]['type'] = 'rtree';
|
|
|
|
} else {
|
|
|
|
$groupedIndexes[$index['Key_name']]['type'] = 'index';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($groupedIndexes) {
|
|
|
|
foreach ($groupedIndexes as $index => $details) {
|
|
|
|
ksort($details['fields']);
|
|
|
|
$indexList[$index] = $this->parseIndexSpec($index, array(
|
|
|
|
'name' => $index,
|
|
|
|
'value' => $this->implodeColumnList($details['fields']),
|
|
|
|
'type' => $details['type']
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $indexList;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tableList() {
|
|
|
|
$tables = array();
|
|
|
|
foreach ($this->query("SHOW TABLES") as $record) {
|
|
|
|
$table = reset($record);
|
|
|
|
$tables[strtolower($table)] = $table;
|
|
|
|
}
|
|
|
|
return $tables;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function enumValuesForField($tableName, $fieldName) {
|
|
|
|
// Get the enum of all page types from the SiteTree table
|
2014-11-18 00:45:54 +01:00
|
|
|
$classnameinfo = $this->query("DESCRIBE \"$tableName\" \"$fieldName\"")->first();
|
2013-06-21 00:32:08 +02:00
|
|
|
preg_match_all("/'[^,]+'/", $classnameinfo["Type"], $matches);
|
|
|
|
|
|
|
|
$classes = array();
|
|
|
|
foreach ($matches[0] as $value) {
|
|
|
|
$classes[] = stripslashes(trim($value, "'"));
|
|
|
|
}
|
|
|
|
return $classes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dbDataType($type) {
|
|
|
|
$values = Array(
|
|
|
|
'unsigned integer' => 'UNSIGNED'
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($values[$type])) return $values[$type];
|
|
|
|
else return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a boolean type-formatted string
|
|
|
|
*
|
|
|
|
* @param 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' . $this->defaultClause($values);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a date type-formatted string
|
|
|
|
* For MySQL, we simply return the word 'date', no other parameters are necessary
|
|
|
|
*
|
|
|
|
* @param 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
|
|
|
|
*
|
|
|
|
* @param 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)");
|
|
|
|
// Avoid empty strings being put in the db
|
|
|
|
if ($values['precision'] == '') {
|
|
|
|
$precision = 1;
|
|
|
|
} else {
|
|
|
|
$precision = $values['precision'];
|
|
|
|
}
|
|
|
|
|
2015-11-02 01:59:45 +01:00
|
|
|
// Fix format of default value to match precision
|
2013-06-21 00:32:08 +02:00
|
|
|
if (isset($values['default']) && is_numeric($values['default'])) {
|
|
|
|
$decs = strpos($precision, ',') !== false
|
|
|
|
? (int) substr($precision, strpos($precision, ',') + 1)
|
|
|
|
: 0;
|
2015-11-02 01:59:45 +01:00
|
|
|
$values['default'] = number_format($values['default'], $decs, '.', '');
|
|
|
|
} else {
|
|
|
|
unset($values['default']);
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
|
|
|
|
2015-11-02 01:59:45 +01:00
|
|
|
return "decimal($precision) not null" . $this->defaultClause($values);
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a enum type-formatted string
|
|
|
|
*
|
|
|
|
* @param 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}'");
|
|
|
|
$valuesString = implode(",", Convert::raw2sql($values['enums'], true));
|
2016-06-15 06:03:16 +02:00
|
|
|
$charset = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'charset');
|
|
|
|
$collation = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'collation');
|
2015-09-05 02:50:26 +02:00
|
|
|
return "enum($valuesString) character set {$charset} collate {$collation}" . $this->defaultClause($values);
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a set type-formatted string
|
|
|
|
*
|
|
|
|
* @param array $values Contains a tokenised list of info about this data type
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function set($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);
|
2014-08-15 08:53:05 +02:00
|
|
|
//DB::requireField($this->tableName, $this->name, "enum('" . implode("','", $this->enum) . "') character set
|
2013-06-21 00:32:08 +02:00
|
|
|
//utf8 collate utf8_general_ci default '{$this->default}'");
|
|
|
|
$valuesString = implode(",", Convert::raw2sql($values['enums'], true));
|
2016-06-15 06:03:16 +02:00
|
|
|
$charset = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'charset');
|
|
|
|
$collation = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'collation');
|
2015-09-05 02:50:26 +02:00
|
|
|
return "set($valuesString) character set {$charset} collate {$collation}" . $this->defaultClause($values);
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a float type-formatted string
|
|
|
|
* For MySQL, we simply return the word 'date', no other parameters are necessary
|
|
|
|
*
|
|
|
|
* @param 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 not null" . $this->defaultClause($values);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a int type-formatted string
|
|
|
|
*
|
|
|
|
* @param 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" . $this->defaultClause($values);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a datetime type-formatted string
|
|
|
|
* For MySQL, we simply return the word 'datetime', no other parameters are necessary
|
|
|
|
*
|
|
|
|
* @param array $values Contains a tokenised list of info about this data type
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-06-15 06:03:16 +02:00
|
|
|
public function datetime($values) {
|
2013-06-21 00:32:08 +02:00
|
|
|
//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
|
|
|
|
*
|
|
|
|
* @param 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");
|
2016-06-15 06:03:16 +02:00
|
|
|
$charset = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'charset');
|
|
|
|
$collation = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'collation');
|
2015-09-05 02:50:26 +02:00
|
|
|
return 'mediumtext character set ' . $charset . ' collate ' . $collation . $this->defaultClause($values);
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a time type-formatted string
|
|
|
|
* For MySQL, we simply return the word 'time', no other parameters are necessary
|
|
|
|
*
|
|
|
|
* @param 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
|
|
|
|
*
|
|
|
|
* @param 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");
|
|
|
|
$default = $this->defaultClause($values);
|
2016-06-15 06:03:16 +02:00
|
|
|
$charset = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'charset');
|
|
|
|
$collation = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'collation');
|
2015-09-05 02:50:26 +02:00
|
|
|
return "varchar({$values['precision']}) character set {$charset} collate {$collation}{$default}";
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the MySQL-proprietary 'Year' datatype
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param array $values Contains a tokenised list of info about this data type
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function year($values) {
|
|
|
|
return 'year(4)';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function IdColumn($asDbValue = false, $hasAutoIncPK = true) {
|
|
|
|
return 'int(11) not null auto_increment';
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Parses and escapes the default values for a specification
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param array $values Contains a tokenised list of info about this data type
|
|
|
|
* @return string Default clause
|
|
|
|
*/
|
|
|
|
protected function defaultClause($values) {
|
|
|
|
if(isset($values['default'])) {
|
|
|
|
return ' default ' . $this->database->quoteString($values['default']);
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|