diff --git a/README.md b/README.md index 7e02dc7..0b83c35 100644 --- a/README.md +++ b/README.md @@ -211,11 +211,6 @@ characters will be mapped to `TEXT`, binary strings will be mapped to does not have a native boolean type, so `true` and `false` will be mapped to integer values `1` and `0` respectively. -> Legacy PHP: Note that on legacy PHP < 5.6.6, a `float` without a - fraction (such as `1.0`) may end up as an `integer` instead. You're - highly recommended to use a supported PHP version or you may have to - use explicit SQL casts to work around this. - #### quit() The `quit(): PromiseInterface` method can be used to diff --git a/res/sqlite-worker.php b/res/sqlite-worker.php index e900f20..bc585b5 100644 --- a/res/sqlite-worker.php +++ b/res/sqlite-worker.php @@ -43,11 +43,11 @@ }); $in = new Decoder($through); - $out = new Encoder($stream, (\PHP_VERSION_ID >= 50606 ? JSON_PRESERVE_ZERO_FRACTION : 0)); + $out = new Encoder($stream); } else { // no socket address given, use process I/O pipes $in = new Decoder(new ReadableResourceStream(\STDIN, $loop)); - $out = new Encoder(new WritableResourceStream(\STDOUT, $loop), (\PHP_VERSION_ID >= 50606 ? JSON_PRESERVE_ZERO_FRACTION : 0)); + $out = new Encoder(new WritableResourceStream(\STDOUT, $loop)); } // report error when input is invalid NDJSON @@ -142,8 +142,9 @@ $value = (int)$value; } elseif (\is_int($value)) { $type = \SQLITE3_INTEGER; - } elseif (\is_float($value)) { + } elseif (isset($value->float)) { $type = \SQLITE3_FLOAT; + $value = (float)$value->float; } elseif (isset($value->base64)) { // base64-decode string parameters as BLOB $type = \SQLITE3_BLOB; @@ -178,6 +179,8 @@ foreach ($row as &$value) { if (\is_string($value) && \preg_match('/[\x00-\x08\x11\x12\x14-\x1f\x7f]/u', $value) !== 0) { $value = ['base64' => \base64_encode($value)]; + } elseif (\is_float($value)) { + $value = ['float' => $value]; } } $rows[] = $row; diff --git a/src/DatabaseInterface.php b/src/DatabaseInterface.php index 4730c47..616abce 100644 --- a/src/DatabaseInterface.php +++ b/src/DatabaseInterface.php @@ -145,11 +145,6 @@ public function exec($sql); * does not have a native boolean type, so `true` and `false` will be mapped * to integer values `1` and `0` respectively. * - * > Legacy PHP: Note that on legacy PHP < 5.6.6, a `float` without a - * fraction (such as `1.0`) may end up as an `integer` instead. You're - * highly recommended to use a supported PHP version or you may have to - * use explicit SQL casts to work around this. - * * @param string $sql SQL statement * @param array $params Parameters which should be bound to query * @return PromiseInterface Resolves with Result instance or rejects with Exception diff --git a/src/Io/ProcessIoDatabase.php b/src/Io/ProcessIoDatabase.php index 281cfa5..2cf4c49 100644 --- a/src/Io/ProcessIoDatabase.php +++ b/src/Io/ProcessIoDatabase.php @@ -80,6 +80,8 @@ public function query($sql, array $params = array()) foreach ($params as &$value) { if (\is_string($value) && \preg_match('/[\x00-\x08\x11\x12\x14-\x1f\x7f]/u', $value) !== 0) { $value = ['base64' => \base64_encode($value)]; + } elseif (\is_float($value)) { + $value = ['float' => $value]; } } @@ -95,6 +97,8 @@ public function query($sql, array $params = array()) foreach ($row as &$value) { if (isset($value['base64'])) { $value = \base64_decode($value['base64']); + } elseif (isset($value['float'])) { + $value = (float)$value['float']; } } $result->rows[] = $row; @@ -150,7 +154,7 @@ public function send($method, array $params) 'id' => $id, 'method' => $method, 'params' => $params - ), \JSON_UNESCAPED_SLASHES | (\PHP_VERSION_ID >= 50606 ? JSON_PRESERVE_ZERO_FRACTION : 0)) . "\n"); + ), \JSON_UNESCAPED_SLASHES) . "\n"); $deferred = new Deferred(); $this->pending[$id] = $deferred; diff --git a/tests/FunctionalDatabaseTest.php b/tests/FunctionalDatabaseTest.php index 1a5ca8a..451a670 100644 --- a/tests/FunctionalDatabaseTest.php +++ b/tests/FunctionalDatabaseTest.php @@ -298,16 +298,13 @@ public function provideSqlDataWillBeReturnedWithType() [ ['42', 42], ['2.5', 2.5], + ['1.0', 1.0], ['null', null], ['"hello"', 'hello'], ['"hellö"', 'hellö'], ['X\'01020300\'', "\x01\x02\x03\x00"], ['X\'3FF3\'', "\x3f\xf3"] ], - (PHP_VERSION_ID < 50606) ? [] : [ - // preserving zero fractions is only supported as of PHP 5.6.6 - ['1.0', 1.0] - ], (SQLite3::version()['versionNumber'] < 3023000) ? [] : [ // boolean identifiers exist only as of SQLite 3.23.0 (2018-04-02) // @link https://www.sqlite.org/lang_expr.html#booleanexpr @@ -345,25 +342,20 @@ public function testQueryValueInStatementResolvesWithResultWithTypeAndRunsUntilQ public function provideDataWillBeReturnedWithType() { - return array_merge( - [ - [0, 'INTEGER'], - [1, 'INTEGER'], - [1.5, 'REAL'], - [null, 'NULL'], - ['hello', 'TEXT'], - ['hellö', 'TEXT'], - ["hello\tworld\r\n", 'TEXT'], - [utf8_decode('hello wörld!'), 'BLOB'], - ["hello\x7fö", 'BLOB'], - ["\x03\x02\x001", 'BLOB'], - ["a\000b", 'BLOB'] - ], - (PHP_VERSION_ID < 50606) ? [] : [ - // preserving zero fractions is only supported as of PHP 5.6.6 - [1.0, 'REAL'] - ] - ); + return [ + [0, 'INTEGER'], + [1, 'INTEGER'], + [1.5, 'REAL'], + [1.0, 'REAL'], + [null, 'NULL'], + ['hello', 'TEXT'], + ['hellö', 'TEXT'], + ["hello\tworld\r\n", 'TEXT'], + [utf8_decode('hello wörld!'), 'BLOB'], + ["hello\x7fö", 'BLOB'], + ["\x03\x02\x001", 'BLOB'], + ["a\000b", 'BLOB'] + ]; } /** @@ -418,16 +410,10 @@ public function testQueryValuePlaceholderNamedResolvesWithResultWithExactTypeAnd public function provideDataWillBeReturnedWithOtherType() { - return array_merge( - [ - [true, 1], - [false, 0], - ], - (PHP_VERSION_ID >= 50606) ? [] : [ - // preserving zero fractions is supported as of PHP 5.6.6, otherwise cast to int - [1.0, 1] - ] - ); + return [ + [true, 1], + [false, 0], + ]; } /**