Skip to content

Commit 5e77a52

Browse files
committed
drivers uses PDO instead of Connection
1 parent d8f101c commit 5e77a52

6 files changed

Lines changed: 58 additions & 60 deletions

File tree

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',
@@ -102,10 +102,10 @@ public function getColumns(string $table): array
102102
FROM
103103
INFORMATION_SCHEMA.COLUMNS
104104
WHERE
105-
TABLE_SCHEMA = {$this->connection->quote($table_schema)}
106-
AND TABLE_NAME = {$this->connection->quote($table_name)}";
105+
TABLE_SCHEMA = {$this->pdo->quote($table_schema)}
106+
AND TABLE_NAME = {$this->pdo->quote($table_name)}";
107107

108-
foreach ($this->connection->query($query) as $row) {
108+
foreach ($this->pdo->query($query) as $row) {
109109
$columns[] = [
110110
'name' => $row['COLUMN_NAME'],
111111
'table' => $table,
@@ -116,7 +116,7 @@ public function getColumns(string $table): array
116116
'default' => $row['COLUMN_DEFAULT'],
117117
'autoincrement' => $row['DOMAIN_NAME'] === 'COUNTER',
118118
'primary' => $row['COLUMN_NAME'] === 'ID',
119-
'vendor' => (array) $row,
119+
'vendor' => $row,
120120
];
121121
}
122122
return $columns;
@@ -141,11 +141,11 @@ public function getIndexes(string $table): array
141141
INNER JOIN sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id
142142
INNER JOIN sys.tables t ON ind.object_id = t.object_id
143143
WHERE
144-
t.name = {$this->connection->quote($table_name)}
144+
t.name = {$this->pdo->quote($table_name)}
145145
ORDER BY
146146
t.name, ind.name, ind.index_id, ic.index_column_id";
147147

148-
foreach ($this->connection->query($query) as $row) {
148+
foreach ($this->pdo->query($query) as $row) {
149149
$indexes[$row['name_index']]['name'] = $row['name_index'];
150150
$indexes[$row['name_index']]['unique'] = $row['is_unique'] !== 'False';
151151
$indexes[$row['name_index']]['primary'] = $row['is_primary_key'] !== 'False';
@@ -181,9 +181,9 @@ public function getForeignKeys(string $table): array
181181
INNER JOIN sys.columns col2
182182
ON col2.column_id = referenced_column_id AND col2.object_id = tab2.object_id
183183
WHERE
184-
tab1.name = {$this->connection->quote($table_name)}";
184+
tab1.name = {$this->pdo->quote($table_name)}";
185185

186-
foreach ($this->connection->query($query) as $id => $row) {
186+
foreach ($this->pdo->query($query) as $id => $row) {
187187
$keys[$id]['name'] = $row['fk_name'];
188188
$keys[$id]['local'] = $row['column'];
189189
$keys[$id]['table'] = $table_schema . '.' . $row['referenced_table'];

src/Database/Drivers/MySqlDriver.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function formatDateInterval(\DateInterval $value): string
8888
public function formatLike(string $value, int $pos): string
8989
{
9090
$value = str_replace('\\', '\\\\', $value);
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 ? "%'" : "'");
9393
}
9494

@@ -112,7 +112,7 @@ public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
112112
public function getTables(): array
113113
{
114114
$tables = [];
115-
foreach ($this->connection->query('SHOW FULL TABLES') as $row) {
115+
foreach ($this->pdo->query('SHOW FULL TABLES') as $row) {
116116
$tables[] = [
117117
'name' => $row[0],
118118
'view' => ($row[1] ?? null) === 'VIEW',
@@ -125,8 +125,8 @@ public function getTables(): array
125125
public function getColumns(string $table): array
126126
{
127127
$columns = [];
128-
foreach ($this->connection->query('SHOW FULL COLUMNS FROM ' . $this->delimite($table)) as $row) {
129-
$row = array_change_key_case((array) $row, CASE_LOWER);
128+
foreach ($this->pdo->query('SHOW FULL COLUMNS FROM ' . $this->delimite($table)) as $row) {
129+
$row = array_change_key_case($row, CASE_LOWER);
130130
$type = explode('(', $row['type']);
131131
$columns[] = [
132132
'name' => $row['field'],
@@ -137,7 +137,7 @@ public function getColumns(string $table): array
137137
'default' => $row['default'],
138138
'autoincrement' => $row['extra'] === 'auto_increment',
139139
'primary' => $row['key'] === 'PRI',
140-
'vendor' => (array) $row,
140+
'vendor' => $row,
141141
];
142142
}
143143
return $columns;
@@ -147,8 +147,8 @@ public function getColumns(string $table): array
147147
public function getIndexes(string $table): array
148148
{
149149
$indexes = [];
150-
foreach ($this->connection->query('SHOW INDEX FROM ' . $this->delimite($table)) as $row) {
151-
$row = array_change_key_case((array) $row, CASE_LOWER);
150+
foreach ($this->pdo->query('SHOW INDEX FROM ' . $this->delimite($table)) as $row) {
151+
$row = array_change_key_case($row, CASE_LOWER);
152152
$indexes[$row['key_name']]['name'] = $row['key_name'];
153153
$indexes[$row['key_name']]['unique'] = !$row['non_unique'];
154154
$indexes[$row['key_name']]['primary'] = $row['key_name'] === 'PRIMARY';
@@ -162,10 +162,10 @@ public function getForeignKeys(string $table): array
162162
{
163163
$keys = [];
164164
$query = 'SELECT CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM information_schema.KEY_COLUMN_USAGE '
165-
. 'WHERE TABLE_SCHEMA = DATABASE() AND REFERENCED_TABLE_NAME IS NOT NULL AND TABLE_NAME = ' . $this->connection->quote($table);
165+
. 'WHERE TABLE_SCHEMA = DATABASE() AND REFERENCED_TABLE_NAME IS NOT NULL AND TABLE_NAME = ' . $this->pdo->quote($table);
166166

167-
foreach ($this->connection->query($query) as $id => $row) {
168-
$row = array_change_key_case((array) $row, CASE_LOWER);
167+
foreach ($this->pdo->query($query) as $id => $row) {
168+
$row = array_change_key_case($row, CASE_LOWER);
169169
$keys[$id]['name'] = $row['constraint_name']; // foreign key name
170170
$keys[$id]['local'] = $row['column_name']; // local columns
171171
$keys[$id]['table'] = $row['referenced_table_name']; // referenced table

src/Database/Drivers/OciDriver.php

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

src/Database/Drivers/PgSqlDriver.php

Lines changed: 11 additions & 12 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
}
@@ -92,7 +92,7 @@ public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
9292
public function getTables(): array
9393
{
9494
$tables = [];
95-
foreach ($this->connection->query("
95+
foreach ($this->pdo->query("
9696
SELECT DISTINCT ON (c.relname)
9797
c.relname::varchar AS name,
9898
c.relkind IN ('v', 'm') AS view,
@@ -106,7 +106,7 @@ public function getTables(): array
106106
ORDER BY
107107
c.relname
108108
") as $row) {
109-
$tables[] = (array) $row;
109+
$tables[] = $row;
110110
}
111111

112112
return $tables;
@@ -116,7 +116,7 @@ public function getTables(): array
116116
public function getColumns(string $table): array
117117
{
118118
$columns = [];
119-
foreach ($this->connection->query("
119+
foreach ($this->pdo->query("
120120
SELECT
121121
a.attname::varchar AS name,
122122
c.relname::varchar AS table,
@@ -137,13 +137,12 @@ public function getColumns(string $table): array
137137
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)
138138
WHERE
139139
c.relkind IN ('r', 'v')
140-
AND c.oid = {$this->connection->quote($this->delimiteFQN($table))}::regclass
140+
AND c.oid = {$this->pdo->quote($this->delimiteFQN($table))}::regclass
141141
AND a.attnum > 0
142142
AND NOT a.attisdropped
143143
ORDER BY
144144
a.attnum
145-
") as $row) {
146-
$column = (array) $row;
145+
") as $column) {
147146
$column['vendor'] = $column;
148147
unset($column['sequence']);
149148

@@ -157,7 +156,7 @@ public function getColumns(string $table): array
157156
public function getIndexes(string $table): array
158157
{
159158
$indexes = [];
160-
foreach ($this->connection->query("
159+
foreach ($this->pdo->query("
161160
SELECT
162161
c2.relname::varchar AS name,
163162
i.indisunique AS unique,
@@ -170,7 +169,7 @@ public function getIndexes(string $table): array
170169
LEFT JOIN pg_catalog.pg_attribute AS a ON c1.oid = a.attrelid AND a.attnum = ANY(i.indkey)
171170
WHERE
172171
c1.relkind = 'r'
173-
AND c1.oid = {$this->connection->quote($this->delimiteFQN($table))}::regclass
172+
AND c1.oid = {$this->pdo->quote($this->delimiteFQN($table))}::regclass
174173
") as $row) {
175174
$indexes[$row['name']]['name'] = $row['name'];
176175
$indexes[$row['name']]['unique'] = $row['unique'];
@@ -185,7 +184,7 @@ public function getIndexes(string $table): array
185184
public function getForeignKeys(string $table): array
186185
{
187186
/* Does't work with multicolumn foreign keys */
188-
return $this->connection->query("
187+
return $this->pdo->query("
189188
SELECT
190189
co.conname::varchar AS name,
191190
al.attname::varchar AS local,
@@ -200,7 +199,7 @@ public function getForeignKeys(string $table): array
200199
JOIN pg_catalog.pg_attribute AS af ON af.attrelid = cf.oid AND af.attnum = co.confkey[1]
201200
WHERE
202201
co.contype = 'f'
203-
AND cl.oid = {$this->connection->quote($this->delimiteFQN($table))}::regclass
202+
AND cl.oid = {$this->pdo->quote($this->delimiteFQN($table))}::regclass
204203
AND nf.nspname = ANY (pg_catalog.current_schemas(FALSE))
205204
")->fetchAll();
206205
}

src/Database/Drivers/SqliteDriver.php

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

8484
public function formatLike(string $value, int $pos): string
8585
{
86-
$value = addcslashes(substr($this->connection->quote($value), 1, -1), '%_\\');
86+
$value = addcslashes(substr($this->pdo->quote($value), 1, -1), '%_\\');
8787
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'") . " ESCAPE '\\'";
8888
}
8989

@@ -106,15 +106,15 @@ public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
106106
public function getTables(): array
107107
{
108108
$tables = [];
109-
foreach ($this->connection->query("
109+
foreach ($this->pdo->query("
110110
SELECT name, type = 'view' as view FROM sqlite_master WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite_%'
111111
UNION ALL
112112
SELECT name, type = 'view' as view FROM sqlite_temp_master WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite_%'
113113
ORDER BY name
114114
") as $row) {
115115
$tables[] = [
116-
'name' => $row->name,
117-
'view' => (bool) $row->view,
116+
'name' => $row['name'],
117+
'view' => (bool) $row['view'],
118118
];
119119
}
120120

@@ -124,14 +124,14 @@ public function getTables(): array
124124

125125
public function getColumns(string $table): array
126126
{
127-
$meta = $this->connection->query("
128-
SELECT sql FROM sqlite_master WHERE type = 'table' AND name = {$this->connection->quote($table)}
127+
$meta = $this->pdo->query("
128+
SELECT sql FROM sqlite_master WHERE type = 'table' AND name = {$this->pdo->quote($table)}
129129
UNION ALL
130-
SELECT sql FROM sqlite_temp_master WHERE type = 'table' AND name = {$this->connection->quote($table)}
130+
SELECT sql FROM sqlite_temp_master WHERE type = 'table' AND name = {$this->pdo->quote($table)}
131131
")->fetch();
132132

133133
$columns = [];
134-
foreach ($this->connection->query("PRAGMA table_info({$this->delimite($table)})") as $row) {
134+
foreach ($this->pdo->query("PRAGMA table_info({$this->delimite($table)})") as $row) {
135135
$column = $row['name'];
136136
$pattern = "/(\"$column\"|`$column`|\\[$column\\]|$column)\\s+[^,]+\\s+PRIMARY\\s+KEY\\s+AUTOINCREMENT/Ui";
137137
$type = explode('(', $row['type']);
@@ -140,11 +140,11 @@ public function getColumns(string $table): array
140140
'table' => $table,
141141
'nativetype' => strtoupper($type[0]),
142142
'size' => isset($type[1]) ? (int) $type[1] : null,
143-
'nullable' => $row['notnull'] === 0,
143+
'nullable' => !$row['notnull'],
144144
'default' => $row['dflt_value'],
145145
'autoincrement' => $meta && preg_match($pattern, (string) $meta['sql']),
146146
'primary' => $row['pk'] > 0,
147-
'vendor' => (array) $row,
147+
'vendor' => $row,
148148
];
149149
}
150150
return $columns;
@@ -154,14 +154,14 @@ public function getColumns(string $table): array
154154
public function getIndexes(string $table): array
155155
{
156156
$indexes = [];
157-
foreach ($this->connection->query("PRAGMA index_list({$this->delimite($table)})") as $row) {
157+
foreach ($this->pdo->query("PRAGMA index_list({$this->delimite($table)})") as $row) {
158158
$indexes[$row['name']]['name'] = $row['name'];
159159
$indexes[$row['name']]['unique'] = (bool) $row['unique'];
160160
$indexes[$row['name']]['primary'] = false;
161161
}
162162

163163
foreach ($indexes as $index => $values) {
164-
$res = $this->connection->query("PRAGMA index_info({$this->delimite($index)})");
164+
$res = $this->pdo->query("PRAGMA index_info({$this->delimite($index)})");
165165
while ($row = $res->fetch()) {
166166
$indexes[$index]['columns'][$row['seqno']] = $row['name'];
167167
}
@@ -198,7 +198,7 @@ public function getIndexes(string $table): array
198198
public function getForeignKeys(string $table): array
199199
{
200200
$keys = [];
201-
foreach ($this->connection->query("PRAGMA foreign_key_list({$this->delimite($table)})") as $row) {
201+
foreach ($this->pdo->query("PRAGMA foreign_key_list({$this->delimite($table)})") as $row) {
202202
$keys[$row['id']]['name'] = $row['id']; // foreign key name
203203
$keys[$row['id']]['local'] = $row['from']; // local columns
204204
$keys[$row['id']]['table'] = $row['table']; // referenced table

src/Database/Drivers/SqlsrvDriver.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
9494
public function getTables(): array
9595
{
9696
$tables = [];
97-
foreach ($this->connection->query("
97+
foreach ($this->pdo->query("
9898
SELECT
9999
name,
100100
CASE type
@@ -107,8 +107,8 @@ public function getTables(): array
107107
type IN ('U', 'V')
108108
") as $row) {
109109
$tables[] = [
110-
'name' => $row->name,
111-
'view' => (bool) $row->view,
110+
'name' => $row['name'],
111+
'view' => (bool) $row['view'],
112112
];
113113
}
114114

@@ -119,7 +119,7 @@ public function getTables(): array
119119
public function getColumns(string $table): array
120120
{
121121
$columns = [];
122-
foreach ($this->connection->query("
122+
foreach ($this->pdo->query("
123123
SELECT
124124
c.name AS name,
125125
o.name AS [table],
@@ -140,9 +140,8 @@ public function getColumns(string $table): array
140140
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
141141
WHERE
142142
o.type IN ('U', 'V')
143-
AND o.name = {$this->connection->quote($table)}
143+
AND o.name = {$this->pdo->quote($table)}
144144
") as $row) {
145-
$row = (array) $row;
146145
$row['vendor'] = $row;
147146
$row['nullable'] = (bool) $row['nullable'];
148147
$row['autoincrement'] = (bool) $row['autoincrement'];
@@ -158,7 +157,7 @@ public function getColumns(string $table): array
158157
public function getIndexes(string $table): array
159158
{
160159
$indexes = [];
161-
foreach ($this->connection->query("
160+
foreach ($this->pdo->query("
162161
SELECT
163162
i.name AS name,
164163
CASE WHEN i.is_unique = 1 OR i.is_unique_constraint = 1
@@ -173,15 +172,15 @@ public function getIndexes(string $table): array
173172
JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
174173
JOIN sys.tables t ON i.object_id = t.object_id
175174
WHERE
176-
t.name = {$this->connection->quote($table)}
175+
t.name = {$this->pdo->quote($table)}
177176
ORDER BY
178177
i.index_id,
179178
ic.index_column_id
180179
") as $row) {
181-
$indexes[$row->name]['name'] = $row->name;
182-
$indexes[$row->name]['unique'] = (bool) $row->unique;
183-
$indexes[$row->name]['primary'] = (bool) $row->primary;
184-
$indexes[$row->name]['columns'][] = $row->column;
180+
$indexes[$row['name']]['name'] = $row['name'];
181+
$indexes[$row['name']]['unique'] = (bool) $row['unique'];
182+
$indexes[$row['name']]['primary'] = (bool) $row['primary'];
183+
$indexes[$row['name']]['columns'][] = $row['column'];
185184
}
186185

187186
return array_values($indexes);
@@ -192,7 +191,7 @@ public function getForeignKeys(string $table): array
192191
{
193192
// Does't work with multicolumn foreign keys
194193
$keys = [];
195-
foreach ($this->connection->query("
194+
foreach ($this->pdo->query("
196195
SELECT
197196
fk.name AS name,
198197
cl.name AS local,
@@ -206,9 +205,9 @@ public function getForeignKeys(string $table): array
206205
JOIN sys.tables tf ON fkc.referenced_object_id = tf.object_id
207206
JOIN sys.columns cf ON fkc.referenced_object_id = cf.object_id AND fkc.referenced_column_id = cf.column_id
208207
WHERE
209-
tl.name = {$this->connection->quote($table)}
208+
tl.name = {$this->pdo->quote($table)}
210209
") as $row) {
211-
$keys[$row->name] = (array) $row;
210+
$keys[$row['name']] = $row;
212211
}
213212

214213
return array_values($keys);

0 commit comments

Comments
 (0)