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 |
10
|
|
|
* @version 1.0.0 |
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 k1.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 ( |
57
|
|
|
"SELECT |
58
|
|
|
f.attname AS \"Field\", |
59
|
|
|
pg_catalog.format_type(f.atttypid,f.atttypmod) AS \"Type\", |
60
|
|
|
CASE |
61
|
|
|
WHEN f.attnotnull=true THEN 'YES' |
62
|
|
|
WHEN f.attnotnull=false THEN 'NO' |
63
|
|
|
ELSE '' |
64
|
|
|
END AS \"Null\", |
65
|
|
|
CASE |
66
|
|
|
WHEN p.contype = 'u' THEN 'MUL' |
67
|
|
|
WHEN p.contype = 'p' THEN 'PRI' |
68
|
|
|
ELSE '' |
69
|
|
|
END AS \"Key\", |
70
|
|
|
CASE |
71
|
|
|
WHEN f.atthasdef = 't' THEN pg_get_expr(adbin, adrelid) |
72
|
|
|
END AS \"Default\" , |
73
|
|
|
CASE WHEN pg_get_expr(adbin, adrelid) LIKE 'nextval(%' THEN 'auto_increment' ELSE '' END AS \"Extra\" |
74
|
|
|
FROM pg_attribute f |
75
|
|
|
JOIN pg_class c ON c.oid = f.attrelid |
76
|
|
|
JOIN pg_type t ON t.oid = f.atttypid |
77
|
|
|
LEFT JOIN pg_attrdef d ON d.adrelid = c.oid AND d.adnum = f.attnum |
78
|
|
|
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace |
79
|
|
|
LEFT JOIN pg_constraint p ON p.conrelid = c.oid AND f.attnum = ANY (p.conkey) |
80
|
|
|
LEFT JOIN pg_class AS g ON p.confrelid = g.oid |
81
|
|
|
LEFT JOIN pg_index AS ix ON f.attnum = ANY(ix.indkey) and c.oid = f.attrelid and c.oid = ix.indrelid |
82
|
|
|
LEFT JOIN pg_class AS i ON ix.indexrelid = i.oid |
83
|
|
|
|
84
|
|
|
WHERE c.relkind = 'r'::char |
85
|
|
|
AND n.nspname = 'public' |
86
|
|
|
and c.relname='{$tableName}' |
87
|
|
|
AND f.attnum > 0 |
88
|
|
|
ORDER BY f.attnum;"); |
89
|
|
|
$fields = $recordset->fetchAll ( \PDO::FETCH_ASSOC ); |
90
|
|
|
foreach ( $fields as $field ) { |
91
|
|
|
$fieldsInfos [$field ['Field']] = [ "Type" => $field ['Type'],"Nullable" => $field ["Null"] ]; |
92
|
|
|
} |
93
|
|
|
return $fieldsInfos; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|