2020-03-16 06:21:11 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Util;
|
|
|
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* PEAR DB type => Doctrine type
|
|
|
|
*/
|
|
|
|
private const types = [
|
|
|
|
'varchar' => 'string',
|
|
|
|
'int' => 'integer',
|
|
|
|
'tinyint' => 'smallint', // no portable tinyint
|
|
|
|
'bigint' => 'bigint',
|
|
|
|
'bool' => 'boolean',
|
|
|
|
'numeric' => 'decimal',
|
|
|
|
'text' => 'text',
|
|
|
|
'datetime' => 'datetime',
|
|
|
|
// 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',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fill in the database $metadata for $className
|
|
|
|
*
|
|
|
|
* @param string $className
|
|
|
|
* @param ClassMetadata $metadata
|
|
|
|
*/
|
2020-03-16 06:21:11 +09:00
|
|
|
public function loadMetadataForClass($className, ClassMetadata $metadata)
|
|
|
|
{
|
|
|
|
$schema = $className::schemaDef();
|
2020-03-18 19:52:08 +09:00
|
|
|
|
|
|
|
$metadata->setPrimaryTable(['name' => $schema['name'],
|
|
|
|
'indexes' => self::kv_to_name_col($schema['indexes']),
|
|
|
|
'uniqueConstraints' => self::kv_to_name_col($schema['unique keys']),
|
|
|
|
'options' => ['comment' => $schema['description']], ]);
|
|
|
|
|
|
|
|
foreach ($schema['fields'] as $name => $opts) {
|
|
|
|
// Convert old to new types
|
2020-03-29 00:40:28 +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']];
|
|
|
|
|
|
|
|
$unique = null;
|
|
|
|
foreach ($schema['unique keys'] as $key => $uniq_arr) {
|
|
|
|
if (in_array($name, $uniq_arr)) {
|
|
|
|
$unique = $key;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-18 19:52:08 +09:00
|
|
|
'comment' => $opts['description'],
|
2020-03-29 00:40:28 +09:00
|
|
|
'default' => $opts['default'] ?? null,
|
|
|
|
'unsigned' => $opts['unsigned'] ?? null,
|
2020-03-18 19:52:08 +09:00
|
|
|
// 'fixed' => bool, unused
|
|
|
|
// 'collation' => string, unused
|
|
|
|
// 'check', unused
|
|
|
|
],
|
|
|
|
// 'columnDefinition', unused
|
|
|
|
];
|
|
|
|
// The optional feilds from earlier were populated with null, remove them
|
2020-03-29 00:40:28 +09:00
|
|
|
$field = array_filter($field, F\not('is_null'));
|
|
|
|
$field['options'] = array_filter($field['options'], F\not('is_null'));
|
2020-03-18 19:52:08 +09:00
|
|
|
|
|
|
|
$metadata->mapField($field);
|
|
|
|
}
|
|
|
|
// TODO foreign keys
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override StaticPHPDriver's method,
|
|
|
|
* we care about classes that have the method `schemaDef`,
|
|
|
|
* instead of `loadMetadata`.
|
|
|
|
*
|
|
|
|
* @param string $className
|
|
|
|
*/
|
|
|
|
public function isTransient($className)
|
|
|
|
{
|
|
|
|
return !method_exists($className, 'schemaDef');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert [$key => $val] to ['name' => $key, 'columns' => $val]
|
|
|
|
*
|
|
|
|
* @param array
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|