Skip to content

Commit 1141a46

Browse files
committed
Support floats without fractions as of PHP 5.6.6
1 parent 091e2c6 commit 1141a46

File tree

5 files changed

+42
-17
lines changed

5 files changed

+42
-17
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ native PHP datatypes. This conversion supports `int`, `float`, `string`
208208
(text) and `null`. SQLite does not have a native boolean type, so `true`
209209
and `false` will be mapped to integer values `1` and `0` respectively.
210210

211+
> Legacy PHP: Note that on legacy PHP < 5.6.6, a `float` without a
212+
fraction (such as `1.0`) may end up as an `integer` instead. You're
213+
highly recommended to use a supported PHP version or you may have to
214+
use explicit SQL casts to work around this.
215+
211216
#### quit()
212217

213218
The `quit(): PromiseInterface<void, Exception>` method can be used to

res/sqlite-worker.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@
4343
});
4444

4545
$in = new Decoder($through);
46-
$out = new Encoder($stream);
46+
$out = new Encoder($stream, (\PHP_VERSION_ID >= 50606 ? JSON_PRESERVE_ZERO_FRACTION : 0));
4747
} else {
4848
// no socket address given, use process I/O pipes
4949
$in = new Decoder(new ReadableResourceStream(\STDIN, $loop));
50-
$out = new Encoder(new WritableResourceStream(\STDOUT, $loop));
50+
$out = new Encoder(new WritableResourceStream(\STDOUT, $loop), (\PHP_VERSION_ID >= 50606 ? JSON_PRESERVE_ZERO_FRACTION : 0));
5151
}
5252

5353
// report error when input is invalid NDJSON

src/DatabaseInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ public function exec($sql);
142142
* (text) and `null`. SQLite does not have a native boolean type, so `true`
143143
* and `false` will be mapped to integer values `1` and `0` respectively.
144144
*
145+
* > Legacy PHP: Note that on legacy PHP < 5.6.6, a `float` without a
146+
* fraction (such as `1.0`) may end up as an `integer` instead. You're
147+
* highly recommended to use a supported PHP version or you may have to
148+
* use explicit SQL casts to work around this.
149+
*
145150
* @param string $sql SQL statement
146151
* @param array $params Parameters which should be bound to query
147152
* @return PromiseInterface<Result> Resolves with Result instance or rejects with Exception

src/Io/ProcessIoDatabase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function send($method, array $params)
133133
'id' => $id,
134134
'method' => $method,
135135
'params' => $params
136-
), \JSON_UNESCAPED_SLASHES) . "\n");
136+
), \JSON_UNESCAPED_SLASHES | (\PHP_VERSION_ID >= 50606 ? JSON_PRESERVE_ZERO_FRACTION : 0)) . "\n");
137137

138138
$deferred = new Deferred();
139139
$this->pending[$id] = $deferred;

tests/FunctionalDatabaseTest.php

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,11 @@ public function provideSqlDataWillBeReturnedWithType()
289289
['"hello"', 'hello'],
290290
['"hellö"', 'hellö']
291291
],
292-
SQLite3::version()['versionNumber'] < 3023000 ? [] : [
292+
(PHP_VERSION_ID < 50606) ? [] : [
293+
// preserving zero fractions is only supported as of PHP 5.6.6
294+
['1.0', 1.0]
295+
],
296+
(SQLite3::version()['versionNumber'] < 3023000) ? [] : [
293297
// boolean identifiers exist only as of SQLite 3.23.0 (2018-04-02)
294298
// @link https://www.sqlite.org/lang_expr.html#booleanexpr
295299
['true', 1],
@@ -326,14 +330,20 @@ public function testQueryValueInStatementResolvesWithResultWithTypeAndRunsUntilQ
326330

327331
public function provideDataWillBeReturnedWithType()
328332
{
329-
return [
330-
[0],
331-
[1],
332-
[1.5],
333-
[null],
334-
['hello'],
335-
['hellö']
336-
];
333+
return array_merge(
334+
[
335+
[0],
336+
[1],
337+
[1.5],
338+
[null],
339+
['hello'],
340+
['hellö']
341+
],
342+
(PHP_VERSION_ID < 50606) ? [] : [
343+
// preserving zero fractions is only supported as of PHP 5.6.6
344+
[1.0]
345+
]
346+
);
337347
}
338348

339349
/**
@@ -388,11 +398,16 @@ public function testQueryValuePlaceholderNamedResolvesWithResultWithExactTypeAnd
388398

389399
public function provideDataWillBeReturnedWithOtherType()
390400
{
391-
return [
392-
'true' => [true, 1],
393-
'false' => [false, 0],
394-
'float without fraction is int' => [1.0, 1]
395-
];
401+
return array_merge(
402+
[
403+
[true, 1],
404+
[false, 0],
405+
],
406+
(PHP_VERSION_ID >= 50606) ? [] : [
407+
// preserving zero fractions is supported as of PHP 5.6.6, otherwise cast to int
408+
[1.0, 1]
409+
]
410+
);
396411
}
397412

398413
/**

0 commit comments

Comments
 (0)