Skip to content

Commit 104b404

Browse files
committed
support float and double for #592
1 parent 20fb83b commit 104b404

File tree

4 files changed

+12
-14
lines changed

4 files changed

+12
-14
lines changed

src/Tqdev/PhpCrudApi/Database/DataConverter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private function convertRecordValue($conversion, $value)
2626
case 'float':
2727
return (float) $value;
2828
case 'decimal':
29-
return sprintf('%.' . $args[0] . 'f', $value);
29+
return number_format($value, $args[0], '.', '');
3030
}
3131
return $value;
3232
}
@@ -39,7 +39,7 @@ private function getRecordValueConversion(ReflectedColumn $column): string
3939
if (in_array($this->driver, ['sqlsrv', 'sqlite']) && in_array($column->getType(), ['integer', 'bigint'])) {
4040
return 'integer';
4141
}
42-
if (in_array($this->driver, ['sqlite']) && in_array($column->getType(), ['float', 'double'])) {
42+
if (in_array($this->driver, ['sqlite', 'pgsql']) && in_array($column->getType(), ['float', 'double'])) {
4343
return 'float';
4444
}
4545
if (in_array($this->driver, ['sqlite']) && in_array($column->getType(), ['decimal'])) {

src/Tqdev/PhpCrudApi/Database/GenericDefinition.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private function getColumnAutoIncrement(ReflectedColumn $column, bool $update):
7272
return $column->getPk() ? ' AUTO_INCREMENT' : '';
7373
case 'pgsql':
7474
case 'sqlsrv':
75-
return '';
75+
return $column->getPk() ? ' IDENTITY(1,1)' : '';
7676
case 'sqlite':
7777
return $column->getPk() ? ' AUTOINCREMENT' : '';
7878
}
@@ -440,7 +440,7 @@ public function removeColumn(string $tableName, string $columnName)
440440
private function query(string $sql, array $arguments): bool
441441
{
442442
$stmt = $this->pdo->prepare($sql);
443-
//echo "- $sql -- " . json_encode($arguments) . "\n";
443+
// echo "- $sql -- " . json_encode($arguments) . "\n";
444444
return $stmt->execute($arguments);
445445
}
446446
}

src/Tqdev/PhpCrudApi/Database/TypeConverter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ public function __construct(string $driver)
2323
'blob' => 'bytea',
2424
'float' => 'real',
2525
'double' => 'double precision',
26-
'varbinary(*)' => 'bytea',
26+
'varbinary' => 'bytea',
2727
],
2828
'sqlsrv' => [
2929
'boolean' => 'bit',
3030
'varchar' => 'nvarchar',
3131
'clob' => 'ntext',
3232
'blob' => 'image',
33-
'timestamp' => 'datetime2',
33+
'time' => 'time(0)',
34+
'timestamp' => 'datetime2(0)',
3435
'double' => 'float',
3536
'float' => 'real',
3637
],

tests/functional/003_columns/014_create_types_table.log

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
skip-for-pgsql: varbinary is not properly supported yet
2-
skip-for-sqlsrv: float has high precision and time and datetime have microseconds
31
===
42
POST /columns
53

@@ -17,7 +15,6 @@ POST /columns
1715
{"name":"timestamp","type":"timestamp"},
1816
{"name":"clob","type":"clob"},
1917
{"name":"blob","type":"blob"},
20-
{"name":"varbinary","type":"varbinary","length":10},
2118
{"name":"geometry","type":"geometry"}
2219
]}
2320
===
@@ -31,14 +28,14 @@ GET /columns/types
3128
===
3229
200
3330
Content-Type: application/json
34-
Content-Length: 615
31+
Content-Length: 563
3532

36-
{"name":"types","type":"table","columns":[{"name":"id","type":"integer","pk":true},{"name":"integer","type":"integer"},{"name":"bigint","type":"bigint"},{"name":"varchar","type":"varchar","length":10},{"name":"decimal","type":"decimal","precision":10,"scale":4},{"name":"float","type":"float"},{"name":"double","type":"double"},{"name":"boolean","type":"boolean"},{"name":"date","type":"date"},{"name":"time","type":"time"},{"name":"timestamp","type":"timestamp"},{"name":"clob","type":"clob"},{"name":"blob","type":"blob"},{"name":"varbinary","type":"varbinary","length":10},{"name":"geometry","type":"geometry"}]}
33+
{"name":"types","type":"table","columns":[{"name":"id","type":"integer","pk":true},{"name":"integer","type":"integer"},{"name":"bigint","type":"bigint"},{"name":"varchar","type":"varchar","length":10},{"name":"decimal","type":"decimal","precision":10,"scale":4},{"name":"float","type":"float"},{"name":"double","type":"double"},{"name":"boolean","type":"boolean"},{"name":"date","type":"date"},{"name":"time","type":"time"},{"name":"timestamp","type":"timestamp"},{"name":"clob","type":"clob"},{"name":"blob","type":"blob"},{"name":"geometry","type":"geometry"}]}
3734
===
3835
POST /records/types
3936
Content-Type: application/json
4037

41-
{"integer":2,"bigint":3,"varchar":"abc","decimal":"1.23","float":2.34,"double":3.45,"boolean":true,"date":"1970-01-01","time":"00:00:01","timestamp":"2001-02-03 04:05:06","clob":"a","blob":"YQ==","varbinary":"YQ==","geometry":"POINT(1 2)"}
38+
{"integer":2,"bigint":3,"varchar":"abc","decimal":"1.23","float":1,"double":23.45,"boolean":true,"date":"1970-01-01","time":"00:00:01","timestamp":"2001-02-03 04:05:06","clob":"a","blob":"YQ==","geometry":"POINT(1 2)"}
4239
===
4340
200
4441
Content-Type: application/json
@@ -50,9 +47,9 @@ GET /records/types/1
5047
===
5148
200
5249
Content-Type: application/json
53-
Content-Length: 248
50+
Content-Length: 227
5451

55-
{"id":1,"integer":2,"bigint":3,"varchar":"abc","decimal":"1.2300","float":2.34,"double":3.45,"boolean":true,"date":"1970-01-01","time":"00:00:01","timestamp":"2001-02-03 04:05:06","clob":"a","blob":"YQ==","varbinary":"YQ==","geometry":"POINT(1 2)"}
52+
{"id":1,"integer":2,"bigint":3,"varchar":"abc","decimal":"1.2300","float":1,"double":23.45,"boolean":true,"date":"1970-01-01","time":"00:00:01","timestamp":"2001-02-03 04:05:06","clob":"a","blob":"YQ==","geometry":"POINT(1 2)"}
5653
===
5754
DELETE /columns/types
5855
===

0 commit comments

Comments
 (0)