Skip to content

Commit 22b36a9

Browse files
committed
Connection::quote() can only quote strings
1 parent 72ba3e4 commit 22b36a9

File tree

15 files changed

+28
-77
lines changed

15 files changed

+28
-77
lines changed

UPGRADE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Upgrade to 3.0
22

3+
## BC BREAK `Statement::quote()` only accepts strings.
4+
5+
`Statement::quote()` and `ExpressionBuilder::literal()` no longer accept arguments of an arbitrary type and and don't implement type-specific handling. Only strings can be quoted.
6+
37
## BC BREAK `Statement` and `Connection` methods return `void`.
48

59
`Connection::connect()`, `Statement::bindParam()`, `::bindValue()`, `::execute()`, `ResultStatement::setFetchMode()` and `::closeCursor()` no longer return a boolean value. They will throw an exception in case of failure.

lib/Doctrine/DBAL/Connection.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -808,13 +808,9 @@ public function quoteIdentifier($str)
808808
/**
809809
* {@inheritDoc}
810810
*/
811-
public function quote($input, $type = null)
811+
public function quote(string $input) : string
812812
{
813-
$connection = $this->getWrappedConnection();
814-
815-
[$value, $bindingType] = $this->getBindingInfo($input, $type);
816-
817-
return $connection->quote($value, $bindingType);
813+
return $this->getWrappedConnection()->quote($input);
818814
}
819815

820816
/**

lib/Doctrine/DBAL/Driver/Connection.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Doctrine\DBAL\Driver;
44

55
use Doctrine\DBAL\DBALException;
6-
use Doctrine\DBAL\ParameterType;
76

87
/**
98
* Connection interface.
@@ -27,13 +26,8 @@ public function query(string $sql) : ResultStatement;
2726

2827
/**
2928
* Quotes a string for use in a query.
30-
*
31-
* @param mixed $input
32-
* @param int $type
33-
*
34-
* @return mixed
3529
*/
36-
public function quote($input, $type = ParameterType::STRING);
30+
public function quote(string $input) : string;
3731

3832
/**
3933
* Executes an SQL statement and return the number of affected rows.

lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Doctrine\DBAL\Driver\ResultStatement;
77
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
88
use Doctrine\DBAL\Driver\Statement as DriverStatement;
9-
use Doctrine\DBAL\ParameterType;
109
use stdClass;
1110
use const DB2_AUTOCOMMIT_OFF;
1211
use const DB2_AUTOCOMMIT_ON;
@@ -101,15 +100,9 @@ public function query(string $sql) : ResultStatement
101100
/**
102101
* {@inheritdoc}
103102
*/
104-
public function quote($input, $type = ParameterType::STRING)
103+
public function quote(string $input) : string
105104
{
106-
$input = db2_escape_string($input);
107-
108-
if ($type === ParameterType::INTEGER) {
109-
return $input;
110-
}
111-
112-
return "'" . $input . "'";
105+
return "'" . db2_escape_string($input) . "'";
113106
}
114107

115108
/**

lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Doctrine\DBAL\Driver\ResultStatement;
88
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
99
use Doctrine\DBAL\Driver\Statement as DriverStatement;
10-
use Doctrine\DBAL\ParameterType;
1110
use mysqli;
1211
use const MYSQLI_INIT_COMMAND;
1312
use const MYSQLI_OPT_CONNECT_TIMEOUT;
@@ -146,7 +145,7 @@ public function query(string $sql) : ResultStatement
146145
/**
147146
* {@inheritdoc}
148147
*/
149-
public function quote($input, $type = ParameterType::STRING)
148+
public function quote(string $input) : string
150149
{
151150
return "'" . $this->conn->escape_string($input) . "'";
152151
}

lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@
66
use Doctrine\DBAL\Driver\ResultStatement;
77
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
88
use Doctrine\DBAL\Driver\Statement as DriverStatement;
9-
use Doctrine\DBAL\ParameterType;
109
use UnexpectedValueException;
1110
use const OCI_COMMIT_ON_SUCCESS;
1211
use const OCI_DEFAULT;
1312
use const OCI_NO_AUTO_COMMIT;
1413
use function addcslashes;
15-
use function is_float;
16-
use function is_int;
1714
use function oci_commit;
1815
use function oci_connect;
1916
use function oci_error;
@@ -22,7 +19,6 @@
2219
use function oci_server_version;
2320
use function preg_match;
2421
use function sprintf;
25-
use function str_replace;
2622

2723
/**
2824
* OCI8 implementation of the Connection interface.
@@ -123,14 +119,9 @@ public function query(string $sql) : ResultStatement
123119
/**
124120
* {@inheritdoc}
125121
*/
126-
public function quote($value, $type = ParameterType::STRING)
122+
public function quote(string $input) : string
127123
{
128-
if (is_int($value) || is_float($value)) {
129-
return $value;
130-
}
131-
$value = str_replace("'", "''", $value);
132-
133-
return "'" . addcslashes($value, "\000\n\r\\\032") . "'";
124+
return "'" . addcslashes($input, "\000\n\r\\\032") . "'";
134125
}
135126

136127
/**

lib/Doctrine/DBAL/Driver/PDOConnection.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Doctrine\DBAL\Driver;
44

5-
use Doctrine\DBAL\ParameterType;
65
use PDO;
76
use function assert;
87

@@ -86,9 +85,9 @@ public function query(string $sql) : ResultStatement
8685
/**
8786
* {@inheritdoc}
8887
*/
89-
public function quote($input, $type = ParameterType::STRING)
88+
public function quote(string $input) : string
9089
{
91-
return $this->connection->quote($input, $type);
90+
return $this->connection->quote($input);
9291
}
9392

9493
/**

lib/Doctrine/DBAL/Driver/PDOSqlsrv/Connection.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Doctrine\DBAL\Driver\PDOConnection;
66
use Doctrine\DBAL\Driver\PDOStatement;
7-
use Doctrine\DBAL\ParameterType;
87
use function strpos;
98
use function substr;
109

@@ -31,9 +30,9 @@ public function lastInsertId($name = null)
3130
/**
3231
* {@inheritDoc}
3332
*/
34-
public function quote($value, $type = ParameterType::STRING)
33+
public function quote(string $input) : string
3534
{
36-
$val = parent::quote($value, $type);
35+
$val = parent::quote($input);
3736

3837
// Fix for a driver version terminating all values with null byte
3938
if (strpos($val, "\0") !== false) {

lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereConnection.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
use Doctrine\DBAL\Driver\ResultStatement;
77
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
88
use Doctrine\DBAL\Driver\Statement as DriverStatement;
9-
use Doctrine\DBAL\ParameterType;
109
use function assert;
11-
use function is_float;
12-
use function is_int;
1310
use function is_resource;
1411
use function is_string;
1512
use function sasql_affected_rows;
@@ -159,12 +156,8 @@ public function query(string $sql) : ResultStatement
159156
/**
160157
* {@inheritdoc}
161158
*/
162-
public function quote($input, $type = ParameterType::STRING)
159+
public function quote(string $input) : string
163160
{
164-
if (is_int($input) || is_float($input)) {
165-
return $input;
166-
}
167-
168161
return "'" . sasql_escape_string($this->connection, $input) . "'";
169162
}
170163

lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvConnection.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66
use Doctrine\DBAL\Driver\ResultStatement;
77
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
88
use Doctrine\DBAL\Driver\Statement as DriverStatement;
9-
use Doctrine\DBAL\ParameterType;
109
use const SQLSRV_ERR_ERRORS;
11-
use function is_float;
12-
use function is_int;
13-
use function sprintf;
1410
use function sqlsrv_begin_transaction;
1511
use function sqlsrv_commit;
1612
use function sqlsrv_configure;
@@ -95,15 +91,9 @@ public function query(string $sql) : ResultStatement
9591
/**
9692
* {@inheritDoc}
9793
*/
98-
public function quote($value, $type = ParameterType::STRING)
94+
public function quote(string $input) : string
9995
{
100-
if (is_int($value)) {
101-
return $value;
102-
} elseif (is_float($value)) {
103-
return sprintf('%F', $value);
104-
}
105-
106-
return "'" . str_replace("'", "''", $value) . "'";
96+
return "'" . str_replace("'", "''", $input) . "'";
10797
}
10898

10999
/**

lib/Doctrine/DBAL/Query/Expression/ExpressionBuilder.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -284,15 +284,10 @@ public function notIn($x, $y)
284284
}
285285

286286
/**
287-
* Quotes a given input parameter.
288-
*
289-
* @param mixed $input The parameter to be quoted.
290-
* @param int|null $type The type of the parameter.
291-
*
292-
* @return string
287+
* Creates an SQL literal expression from the string.
293288
*/
294-
public function literal($input, $type = null)
289+
public function literal(string $input)
295290
{
296-
return $this->connection->quote($input, $type);
291+
return $this->connection->quote($input);
297292
}
298293
}

lib/Doctrine/DBAL/Sharding/SQLAzure/SQLAzureShardManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public function splitFederation($splitDistributionValue)
202202

203203
$sql = 'ALTER FEDERATION ' . $this->getFederationName() . ' ' .
204204
'SPLIT AT (' . $this->getDistributionKey() . ' = ' .
205-
$this->conn->quote($splitDistributionValue, $type->getBindingType()) . ')';
205+
$this->conn->quote($splitDistributionValue) . ')';
206206
$this->conn->exec($sql);
207207
}
208208
}

tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
use Doctrine\DBAL\ConnectionException;
77
use Doctrine\DBAL\Driver\Connection as DriverConnection;
88
use Doctrine\DBAL\DriverManager;
9-
use Doctrine\DBAL\ParameterType;
109
use Doctrine\DBAL\Platforms\AbstractPlatform;
11-
use Doctrine\DBAL\Types\Type;
1210
use Doctrine\Tests\DbalFunctionalTestCase;
1311
use Error;
1412
use Exception;
@@ -252,8 +250,8 @@ public function testTransactionalReturnValue()
252250
public function testQuote()
253251
{
254252
self::assertEquals(
255-
$this->connection->quote('foo', Type::STRING),
256-
$this->connection->quote('foo', ParameterType::STRING)
253+
$this->connection->quote('foo'),
254+
$this->connection->quote('foo')
257255
);
258256
}
259257

tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ public function testPrepareWithQuoted()
177177
$paramStr = 'foo';
178178

179179
$stmt = $this->connection->prepare(sprintf(
180-
'SELECT test_int, test_string FROM %s WHERE test_int = %s AND test_string = %s',
180+
'SELECT test_int, test_string FROM %s WHERE test_int = %d AND test_string = %s',
181181
$this->connection->quoteIdentifier($table),
182-
$this->connection->quote($paramInt),
182+
$paramInt,
183183
$this->connection->quote($paramStr)
184184
));
185185
self::assertInstanceOf(Statement::class, $stmt);

tests/Doctrine/Tests/DBAL/Functional/WriteTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testExecuteUpdateFirstTypeIsNull()
4646

4747
public function testExecuteUpdate()
4848
{
49-
$sql = 'INSERT INTO write_table (test_int) VALUES ( ' . $this->connection->quote(1) . ')';
49+
$sql = 'INSERT INTO write_table (test_int) VALUES (1)';
5050
$affected = $this->connection->executeUpdate($sql);
5151

5252
self::assertEquals(1, $affected, 'executeUpdate() should return the number of affected rows!');

0 commit comments

Comments
 (0)