Skip to content

Commit 2db5b5e

Browse files
committed
Improve type definitions for scalar query params
1 parent 32c455d commit 2db5b5e

File tree

4 files changed

+18
-48
lines changed

4 files changed

+18
-48
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ given event loop instance.
202202

203203
#### query()
204204

205-
The `query(string $query, array $params = []): PromiseInterface<MysqlResult>` method can be used to
205+
The `query(string $query, list<string|int|float|bool|null> $params = []): PromiseInterface<MysqlResult>` method can be used to
206206
perform an async query.
207207

208208
This method returns a promise that will resolve with a `MysqlResult` on
@@ -258,7 +258,7 @@ suited for exposing multiple possible results.
258258

259259
#### queryStream()
260260

261-
The `queryStream(string $sql, array $params = []): ReadableStreamInterface` method can be used to
261+
The `queryStream(string $sql, list<string|int|float|bool|null> $params = []): ReadableStreamInterface` method can be used to
262262
perform an async query and stream the rows of the result set.
263263

264264
This method returns a readable stream that will emit each row of the

src/Io/Query.php

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,9 @@ public function __construct($sql)
4949
/**
5050
* Binding params for the query, multiple arguments support.
5151
*
52-
* @param mixed $param
53-
* @return self
52+
* @param list<string|int|float|bool|null> $params
53+
* @return $this
5454
*/
55-
public function bindParams()
56-
{
57-
$this->builtSql = null;
58-
$this->params = func_get_args();
59-
60-
return $this;
61-
}
62-
6355
public function bindParamsFromArray(array $params)
6456
{
6557
$this->builtSql = null;
@@ -68,21 +60,6 @@ public function bindParamsFromArray(array $params)
6860
return $this;
6961
}
7062

71-
/**
72-
* Binding params for the query, multiple arguments support.
73-
*
74-
* @param mixed $param
75-
* @return self
76-
* @deprecated
77-
*/
78-
public function params()
79-
{
80-
$this->params = func_get_args();
81-
$this->builtSql = null;
82-
83-
return $this;
84-
}
85-
8663
public function escape($str)
8764
{
8865
return strtr($str, $this->escapeChars);
@@ -105,13 +82,6 @@ protected function resolveValueForSql($value)
10582
case 'string':
10683
$value = "'" . $this->escape($value) . "'";
10784
break;
108-
case 'array':
109-
$nvalue = [];
110-
foreach ($value as $v) {
111-
$nvalue[] = $this->resolveValueForSql($v);
112-
}
113-
$value = implode(',', $nvalue);
114-
break;
11585
case 'NULL':
11686
$value = 'NULL';
11787
break;

src/MysqlClient.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ public function __construct(
152152
* could allow for possible SQL injection attacks and this API is not
153153
* suited for exposing multiple possible results.
154154
*
155-
* @param string $sql SQL statement
156-
* @param array $params Parameters which should be bound to query
155+
* @param string $sql SQL statement
156+
* @param list<string|int|float|bool|null> $params Parameters which should be bound to query
157157
* @return PromiseInterface<MysqlResult>
158158
* Resolves with a `MysqlResult` on success or rejects with an `Exception` on error.
159159
*/
@@ -229,11 +229,11 @@ public function query($sql, array $params = [])
229229
* could allow for possible SQL injection attacks and this API is not
230230
* suited for exposing multiple possible results.
231231
*
232-
* @param string $sql SQL statement
233-
* @param array $params Parameters which should be bound to query
232+
* @param string $sql SQL statement
233+
* @param list<string|int|float|bool|null> $params Parameters which should be bound to query
234234
* @return ReadableStreamInterface
235235
*/
236-
public function queryStream($sql, $params = [])
236+
public function queryStream($sql, array $params = [])
237237
{
238238
if ($this->closed || $this->quitting) {
239239
throw new Exception('Connection closed');

tests/Io/QueryTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,48 @@ class QueryTest extends TestCase
1010
public function testBindParams()
1111
{
1212
$query = new Query('select * from test where id = ? and name = ?');
13-
$sql = $query->bindParams(100, 'test')->getSql();
13+
$sql = $query->bindParamsFromArray([100, 'test'])->getSql();
1414
$this->assertEquals("select * from test where id = 100 and name = 'test'", $sql);
1515

16-
$query = new Query('select * from test where id in (?) and name = ?');
17-
$sql = $query->bindParams([1, 2], 'test')->getSql();
16+
$query = new Query('select * from test where id in (?,?) and name = ?');
17+
$sql = $query->bindParamsFromArray([1, 2, 'test'])->getSql();
1818
$this->assertEquals("select * from test where id in (1,2) and name = 'test'", $sql);
1919
/*
2020
$query = new Query('select * from test where id = :id and name = :name');
21-
$sql = $query->params([':id' => 100, ':name' => 'test'])->getSql();
21+
$sql = $query->bindParamsFromArray([':id' => 100, ':name' => 'test'])->getSql();
2222
$this->assertEquals("select * from test where id = 100 and name = 'test'", $sql);
2323
2424
$query = new Query('select * from test where id = :id and name = ?');
25-
$sql = $query->params('test', [':id' => 100])->getSql();
25+
$sql = $query->bindParamsFromArray(['test', ':id' => 100])->getSql();
2626
$this->assertEquals("select * from test where id = 100 and name = 'test'", $sql);
2727
*/
2828
}
2929

3030
public function testGetSqlReturnsQuestionMarkReplacedWhenBound()
3131
{
3232
$query = new Query('select ?');
33-
$sql = $query->bindParams('hello')->getSql();
33+
$sql = $query->bindParamsFromArray(['hello'])->getSql();
3434
$this->assertEquals("select 'hello'", $sql);
3535
}
3636

3737
public function testGetSqlReturnsQuestionMarkReplacedWhenBoundFromLastCall()
3838
{
3939
$query = new Query('select ?');
40-
$sql = $query->bindParams('foo')->bindParams('bar')->getSql();
40+
$sql = $query->bindParamsFromArray(['foo'])->bindParamsFromArray(['bar'])->getSql();
4141
$this->assertEquals("select 'bar'", $sql);
4242
}
4343

4444
public function testGetSqlReturnsQuestionMarkReplacedWithNullValueWhenBound()
4545
{
4646
$query = new Query('select ?');
47-
$sql = $query->bindParams(null)->getSql();
47+
$sql = $query->bindParamsFromArray([null])->getSql();
4848
$this->assertEquals("select NULL", $sql);
4949
}
5050

5151
public function testGetSqlReturnsQuestionMarkReplacedFromBoundWhenBound()
5252
{
5353
$query = new Query('select CONCAT(?, ?)');
54-
$sql = $query->bindParams('hello??', 'world??')->getSql();
54+
$sql = $query->bindParamsFromArray(['hello??', 'world??'])->getSql();
5555
$this->assertEquals("select CONCAT('hello??', 'world??')", $sql);
5656
}
5757

0 commit comments

Comments
 (0)