Skip to content

Commit 942c9cd

Browse files
authored
Merge pull request #17 from clue-labs/legacy-float
Support float values without fraction on legacy PHP < 5.6.6
2 parents 5fc5120 + dbac4de commit 942c9cd

File tree

5 files changed

+30
-47
lines changed

5 files changed

+30
-47
lines changed

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,6 @@ characters will be mapped to `TEXT`, binary strings will be mapped to
211211
does not have a native boolean type, so `true` and `false` will be mapped
212212
to integer values `1` and `0` respectively.
213213

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

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

res/sqlite-worker.php

Lines changed: 6 additions & 3 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, (\PHP_VERSION_ID >= 50606 ? JSON_PRESERVE_ZERO_FRACTION : 0));
46+
$out = new Encoder($stream);
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), (\PHP_VERSION_ID >= 50606 ? JSON_PRESERVE_ZERO_FRACTION : 0));
50+
$out = new Encoder(new WritableResourceStream(\STDOUT, $loop));
5151
}
5252

5353
// report error when input is invalid NDJSON
@@ -142,8 +142,9 @@
142142
$value = (int)$value;
143143
} elseif (\is_int($value)) {
144144
$type = \SQLITE3_INTEGER;
145-
} elseif (\is_float($value)) {
145+
} elseif (isset($value->float)) {
146146
$type = \SQLITE3_FLOAT;
147+
$value = (float)$value->float;
147148
} elseif (isset($value->base64)) {
148149
// base64-decode string parameters as BLOB
149150
$type = \SQLITE3_BLOB;
@@ -178,6 +179,8 @@
178179
foreach ($row as &$value) {
179180
if (\is_string($value) && \preg_match('/[\x00-\x08\x11\x12\x14-\x1f\x7f]/u', $value) !== 0) {
180181
$value = ['base64' => \base64_encode($value)];
182+
} elseif (\is_float($value)) {
183+
$value = ['float' => $value];
181184
}
182185
}
183186
$rows[] = $row;

src/DatabaseInterface.php

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

src/Io/ProcessIoDatabase.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public function query($sql, array $params = array())
8080
foreach ($params as &$value) {
8181
if (\is_string($value) && \preg_match('/[\x00-\x08\x11\x12\x14-\x1f\x7f]/u', $value) !== 0) {
8282
$value = ['base64' => \base64_encode($value)];
83+
} elseif (\is_float($value)) {
84+
$value = ['float' => $value];
8385
}
8486
}
8587

@@ -95,6 +97,8 @@ public function query($sql, array $params = array())
9597
foreach ($row as &$value) {
9698
if (isset($value['base64'])) {
9799
$value = \base64_decode($value['base64']);
100+
} elseif (isset($value['float'])) {
101+
$value = (float)$value['float'];
98102
}
99103
}
100104
$result->rows[] = $row;
@@ -150,7 +154,7 @@ public function send($method, array $params)
150154
'id' => $id,
151155
'method' => $method,
152156
'params' => $params
153-
), \JSON_UNESCAPED_SLASHES | (\PHP_VERSION_ID >= 50606 ? JSON_PRESERVE_ZERO_FRACTION : 0)) . "\n");
157+
), \JSON_UNESCAPED_SLASHES) . "\n");
154158

155159
$deferred = new Deferred();
156160
$this->pending[$id] = $deferred;

tests/FunctionalDatabaseTest.php

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -298,16 +298,13 @@ public function provideSqlDataWillBeReturnedWithType()
298298
[
299299
['42', 42],
300300
['2.5', 2.5],
301+
['1.0', 1.0],
301302
['null', null],
302303
['"hello"', 'hello'],
303304
['"hellö"', 'hellö'],
304305
['X\'01020300\'', "\x01\x02\x03\x00"],
305306
['X\'3FF3\'', "\x3f\xf3"]
306307
],
307-
(PHP_VERSION_ID < 50606) ? [] : [
308-
// preserving zero fractions is only supported as of PHP 5.6.6
309-
['1.0', 1.0]
310-
],
311308
(SQLite3::version()['versionNumber'] < 3023000) ? [] : [
312309
// boolean identifiers exist only as of SQLite 3.23.0 (2018-04-02)
313310
// @link https://www.sqlite.org/lang_expr.html#booleanexpr
@@ -345,25 +342,20 @@ public function testQueryValueInStatementResolvesWithResultWithTypeAndRunsUntilQ
345342

346343
public function provideDataWillBeReturnedWithType()
347344
{
348-
return array_merge(
349-
[
350-
[0, 'INTEGER'],
351-
[1, 'INTEGER'],
352-
[1.5, 'REAL'],
353-
[null, 'NULL'],
354-
['hello', 'TEXT'],
355-
['hellö', 'TEXT'],
356-
["hello\tworld\r\n", 'TEXT'],
357-
[utf8_decode('hello wörld!'), 'BLOB'],
358-
["hello\x7fö", 'BLOB'],
359-
["\x03\x02\x001", 'BLOB'],
360-
["a\000b", 'BLOB']
361-
],
362-
(PHP_VERSION_ID < 50606) ? [] : [
363-
// preserving zero fractions is only supported as of PHP 5.6.6
364-
[1.0, 'REAL']
365-
]
366-
);
345+
return [
346+
[0, 'INTEGER'],
347+
[1, 'INTEGER'],
348+
[1.5, 'REAL'],
349+
[1.0, 'REAL'],
350+
[null, 'NULL'],
351+
['hello', 'TEXT'],
352+
['hellö', 'TEXT'],
353+
["hello\tworld\r\n", 'TEXT'],
354+
[utf8_decode('hello wörld!'), 'BLOB'],
355+
["hello\x7fö", 'BLOB'],
356+
["\x03\x02\x001", 'BLOB'],
357+
["a\000b", 'BLOB']
358+
];
367359
}
368360

369361
/**
@@ -418,16 +410,10 @@ public function testQueryValuePlaceholderNamedResolvesWithResultWithExactTypeAnd
418410

419411
public function provideDataWillBeReturnedWithOtherType()
420412
{
421-
return array_merge(
422-
[
423-
[true, 1],
424-
[false, 0],
425-
],
426-
(PHP_VERSION_ID >= 50606) ? [] : [
427-
// preserving zero fractions is supported as of PHP 5.6.6, otherwise cast to int
428-
[1.0, 1]
429-
]
430-
);
413+
return [
414+
[true, 1],
415+
[false, 0],
416+
];
431417
}
432418

433419
/**

0 commit comments

Comments
 (0)