Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
PgsqlDriverMetas.php
Go to the documentation of this file.
1<?php
2
4
6
16
17 public function __construct($dbInstance) {
18 parent::__construct($dbInstance);
19 $this->operations[DbOperations::AUTO_INC]='CREATE SEQUENCE {seqName};ALTER TABLE {tableName} ALTER COLUMN {fieldName} SET DEFAULT nextval({seqName});';
20 $this->operations[DbOperations::MODIFY_FIELD]='ALTER TABLE {tableName} ALTER COLUMN {fieldName} TYPE {attributes}';
21 }
22
23 public function getForeignKeys($tableName, $pkName, $dbName = null): array {
24 $recordset = $this->dbInstance->query ( 'SELECT k1.constraint_catalog as "CONSTRAINT_CATALOG", k1.constraint_schema as "CONSTRAINT_SCHEMA",
25 k1.constraint_name as "CONSTRAINT_NAME",
26 k1.table_catalog as "TABLE_CATALOG",
27 k1.table_schema as "TABLE_SCHEMA",
28 k1.table_name as "TABLE_NAME",
29 k1.column_name as "COLUMN_NAME",
30 k1.ordinal_position as "ORDINAL_POSITION" ,
31 k1.position_in_unique_constraint as "POSITION_IN_UNIQUE_CONSTRAINT",
32 k2.table_schema AS "REFERENCED_TABLE_SCHEMA",
33 k2.table_name AS "REFERENCED_TABLE_NAME",
34 k2.column_name AS "REFERENCED_COLUMN_NAME"
35 FROM information_schema.key_column_usage k1
36 JOIN information_schema.referential_constraints fk USING (constraint_schema, constraint_name)
37 JOIN information_schema.key_column_usage k2
38 ON k2.constraint_schema = fk.unique_constraint_schema
39 AND k2.constraint_name = fk.unique_constraint_name
40 AND k2.ordinal_position = k1.position_in_unique_constraint
41 WHERE k1.table_schema = \'public\'
42 and k2.column_name=\'' . $pkName . '\'
43 AND k2.table_name = \'' . $tableName . '\';' );
44 return $recordset->fetchAll ( \PDO::FETCH_ASSOC );
45 }
46
47 public function getTablesName(): array {
48 $query = $this->dbInstance->query ( 'SELECT tablename as schemaname FROM pg_catalog.pg_tables WHERE schemaname != \'pg_catalog\' AND schemaname != \'information_schema\';' );
49 return $query->fetchAll ( \PDO::FETCH_COLUMN );
50 }
51
52 public function getPrimaryKeys($tableName): array {
53 $fieldkeys = array ();
54 $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;" );
55 $keys = $recordset->fetchAll ( \PDO::FETCH_ASSOC );
56 foreach ( $keys as $key ) {
57 $fieldkeys [] = $key ['attname'];
58 }
59 return $fieldkeys;
60 }
61
62 public function getFieldsInfos($tableName): array {
63 $fieldsInfos = array ();
64 $recordset = $this->dbInstance->query ( "SELECT
65 f.attname AS \"Field\",
66 pg_catalog.format_type(f.atttypid,f.atttypmod) AS \"Type\",
67 CASE
68 WHEN f.attnotnull=true THEN 'YES'
69 WHEN f.attnotnull=false THEN 'NO'
70 ELSE ''
71 END AS \"Null\",
72 CASE
73 WHEN p.contype = 'u' THEN 'MUL'
74 WHEN p.contype = 'p' THEN 'PRI'
75 ELSE ''
76 END AS \"Key\",
77 CASE
78 WHEN f.atthasdef = 't' THEN pg_get_expr(adbin, adrelid)
79 END AS \"Default\" ,
80 CASE WHEN pg_get_expr(adbin, adrelid) LIKE 'nextval(%' THEN 'auto_increment' ELSE '' END AS \"Extra\"
81 FROM pg_attribute f
82 JOIN pg_class c ON c.oid = f.attrelid
83 JOIN pg_type t ON t.oid = f.atttypid
84 LEFT JOIN pg_attrdef d ON d.adrelid = c.oid AND d.adnum = f.attnum
85 LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
86 LEFT JOIN pg_constraint p ON p.conrelid = c.oid AND f.attnum = ANY (p.conkey)
87 LEFT JOIN pg_class AS g ON p.confrelid = g.oid
88 LEFT JOIN pg_index AS ix ON f.attnum = ANY(ix.indkey) and c.oid = f.attrelid and c.oid = ix.indrelid
89 LEFT JOIN pg_class AS i ON ix.indexrelid = i.oid
90
91 WHERE c.relkind = 'r'::char
92 AND n.nspname = 'public'
93 and c.relname='{$tableName}'
94 AND f.attnum > 0
95 ORDER BY f.attnum;" );
96 $fields = $recordset->fetchAll ( \PDO::FETCH_ASSOC );
97 foreach ( $fields as $field ) {
98 $fieldsInfos [$field ['Field']] = [ "Type" => $field ['Type'],"Nullable" => $field ["Null"] ];
99 }
100 return $fieldsInfos;
101 }
102
103 public function getRowNum(string $tableName, string $pkName, string $condition): int {
104 $query = $this->dbInstance->query ( "SELECT num FROM (SELECT *,row_number() OVER (ORDER BY {$pkName}) AS num FROM \"{$tableName}\") x where " . $condition );
105 if ($query) {
106 return $query->fetchColumn ( 0 );
107 }
108 return 0;
109 }
110
111 public function groupConcat(string $fields, string $separator): string {
112 return "array_to_string(array_agg({$fields}), '{$separator}')";
113 }
114
115 public function toStringOperator() {
116 return '::TEXT ';
117 }
118}
Ubiquity\db\providers$DriverMetaDatas This class is part of Ubiquity.
Ubiquity\db\providers\pdo\drivers$PgsqlDriverMetas This class is part of Ubiquity.
getForeignKeys($tableName, $pkName, $dbName=null)
groupConcat(string $fields, string $separator)
Returns the SQL callback for fields concatenation.
getRowNum(string $tableName, string $pkName, string $condition)
Returns the line number of a data record.
getTablesName()
Returns all table names in the database.