1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\db\providers\pdo\drivers; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Ubiquity\db\providers\pdo\drivers$PgsqlDriverMetas |
7
|
|
|
* This class is part of Ubiquity |
8
|
|
|
* |
9
|
|
|
* @author Ulaş SAYGIN |
10
|
|
|
* @version 1.0.1 |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
class PgsqlDriverMetas extends AbstractDriverMetaDatas { |
14
|
|
|
|
15
|
|
|
public function getForeignKeys($tableName, $pkName, $dbName = null): array { |
16
|
|
|
$recordset = $this->dbInstance->query ( 'SELECT k1.constraint_catalog as "CONSTRAINT_CATALOG", k1.constraint_schema as "CONSTRAINT_SCHEMA", |
17
|
|
|
k1.constraint_name as "CONSTRAINT_NAME", |
18
|
|
|
k1.table_catalog as "TABLE_CATALOG", |
19
|
|
|
k1.table_schema as "TABLE_SCHEMA", |
20
|
|
|
k1.table_name as "TABLE_NAME", |
21
|
|
|
k1.column_name as "COLUMN_NAME", |
22
|
|
|
k1.ordinal_position as "ORDINAL_POSITION" , |
23
|
|
|
k1.position_in_unique_constraint as "POSITION_IN_UNIQUE_CONSTRAINT", |
24
|
|
|
k2.table_schema AS "REFERENCED_TABLE_SCHEMA", |
25
|
|
|
k2.table_name AS "REFERENCED_TABLE_NAME", |
26
|
|
|
k2.column_name AS "REFERENCED_COLUMN_NAME" |
27
|
|
|
FROM information_schema.key_column_usage k1 |
28
|
|
|
JOIN information_schema.referential_constraints fk USING (constraint_schema, constraint_name) |
29
|
|
|
JOIN information_schema.key_column_usage k2 |
30
|
|
|
ON k2.constraint_schema = fk.unique_constraint_schema |
31
|
|
|
AND k2.constraint_name = fk.unique_constraint_name |
32
|
|
|
AND k2.ordinal_position = k1.position_in_unique_constraint |
33
|
|
|
WHERE k1.table_schema = \'public\' |
34
|
|
|
and k2.column_name=\'' . $pkName . '\' |
35
|
|
|
AND k2.table_name = \'' . $tableName . '\';' ); |
36
|
|
|
return $recordset->fetchAll ( \PDO::FETCH_ASSOC ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getTablesName(): array { |
40
|
|
|
$query = $this->dbInstance->query ( 'SELECT tablename as schemaname FROM pg_catalog.pg_tables WHERE schemaname != \'pg_catalog\' AND schemaname != \'information_schema\';' ); |
41
|
|
|
return $query->fetchAll ( \PDO::FETCH_COLUMN ); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getPrimaryKeys($tableName): array { |
45
|
|
|
$fieldkeys = array (); |
46
|
|
|
$recordset = $this->dbInstance->query ( "SELECT a.attname, format_type(a.atttypid, a.atttypmod) AS data_type FROM pg_index i JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey) WHERE i.indrelid = '{$tableName}'::regclass AND i.indisprimary;" ); |
47
|
|
|
$keys = $recordset->fetchAll ( \PDO::FETCH_ASSOC ); |
48
|
|
|
foreach ( $keys as $key ) { |
49
|
|
|
$fieldkeys [] = $key ['attname']; |
50
|
|
|
} |
51
|
|
|
return $fieldkeys; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getFieldsInfos($tableName): array { |
55
|
|
|
$fieldsInfos = array (); |
56
|
|
|
$recordset = $this->dbInstance->query ( "SELECT |
57
|
|
|
f.attname AS \"Field\", |
58
|
|
|
pg_catalog.format_type(f.atttypid,f.atttypmod) AS \"Type\", |
59
|
|
|
CASE |
60
|
|
|
WHEN f.attnotnull=true THEN 'YES' |
61
|
|
|
WHEN f.attnotnull=false THEN 'NO' |
62
|
|
|
ELSE '' |
63
|
|
|
END AS \"Null\", |
64
|
|
|
CASE |
65
|
|
|
WHEN p.contype = 'u' THEN 'MUL' |
66
|
|
|
WHEN p.contype = 'p' THEN 'PRI' |
67
|
|
|
ELSE '' |
68
|
|
|
END AS \"Key\", |
69
|
|
|
CASE |
70
|
|
|
WHEN f.atthasdef = 't' THEN pg_get_expr(adbin, adrelid) |
71
|
|
|
END AS \"Default\" , |
72
|
|
|
CASE WHEN pg_get_expr(adbin, adrelid) LIKE 'nextval(%' THEN 'auto_increment' ELSE '' END AS \"Extra\" |
73
|
|
|
FROM pg_attribute f |
74
|
|
|
JOIN pg_class c ON c.oid = f.attrelid |
75
|
|
|
JOIN pg_type t ON t.oid = f.atttypid |
76
|
|
|
LEFT JOIN pg_attrdef d ON d.adrelid = c.oid AND d.adnum = f.attnum |
77
|
|
|
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace |
78
|
|
|
LEFT JOIN pg_constraint p ON p.conrelid = c.oid AND f.attnum = ANY (p.conkey) |
79
|
|
|
LEFT JOIN pg_class AS g ON p.confrelid = g.oid |
80
|
|
|
LEFT JOIN pg_index AS ix ON f.attnum = ANY(ix.indkey) and c.oid = f.attrelid and c.oid = ix.indrelid |
81
|
|
|
LEFT JOIN pg_class AS i ON ix.indexrelid = i.oid |
82
|
|
|
|
83
|
|
|
WHERE c.relkind = 'r'::char |
84
|
|
|
AND n.nspname = 'public' |
85
|
|
|
and c.relname='{$tableName}' |
86
|
|
|
AND f.attnum > 0 |
87
|
|
|
ORDER BY f.attnum;" ); |
88
|
|
|
$fields = $recordset->fetchAll ( \PDO::FETCH_ASSOC ); |
89
|
|
|
foreach ( $fields as $field ) { |
90
|
|
|
$fieldsInfos [$field ['Field']] = [ "Type" => $field ['Type'],"Nullable" => $field ["Null"] ]; |
91
|
|
|
} |
92
|
|
|
return $fieldsInfos; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getRowNum(string $tableName, string $pkName, string $condition): int { |
96
|
|
|
$query = $this->dbInstance->query ( "SELECT num FROM (SELECT *,row_number() OVER (ORDER BY {$pkName}) AS num FROM \"{$tableName}\") x where " . $condition ); |
97
|
|
|
if ($query) { |
98
|
|
|
return $query->fetchColumn ( 0 ); |
99
|
|
|
} |
100
|
|
|
return 0; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function groupConcat(string $fields, string $separator): string { |
104
|
|
|
return "array_to_string(array_agg({$fields}), '{$separator}')"; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function toStringOperator() { |
108
|
|
|
return '::TEXT '; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|