Skip to content

Commit e031ce2

Browse files
committed
Explicitly cast bool to int to avoid legacy PHP test failures
1 parent 13a2c5e commit e031ce2

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

res/sqlite-worker.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,13 @@
134134
} else {
135135
$statement = $db->prepare($data->params[0]);
136136
foreach ($data->params[1] as $index => $value) {
137+
// explicitly cast bool to int because SQLite does not have a native boolean
138+
$value = ($value === true || $value === false) ? (int)$value : $value;
139+
137140
$statement->bindValue(
138141
$index + 1,
139142
$value,
140-
$value === null ? \SQLITE3_NULL : \is_int($value) || \is_bool($value) ? \SQLITE3_INTEGER : \is_float($value) ? \SQLITE3_FLOAT : \SQLITE3_TEXT
143+
$value === null ? \SQLITE3_NULL : \is_int($value) ? \SQLITE3_INTEGER : \is_float($value) ? \SQLITE3_FLOAT : \SQLITE3_TEXT
141144
);
142145
}
143146
$result = @$statement->execute();
@@ -173,10 +176,13 @@
173176
} elseif ($data->method === 'query' && $db !== null && \count($data->params) === 2 && \is_string($data->params[0]) && \is_object($data->params[1])) {
174177
$statement = $db->prepare($data->params[0]);
175178
foreach ($data->params[1] as $index => $value) {
179+
// explicitly cast bool to int because SQLite does not have a native boolean
180+
$value = ($value === true || $value === false) ? (int)$value : $value;
181+
176182
$statement->bindValue(
177183
$index,
178184
$value,
179-
$value === null ? \SQLITE3_NULL : \is_int($value) || \is_bool($value) ? \SQLITE3_INTEGER : \is_float($value) ? \SQLITE3_FLOAT : \SQLITE3_TEXT
185+
$value === null ? \SQLITE3_NULL : \is_int($value) ? \SQLITE3_INTEGER : \is_float($value) ? \SQLITE3_FLOAT : \SQLITE3_TEXT
180186
);
181187
}
182188
$result = @$statement->execute();

tests/FunctionalDatabaseTest.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,21 @@ public function testQueryStringResolvesWithResultWithTypeStringAndRunsUntilQuit(
281281

282282
public function provideSqlDataWillBeReturnedWithType()
283283
{
284-
return [
285-
['42', 42],
286-
['1.5', 1.5],
287-
['null', null],
288-
['"hello"', 'hello'],
289-
['"hellö"', 'hellö'],
290-
['true', 1],
291-
['false', 0],
292-
];
284+
return array_merge(
285+
[
286+
['42', 42],
287+
['1.5', 1.5],
288+
['null', null],
289+
['"hello"', 'hello'],
290+
['"hellö"', 'hellö']
291+
],
292+
(SQLite3::version()['versionNumber'] < 3023000) ? [] : [
293+
// boolean identifiers exist only as of SQLite 3.23.0 (2018-04-02)
294+
// @link https://www.sqlite.org/lang_expr.html#booleanexpr
295+
['true', 1],
296+
['false', 0]
297+
]
298+
);
293299
}
294300

295301
/**
@@ -308,7 +314,7 @@ public function testQueryValueInStatementResolvesWithResultWithTypeAndRunsUntilQ
308314
$promise->then(function (DatabaseInterface $db) use (&$data, $value){
309315
$db->query('SELECT ' . $value . ' AS value')->then(function (Result $result) use (&$data) {
310316
$data = $result->rows;
311-
});
317+
}, 'printf');
312318

313319
$db->quit();
314320
});
@@ -323,6 +329,7 @@ public function provideDataWillBeReturnedWithType()
323329
return [
324330
[0],
325331
[1],
332+
[42],
326333
[1.5],
327334
[null],
328335
['hello'],
@@ -345,7 +352,7 @@ public function testQueryValuePlaceholderPositionalResolvesWithResultWithExactTy
345352
$promise->then(function (DatabaseInterface $db) use (&$data, $value){
346353
$db->query('SELECT ? AS value', array($value))->then(function (Result $result) use (&$data) {
347354
$data = $result->rows;
348-
});
355+
}, 'printf');
349356

350357
$db->quit();
351358
});
@@ -370,7 +377,7 @@ public function testQueryValuePlaceholderNamedResolvesWithResultWithExactTypeAnd
370377
$promise->then(function (DatabaseInterface $db) use (&$data, $value){
371378
$db->query('SELECT :value AS value', array('value' => $value))->then(function (Result $result) use (&$data) {
372379
$data = $result->rows;
373-
});
380+
}, 'printf');
374381

375382
$db->quit();
376383
});
@@ -385,7 +392,7 @@ public function provideDataWillBeReturnedWithOtherType()
385392
return [
386393
'true' => [true, 1],
387394
'false' => [false, 0],
388-
'float without fraction is int' => [1.0, 1]
395+
'float without fraction is int' => [2.0, 2]
389396
];
390397
}
391398

0 commit comments

Comments
 (0)