19 $this->operations[
DbOperations::AUTO_INC]=
'CREATE SEQUENCE {seqName};ALTER TABLE {tableName} ALTER COLUMN {fieldName} SET DEFAULT nextval({seqName});';
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 );
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 );
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'];
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\",
68 WHEN f.attnotnull=true THEN 'YES'
69 WHEN f.attnotnull=false THEN 'NO'
73 WHEN p.contype = 'u' THEN 'MUL'
74 WHEN p.contype = 'p' THEN 'PRI'
78 WHEN f.atthasdef = 't' THEN pg_get_expr(adbin, adrelid)
80 CASE WHEN pg_get_expr(adbin, adrelid) LIKE 'nextval(%' THEN 'auto_increment' ELSE '' END AS \"Extra\"
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
91 WHERE c.relkind = 'r'::char
92 AND n.nspname = 'public'
93 and c.relname='{$tableName}'
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"] ];
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 );
106 return $query->fetchColumn ( 0 );
111 public function groupConcat(
string $fields,
string $separator): string {
112 return
"array_to_string(array_agg({$fields}), '{$separator}')";