|
@@ -3560,13 +3560,54 @@ namespace Tqdev\PhpCrudApi\Column\Reflection {
|
3560
|
3560
|
$this->sanitize();
|
3561
|
3561
|
}
|
3562
|
3562
|
|
|
3563
|
+ private static function parseColumnType(string $columnType, int &$length, int &$precision, int &$scale) /*: void*/
|
|
3564
|
+ {
|
|
3565
|
+ $pos = strpos($columnType, '(');
|
|
3566
|
+ if ($pos) {
|
|
3567
|
+ $dataSize = rtrim(substr($columnType, $pos + 1), ')');
|
|
3568
|
+ if ($length) {
|
|
3569
|
+ $length = (int) $dataSize;
|
|
3570
|
+ } else {
|
|
3571
|
+ $pos = strpos($dataSize, ',');
|
|
3572
|
+ if ($pos) {
|
|
3573
|
+ $precision = (int) substr($dataSize, 0, $pos);
|
|
3574
|
+ $scale = (int) substr($dataSize, $pos + 1);
|
|
3575
|
+ } else {
|
|
3576
|
+ $precision = (int) $dataSize;
|
|
3577
|
+ $scale = 0;
|
|
3578
|
+ }
|
|
3579
|
+ }
|
|
3580
|
+ }
|
|
3581
|
+ }
|
|
3582
|
+
|
|
3583
|
+ private static function getDataSize(int $length, int $precision, int $scale): string
|
|
3584
|
+ {
|
|
3585
|
+ $dataSize = '';
|
|
3586
|
+ if ($length) {
|
|
3587
|
+ $dataSize = $length;
|
|
3588
|
+ } elseif ($precision) {
|
|
3589
|
+ if ($scale) {
|
|
3590
|
+ $dataSize = $precision . ',' . $scale;
|
|
3591
|
+ } else {
|
|
3592
|
+ $dataSize = $precision;
|
|
3593
|
+ }
|
|
3594
|
+ }
|
|
3595
|
+ return $dataSize;
|
|
3596
|
+ }
|
|
3597
|
+
|
3563
|
3598
|
public static function fromReflection(GenericReflection $reflection, array $columnResult): ReflectedColumn
|
3564
|
3599
|
{
|
3565
|
3600
|
$name = $columnResult['COLUMN_NAME'];
|
|
3601
|
+ $dataType = $columnResult['DATA_TYPE'];
|
3566
|
3602
|
$length = (int) $columnResult['CHARACTER_MAXIMUM_LENGTH'];
|
3567
|
|
- $type = $reflection->toJdbcType($columnResult['DATA_TYPE'], $length);
|
3568
|
3603
|
$precision = (int) $columnResult['NUMERIC_PRECISION'];
|
3569
|
3604
|
$scale = (int) $columnResult['NUMERIC_SCALE'];
|
|
3605
|
+ $columnType = $columnResult['COLUMN_TYPE'];
|
|
3606
|
+ if ($columnType) {
|
|
3607
|
+ self::parseColumnType($columnType, $length, $precision, $scale);
|
|
3608
|
+ }
|
|
3609
|
+ $dataSize = self::getDataSize($length, $precision, $scale);
|
|
3610
|
+ $type = $reflection->toJdbcType($dataType, $dataSize);
|
3570
|
3611
|
$nullable = in_array(strtoupper($columnResult['IS_NULLABLE']), ['TRUE', 'YES', 'T', 'Y', '1']);
|
3571
|
3612
|
$pk = false;
|
3572
|
3613
|
$fk = '';
|
|
@@ -4107,9 +4148,10 @@ namespace Tqdev\PhpCrudApi\Column {
|
4107
|
4148
|
|
4108
|
4149
|
private function database(): ReflectedDatabase
|
4109
|
4150
|
{
|
4110
|
|
- if (!$this->database) {
|
4111
|
|
- $this->database = $this->loadDatabase(true);
|
|
4151
|
+ if ($this->database) {
|
|
4152
|
+ return $this->database;
|
4112
|
4153
|
}
|
|
4154
|
+ $this->database = $this->loadDatabase(true);
|
4113
|
4155
|
return $this->database;
|
4114
|
4156
|
}
|
4115
|
4157
|
|
|
@@ -5961,11 +6003,11 @@ namespace Tqdev\PhpCrudApi\Database {
|
5961
|
6003
|
{
|
5962
|
6004
|
switch ($this->driver) {
|
5963
|
6005
|
case 'mysql':
|
5964
|
|
- return 'SELECT "COLUMN_NAME", "IS_NULLABLE", "DATA_TYPE", if ("DATA_TYPE"=\'tinyint\' OR "DATA_TYPE"=\'bit\',SUBSTRING_INDEX(SUBSTRING_INDEX("COLUMN_TYPE",\'(\',-1),\')\',1),"CHARACTER_MAXIMUM_LENGTH") as "CHARACTER_MAXIMUM_LENGTH", "NUMERIC_PRECISION", "NUMERIC_SCALE" FROM "INFORMATION_SCHEMA"."COLUMNS" WHERE "TABLE_NAME" = ? AND "TABLE_SCHEMA" = ?';
|
|
6006
|
+ return 'SELECT "COLUMN_NAME", "IS_NULLABLE", "DATA_TYPE", "CHARACTER_MAXIMUM_LENGTH" as "CHARACTER_MAXIMUM_LENGTH", "NUMERIC_PRECISION", "NUMERIC_SCALE", "COLUMN_TYPE" FROM "INFORMATION_SCHEMA"."COLUMNS" WHERE "TABLE_NAME" = ? AND "TABLE_SCHEMA" = ?';
|
5965
|
6007
|
case 'pgsql':
|
5966
|
|
- return 'SELECT a.attname AS "COLUMN_NAME", case when a.attnotnull then \'NO\' else \'YES\' end as "IS_NULLABLE", pg_catalog.format_type(a.atttypid, -1) as "DATA_TYPE", case when a.atttypmod < 0 then NULL else a.atttypmod-4 end as "CHARACTER_MAXIMUM_LENGTH", case when a.atttypid != 1700 then NULL else ((a.atttypmod - 4) >> 16) & 65535 end as "NUMERIC_PRECISION", case when a.atttypid != 1700 then NULL else (a.atttypmod - 4) & 65535 end as "NUMERIC_SCALE" FROM pg_attribute a JOIN pg_class pgc ON pgc.oid = a.attrelid WHERE pgc.relname = ? AND \'\' <> ? AND a.attnum > 0 AND NOT a.attisdropped;';
|
|
6008
|
+ return 'SELECT a.attname AS "COLUMN_NAME", case when a.attnotnull then \'NO\' else \'YES\' end as "IS_NULLABLE", pg_catalog.format_type(a.atttypid, -1) as "DATA_TYPE", case when a.atttypmod < 0 then NULL else a.atttypmod-4 end as "CHARACTER_MAXIMUM_LENGTH", case when a.atttypid != 1700 then NULL else ((a.atttypmod - 4) >> 16) & 65535 end as "NUMERIC_PRECISION", case when a.atttypid != 1700 then NULL else (a.atttypmod - 4) & 65535 end as "NUMERIC_SCALE", \'\' AS "COLUMN_TYPE" FROM pg_attribute a JOIN pg_class pgc ON pgc.oid = a.attrelid WHERE pgc.relname = ? AND \'\' <> ? AND a.attnum > 0 AND NOT a.attisdropped;';
|
5967
|
6009
|
case 'sqlsrv':
|
5968
|
|
- return 'SELECT c.name AS "COLUMN_NAME", c.is_nullable AS "IS_NULLABLE", t.Name AS "DATA_TYPE", (c.max_length/2) AS "CHARACTER_MAXIMUM_LENGTH", c.precision AS "NUMERIC_PRECISION", c.scale AS "NUMERIC_SCALE" FROM sys.columns c INNER JOIN sys.types t ON c.user_type_id = t.user_type_id WHERE c.object_id = OBJECT_ID(?) AND \'\' <> ?';
|
|
6010
|
+ return 'SELECT c.name AS "COLUMN_NAME", c.is_nullable AS "IS_NULLABLE", t.Name AS "DATA_TYPE", (c.max_length/2) AS "CHARACTER_MAXIMUM_LENGTH", c.precision AS "NUMERIC_PRECISION", c.scale AS "NUMERIC_SCALE", \'\' AS "COLUMN_TYPE" FROM sys.columns c INNER JOIN sys.types t ON c.user_type_id = t.user_type_id WHERE c.object_id = OBJECT_ID(?) AND \'\' <> ?';
|
5969
|
6011
|
}
|
5970
|
6012
|
}
|
5971
|
6013
|
|
|
@@ -6070,7 +6112,7 @@ namespace Tqdev\PhpCrudApi\Database {
|
6070
|
6112
|
return $foreignKeys;
|
6071
|
6113
|
}
|
6072
|
6114
|
|
6073
|
|
- public function toJdbcType(string $type, int $size): string
|
|
6115
|
+ public function toJdbcType(string $type, string $size): string
|
6074
|
6116
|
{
|
6075
|
6117
|
return $this->typeConverter->toJdbc($type, $size);
|
6076
|
6118
|
}
|
|
@@ -6377,7 +6419,7 @@ namespace Tqdev\PhpCrudApi\Database {
|
6377
|
6419
|
'geometry' => true,
|
6378
|
6420
|
];
|
6379
|
6421
|
|
6380
|
|
- public function toJdbc(string $type, int $size): string
|
|
6422
|
+ public function toJdbc(string $type, string $size): string
|
6381
|
6423
|
{
|
6382
|
6424
|
$jdbcType = strtolower($type);
|
6383
|
6425
|
if (isset($this->toJdbc[$this->driver]["$jdbcType($size)"])) {
|
|
@@ -10299,6 +10341,7 @@ namespace Tqdev\PhpCrudApi {
|
10299
|
10341
|
'username' => 'php-crud-api',
|
10300
|
10342
|
'password' => 'php-crud-api',
|
10301
|
10343
|
'database' => 'php-crud-api',
|
|
10344
|
+ 'controllers' => 'records,columns,openapi',
|
10302
|
10345
|
]);
|
10303
|
10346
|
$request = RequestFactory::fromGlobals();
|
10304
|
10347
|
$api = new Api($config);
|