Skip to content

Commit 1d05338

Browse files
committed
More PostGre port
1 parent 5daf80e commit 1d05338

File tree

6 files changed

+154
-37
lines changed

6 files changed

+154
-37
lines changed

src/PHPFUI/ORM/PDOInstance.php

Lines changed: 112 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,57 @@ public function describeTable(string $table) : array
5252
}
5353
elseif ($this->postGre)
5454
{
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+
5660
$rows = $this->getRows($sql, [$table]);
61+
62+
foreach ($rows as $index => $row)
63+
{
64+
if ($row['Type'] == 'character varying' && $row['character_maximum_length'] != null)
65+
{
66+
$row['Type'] = 'varchar(' . $row['character_maximum_length'] . ')';
67+
}
68+
else if ($row['Type'] == 'text')
69+
{
70+
$row['Type'] = 'longtext';
71+
}
72+
$row['Extra'] = $row['Default'] ? \str_replace('nextval', 'auto_increment', $row['Default']) : '';
73+
$rows[$index] = $row;
74+
}
5775
}
5876
else
5977
{
6078
$autoIncrement = (bool)$this->getValue("SELECT count(*) FROM sqlite_master where tbl_name='{$table}' and sql like '%autoincrement%'");
6179
$rows = $this->getRows("pragma table_info('{$table}')");
6280
}
6381

82+
$fields = [];
6483
foreach ($rows as $row)
6584
{
66-
$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 ($rows as $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+
}
67106
}
68107

69108
return $fields;
@@ -156,16 +195,80 @@ public function getIndexes(string $table) : array
156195

157196
if (\str_starts_with($this->dsn, 'mysql'))
158197
{
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 ($rows as $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]);
233+
234+
foreach ($rows as $row)
235+
{
236+
$name = \substr($row['indexname'], \strlen($table) + 1);
237+
238+
if (! isset($fields[$name]))
239+
{
240+
$index = new \PHPFUI\ORM\Schema\Index();
241+
$index->primaryKey = false;
242+
$index->name = $name;
243+
$index->extra = $row['indexdef'];
244+
$fields[$name] = $index;
245+
}
246+
}
247+
248+
return $fields;
160249
}
161250
else
162251
{
163-
$rows = $this->getRows("SELECT * FROM sqlite_master WHERE type = 'index' and tbl_name='{$table}'");
252+
$rows = $this->getRows("SELECT * FROM sqlite_master WHERE type = 'index' and tbl_name=?", [$table]);
164253
}
165254

166255
foreach ($rows as $row)
167256
{
168-
$fields[] = new \PHPFUI\ORM\Schema\Index($this, $row);
257+
$index = new \PHPFUI\ORM\Schema\Index();
258+
259+
if (\str_starts_with($this->getDSN(), 'mysql'))
260+
{
261+
$index->primaryKey = 'PRIMARY' == $row['Key_name'];
262+
$index->name = $row['Column_name'];
263+
$index->extra = \implode(',', $row);
264+
}
265+
else
266+
{
267+
$index->name = $row['name'];
268+
$index->extra = $row['sql'] ?? '';
269+
$index->primaryKey = false;
270+
}
271+
$fields[$index->name] = $index;
169272
}
170273

171274
return $fields;
@@ -360,7 +463,10 @@ public function reportErrors() : void
360463
}
361464
}
362465

