5
5
use Illuminate \Support \Facades \DB ;
6
6
use Vcian \LaravelDBAuditor \Constants \Constant ;
7
7
use Vcian \LaravelDBAuditor \Traits \Audit ;
8
+ use Vcian \LaravelDBAuditor \Traits \DBConstraint ;
8
9
9
10
class DatabaseConstraintClass
10
11
{
11
12
use Audit;
12
13
protected string $ driver , $ database ;
13
14
14
- public function __construct (protected string $ table )
15
+ public function __construct (protected string $ table, protected string $ fields = '' )
15
16
{
16
17
$ this ->driver = connection_driver ();
17
18
$ this ->database = database_name ();
@@ -21,10 +22,20 @@ public function __invoke(): array
21
22
{
22
23
return match ($ this ->driver ) {
23
24
'sqlite ' => $ this ->sqlite (),
25
+ 'pgsql ' => $ this ->pgsql (),
24
26
default => $ this ->mysql (),
25
27
};
26
28
}
27
29
30
+ /**
31
+ * @param $query
32
+ * @return array
33
+ */
34
+ public function select ($ query ): array
35
+ {
36
+ return DB ::select ($ query );
37
+ }
38
+
28
39
/**
29
40
* Sqlite query.
30
41
*
@@ -62,12 +73,75 @@ public function mysql(): array
62
73
];
63
74
}
64
75
65
- /**
66
- * @param $query
67
- * @return array
68
- */
69
- public function select ($ query ): array
76
+
77
+ public function pgsql (): array
70
78
{
71
- return DB ::select ($ query );
79
+ return [
80
+ 'primary ' => $ this ->getPgsqlConstraintField ($ this ->table , Constant::CONSTRAINT_PRIMARY_KEY ),
81
+ 'unique ' => $ this ->getPgsqlConstraintField ($ this ->table , Constant::CONSTRAINT_UNIQUE_KEY ),
82
+ 'foreign ' => $ this ->getPgsqlConstraintField ($ this ->table , Constant::CONSTRAINT_FOREIGN_KEY ),
83
+ 'index ' => $ this ->getPgsqlConstraintField ($ this ->table , Constant::CONSTRAINT_INDEX_KEY ),
84
+ ];
85
+ }
86
+
87
+ public function getPgsqlConstraintField (string $ tableName , string $ input ): array
88
+ {
89
+ if ($ input === Constant::CONSTRAINT_FOREIGN_KEY ){
90
+ return collect (DB ::select ("SELECT
91
+ conname AS constraint_name,
92
+ a.attname AS column_name,
93
+ confrelid::regclass AS foreign_table,
94
+ af.attname AS foreign_column
95
+ FROM
96
+ pg_constraint AS c
97
+ JOIN
98
+ pg_class AS t ON c.conrelid = t.oid
99
+ JOIN
100
+ pg_attribute AS a ON a.attnum = ANY(c.conkey) AND a.attrelid = t.oid
101
+ JOIN
102
+ pg_attribute AS af ON af.attnum = ANY(c.confkey) AND af.attrelid = c.confrelid
103
+ WHERE
104
+ t.relname = ?
105
+ AND c.contype = 'f' " , [$ tableName ]))
106
+ ->flatten ()
107
+ ->toArray ();
108
+ }
109
+
110
+ if ($ input === Constant::CONSTRAINT_INDEX_KEY ) {
111
+ return collect (DB ::select ("SELECT
112
+ i.relname AS index_name,
113
+ a.attname AS column_name
114
+ FROM
115
+ pg_class AS t
116
+ JOIN
117
+ pg_index AS ix ON t.oid = ix.indrelid
118
+ JOIN
119
+ pg_class AS i ON i.oid = ix.indexrelid
120
+ JOIN
121
+ pg_attribute AS a ON a.attnum = ANY(ix.indkey) AND a.attrelid = t.oid
122
+ WHERE t.relname = ? " ,[$ tableName ]))
123
+ ->select ('column_name ' )
124
+ ->flatten ()
125
+ ->toArray ();
126
+ } else {
127
+ $ conType = Constant::CONSTRAINT_PRIMARY_KEY ? 'p ' : 'u ' ;
128
+ return collect (DB ::select ("
129
+ SELECT
130
+ conname AS constraint_name,
131
+ a.attname AS column_name
132
+ FROM
133
+ pg_constraint AS c
134
+ JOIN
135
+ pg_class AS t ON c.conrelid = t.oid
136
+ JOIN
137
+ pg_attribute AS a ON a.attnum = ANY(c.conkey) AND a.attrelid = t.oid
138
+ WHERE
139
+ t.relname = ?
140
+ AND c.contype = ? " ,[$ tableName ,$ conType ]))
141
+ ->select ('column_name ' )
142
+ ->flatten ()
143
+ ->toArray ();
144
+ }
145
+
72
146
}
73
147
}
0 commit comments