[DATABASE] Re-introduce the enum type abstraction

This commit is contained in:
Alexei Sorokin 2019-09-11 12:48:28 +03:00 committed by Diogo Peralta Cordeiro
parent 6b4344968d
commit 0e0c375e65
4 changed files with 52 additions and 59 deletions

View File

@ -17,9 +17,9 @@
/** /**
* Database schema for MariaDB * Database schema for MariaDB
* *
* @category Database * @category Database
* @package GNUsocial * @package GNUsocial
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @copyright 2019 Free Software Foundation, Inc http://www.fsf.org * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/ */
@ -38,8 +38,7 @@ defined('GNUSOCIAL') || die();
*/ */
class MysqlSchema extends Schema class MysqlSchema extends Schema
{ {
static $_single = null; public static $_single = null;
protected $conn = null;
/** /**
* Main public entry point. Use this to get * Main public entry point. Use this to get
@ -48,8 +47,7 @@ class MysqlSchema extends Schema
* @param null $conn * @param null $conn
* @return Schema the (single) Schema object * @return Schema the (single) Schema object
*/ */
public static function get($conn = null)
static function get($conn = null)
{ {
if (empty(self::$_single)) { if (empty(self::$_single)) {
self::$_single = new Schema($conn); self::$_single = new Schema($conn);
@ -69,7 +67,6 @@ class MysqlSchema extends Schema
* @throws PEAR_Exception * @throws PEAR_Exception
* @throws SchemaTableMissingException * @throws SchemaTableMissingException
*/ */
public function getTableDef($table) public function getTableDef($table)
{ {
$def = []; $def = [];
@ -82,7 +79,6 @@ class MysqlSchema extends Schema
} }
foreach ($columns as $row) { foreach ($columns as $row) {
$name = $row['COLUMN_NAME']; $name = $row['COLUMN_NAME'];
$field = []; $field = [];
@ -166,9 +162,9 @@ class MysqlSchema extends Schema
if ($name == 'PRIMARY') { if ($name == 'PRIMARY') {
$type = 'primary key'; $type = 'primary key';
} else if ($row['Non_unique'] == 0) { } elseif ($row['Non_unique'] == 0) {
$type = 'unique keys'; $type = 'unique keys';
} else if ($row['Index_type'] == 'FULLTEXT') { } elseif ($row['Index_type'] === 'FULLTEXT') {
$type = 'fulltext indexes'; $type = 'fulltext indexes';
} else { } else {
$type = 'indexes'; $type = 'indexes';
@ -198,8 +194,7 @@ class MysqlSchema extends Schema
* @throws PEAR_Exception * @throws PEAR_Exception
* @throws SchemaTableMissingException * @throws SchemaTableMissingException
*/ */
public function getTableProperties($table, $props)
function getTableProperties($table, $props)
{ {
$data = $this->fetchMetaInfo($table, 'TABLES'); $data = $this->fetchMetaInfo($table, 'TABLES');
if ($data) { if ($data) {
@ -218,7 +213,7 @@ class MysqlSchema extends Schema
* @return array of arrays * @return array of arrays
* @throws PEAR_Exception * @throws PEAR_Exception
*/ */
function fetchMetaInfo($table, $infoTable, $orderBy = null) public function fetchMetaInfo($table, $infoTable, $orderBy = null)
{ {
$query = "SELECT * FROM INFORMATION_SCHEMA.%s " . $query = "SELECT * FROM INFORMATION_SCHEMA.%s " .
"WHERE TABLE_SCHEMA='%s' AND TABLE_NAME='%s'"; "WHERE TABLE_SCHEMA='%s' AND TABLE_NAME='%s'";
@ -237,7 +232,7 @@ class MysqlSchema extends Schema
* @return array of arrays * @return array of arrays
* @throws PEAR_Exception * @throws PEAR_Exception
*/ */
function fetchIndexInfo($table) public function fetchIndexInfo($table)
{ {
$query = "SHOW INDEX FROM `%s`"; $query = "SHOW INDEX FROM `%s`";
$sql = sprintf($query, $table); $sql = sprintf($query, $table);
@ -253,7 +248,7 @@ class MysqlSchema extends Schema
* @param string $name * @param string $name
* @param array $def * @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); $statements[] = "CREATE FULLTEXT INDEX $name ON $table " . $this->buildIndexList($def);
} }
@ -266,13 +261,13 @@ class MysqlSchema extends Schema
* @return string; * @return string;
* *
*/ */
function endCreateTable($name, array $def) public function endCreateTable($name, array $def)
{ {
$engine = $this->preferredEngine($def); $engine = $this->preferredEngine($def);
return ") ENGINE=$engine CHARACTER SET utf8mb4 COLLATE utf8mb4_bin"; 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 /* MyISAM is no longer required for fulltext indexes, fortunately
if (!empty($def['fulltext indexes'])) { if (!empty($def['fulltext indexes'])) {
@ -288,7 +283,7 @@ class MysqlSchema extends Schema
* @param $columnName * @param $columnName
* @return string * @return string
*/ */
function _uniqueKey($tableName, $columnName) public function _uniqueKey($tableName, $columnName)
{ {
return $this->_key($tableName, $columnName); return $this->_key($tableName, $columnName);
} }
@ -299,7 +294,7 @@ class MysqlSchema extends Schema
* @param $columnName * @param $columnName
* @return string * @return string
*/ */
function _key($tableName, $columnName) public function _key($tableName, $columnName)
{ {
return "{$tableName}_{$columnName}_idx"; return "{$tableName}_{$columnName}_idx";
} }
@ -310,7 +305,7 @@ class MysqlSchema extends Schema
* *
* @param array $phrase * @param array $phrase
*/ */
function appendAlterDropPrimary(array &$phrase) public function appendAlterDropPrimary(array &$phrase)
{ {
$phrase[] = 'DROP PRIMARY KEY'; $phrase[] = 'DROP PRIMARY KEY';
} }
@ -322,7 +317,7 @@ class MysqlSchema extends Schema
* @param array $phrase * @param array $phrase
* @param string $keyName MySQL * @param string $keyName MySQL
*/ */
function appendAlterDropUnique(array &$phrase, $keyName) public function appendAlterDropUnique(array &$phrase, $keyName)
{ {
$phrase[] = 'DROP INDEX ' . $keyName; $phrase[] = 'DROP INDEX ' . $keyName;
} }
@ -335,7 +330,7 @@ class MysqlSchema extends Schema
* @param array $def * @param array $def
* @throws Exception * @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 // Check for table properties: make sure we're using a sane
// engine type and charset/collation. // engine type and charset/collation.
@ -370,15 +365,15 @@ class MysqlSchema extends Schema
* Appropriate for use in CREATE TABLE or * Appropriate for use in CREATE TABLE or
* ALTER TABLE statements. * ALTER TABLE statements.
* *
* @param string $name column name to create
* @param array $cd column to create * @param array $cd column to create
* *
* @return string correct SQL for that column * @return string correct SQL for that column
*/ */
public function columnSql(string $name, array $cd)
function columnSql(array $cd)
{ {
$line = []; $line = [];
$line[] = parent::columnSql($cd); $line[] = parent::columnSql($name, $cd);
// This'll have been added from our transform of 'serial' type // This'll have been added from our transform of 'serial' type
if (!empty($cd['auto_increment'])) { if (!empty($cd['auto_increment'])) {
@ -393,7 +388,7 @@ class MysqlSchema extends Schema
return implode(' ', $line); return implode(' ', $line);
} }
function mapType($column) public function mapType($column)
{ {
$map = [ $map = [
'integer' => 'int', 'integer' => 'int',
@ -411,7 +406,7 @@ class MysqlSchema extends Schema
if ($type == 'int' && if ($type == 'int' &&
in_array($size, ['tiny', 'small', 'medium', 'big'])) { in_array($size, ['tiny', 'small', 'medium', 'big'])) {
$type = $size . $type; $type = $size . $type;
} else if (in_array($type, ['blob', 'text']) && } elseif (in_array($type, ['blob', 'text']) &&
in_array($size, ['tiny', 'medium', 'long'])) { in_array($size, ['tiny', 'medium', 'long'])) {
$type = $size . $type; $type = $size . $type;
} }
@ -420,13 +415,15 @@ class MysqlSchema extends Schema
return $type; return $type;
} }
function typeAndSize($column) public function typeAndSize(string $name, array $column)
{ {
if ($column['type'] == 'enum') { if ($column['type'] === 'enum') {
$vals = array_map([$this, 'quote'], $column['enum']); foreach ($column['enum'] as &$val) {
$vals[] = "'" . $val . "'";
}
return 'enum(' . implode(',', $vals) . ')'; return 'enum(' . implode(',', $vals) . ')';
} else if ($this->_isString($column)) { } elseif ($this->_isString($column)) {
$col = parent::typeAndSize($column); $col = parent::typeAndSize($name, $column);
if (!empty($column['charset'])) { if (!empty($column['charset'])) {
$col .= ' CHARSET ' . $column['charset']; $col .= ' CHARSET ' . $column['charset'];
} }
@ -435,7 +432,7 @@ class MysqlSchema extends Schema
} }
return $col; return $col;
} else { } else {
return parent::typeAndSize($column); return parent::typeAndSize($name, $column);
} }
} }
@ -449,7 +446,7 @@ class MysqlSchema extends Schema
* @param array $tableDef * @param array $tableDef
* @return array * @return array
*/ */
function filterDef(array $tableDef) public function filterDef(array $tableDef)
{ {
$version = $this->conn->getVersion(); $version = $this->conn->getVersion();
foreach ($tableDef['fields'] as $name => &$col) { foreach ($tableDef['fields'] as $name => &$col) {

View File

@ -40,7 +40,6 @@ defined('GNUSOCIAL') || die();
*/ */
class PgsqlSchema extends Schema class PgsqlSchema extends Schema
{ {
/** /**
* Returns a table definition array for the table * Returns a table definition array for the table
* in the schema with the given name. * in the schema with the given name.
@ -277,15 +276,15 @@ class PgsqlSchema extends Schema
* Appropriate for use in CREATE TABLE or * Appropriate for use in CREATE TABLE or
* ALTER TABLE statements. * ALTER TABLE statements.
* *
* @param string $name column name to create
* @param array $cd column to create * @param array $cd column to create
* *
* @return string correct SQL for that column * @return string correct SQL for that column
*/ */
public function columnSql(string $name, array $cd)
public function columnSql(array $cd)
{ {
$line = []; $line = [];
$line[] = parent::columnSql($cd); $line[] = parent::columnSql($name, $cd);
/* /*
if ($table['foreign keys'][$name]) { 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); return implode(' ', $line);
} }
@ -313,8 +319,8 @@ class PgsqlSchema extends Schema
{ {
$prefix = 'ALTER COLUMN ' . $this->quoteIdentifier($columnName) . ' '; $prefix = 'ALTER COLUMN ' . $this->quoteIdentifier($columnName) . ' ';
$oldType = $this->mapType($old); $oldType = $this->typeAndSize($columnName, $old);
$newType = $this->mapType($cd); $newType = $this->typeAndSize($columnName, $cd);
if ($oldType != $newType) { if ($oldType != $newType) {
$phrase[] = $prefix . 'TYPE ' . $newType; $phrase[] = $prefix . 'TYPE ' . $newType;
} }
@ -353,6 +359,7 @@ class PgsqlSchema extends Schema
'numeric' => 'decimal', 'numeric' => 'decimal',
'datetime' => 'timestamp', 'datetime' => 'timestamp',
'blob' => 'bytea', 'blob' => 'bytea',
'enum' => 'text',
]; ];
$type = $column['type']; $type = $column['type'];
@ -375,17 +382,6 @@ class PgsqlSchema extends Schema
return $type; 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 * Filter the given table definition array to match features available
* in this database. * in this database.

View File

@ -709,7 +709,7 @@ class Schema
$phrase[] = 'ADD COLUMN ' . $phrase[] = 'ADD COLUMN ' .
$this->quoteIdentifier($columnName) . $this->quoteIdentifier($columnName) .
' ' . ' ' .
$this->columnSql($cd); $this->columnSql($columnName, $cd);
} }
/** /**
@ -726,7 +726,7 @@ class Schema
$phrase[] = 'MODIFY COLUMN ' . $phrase[] = 'MODIFY COLUMN ' .
$this->quoteIdentifier($columnName) . $this->quoteIdentifier($columnName) .
' ' . ' ' .
$this->columnSql($cd); $this->columnSql($columnName, $cd);
} }
/** /**
@ -870,15 +870,15 @@ class Schema
* Appropriate for use in CREATE TABLE or * Appropriate for use in CREATE TABLE or
* ALTER TABLE statements. * ALTER TABLE statements.
* *
* @param string $name column name to create
* @param array $cd column to create * @param array $cd column to create
* *
* @return string correct SQL for that column * @return string correct SQL for that column
*/ */
public function columnSql(string $name, array $cd)
public function columnSql(array $cd)
{ {
$line = []; $line = [];
$line[] = $this->typeAndSize($cd); $line[] = $this->typeAndSize($name, $cd);
if (isset($cd['default'])) { if (isset($cd['default'])) {
$line[] = 'default'; $line[] = 'default';
@ -901,7 +901,7 @@ class Schema
return $column; return $column;
} }
public function typeAndSize(array $column) public function typeAndSize(string $name, array $column)
{ {
//$type = $this->mapType($column)['type']; //$type = $this->mapType($column)['type'];
$type = $column['type']; $type = $column['type'];

View File

@ -68,7 +68,7 @@ class FeedSub extends Managed_DataObject
'uri' => array('type' => 'varchar', 'not null' => true, 'length' => 191, 'description' => 'FeedSub uri'), 'uri' => array('type' => 'varchar', 'not null' => true, 'length' => 191, 'description' => 'FeedSub uri'),
'huburi' => array('type' => 'text', 'description' => 'FeedSub hub-uri'), 'huburi' => array('type' => 'text', 'description' => 'FeedSub hub-uri'),
'secret' => array('type' => 'text', 'description' => 'FeedSub stored secret'), '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_start' => array('type' => 'datetime', 'description' => 'subscription start'),
'sub_end' => array('type' => 'datetime', 'description' => 'subscription end'), 'sub_end' => array('type' => 'datetime', 'description' => 'subscription end'),
'last_update' => array('type' => 'datetime', 'description' => 'when this record was last updated'), 'last_update' => array('type' => 'datetime', 'description' => 'when this record was last updated'),