Skip to content

Commit 7fcb54f

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

File tree

5 files changed

+38
-16
lines changed

5 files changed

+38
-16
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: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ public function provideSqlDataWillBeReturnedWithType()
285285
[
286286
['42', 42],
287287
['2.5', 2.5],
288+
['1.0', 1.0],
288289
['null', null],
289290
['"hello"', 'hello'],
290291
['"hellö"', 'hellö']
@@ -326,14 +327,20 @@ public function testQueryValueInStatementResolvesWithResultWithTypeAndRunsUntilQ
326327

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

339346
/**
@@ -388,11 +395,16 @@ public function testQueryValuePlaceholderNamedResolvesWithResultWithExactTypeAnd
388395

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

398410
/**

0 commit comments

Comments
 (0)