Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<void, Exception>` method can be used to
Expand Down
9 changes: 6 additions & 3 deletions res/sqlite-worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 0 additions & 5 deletions src/DatabaseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<Result> Resolves with Result instance or rejects with Exception
Expand Down
6 changes: 5 additions & 1 deletion src/Io/ProcessIoDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
52 changes: 19 additions & 33 deletions tests/FunctionalDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']
];
}

/**
Expand Down Expand Up @@ -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],
];
}

/**
Expand Down