363-
private function getPreparedStatement(string $sql, array $input) : ?\PDOStatement
466+
/**
467+
* @param array<mixed> $input
468+
*/
469+
private function getPreparedStatement(string $sql, array $input) : \PDOStatement
364470
{
365471
$this->lastParameters = $input;
366472

src/PHPFUI/ORM/RelatedRecord.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function setValue(mixed $value, array $parameters) : void
3131
$class = \array_shift($parameters);
3232
$field = \array_shift($parameters);
3333

34-
if (! ($value instanceof $class))
34+
if (\get_debug_type($value) != $class)
3535
{
3636
throw new \PHPFUI\ORM\Exception(__METHOD__ . ': Error - ' . \get_debug_type($value) . ' is not an instance of ' . $class);
3737
}

src/PHPFUI/ORM/Schema/Field.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44

55
class Field
66
{
7-
public readonly bool $autoIncrement;
7+
public bool $autoIncrement;
88

9-
public readonly ?string $defaultValue;
9+
public ?string $defaultValue;
1010

1111
public readonly string $extra;
1212

1313
public readonly string $name;
1414

1515
public readonly bool $nullable;
1616

17-
public readonly bool $primaryKey;
17+
public bool $primaryKey;
1818

1919
public readonly string $type;
2020

@@ -23,7 +23,7 @@ class Field
2323
*/
2424
public function __construct(\PHPFUI\ORM\PDOInstance $pdo, array $fields, bool $autoIncrement)
2525
{
26-
if (\str_starts_with($pdo->getDSN(), 'mysql'))
26+
if (\str_starts_with($pdo->getDSN(), 'mysql') || $pdo->postGre)
2727
{
2828
$this->name = $fields['Field'];
2929
$this->type = \strtolower($fields['Type']);

src/PHPFUI/ORM/Schema/Index.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@
44

55
class Index
66
{
7-
public readonly string $extra;
7+
public string $extra;
88

9-
public readonly string $name;
9+
public string $name;
1010

11-
public readonly bool $primaryKey;
11+
public bool $primaryKey;
1212

13-
/**
14-
* @param array<string,mixed> $fields
15-
*/
16-
public function __construct(\PHPFUI\ORM\PDOInstance $pdo, array $fields)
17-
{
18-
if (\str_starts_with($pdo->getDSN(), 'mysql'))
19-
{
20-
$this->primaryKey = 'PRIMARY' == $fields['Key_name'];
21-
$this->name = $fields['Column_name'];
22-
$this->extra = \implode(',', $fields);
23-
}
24-
else
25-
{
26-
$this->name = $fields['name'];
27-
$this->extra = $fields['sql'] ?? '';
28-
$this->primaryKey = false;
29-
}
30-
}
13+
// /**
14+
// * @param array<string,mixed> $fields
15+
// */
16+
// public function __construct(\PHPFUI\ORM\PDOInstance $pdo, array $fields, bool $primaryKey = false)
17+
// {
18+
// if (\str_starts_with($pdo->getDSN(), 'mysql'))
19+
// {
20+
// $this->primaryKey = 'PRIMARY' == $fields['Key_name'];
21+
// $this->name = $fields['Column_name'];
22+
// $this->extra = \implode(',', $fields);
23+
// }
24+
// else
25+
// {
26+
// $this->name = $fields['name'];
27+
// $this->extra = $fields['sql'] ?? '';
28+
// $this->primaryKey = $primaryKey;
29+
// }
30+
// }
3131
}

src/PHPFUI/ORM/Tool/Generate/Base.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,19 @@ protected function getTypeLength(string &$type) : float
105105
'tinyint' => 'int',
106106
'mediumint' => 'int',
107107
'bigint' => 'int',
108+
'smallserial' => 'int',
109+
'serial' => 'int',
110+
'bigserial' => 'int',
108111
'decimal' => 'float',
109112
'numeric' => 'float',
110113
'float' => 'float',
111114
'double' => 'float',
115+
'real' => 'float',
116+
'double precision' => 'float',
117+
'money' => 'float',
112118
'bit' => 'bool',
119+
'boolean' => 'bool',
113120
'year' => 'int',
114-
'timestamp' => 'string',
115-
'datetime' => 'string',
116121
];
117122
$type = $types[$type] ?? 'string';
118123

src/PHPFUI/ORM/Tool/Generate/CRUD.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ abstract class ~~CLASS~~ extends \PHPFUI\ORM\Record
3939
4040
public function initFieldDefinitions() : static
4141
{
42-
if (! count(static::$fields))
42+
if (! \count(static::$fields))
4343
{
4444
static::$fields = [
45-
~~FIELD_ARRAY~~ ];
45+
~~FIELD_ARRAY~~ ];
4646
}
4747
4848
return $this;
@@ -157,7 +157,7 @@ class ~~CLASS~~ extends \PHPFUI\ORM\Table
157157

158158
protected function getFieldDefinition(\PHPFUI\ORM\Schema\Field $field) : string
159159
{
160-
$retVal = "\t\t\t" . $this->quote($field->name) . ' => new \PHPFUI\ORM\FieldDefinition(';
160+
$retVal = "\t\t\t\t" . $this->quote($field->name) . ' => new \PHPFUI\ORM\FieldDefinition(';
161161
$retVal .= $this->quoteLine(\str_replace("'", '"', $field->type));
162162
$type = $field->type;
163163
$length = $this->getTypeLength($type);
@@ -166,6 +166,12 @@ protected function getFieldDefinition(\PHPFUI\ORM\Schema\Field $field) : string
166166
$allowNulls = $field->nullable;
167167
$defaultValue = null;
168168

169+
$doubleColon = $field->defaultValue ? strpos($field->defaultValue, '::') : null;
170+
if ($doubleColon)
171+
{
172+
$field->defaultValue = substr($field->defaultValue, 0, $doubleColon);
173+
}
174+
169175
switch ($type)
170176
{
171177
case 'int':

0 commit comments

Comments
 (0)