Skip to content

Commit

Permalink
drivers uses PDO instead of Connection
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 20, 2022
1 parent 4107396 commit 1fa4fdd
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 70 deletions.
18 changes: 9 additions & 9 deletions src/Database/Drivers/MsSqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
public function getTables(): array
{
$tables = [];
foreach ($this->connection->query('SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES') as $row) {
foreach ($this->pdo->query('SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES') as $row) {
$tables[] = [
'name' => $row['TABLE_SCHEMA'] . '.' . $row['TABLE_NAME'],
'view' => ($row['TABLE_TYPE'] ?? null) === 'VIEW',
Expand Down Expand Up @@ -103,10 +103,10 @@ public function getColumns(string $table): array
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_SCHEMA = {$this->connection->quote($table_schema)}
AND TABLE_NAME = {$this->connection->quote($table_name)}";
TABLE_SCHEMA = {$this->pdo->quote($table_schema)}
AND TABLE_NAME = {$this->pdo->quote($table_name)}";

foreach ($this->connection->query($query) as $row) {
foreach ($this->pdo->query($query, \PDO::FETCH_ASSOC) as $row) {
$columns[] = [
'name' => $row['COLUMN_NAME'],
'table' => $table,
Expand All @@ -117,7 +117,7 @@ public function getColumns(string $table): array
'default' => $row['COLUMN_DEFAULT'],
'autoincrement' => $row['DOMAIN_NAME'] === 'COUNTER',
'primary' => $row['COLUMN_NAME'] === 'ID',
'vendor' => (array) $row,
'vendor' => $row,
];
}

Expand All @@ -143,11 +143,11 @@ public function getIndexes(string $table): array
INNER JOIN sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id
INNER JOIN sys.tables t ON ind.object_id = t.object_id
WHERE
t.name = {$this->connection->quote($table_name)}
t.name = {$this->pdo->quote($table_name)}
ORDER BY
t.name, ind.name, ind.index_id, ic.index_column_id";

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

foreach ($this->connection->query($query) as $row) {
foreach ($this->pdo->query($query) as $row) {
$keys[] = [
'name' => $row['fk_name'],
'local' => $row['column'],
Expand Down
20 changes: 10 additions & 10 deletions src/Database/Drivers/MySqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function formatDateInterval(\DateInterval $value): string
public function formatLike(string $value, int $pos): string
{
$value = str_replace('\\', '\\\\', $value);
$value = addcslashes(substr($this->connection->quote($value), 1, -1), '%_');
$value = addcslashes(substr($this->pdo->quote($value), 1, -1), '%_');
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
}

Expand All @@ -113,7 +113,7 @@ public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
public function getTables(): array
{
$tables = [];
foreach ($this->connection->query('SHOW FULL TABLES') as $row) {
foreach ($this->pdo->query('SHOW FULL TABLES') as $row) {
$tables[] = [
'name' => $row[0],
'view' => ($row[1] ?? null) === 'VIEW',
Expand All @@ -127,8 +127,8 @@ public function getTables(): array
public function getColumns(string $table): array
{
$columns = [];
foreach ($this->connection->query('SHOW FULL COLUMNS FROM ' . $this->delimite($table)) as $row) {
$row = array_change_key_case((array) $row, CASE_LOWER);
foreach ($this->pdo->query('SHOW FULL COLUMNS FROM ' . $this->delimite($table), \PDO::FETCH_ASSOC) as $row) {
$row = array_change_key_case($row, CASE_LOWER);
$type = explode('(', $row['type']);
$columns[] = [
'name' => $row['field'],
Expand All @@ -139,7 +139,7 @@ public function getColumns(string $table): array
'default' => $row['default'],
'autoincrement' => $row['extra'] === 'auto_increment',
'primary' => $row['key'] === 'PRI',
'vendor' => (array) $row,
'vendor' => $row,
];
}

Expand All @@ -150,8 +150,8 @@ public function getColumns(string $table): array
public function getIndexes(string $table): array
{
$indexes = [];
foreach ($this->connection->query('SHOW INDEX FROM ' . $this->delimite($table)) as $row) {
$row = array_change_key_case((array) $row, CASE_LOWER);
foreach ($this->pdo->query('SHOW INDEX FROM ' . $this->delimite($table)) as $row) {
$row = array_change_key_case($row, CASE_LOWER);
$indexes[$row['key_name']]['name'] = $row['key_name'];
$indexes[$row['key_name']]['unique'] = !$row['non_unique'];
$indexes[$row['key_name']]['primary'] = $row['key_name'] === 'PRIMARY';
Expand All @@ -166,10 +166,10 @@ public function getForeignKeys(string $table): array
{
$keys = [];
$query = 'SELECT CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM information_schema.KEY_COLUMN_USAGE '
. 'WHERE TABLE_SCHEMA = DATABASE() AND REFERENCED_TABLE_NAME IS NOT NULL AND TABLE_NAME = ' . $this->connection->quote($table);
. 'WHERE TABLE_SCHEMA = DATABASE() AND REFERENCED_TABLE_NAME IS NOT NULL AND TABLE_NAME = ' . $this->pdo->quote($table);

foreach ($this->connection->query($query) as $row) {
$row = array_change_key_case((array) $row, CASE_LOWER);
foreach ($this->pdo->query($query) as $row) {
$row = array_change_key_case($row, CASE_LOWER);
$keys[] = [
'name' => $row['constraint_name'], // foreign key name
'local' => $row['column_name'], // local columns
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Drivers/OciDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
public function getTables(): array
{
$tables = [];
foreach ($this->connection->query('SELECT * FROM cat') as $row) {
foreach ($this->pdo->query('SELECT * FROM cat') as $row) {
if ($row[1] === 'TABLE' || $row[1] === 'VIEW') {
$tables[] = [
'name' => $row[0],
Expand Down
32 changes: 13 additions & 19 deletions src/Database/Drivers/PgSqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public function formatDateInterval(\DateInterval $value): string

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

public function getTables(): array
{
$tables = [];
foreach ($this->connection->query("
return $this->pdo->query("
SELECT DISTINCT ON (c.relname)
c.relname::varchar AS name,
c.relkind IN ('v', 'm') AS view,
Expand All @@ -107,18 +106,14 @@ public function getTables(): array
AND n.nspname = ANY (pg_catalog.current_schemas(FALSE))
ORDER BY
c.relname
") as $row) {
$tables[] = (array) $row;
}

return $tables;
")->fetchAll(\PDO::FETCH_ASSOC);
}


public function getColumns(string $table): array
{
$columns = [];
foreach ($this->connection->query("
foreach ($this->pdo->query("
SELECT
a.attname::varchar AS name,
c.relname::varchar AS table,
Expand All @@ -139,13 +134,12 @@ public function getColumns(string $table): array
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)
WHERE
c.relkind IN ('r', 'v', 'm', 'p')
AND c.oid = {$this->connection->quote($this->delimiteFQN($table))}::regclass
AND c.oid = {$this->pdo->quote($this->delimiteFQN($table))}::regclass
AND a.attnum > 0
AND NOT a.attisdropped
ORDER BY
a.attnum
") as $row) {
$column = (array) $row;
", \PDO::FETCH_ASSOC) as $column) {
$column['vendor'] = $column;
unset($column['sequence']);

Expand All @@ -159,7 +153,7 @@ public function getColumns(string $table): array
public function getIndexes(string $table): array
{
$indexes = [];
foreach ($this->connection->query("
foreach ($this->pdo->query("
SELECT
c2.relname::varchar AS name,
i.indisunique AS unique,
Expand All @@ -172,7 +166,7 @@ public function getIndexes(string $table): array
LEFT JOIN pg_catalog.pg_attribute AS a ON c1.oid = a.attrelid AND a.attnum = ANY(i.indkey)
WHERE
c1.relkind IN ('r', 'p')
AND c1.oid = {$this->connection->quote($this->delimiteFQN($table))}::regclass
AND c1.oid = {$this->pdo->quote($this->delimiteFQN($table))}::regclass
") as $row) {
$indexes[$row['name']]['name'] = $row['name'];
$indexes[$row['name']]['unique'] = $row['unique'];
Expand All @@ -186,8 +180,8 @@ public function getIndexes(string $table): array

public function getForeignKeys(string $table): array
{
/* Does't work with multicolumn foreign keys */
return $this->connection->query("
/* Doesn't work with multi-column foreign keys */
return $this->pdo->query("
SELECT
co.conname::varchar AS name,
al.attname::varchar AS local,
Expand All @@ -202,9 +196,9 @@ public function getForeignKeys(string $table): array
JOIN pg_catalog.pg_attribute AS af ON af.attrelid = cf.oid AND af.attnum = co.confkey[1]
WHERE
co.contype = 'f'
AND cl.oid = {$this->connection->quote($this->delimiteFQN($table))}::regclass
AND cl.oid = {$this->pdo->quote($this->delimiteFQN($table))}::regclass
AND nf.nspname = ANY (pg_catalog.current_schemas(FALSE))
")->fetchAll();
")->fetchAll(\PDO::FETCH_ASSOC);
}


Expand Down
27 changes: 13 additions & 14 deletions src/Database/Drivers/SqliteDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function formatDateInterval(\DateInterval $value): string

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

Expand All @@ -106,15 +106,15 @@ public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
public function getTables(): array
{
$tables = [];
foreach ($this->connection->query("
foreach ($this->pdo->query("
SELECT name, type = 'view' as view FROM sqlite_master WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite_%'
UNION ALL
SELECT name, type = 'view' as view FROM sqlite_temp_master WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite_%'
ORDER BY name
") as $row) {
$tables[] = [
'name' => $row->name,
'view' => (bool) $row->view,
'name' => $row['name'],
'view' => (bool) $row['view'],
];
}

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

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

$columns = [];
foreach ($this->connection->query("PRAGMA table_info({$this->delimite($table)})") as $row) {
foreach ($this->pdo->query("PRAGMA table_info({$this->delimite($table)})", \PDO::FETCH_ASSOC) as $row) {
$column = $row['name'];
$pattern = "/(\"$column\"|`$column`|\\[$column\\]|$column)\\s+[^,]+\\s+PRIMARY\\s+KEY\\s+AUTOINCREMENT/Ui";
$type = explode('(', $row['type']);
Expand All @@ -140,11 +140,11 @@ public function getColumns(string $table): array
'table' => $table,
'nativetype' => strtoupper($type[0]),
'size' => isset($type[1]) ? (int) $type[1] : null,
'nullable' => $row['notnull'] === 0,
'nullable' => !$row['notnull'],
'default' => $row['dflt_value'],
'autoincrement' => $meta && preg_match($pattern, (string) $meta['sql']),
'primary' => $row['pk'] > 0,
'vendor' => (array) $row,
'vendor' => $row,
];
}

Expand All @@ -155,15 +155,14 @@ public function getColumns(string $table): array
public function getIndexes(string $table): array
{
$indexes = [];
foreach ($this->connection->query("PRAGMA index_list({$this->delimite($table)})") as $row) {
foreach ($this->pdo->query("PRAGMA index_list({$this->delimite($table)})") as $row) {
$indexes[$row['name']]['name'] = $row['name'];
$indexes[$row['name']]['unique'] = (bool) $row['unique'];
$indexes[$row['name']]['primary'] = false;
}

foreach ($indexes as $index => $values) {
$res = $this->connection->query("PRAGMA index_info({$this->delimite($index)})");
while ($row = $res->fetch()) {
foreach ($this->pdo->query("PRAGMA index_info({$this->delimite($index)})") as $row) {
$indexes[$index]['columns'][$row['seqno']] = $row['name'];
}
}
Expand Down Expand Up @@ -200,7 +199,7 @@ public function getIndexes(string $table): array
public function getForeignKeys(string $table): array
{
$keys = [];
foreach ($this->connection->query("PRAGMA foreign_key_list({$this->delimite($table)})") as $row) {
foreach ($this->pdo->query("PRAGMA foreign_key_list({$this->delimite($table)})") as $row) {
$keys[] = [
'name' => $row['id'], // foreign key name
'local' => $row['from'], // local columns
Expand Down
Loading

0 comments on commit 1fa4fdd

Please sign in to comment.