Skip to content

Commit 38b88d5

Browse files
committed
drivers uses PDO instead of Connection
1 parent 1297897 commit 38b88d5

File tree

6 files changed

+54
-62
lines changed

6 files changed

+54
-62
lines changed

src/Database/Drivers/MsSqlDriver.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
7575
public function getTables(): array
7676
{
7777
$tables = [];
78-
foreach ($this->connection->query('SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES') as $row) {
78+
foreach ($this->pdo->query('SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES') as $row) {
7979
$tables[] = [
8080
'name' => $row['TABLE_SCHEMA'] . '.' . $row['TABLE_NAME'],
8181
'view' => ($row['TABLE_TYPE'] ?? null) === 'VIEW',
@@ -103,11 +103,11 @@ public function getColumns(string $table): array
103103
FROM
104104
INFORMATION_SCHEMA.COLUMNS
105105
WHERE
106-
TABLE_SCHEMA = {$this->connection->quote($table_schema)}
107-
AND TABLE_NAME = {$this->connection->quote($table_name)}
106+
TABLE_SCHEMA = {$this->pdo->quote($table_schema)}
107+
AND TABLE_NAME = {$this->pdo->quote($table_name)}
108108
X;
109109

110-
foreach ($this->connection->query($query) as $row) {
110+
foreach ($this->pdo->query($query, \PDO::FETCH_ASSOC) as $row) {
111111
$columns[] = [
112112
'name' => $row['COLUMN_NAME'],
113113
'table' => $table,
@@ -119,7 +119,7 @@ public function getColumns(string $table): array
119119
'default' => $row['COLUMN_DEFAULT'],
120120
'autoincrement' => $row['DOMAIN_NAME'] === 'COUNTER',
121121
'primary' => $row['COLUMN_NAME'] === 'ID',
122-
'vendor' => (array) $row,
122+
'vendor' => $row,
123123
];
124124
}
125125

@@ -145,12 +145,12 @@ public function getIndexes(string $table): array
145145
INNER JOIN sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id
146146
INNER JOIN sys.tables t ON ind.object_id = t.object_id
147147
WHERE
148-
t.name = {$this->connection->quote($table_name)}
148+
t.name = {$this->pdo->quote($table_name)}
149149
ORDER BY
150150
t.name, ind.name, ind.index_id, ic.index_column_id
151151
X;
152152

153-
foreach ($this->connection->query($query) as $row) {
153+
foreach ($this->pdo->query($query) as $row) {
154154
$id = $row['name_index'];
155155
$indexes[$id]['name'] = $id;
156156
$indexes[$id]['unique'] = $row['is_unique'] !== 'False';
@@ -188,10 +188,10 @@ public function getForeignKeys(string $table): array
188188
INNER JOIN sys.columns col2
189189
ON col2.column_id = referenced_column_id AND col2.object_id = tab2.object_id
190190
WHERE
191-
tab1.name = {$this->connection->quote($table_name)}
191+
tab1.name = {$this->pdo->quote($table_name)}
192192
X;
193193

194-
foreach ($this->connection->query($query) as $id => $row) {
194+
foreach ($this->pdo->query($query) as $id => $row) {
195195
$keys[$id]['name'] = $row['fk_name'];
196196
$keys[$id]['local'] = $row['column'];
197197
$keys[$id]['table'] = $table_schema . '.' . $row['referenced_table'];

src/Database/Drivers/MySqlDriver.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function formatDateInterval(\DateInterval $value): string
110110
public function formatLike(string $value, int $pos): string
111111
{
112112
$value = str_replace('\\', '\\\\', $value);
113-
$value = addcslashes(substr($this->connection->quote($value), 1, -1), '%_');
113+
$value = addcslashes(substr($this->pdo->quote($value), 1, -1), '%_');
114114
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
115115
}
116116

@@ -134,7 +134,7 @@ public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
134134
public function getTables(): array
135135
{
136136
$tables = [];
137-
foreach ($this->connection->query('SHOW FULL TABLES') as $row) {
137+
foreach ($this->pdo->query('SHOW FULL TABLES') as $row) {
138138
$tables[] = [
139139
'name' => $row[0],
140140
'view' => ($row[1] ?? null) === 'VIEW',
@@ -148,8 +148,8 @@ public function getTables(): array
148148
public function getColumns(string $table): array
149149
{
150150
$columns = [];
151-
foreach ($this->connection->query('SHOW FULL COLUMNS FROM ' . $this->delimite($table)) as $row) {
152-
$row = array_change_key_case((array) $row, CASE_LOWER);
151+
foreach ($this->pdo->query('SHOW FULL COLUMNS FROM ' . $this->delimite($table), \PDO::FETCH_ASSOC) as $row) {
152+
$row = array_change_key_case($row, CASE_LOWER);
153153
$pair = explode('(', $row['type']);
154154
$type = match (true) {
155155
$pair[0] === 'decimal' && str_ends_with($pair[1], ',0)') => Type::Integer,
@@ -178,7 +178,7 @@ public function getColumns(string $table): array
178178
public function getIndexes(string $table): array
179179
{
180180
$indexes = [];
181-
foreach ($this->connection->query('SHOW INDEX FROM ' . $this->delimite($table)) as $row) {
181+
foreach ($this->pdo->query('SHOW INDEX FROM ' . $this->delimite($table)) as $row) {
182182
$id = $row['Key_name'];
183183
$indexes[$id]['name'] = $id;
184184
$indexes[$id]['unique'] = !$row['Non_unique'];
@@ -193,12 +193,12 @@ public function getIndexes(string $table): array
193193
public function getForeignKeys(string $table): array
194194
{
195195
$keys = [];
196-
foreach ($this->connection->query(<<<X
196+
foreach ($this->pdo->query(<<<X
197197
SELECT CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME
198198
FROM information_schema.KEY_COLUMN_USAGE
199199
WHERE TABLE_SCHEMA = DATABASE()
200200
AND REFERENCED_TABLE_NAME IS NOT NULL
201-
AND TABLE_NAME = {$this->connection->quote($table)}
201+
AND TABLE_NAME = {$this->pdo->quote($table)}
202202
X) as $id => $row) {
203203
$keys[$id]['name'] = $row['CONSTRAINT_NAME'];
204204
$keys[$id]['local'] = $row['COLUMN_NAME'];

src/Database/Drivers/OciDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
102102
public function getTables(): array
103103
{
104104
$tables = [];
105-
foreach ($this->connection->query('SELECT * FROM cat') as $row) {
105+
foreach ($this->pdo->query('SELECT * FROM cat') as $row) {
106106
if ($row[1] === 'TABLE' || $row[1] === 'VIEW') {
107107
$tables[] = [
108108
'name' => $row[0],

src/Database/Drivers/PgSqlDriver.php

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ public function formatDateInterval(\DateInterval $value): string
6565

6666
public function formatLike(string $value, int $pos): string
6767
{
68-
$bs = substr($this->connection->quote('\\'), 1, -1); // standard_conforming_strings = on/off
69-
$value = substr($this->connection->quote($value), 1, -1);
68+
$bs = substr($this->pdo->quote('\\'), 1, -1); // standard_conforming_strings = on/off
69+
$value = substr($this->pdo->quote($value), 1, -1);
7070
$value = strtr($value, ['%' => $bs . '%', '_' => $bs . '_', '\\' => '\\\\']);
7171
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
7272
}
@@ -93,8 +93,7 @@ public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
9393

9494
public function getTables(): array
9595
{
96-
$tables = [];
97-
foreach ($this->connection->query(<<<'X'
96+
return $this->pdo->query(<<<'X'
9897
SELECT DISTINCT ON (c.relname)
9998
c.relname::varchar AS name,
10099
c.relkind IN ('v', 'm') AS view,
@@ -107,18 +106,14 @@ public function getTables(): array
107106
AND n.nspname = ANY (pg_catalog.current_schemas(FALSE))
108107
ORDER BY
109108
c.relname
110-
X) as $row) {
111-
$tables[] = (array) $row;
112-
}
113-
114-
return $tables;
109+
X)->fetchAll(\PDO::FETCH_ASSOC);
115110
}
116111

117112

118113
public function getColumns(string $table): array
119114
{
120115
$columns = [];
121-
foreach ($this->connection->query(<<<X
116+
foreach ($this->pdo->query(<<<X
122117
SELECT
123118
a.attname::varchar AS name,
124119
c.relname::varchar AS table,
@@ -140,13 +135,12 @@ public function getColumns(string $table): array
140135
LEFT JOIN pg_catalog.pg_constraint AS co ON co.connamespace = c.relnamespace AND contype = 'p' AND co.conrelid = c.oid AND a.attnum = ANY(co.conkey)
141136
WHERE
142137
c.relkind IN ('r', 'v', 'm', 'p')
143-
AND c.oid = {$this->connection->quote($this->delimiteFQN($table))}::regclass
138+
AND c.oid = {$this->pdo->quote($this->delimiteFQN($table))}::regclass
144139
AND a.attnum > 0
145140
AND NOT a.attisdropped
146141
ORDER BY
147142
a.attnum
148-
X) as $row) {
149-
$column = (array) $row;
143+
X, \PDO::FETCH_ASSOC) as $column) {
150144
$column['type'] = Nette\Database\Helpers::detectType($column['type']);
151145
$column['vendor'] = $column;
152146
unset($column['sequence']);
@@ -161,7 +155,7 @@ public function getColumns(string $table): array
161155
public function getIndexes(string $table): array
162156
{
163157
$indexes = [];
164-
foreach ($this->connection->query(<<<X
158+
foreach ($this->pdo->query(<<<X
165159
SELECT
166160
c2.relname::varchar AS name,
167161
i.indisunique AS unique,
@@ -174,7 +168,7 @@ public function getIndexes(string $table): array
174168
LEFT JOIN pg_catalog.pg_attribute AS a ON c1.oid = a.attrelid AND a.attnum = ANY(i.indkey)
175169
WHERE
176170
c1.relkind IN ('r', 'p')
177-
AND c1.oid = {$this->connection->quote($this->delimiteFQN($table))}::regclass
171+
AND c1.oid = {$this->pdo->quote($this->delimiteFQN($table))}::regclass
178172
X) as $row) {
179173
$id = $row['name'];
180174
$indexes[$id]['name'] = $id;
@@ -190,7 +184,7 @@ public function getIndexes(string $table): array
190184
public function getForeignKeys(string $table): array
191185
{
192186
/* Doesn't work with multi-column foreign keys */
193-
return $this->connection->query(<<<X
187+
return $this->pdo->query(<<<X
194188
SELECT
195189
co.conname::varchar AS name,
196190
al.attname::varchar AS local,
@@ -205,9 +199,9 @@ public function getForeignKeys(string $table): array
205199
JOIN pg_catalog.pg_attribute AS af ON af.attrelid = cf.oid AND af.attnum = co.confkey[1]
206200
WHERE
207201
co.contype = 'f'
208-
AND cl.oid = {$this->connection->quote($this->delimiteFQN($table))}::regclass
202+
AND cl.oid = {$this->pdo->quote($this->delimiteFQN($table))}::regclass
209203
AND nf.nspname = ANY (pg_catalog.current_schemas(FALSE))
210-
X)->fetchAll();
204+
X)->fetchAll(\PDO::FETCH_ASSOC);
211205
}
212206

213207

src/Database/Drivers/SqliteDriver.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function formatDateInterval(\DateInterval $value): string
8888

8989
public function formatLike(string $value, int $pos): string
9090
{
91-
$value = addcslashes(substr($this->connection->quote($value), 1, -1), '%_\\');
91+
$value = addcslashes(substr($this->pdo->quote($value), 1, -1), '%_\\');
9292
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'") . " ESCAPE '\\'";
9393
}
9494

@@ -111,7 +111,7 @@ public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
111111
public function getTables(): array
112112
{
113113
$tables = [];
114-
foreach ($this->connection->query(<<<'X'
114+
foreach ($this->pdo->query(<<<'X'
115115
SELECT name, type = 'view' as view
116116
FROM sqlite_master
117117
WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite_%'
@@ -122,8 +122,8 @@ public function getTables(): array
122122
ORDER BY name
123123
X) as $row) {
124124
$tables[] = [
125-
'name' => $row->name,
126-
'view' => (bool) $row->view,
125+
'name' => $row['name'],
126+
'view' => (bool) $row['view'],
127127
];
128128
}
129129

@@ -133,18 +133,18 @@ public function getTables(): array
133133

134134
public function getColumns(string $table): array
135135
{
136-
$meta = $this->connection->query(<<<X
136+
$meta = $this->pdo->query(<<<X
137137
SELECT sql
138138
FROM sqlite_master
139-
WHERE type = 'table' AND name = {$this->connection->quote($table)}
139+
WHERE type = 'table' AND name = {$this->pdo->quote($table)}
140140
UNION ALL
141141
SELECT sql
142142
FROM sqlite_temp_master
143-
WHERE type = 'table' AND name = {$this->connection->quote($table)}
143+
WHERE type = 'table' AND name = {$this->pdo->quote($table)}
144144
X)->fetch();
145145

146146
$columns = [];
147-
foreach ($this->connection->query("PRAGMA table_info({$this->delimite($table)})") as $row) {
147+
foreach ($this->pdo->query("PRAGMA table_info({$this->delimite($table)})", \PDO::FETCH_ASSOC) as $row) {
148148
$column = $row['name'];
149149
$pattern = "/(\"$column\"|`$column`|\\[$column\\]|$column)\\s+[^,]+\\s+PRIMARY\\s+KEY\\s+AUTOINCREMENT/Ui";
150150
$pair = explode('(', $row['type']);
@@ -158,11 +158,11 @@ public function getColumns(string $table): array
158158
'type' => $type,
159159
'nativetype' => strtoupper($pair[0]),
160160
'size' => isset($pair[1]) ? (int) $pair[1] : null,
161-
'nullable' => $row['notnull'] === 0,
161+
'nullable' => !$row['notnull'],
162162
'default' => $row['dflt_value'],
163163
'autoincrement' => $meta && preg_match($pattern, (string) $meta['sql']),
164164
'primary' => $row['pk'] > 0,
165-
'vendor' => (array) $row,
165+
'vendor' => $row,
166166
];
167167
}
168168

@@ -173,16 +173,15 @@ public function getColumns(string $table): array
173173
public function getIndexes(string $table): array
174174
{
175175
$indexes = [];
176-
foreach ($this->connection->query("PRAGMA index_list({$this->delimite($table)})") as $row) {
176+
foreach ($this->pdo->query("PRAGMA index_list({$this->delimite($table)})") as $row) {
177177
$id = $row['name'];
178178
$indexes[$id]['name'] = $id;
179179
$indexes[$id]['unique'] = (bool) $row['unique'];
180180
$indexes[$id]['primary'] = false;
181181
}
182182

183183
foreach ($indexes as $index => $values) {
184-
$res = $this->connection->query("PRAGMA index_info({$this->delimite($index)})");
185-
while ($row = $res->fetch()) {
184+
foreach ($this->pdo->query("PRAGMA index_info({$this->delimite($index)})") as $row) {
186185
$indexes[$index]['columns'][] = $row['name'];
187186
}
188187
}
@@ -219,7 +218,7 @@ public function getIndexes(string $table): array
219218
public function getForeignKeys(string $table): array
220219
{
221220
$keys = [];
222-
foreach ($this->connection->query("PRAGMA foreign_key_list({$this->delimite($table)})") as $row) {
221+
foreach ($this->pdo->query("PRAGMA foreign_key_list({$this->delimite($table)})") as $row) {
223222
$id = $row['id'];
224223
$keys[$id]['name'] = $id;
225224
$keys[$id]['local'] = $row['from'];

src/Database/Drivers/SqlsrvDriver.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
9999
public function getTables(): array
100100
{
101101
$tables = [];
102-
foreach ($this->connection->query(<<<'X'
102+
foreach ($this->pdo->query(<<<'X'
103103
SELECT
104104
name,
105105
CASE type
@@ -112,8 +112,8 @@ public function getTables(): array
112112
type IN ('U', 'V')
113113
X) as $row) {
114114
$tables[] = [
115-
'name' => $row->name,
116-
'view' => (bool) $row->view,
115+
'name' => $row['name'],
116+
'view' => (bool) $row['view'],
117117
];
118118
}
119119

@@ -124,7 +124,7 @@ public function getTables(): array
124124
public function getColumns(string $table): array
125125
{
126126
$columns = [];
127-
foreach ($this->connection->query(<<<X
127+
foreach ($this->pdo->query(<<<X
128128
SELECT
129129
c.name AS name,
130130
o.name AS [table],
@@ -146,9 +146,8 @@ public function getColumns(string $table): array
146146
LEFT JOIN sys.index_columns i ON k.parent_object_id = i.object_id AND i.index_id = k.unique_index_id AND i.column_id = c.column_id
147147
WHERE
148148
o.type IN ('U', 'V')
149-
AND o.name = {$this->connection->quote($table)}
150-
X) as $row) {
151-
$row = (array) $row;
149+
AND o.name = {$this->pdo->quote($table)}
150+
X, \PDO::FETCH_ASSOC) as $row) {
152151
$row['type'] = Nette\Database\Helpers::detectType($row['type']);
153152
$row['vendor'] = $row;
154153
$row['nullable'] = (bool) $row['nullable'];
@@ -165,7 +164,7 @@ public function getColumns(string $table): array
165164
public function getIndexes(string $table): array
166165
{
167166
$indexes = [];
168-
foreach ($this->connection->query(<<<X
167+
foreach ($this->pdo->query(<<<X
169168
SELECT
170169
i.name AS name,
171170
CASE WHEN i.is_unique = 1 OR i.is_unique_constraint = 1
@@ -180,7 +179,7 @@ public function getIndexes(string $table): array
180179
JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
181180
JOIN sys.tables t ON i.object_id = t.object_id
182181
WHERE
183-
t.name = {$this->connection->quote($table)}
182+
t.name = {$this->pdo->quote($table)}
184183
ORDER BY
185184
i.index_id,
186185
ic.index_column_id
@@ -200,7 +199,7 @@ public function getForeignKeys(string $table): array
200199
{
201200
// Does't work with multicolumn foreign keys
202201
$keys = [];
203-
foreach ($this->connection->query(<<<X
202+
foreach ($this->pdo->query(<<<X
204203
SELECT
205204
fk.name AS name,
206205
cl.name AS local,
@@ -214,9 +213,9 @@ public function getForeignKeys(string $table): array
214213
JOIN sys.tables tf ON fkc.referenced_object_id = tf.object_id
215214
JOIN sys.columns cf ON fkc.referenced_object_id = cf.object_id AND fkc.referenced_column_id = cf.column_id
216215
WHERE
217-
tl.name = {$this->connection->quote($table)}
218-
X) as $row) {
219-
$keys[$row->name] = (array) $row;
216+
tl.name = {$this->pdo->quote($table)}
217+
X, \PDO::FETCH_ASSOC) as $row) {
218+
$keys[$row['name']] = $row;
220219
}
221220

222221
return array_values($keys);

0 commit comments

Comments
 (0)