[DATABASE] Re-introduce the enum type abstraction
This commit is contained in:
parent
af63e9a7ad
commit
7ee8aa7838
|
@ -17,9 +17,9 @@
|
|||
/**
|
||||
* Database schema for MariaDB
|
||||
*
|
||||
* @category Database
|
||||
* @package GNUsocial
|
||||
* @author Evan Prodromou <evan@status.net>
|
||||
* @category Database
|
||||
* @package GNUsocial
|
||||
* @author Evan Prodromou <evan@status.net>
|
||||
* @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
|
||||
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||
*/
|
||||
|
@ -38,8 +38,7 @@ defined('GNUSOCIAL') || die();
|
|||
*/
|
||||
class MysqlSchema extends Schema
|
||||
{
|
||||
static $_single = null;
|
||||
protected $conn = null;
|
||||
public static $_single = null;
|
||||
|
||||
/**
|
||||
* Main public entry point. Use this to get
|
||||
|
@ -48,8 +47,7 @@ class MysqlSchema extends Schema
|
|||
* @param null $conn
|
||||
* @return Schema the (single) Schema object
|
||||
*/
|
||||
|
||||
static function get($conn = null)
|
||||
public static function get($conn = null)
|
||||
{
|
||||
if (empty(self::$_single)) {
|
||||
self::$_single = new Schema($conn);
|
||||
|
@ -69,7 +67,6 @@ class MysqlSchema extends Schema
|
|||
* @throws PEAR_Exception
|
||||
* @throws SchemaTableMissingException
|
||||
*/
|
||||
|
||||
public function getTableDef($table)
|
||||
{
|
||||
$def = [];
|
||||
|
@ -82,7 +79,6 @@ class MysqlSchema extends Schema
|
|||
}
|
||||
|
||||
foreach ($columns as $row) {
|
||||
|
||||
$name = $row['COLUMN_NAME'];
|
||||
$field = [];
|
||||
|
||||
|
@ -166,9 +162,9 @@ class MysqlSchema extends Schema
|
|||
|
||||
if ($name == 'PRIMARY') {
|
||||
$type = 'primary key';
|
||||
} else if ($row['Non_unique'] == 0) {
|
||||
} elseif ($row['Non_unique'] == 0) {
|
||||
$type = 'unique keys';
|
||||
} else if ($row['Index_type'] == 'FULLTEXT') {
|
||||
} elseif ($row['Index_type'] === 'FULLTEXT') {
|
||||
$type = 'fulltext indexes';
|
||||
} else {
|
||||
$type = 'indexes';
|
||||
|
@ -198,8 +194,7 @@ class MysqlSchema extends Schema
|
|||
* @throws PEAR_Exception
|
||||
* @throws SchemaTableMissingException
|
||||
*/
|
||||
|
||||
function getTableProperties($table, $props)
|
||||
public function getTableProperties($table, $props)
|
||||
{
|
||||
$data = $this->fetchMetaInfo($table, 'TABLES');
|
||||
if ($data) {
|
||||
|
@ -218,7 +213,7 @@ class MysqlSchema extends Schema
|
|||
* @return array of arrays
|
||||
* @throws PEAR_Exception
|
||||
*/
|
||||
function fetchMetaInfo($table, $infoTable, $orderBy = null)
|
||||
public function fetchMetaInfo($table, $infoTable, $orderBy = null)
|
||||
{
|
||||
$query = "SELECT * FROM INFORMATION_SCHEMA.%s " .
|
||||
"WHERE TABLE_SCHEMA='%s' AND TABLE_NAME='%s'";
|
||||
|
@ -237,7 +232,7 @@ class MysqlSchema extends Schema
|
|||
* @return array of arrays
|
||||
* @throws PEAR_Exception
|
||||
*/
|
||||
function fetchIndexInfo($table)
|
||||
public function fetchIndexInfo($table)
|
||||
{
|
||||
$query = "SHOW INDEX FROM `%s`";
|
||||
$sql = sprintf($query, $table);
|
||||
|
@ -253,7 +248,7 @@ class MysqlSchema extends Schema
|
|||
* @param string $name
|
||||
* @param array $def
|
||||
*/
|
||||
function appendCreateFulltextIndex(array &$statements, $table, $name, array $def)
|
||||
public function appendCreateFulltextIndex(array &$statements, $table, $name, array $def)
|
||||
{
|
||||
$statements[] = "CREATE FULLTEXT INDEX $name ON $table " . $this->buildIndexList($def);
|
||||
}
|
||||
|
@ -266,13 +261,13 @@ class MysqlSchema extends Schema
|
|||
* @return string;
|
||||
*
|
||||
*/
|
||||
function endCreateTable($name, array $def)
|
||||
public function endCreateTable($name, array $def)
|
||||
{
|
||||
$engine = $this->preferredEngine($def);
|
||||
return ") ENGINE=$engine CHARACTER SET utf8mb4 COLLATE utf8mb4_bin";
|
||||
}
|
||||
|
||||
function preferredEngine($def)
|
||||
public function preferredEngine($def)
|
||||
{
|
||||
/* MyISAM is no longer required for fulltext indexes, fortunately
|
||||
if (!empty($def['fulltext indexes'])) {
|
||||
|
@ -288,7 +283,7 @@ class MysqlSchema extends Schema
|
|||
* @param $columnName
|
||||
* @return string
|
||||
*/
|
||||
function _uniqueKey($tableName, $columnName)
|
||||
public function _uniqueKey($tableName, $columnName)
|
||||
{
|
||||
return $this->_key($tableName, $columnName);
|
||||
}
|
||||
|
@ -299,7 +294,7 @@ class MysqlSchema extends Schema
|
|||
* @param $columnName
|
||||
* @return string
|
||||
*/
|
||||
function _key($tableName, $columnName)
|
||||
public function _key($tableName, $columnName)
|
||||
{
|
||||
return "{$tableName}_{$columnName}_idx";
|
||||
}
|
||||
|
@ -310,7 +305,7 @@ class MysqlSchema extends Schema
|
|||
*
|
||||
* @param array $phrase
|
||||
*/
|
||||
function appendAlterDropPrimary(array &$phrase)
|
||||
public function appendAlterDropPrimary(array &$phrase)
|
||||
{
|
||||
$phrase[] = 'DROP PRIMARY KEY';
|
||||
}
|
||||
|
@ -322,7 +317,7 @@ class MysqlSchema extends Schema
|
|||
* @param array $phrase
|
||||
* @param string $keyName MySQL
|
||||
*/
|
||||
function appendAlterDropUnique(array &$phrase, $keyName)
|
||||
public function appendAlterDropUnique(array &$phrase, $keyName)
|
||||
{
|
||||
$phrase[] = 'DROP INDEX ' . $keyName;
|
||||
}
|
||||
|
@ -335,7 +330,7 @@ class MysqlSchema extends Schema
|
|||
* @param array $def
|
||||
* @throws Exception
|
||||
*/
|
||||
function appendAlterExtras(array &$phrase, $tableName, array $def)
|
||||
public function appendAlterExtras(array &$phrase, $tableName, array $def)
|
||||
{
|
||||
// Check for table properties: make sure we're using a sane
|
||||
// engine type and charset/collation.
|
||||
|
@ -370,15 +365,15 @@ class MysqlSchema extends Schema
|
|||
* Appropriate for use in CREATE TABLE or
|
||||
* ALTER TABLE statements.
|
||||
*
|
||||
* @param string $name column name to create
|
||||
* @param array $cd column to create
|
||||
*
|
||||
* @return string correct SQL for that column
|
||||
*/
|
||||
|
||||
function columnSql(array $cd)
|
||||
public function columnSql(string $name, array $cd)
|
||||
{
|
||||
$line = [];
|
||||
$line[] = parent::columnSql($cd);
|
||||
$line[] = parent::columnSql($name, $cd);
|
||||
|
||||
// This'll have been added from our transform of 'serial' type
|
||||
if (!empty($cd['auto_increment'])) {
|
||||
|
@ -393,7 +388,7 @@ class MysqlSchema extends Schema
|
|||
return implode(' ', $line);
|
||||
}
|
||||
|
||||
function mapType($column)
|
||||
public function mapType($column)
|
||||
{
|
||||
$map = [
|
||||
'integer' => 'int',
|
||||
|
@ -411,7 +406,7 @@ class MysqlSchema extends Schema
|
|||
if ($type == 'int' &&
|
||||
in_array($size, ['tiny', 'small', 'medium', 'big'])) {
|
||||
$type = $size . $type;
|
||||
} else if (in_array($type, ['blob', 'text']) &&
|
||||
} elseif (in_array($type, ['blob', 'text']) &&
|
||||
in_array($size, ['tiny', 'medium', 'long'])) {
|
||||
$type = $size . $type;
|
||||
}
|
||||
|
@ -420,13 +415,15 @@ class MysqlSchema extends Schema
|
|||
return $type;
|
||||
}
|
||||
|
||||
function typeAndSize($column)
|
||||
public function typeAndSize(string $name, array $column)
|
||||
{
|
||||
if ($column['type'] == 'enum') {
|
||||
$vals = array_map([$this, 'quote'], $column['enum']);
|
||||
if ($column['type'] === 'enum') {
|
||||
foreach ($column['enum'] as &$val) {
|
||||
$vals[] = "'" . $val . "'";
|
||||
}
|
||||
return 'enum(' . implode(',', $vals) . ')';
|
||||
} else if ($this->_isString($column)) {
|
||||
$col = parent::typeAndSize($column);
|
||||
} elseif ($this->_isString($column)) {
|
||||
$col = parent::typeAndSize($name, $column);
|
||||
if (!empty($column['charset'])) {
|
||||
$col .= ' CHARSET ' . $column['charset'];
|
||||
}
|
||||
|
@ -435,7 +432,7 @@ class MysqlSchema extends Schema
|
|||
}
|
||||
return $col;
|
||||
} else {
|
||||
return parent::typeAndSize($column);
|
||||
return parent::typeAndSize($name, $column);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -449,7 +446,7 @@ class MysqlSchema extends Schema
|
|||
* @param array $tableDef
|
||||
* @return array
|
||||
*/
|
||||
function filterDef(array $tableDef)
|
||||
public function filterDef(array $tableDef)
|
||||
{
|
||||
$version = $this->conn->getVersion();
|
||||
foreach ($tableDef['fields'] as $name => &$col) {
|
||||
|
|
|
@ -40,7 +40,6 @@ defined('GNUSOCIAL') || die();
|
|||
*/
|
||||
class PgsqlSchema extends Schema
|
||||
{
|
||||
|
||||
/**
|
||||
* Returns a table definition array for the table
|
||||
* in the schema with the given name.
|
||||
|
@ -277,15 +276,15 @@ class PgsqlSchema extends Schema
|
|||
* Appropriate for use in CREATE TABLE or
|
||||
* ALTER TABLE statements.
|
||||
*
|
||||
* @param string $name column name to create
|
||||
* @param array $cd column to create
|
||||
*
|
||||
* @return string correct SQL for that column
|
||||
*/
|
||||
|
||||
public function columnSql(array $cd)
|
||||
public function columnSql(string $name, array $cd)
|
||||
{
|
||||
$line = [];
|
||||
$line[] = parent::columnSql($cd);
|
||||
$line[] = parent::columnSql($name, $cd);
|
||||
|
||||
/*
|
||||
if ($table['foreign keys'][$name]) {
|
||||
|
@ -297,6 +296,13 @@ class PgsqlSchema extends Schema
|
|||
}
|
||||
*/
|
||||
|
||||
if (!empty($cd['enum'])) {
|
||||
foreach($cd['enum'] as &$val) {
|
||||
$vals[] = "'" . $val . "'";
|
||||
}
|
||||
$line[] = 'CHECK (' . $name . ' IN (' . implode(',', $vals) . '))';
|
||||
}
|
||||
|
||||
return implode(' ', $line);
|
||||
}
|
||||
|
||||
|
@ -313,8 +319,8 @@ class PgsqlSchema extends Schema
|
|||
{
|
||||
$prefix = 'ALTER COLUMN ' . $this->quoteIdentifier($columnName) . ' ';
|
||||
|
||||
$oldType = $this->mapType($old);
|
||||
$newType = $this->mapType($cd);
|
||||
$oldType = $this->typeAndSize($columnName, $old);
|
||||
$newType = $this->typeAndSize($columnName, $cd);
|
||||
if ($oldType != $newType) {
|
||||
$phrase[] = $prefix . 'TYPE ' . $newType;
|
||||
}
|
||||
|
@ -353,6 +359,7 @@ class PgsqlSchema extends Schema
|
|||
'numeric' => 'decimal',
|
||||
'datetime' => 'timestamp',
|
||||
'blob' => 'bytea',
|
||||
'enum' => 'text',
|
||||
];
|
||||
|
||||
$type = $column['type'];
|
||||
|
@ -375,17 +382,6 @@ class PgsqlSchema extends Schema
|
|||
return $type;
|
||||
}
|
||||
|
||||
// @fixme need name... :P
|
||||
public function typeAndSize($column)
|
||||
{
|
||||
if ($column['type'] == 'enum') {
|
||||
$vals = array_map([$this, 'quote'], $column['enum']);
|
||||
return "text check ($name in " . implode(',', $vals) . ')';
|
||||
} else {
|
||||
return parent::typeAndSize($column);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the given table definition array to match features available
|
||||
* in this database.
|
||||
|
|
|
@ -709,7 +709,7 @@ class Schema
|
|||
$phrase[] = 'ADD COLUMN ' .
|
||||
$this->quoteIdentifier($columnName) .
|
||||
' ' .
|
||||
$this->columnSql($cd);
|
||||
$this->columnSql($columnName, $cd);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -726,7 +726,7 @@ class Schema
|
|||
$phrase[] = 'MODIFY COLUMN ' .
|
||||
$this->quoteIdentifier($columnName) .
|
||||
' ' .
|
||||
$this->columnSql($cd);
|
||||
$this->columnSql($columnName, $cd);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -870,15 +870,15 @@ class Schema
|
|||
* Appropriate for use in CREATE TABLE or
|
||||
* ALTER TABLE statements.
|
||||
*
|
||||
* @param string $name column name to create
|
||||
* @param array $cd column to create
|
||||
*
|
||||
* @return string correct SQL for that column
|
||||
*/
|
||||
|
||||
public function columnSql(array $cd)
|
||||
public function columnSql(string $name, array $cd)
|
||||
{
|
||||
$line = [];
|
||||
$line[] = $this->typeAndSize($cd);
|
||||
$line[] = $this->typeAndSize($name, $cd);
|
||||
|
||||
if (isset($cd['default'])) {
|
||||
$line[] = 'default';
|
||||
|
@ -901,7 +901,7 @@ class Schema
|
|||
return $column;
|
||||
}
|
||||
|
||||
public function typeAndSize(array $column)
|
||||
public function typeAndSize(string $name, array $column)
|
||||
{
|
||||
//$type = $this->mapType($column)['type'];
|
||||
$type = $column['type'];
|
||||
|
|
|
@ -68,7 +68,7 @@ class FeedSub extends Managed_DataObject
|
|||
'uri' => array('type' => 'varchar', 'not null' => true, 'length' => 191, 'description' => 'FeedSub uri'),
|
||||
'huburi' => array('type' => 'text', 'description' => 'FeedSub hub-uri'),
|
||||
'secret' => array('type' => 'text', 'description' => 'FeedSub stored secret'),
|
||||
'sub_state' => array('type' => 'enum("subscribe","active","unsubscribe","inactive","nohub")', 'not null' => true, 'description' => 'subscription state'),
|
||||
'sub_state' => array('type' => 'enum', 'enum' => array('subscribe', 'active', 'unsubscribe', 'inactive', 'nohub'), 'not null' => true, 'description' => 'subscription state'),
|
||||
'sub_start' => array('type' => 'datetime', 'description' => 'subscription start'),
|
||||
'sub_end' => array('type' => 'datetime', 'description' => 'subscription end'),
|
||||
'last_update' => array('type' => 'datetime', 'description' => 'when this record was last updated'),
|
||||
|
|
Loading…
Reference in New Issue
Block a user