2020-03-16 06:21:11 +09:00
|
|
|
<?php
|
|
|
|
|
2020-05-21 01:53:53 +09:00
|
|
|
// {{{ License
|
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// }}}
|
|
|
|
|
2020-03-22 05:18:05 +09:00
|
|
|
/**
|
|
|
|
* Doctrine metadata driver which implements our old `schemaDef` interface
|
|
|
|
*
|
|
|
|
* @package GNUsocial
|
|
|
|
* @category DB
|
|
|
|
*
|
|
|
|
* @author Hugo Sales <hugo@fc.up.pt>
|
|
|
|
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
|
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2020-06-05 06:00:05 +09:00
|
|
|
namespace App\Core\DB;
|
2020-03-16 06:21:11 +09:00
|
|
|
|
|
|
|
use Doctrine\Persistence\Mapping\ClassMetadata;
|
|
|
|
use Doctrine\Persistence\Mapping\Driver\StaticPHPDriver;
|
2020-03-29 00:40:28 +09:00
|
|
|
use Functional as F;
|
2020-03-16 06:21:11 +09:00
|
|
|
|
|
|
|
class SchemaDefDriver extends StaticPHPDriver
|
|
|
|
{
|
2020-03-18 19:52:08 +09:00
|
|
|
/**
|
2020-07-05 22:12:35 +09:00
|
|
|
* V2 DB type => Doctrine type
|
2020-03-18 19:52:08 +09:00
|
|
|
*/
|
|
|
|
private const types = [
|
2020-07-05 22:12:35 +09:00
|
|
|
'varchar' => 'string',
|
|
|
|
'char' => 'string', // char is a fixed witdh varchar
|
|
|
|
'int' => 'integer',
|
|
|
|
'serial' => 'integer',
|
|
|
|
'tinyint' => 'smallint', // no portable tinyint
|
|
|
|
'bigint' => 'bigint',
|
|
|
|
'bool' => 'boolean',
|
|
|
|
'numeric' => 'decimal',
|
|
|
|
'text' => 'text',
|
|
|
|
'datetime' => 'datetime',
|
|
|
|
'timestamp' => 'datetime',
|
2020-03-18 19:52:08 +09:00
|
|
|
// Unused in V2, but might start being used
|
|
|
|
'date' => 'date',
|
|
|
|
'time' => 'time',
|
|
|
|
'datetimez' => 'datetimez',
|
|
|
|
'object' => 'object',
|
|
|
|
'array' => 'array',
|
|
|
|
'simplearray' => 'simplearray',
|
|
|
|
'json_array' => 'json_array',
|
|
|
|
'float' => 'float',
|
|
|
|
'guid' => 'guid',
|
|
|
|
'blob' => 'blob',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
2020-03-31 02:15:30 +09:00
|
|
|
* Fill in the database $metadata for $class_name
|
2020-03-18 19:52:08 +09:00
|
|
|
*
|
2020-03-31 02:15:30 +09:00
|
|
|
* @param string $class_name
|
2020-03-18 19:52:08 +09:00
|
|
|
* @param ClassMetadata $metadata
|
|
|
|
*/
|
2020-03-31 02:15:30 +09:00
|
|
|
public function loadMetadataForClass($class_name, ClassMetadata $metadata)
|
2020-03-16 06:21:11 +09:00
|
|
|
{
|
2020-03-31 02:15:30 +09:00
|
|
|
$schema = $class_name::schemaDef();
|
2020-03-18 19:52:08 +09:00
|
|
|
|
|
|
|
$metadata->setPrimaryTable(['name' => $schema['name'],
|
2020-03-31 02:15:30 +09:00
|
|
|
'indexes' => self::kv_to_name_col($schema['indexes'] ?? []),
|
|
|
|
'uniqueConstraints' => self::kv_to_name_col($schema['unique keys'] ?? []),
|
2020-03-30 23:43:45 +09:00
|
|
|
'options' => ['comment' => $schema['description'] ?? ''],
|
|
|
|
]);
|
2020-03-18 19:52:08 +09:00
|
|
|
|
|
|
|
foreach ($schema['fields'] as $name => $opts) {
|
2020-03-31 02:15:30 +09:00
|
|
|
// TODO
|
2020-03-18 19:52:08 +09:00
|
|
|
// Convert old to new types
|
2020-05-15 06:56:44 +09:00
|
|
|
$type = $name === 'date'
|
|
|
|
// Old date fields were stored as int, store as datetime/timestamp
|
|
|
|
? 'datetime'
|
|
|
|
// For ints, prepend the size (smallint)
|
|
|
|
// The size field doesn't exist otherwise
|
|
|
|
:
|
|
|
|
self::types[($opts['size'] ?? '') . $opts['type']];
|
2020-03-29 00:40:28 +09:00
|
|
|
|
|
|
|
$unique = null;
|
2020-03-31 02:15:30 +09:00
|
|
|
foreach ($schema['unique keys'] ?? [] as $key => $uniq_arr) {
|
2020-03-29 00:40:28 +09:00
|
|
|
if (in_array($name, $uniq_arr)) {
|
|
|
|
$unique = $key;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-05 22:12:35 +09:00
|
|
|
$default = $opts['default'] ?? null;
|
2020-05-15 06:56:44 +09:00
|
|
|
|
2020-03-18 19:52:08 +09:00
|
|
|
$field = [
|
2020-03-29 00:40:28 +09:00
|
|
|
// boolean, optional
|
|
|
|
'id' => in_array($name, $schema['primary key']),
|
|
|
|
// string
|
2020-03-18 19:52:08 +09:00
|
|
|
'fieldName' => $name,
|
2020-03-29 00:40:28 +09:00
|
|
|
// string
|
|
|
|
'type' => $type,
|
|
|
|
// stringn, optional
|
|
|
|
'unique' => $unique,
|
|
|
|
// String length, ignored if not a string
|
|
|
|
// int, optional
|
|
|
|
'length' => $opts['length'] ?? null,
|
|
|
|
// boolean, optional
|
|
|
|
'nullable' => !($opts['not null'] ?? false),
|
|
|
|
// Numeric precision and scale, ignored if not a number
|
|
|
|
// integer, optional
|
|
|
|
'precision' => $opts['precision'] ?? null,
|
|
|
|
// integer, optional
|
|
|
|
'scale' => $opts['scale'] ?? null,
|
|
|
|
'options' => [
|
2020-03-31 02:15:30 +09:00
|
|
|
'comment' => $opts['description'] ?? null,
|
2020-05-15 06:56:44 +09:00
|
|
|
'default' => $default,
|
2020-05-12 02:39:12 +09:00
|
|
|
'unsigned' => $opts['unsigned'] ?? null,
|
2020-03-31 02:15:30 +09:00
|
|
|
// bool, optional
|
|
|
|
'fixed' => $opts['type'] === 'char',
|
2020-03-18 19:52:08 +09:00
|
|
|
// 'collation' => string, unused
|
|
|
|
// 'check', unused
|
|
|
|
],
|
|
|
|
// 'columnDefinition', unused
|
|
|
|
];
|
|
|
|
// The optional feilds from earlier were populated with null, remove them
|
2020-05-12 02:39:12 +09:00
|
|
|
$field = array_filter($field, F\not('is_null'));
|
2020-03-29 00:40:28 +09:00
|
|
|
$field['options'] = array_filter($field['options'], F\not('is_null'));
|
2020-03-18 19:52:08 +09:00
|
|
|
|
|
|
|
$metadata->mapField($field);
|
2020-03-31 02:15:30 +09:00
|
|
|
if ($opts['type'] === 'serial') {
|
|
|
|
$metadata->setIdGeneratorType($metadata::GENERATOR_TYPE_AUTO);
|
|
|
|
}
|
2020-03-18 19:52:08 +09:00
|
|
|
}
|
|
|
|
// TODO foreign keys
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override StaticPHPDriver's method,
|
|
|
|
* we care about classes that have the method `schemaDef`,
|
|
|
|
* instead of `loadMetadata`.
|
|
|
|
*
|
2020-03-31 02:15:30 +09:00
|
|
|
* @param string $class_name
|
2020-05-12 02:39:12 +09:00
|
|
|
*
|
2020-05-11 05:43:15 +09:00
|
|
|
* @return bool
|
2020-03-18 19:52:08 +09:00
|
|
|
*/
|
2020-03-31 02:15:30 +09:00
|
|
|
public function isTransient($class_name)
|
2020-03-18 19:52:08 +09:00
|
|
|
{
|
2020-03-31 02:15:30 +09:00
|
|
|
return !method_exists($class_name, 'schemaDef');
|
2020-03-18 19:52:08 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert [$key => $val] to ['name' => $key, 'columns' => $val]
|
2020-05-12 02:39:12 +09:00
|
|
|
*
|
2020-05-11 05:43:15 +09:00
|
|
|
* @param array $arr
|
2020-05-12 02:39:12 +09:00
|
|
|
*
|
2020-05-11 05:43:15 +09:00
|
|
|
* @return array
|
2020-03-18 19:52:08 +09:00
|
|
|
*/
|
|
|
|
private static function kv_to_name_col(array $arr): array
|
|
|
|
{
|
|
|
|
$res = [];
|
|
|
|
foreach ($arr as $name => $cols) {
|
|
|
|
$res[] = ['name' => $name, 'columns' => $cols];
|
|
|
|
}
|
|
|
|
return $res;
|
2020-03-16 06:21:11 +09:00
|
|
|
}
|
|
|
|
}
|