You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/PHPFUI/ORM/PDOInstance.php
+112-6Lines changed: 112 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -52,18 +52,57 @@ public function describeTable(string $table) : array
52
52
}
53
53
elseif ($this->postGre)
54
54
{
55
-
$sql = "SELECT column_name as name,data_type as type,character_maximum_length,is_nullable as notnull,column_default as dflt_value FROM information_schema.columns WHERE table_schema = 'public' AND table_name = ? ORDER BY ordinal_position;";
55
+
$sql = 'SELECT column_name as "Field",data_type as "Type",character_maximum_length,is_nullable as "Null",column_default as "Default"
56
+
FROM information_schema.columns
57
+
WHERE table_schema = \'public\' AND table_name = ?
58
+
ORDER BY ordinal_position;';
59
+
56
60
$rows = $this->getRows($sql, [$table]);
61
+
62
+
foreach ($rowsas$index => $row)
63
+
{
64
+
if ($row['Type'] == 'character varying' && $row['character_maximum_length'] != null)
$fields[] = new \PHPFUI\ORM\Schema\Field($this, $row, $autoIncrement);
85
+
$field = new \PHPFUI\ORM\Schema\Field($this, $row, $autoIncrement);
86
+
$fields[$field->name] = $field;
87
+
}
88
+
89
+
if ($this->postGre)
90
+
{
91
+
// get non auto increment primary keys
92
+
$rows = $this->getRows("SELECT kcu.column_name as name, tc.constraint_type as sql FROM information_schema.table_constraints AS tc
93
+
JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name AND tc.table_schema = kcu.table_schema
94
+
WHERE tc.constraint_type = 'PRIMARY KEY' AND tc.table_name = ?", [$table]);
95
+
96
+
foreach ($rowsas$row)
97
+
{
98
+
$fields[$row['name']]->primaryKey = true;
99
+
}
100
+
$row = $this->getRow("SELECT column_name as name,column_default as sql FROM information_schema.columns WHERE table_name = ? AND column_default LIKE 'nextval(%'", [$table]);
101
+
102
+
if (\count($row))
103
+
{
104
+
$fields[$row['name']]->autoIncrement = true;
105
+
}
67
106
}
68
107
69
108
return$fields;
@@ -156,16 +195,80 @@ public function getIndexes(string $table) : array
156
195
157
196
if (\str_starts_with($this->dsn, 'mysql'))
158
197
{
159
-
$rows = $this->getRows('SHOW INDEXES FROM ' . $table);
198
+
$rows = $this->getRows('SHOW INDEXES FROM ?', [$table]);
199
+
}
200
+
elseif ($this->postGre)
201
+
{
202
+
// get auto increment primary keys
203
+
$fields = $this->getRow("SELECT column_name as name,column_default as sql FROM information_schema.columns WHERE table_name = ? AND column_default LIKE 'nextval(%'", [$table]);
204
+
205
+
if (\count($fields))
206
+
{
207
+
$index = new \PHPFUI\ORM\Schema\Index();
208
+
$index->primaryKey = true;
209
+
$index->name = $fields['name'];
210
+
$index->extra = $fields['sql'];
211
+
$fields[$index->name] = $index;
212
+
}
213
+
214
+
// get non auto increment primary keys
215
+
$rows = $this->getRows("SELECT kcu.column_name as name, tc.constraint_type as sql FROM information_schema.table_constraints AS tc
216
+
JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name AND tc.table_schema = kcu.table_schema
217
+
WHERE tc.constraint_type = 'PRIMARY KEY' AND tc.table_name = ?", [$table]);
218
+
219
+
foreach ($rowsas$row)
220
+
{
221
+
$index = new \PHPFUI\ORM\Schema\Index();
222
+
$index->primaryKey = true;
223
+
$index->name = $fields['name'];
224
+
$index->extra = $fields['sql'];
225
+
226
+
if (! isset($fields[$index->name]))
227
+
{
228
+
$fields[$index->name] = $index;
229
+
}
230
+
}
231
+
// get the rest of the index fields
232
+
$rows = $this->getRows('SELECT * FROM pg_indexes WHERE tablename = ?', [$table]);
0 commit comments