File: SqlDumper.php Lines 7 to 33 |
|
7 | */ |
|
8 | class SqlDumper |
|
9 | { |
|
10 | /** |
|
11 | * 実行するPrepared Statementを non-prepared なSQLにして返す |
|
12 | * @param string $psql (non) prepared statement sql |
|
13 | * @param array $params placeholders |
|
14 | * @return string binded sql |
|
15 | */ |
| | public static function dump($psql, $params) |
|
17 | { |
|
18 | $keys = []; |
|
19 | $values = []; |
|
20 | |
|
21 | $isNamedPlaceholders = false; |
|
22 | if (is_array($params) && !empty($params) && is_string(key($params))) { |
| | uksort($params, function ($k1, $k2) { |
|
24 | return strlen($k2) - strlen($k1); |
|
25 | }); |
|
26 | $isNamedPlaceholders = true; |
|
27 | } |
|
28 | |
|
29 | foreach ($params as $key => $value) { |
|
30 | if (is_string($key)) { |
|
31 | $keys[] = '/:' . ltrim($key, ':') . '/'; |
|
32 | } else { |
|
33 | $keys[] = '/[?]/'; |
|
File: SqlDumper.php Lines 40 to 52 |
|
40 | } elseif (is_float($value)) { |
|
41 | $values[] = strval($value); |
|
42 | } elseif (is_null($value)) { |
|
43 | $values[] = 'null'; |
|
44 | } |
|
45 | } |
|
46 | if ($isNamedPlaceholders) { |
|
47 | return preg_replace($keys, $values, $psql); |
|
48 | } else { |
| | return preg_replace($keys, $values, $psql, 1, $count); |
|
50 | } |
|
51 | } |
|
52 | } |
|