Skip to content

Commit d88323e

Browse files
committed
Merge branch 'master' of github.com:mevdschee/php-crud-api
2 parents 3cb99f2 + 0c0c9de commit d88323e

File tree

10 files changed

+590
-396
lines changed

10 files changed

+590
-396
lines changed

api.php

Lines changed: 181 additions & 83 deletions
Large diffs are not rendered by default.

src/Tqdev/PhpCrudApi/Column/Reflection/ReflectedColumn.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,55 @@ public function __construct(string $name, string $type, int $length, int $precis
3232
$this->sanitize();
3333
}
3434

35+
private static function parseColumnType(string $columnType, int &$length, int &$precision, int &$scale) /*: void*/
36+
{
37+
if (!$columnType) {
38+
return;
39+
}
40+
$pos = strpos($columnType, '(');
41+
if ($pos) {
42+
$dataSize = rtrim(substr($columnType, $pos + 1), ')');
43+
if ($length) {
44+
$length = (int) $dataSize;
45+
} else {
46+
$pos = strpos($dataSize, ',');
47+
if ($pos) {
48+
$precision = (int) substr($dataSize, 0, $pos);
49+
$scale = (int) substr($dataSize, $pos + 1);
50+
} else {
51+
$precision = (int) $dataSize;
52+
$scale = 0;
53+
}
54+
}
55+
}
56+
}
57+
58+
private static function getDataSize(int $length, int $precision, int $scale): string
59+
{
60+
$dataSize = '';
61+
if ($length) {
62+
$dataSize = $length;
63+
} elseif ($precision) {
64+
if ($scale) {
65+
$dataSize = $precision . ',' . $scale;
66+
} else {
67+
$dataSize = $precision;
68+
}
69+
}
70+
return $dataSize;
71+
}
72+
3573
public static function fromReflection(GenericReflection $reflection, array $columnResult): ReflectedColumn
3674
{
3775
$name = $columnResult['COLUMN_NAME'];
76+
$dataType = $columnResult['DATA_TYPE'];
3877
$length = (int) $columnResult['CHARACTER_MAXIMUM_LENGTH'];
39-
$type = $reflection->toJdbcType($columnResult['DATA_TYPE'], $length);
4078
$precision = (int) $columnResult['NUMERIC_PRECISION'];
4179
$scale = (int) $columnResult['NUMERIC_SCALE'];
80+
$columnType = $columnResult['COLUMN_TYPE'];
81+
self::parseColumnType($columnType, $length, $precision, $scale);
82+
$dataSize = self::getDataSize($length, $precision, $scale);
83+
$type = $reflection->toJdbcType($dataType, $dataSize);
4284
$nullable = in_array(strtoupper($columnResult['IS_NULLABLE']), ['TRUE', 'YES', 'T', 'Y', '1']);
4385
$pk = false;
4486
$fk = '';

src/Tqdev/PhpCrudApi/Column/ReflectionService.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ public function __construct(GenericDB $db, Cache $cache, int $ttl)
2626

2727
private function database(): ReflectedDatabase
2828
{
29-
if (!$this->database) {
30-
$this->database = $this->loadDatabase(true);
29+
if ($this->database) {
30+
return $this->database;
3131
}
32+
$this->database = $this->loadDatabase(true);
3233
return $this->database;
3334
}
3435

src/Tqdev/PhpCrudApi/Database/GenericReflection.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ private function getTableColumnsSQL(): string
4747
{
4848
switch ($this->driver) {
4949
case 'mysql':
50-
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" = ?';
50+
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" = ?';
5151
case 'pgsql':
52-
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;';
52+
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;';
5353
case 'sqlsrv':
54-
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 \'\' <> ?';
54+
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 \'\' <> ?';
5555
}
5656
}
5757

@@ -156,7 +156,7 @@ public function getTableForeignKeys(string $tableName): array
156156
return $foreignKeys;
157157
}
158158

159-
public function toJdbcType(string $type, int $size): string
159+
public function toJdbcType(string $type, string $size): string
160160
{
161161
return $this->typeConverter->toJdbc($type, $size);
162162
}

src/Tqdev/PhpCrudApi/Database/TypeConverter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public function __construct(string $driver)
166166
'geometry' => true,
167167
];
168168

169-
public function toJdbc(string $type, int $size): string
169+
public function toJdbc(string $type, string $size): string
170170
{
171171
$jdbcType = strtolower($type);
172172
if (isset($this->toJdbc[$this->driver]["$jdbcType($size)"])) {

src/Tqdev/PhpCrudApi/Middleware/Router/SimpleRouter.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class SimpleRouter implements Router
2626

2727
public function __construct(string $basePath, Responder $responder, Cache $cache, int $ttl, bool $debug)
2828
{
29-
$this->basePath = $this->detectBasePath($basePath);
29+
$this->basePath = rtrim($this->detectBasePath($basePath), '/');
3030
$this->responder = $responder;
3131
$this->cache = $cache;
3232
$this->ttl = $ttl;
@@ -111,9 +111,8 @@ private function getRouteNumbers(ServerRequestInterface $request): array
111111
private function removeBasePath(ServerRequestInterface $request): ServerRequestInterface
112112
{
113113
$path = $request->getUri()->getPath();
114-
$basePath = rtrim($this->basePath, '/');
115-
if (substr($path, 0, strlen($basePath)) == $basePath) {
116-
$path = substr($path, strlen($basePath));
114+
if (substr($path, 0, strlen($this->basePath)) == $this->basePath) {
115+
$path = substr($path, strlen($this->basePath));
117116
$request = $request->withUri($request->getUri()->withPath($path));
118117
}
119118
return $request;

0 commit comments

Comments
 (0)