Skip to content

Commit a1146a9

Browse files
committed
update(PDO): default SET sql_mode='ANSI_QUOTES'
1 parent 1557c30 commit a1146a9

3 files changed

Lines changed: 99 additions & 22 deletions

File tree

src/PDO.php

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414

1515
use PDO as PDOAlias;
1616
use PDOStatement as PDOStatementAlias;
17-
use PHPUnit\Framework\MockObject\InvalidMethodNameException;
1817
use Ripple\Database\Interface\ClientInterface;
1918
use Ripple\Database\MySQL\Client;
2019
use InvalidArgumentException;
2120

2221
use function strtolower;
22+
use function intval;
23+
use function is_numeric;
24+
use function strlen;
2325

2426
/**
2527
* @override PDO
@@ -49,6 +51,7 @@ public function __construct(
4951
'mysql' => new Client($this->dsn, $this->username, $this->password, $this->options),
5052
default => throw new InvalidArgumentException()
5153
};
54+
$this->client->query("SET sql_mode='ANSI_QUOTES';");
5255
}
5356

5457
/**
@@ -187,15 +190,46 @@ public function errorInfo(): array
187190
}
188191

189192
/**
190-
* 引号转义
191-
* @param string $string
193+
* @param mixed $string
192194
* @param int $type
193195
* @return string|false
194-
* @deprecated
195196
*/
196-
public function quote(string $string, int $type = PDOAlias::PARAM_STR): string|false
197+
public function quote(mixed $string, int $type = PDOAlias::PARAM_STR): string|false
197198
{
198-
throw new InvalidMethodNameException('Method quote() is not supported.');
199+
switch ($type) {
200+
case PDOAlias::PARAM_NULL:
201+
return 'NULL';
202+
case PDOAlias::PARAM_INT:
203+
if (is_numeric($string)) {
204+
return (string)intval($string);
205+
}
206+
return false;
207+
case PDOAlias::PARAM_BOOL:
208+
return $string ? '1' : '0';
209+
case PDOAlias::PARAM_STR:
210+
default:
211+
$escaped = '';
212+
$len = strlen($string);
213+
for ($i = 0; $i < $len; $i++) {
214+
$c = $string[$i];
215+
if ($c === "\0") {
216+
$escaped .= '\\0';
217+
} elseif ($c === "\n") {
218+
$escaped .= '\\n';
219+
} elseif ($c === "\r") {
220+
$escaped .= '\\r';
221+
} elseif ($c === '\\') {
222+
$escaped .= '\\\\';
223+
} elseif ($c === '\'') {
224+
$escaped .= '\\\'';
225+
} elseif ($c === '"') {
226+
$escaped .= '\\"';
227+
} else {
228+
$escaped .= $c;
229+
}
230+
}
231+
return "'$escaped'";
232+
}
199233
}
200234

201235
/**

src/PDOPool.php

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
use PDO as PDOAlias;
1616
use PDOStatement as PDOStatementAlias;
17-
use PHPUnit\Framework\MockObject\InvalidMethodNameException;
1817
use SplObjectStorage;
1918
use Exception;
2019
use RuntimeException;
@@ -23,6 +22,9 @@
2322

2423
use function count;
2524
use function time;
25+
use function intval;
26+
use function is_numeric;
27+
use function strlen;
2628

2729
/**
2830
* PDO连接池
@@ -468,14 +470,46 @@ public function errorInfo(): array
468470
}
469471

470472
/**
471-
* 引号转义(已废弃)
472-
* @param string $string 要转义的字符串
473-
* @param int $type 参数类型
474-
* @return string|false 转义后的字符串或false
473+
* @param mixed $string
474+
* @param int $type
475+
* @return string|false
475476
*/
476-
public function quote(string $string, int $type = PDOAlias::PARAM_STR): string|false
477+
public function quote(mixed $string, int $type = PDOAlias::PARAM_STR): string|false
477478
{
478-
throw new InvalidMethodNameException('Method quote() is not supported.');
479+
switch ($type) {
480+
case PDOAlias::PARAM_NULL:
481+
return 'NULL';
482+
case PDOAlias::PARAM_INT:
483+
if (is_numeric($string)) {
484+
return (string)intval($string);
485+
}
486+
return false;
487+
case PDOAlias::PARAM_BOOL:
488+
return $string ? '1' : '0';
489+
case PDOAlias::PARAM_STR:
490+
default:
491+
$escaped = '';
492+
$len = strlen($string);
493+
for ($i = 0; $i < $len; $i++) {
494+
$c = $string[$i];
495+
if ($c === "\0") {
496+
$escaped .= '\\0';
497+
} elseif ($c === "\n") {
498+
$escaped .= '\\n';
499+
} elseif ($c === "\r") {
500+
$escaped .= '\\r';
501+
} elseif ($c === '\\') {
502+
$escaped .= '\\\\';
503+
} elseif ($c === '\'') {
504+
$escaped .= '\\\'';
505+
} elseif ($c === '"') {
506+
$escaped .= '\\"';
507+
} else {
508+
$escaped .= $c;
509+
}
510+
}
511+
return "'$escaped'";
512+
}
479513
}
480514

481515
/**

src/PDOStatement.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class PDOStatement extends PDOStatementAlias
3232
* @param StatementInterface $statement
3333
* @param array|null $options
3434
*/
35-
public function __construct(private readonly StatementInterface $statement, private readonly ?array $options = [])
35+
public function __construct(private readonly StatementInterface $statement, private ?array $options = [])
3636
{
3737
}
3838

@@ -145,21 +145,30 @@ public function getColumnMeta(int $column): array|false
145145
}
146146

147147
$flags = array_filter([
148-
'not_null' => $col['flags'] & 1,
148+
'not_null' => $col['flags'] & 1,
149149
'primary_key' => $col['flags'] & 2,
150150
]);
151151

152152
return [
153-
'name' => $col['name'],
154-
'table' => $col['table'] ?? '',
155-
'len' => $col['columnLength'] ?? 0,
156-
'precision' => $col['decimals'] ?? 0,
153+
'name' => $col['name'],
154+
'table' => $col['table'] ?? '',
155+
'len' => $col['columnLength'] ?? 0,
156+
'precision' => $col['decimals'] ?? 0,
157157
'native_type' => $col['type']->name,
158-
'flags' => array_keys($flags),
159-
'pdo_type' => match ($col['type']->name) {
158+
'flags' => array_keys($flags),
159+
'pdo_type' => match ($col['type']->name) {
160160
'LONGLONG' => PDO::PARAM_INT,
161-
default => PDO::PARAM_STR,
161+
default => PDO::PARAM_STR,
162162
},
163163
];
164164
}
165+
166+
/**
167+
* @inheritDoc..$args
168+
* @return void
169+
*/
170+
public function setFetchMode($mode, ...$args): void
171+
{
172+
$this->options[PDO::ATTR_DEFAULT_FETCH_MODE] = $mode;
173+
}
165174
}

0 commit comments

Comments
 (0)