Skip to content

Commit 5daf80e

Browse files
committed
Start of PostGre port
1 parent 0620b86 commit 5daf80e

File tree

4 files changed

+42
-30
lines changed

4 files changed

+42
-30
lines changed

src/PHPFUI/ORM.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,16 @@ public static function getValueArray(string $sql, array $input = []) : array
273273
/**
274274
* @return string primary key of the last record inserted
275275
*/
276-
public static function lastInsertId(string $name = '') : string
276+
public static function lastInsertId(string $name = '', string $table = '') : string
277277
{
278-
return self::getInstance()->lastInsertId($name);
278+
$pdo = self::getInstance();
279+
280+
if ($pdo->postGre && $table)
281+
{
282+
$name = $table . '_' . $name . '_seq';
283+
}
284+
285+
return $pdo->lastInsertId($name);
279286
}
280287

281288
/**

src/PHPFUI/ORM/PDOInstance.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
class PDOInstance extends \PDO
66
{
7+
public readonly bool $postGre;
8+
79
/** @var array<string> */
810
private array $lastError = [];
911

@@ -22,6 +24,7 @@ class PDOInstance extends \PDO
2224
*/
2325
public function __construct(private string $dsn, ?string $username = null, ?string $password = null, ?array $options = null)
2426
{
27+
$this->postGre = \str_starts_with($dsn, 'pgsql');
2528
parent::__construct($dsn, $username, $password, $options);
2629
}
2730

@@ -47,6 +50,11 @@ public function describeTable(string $table) : array
4750
{
4851
$rows = $this->getRows("describe `{$table}`;");
4952
}
53+
elseif ($this->postGre)
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;";
56+
$rows = $this->getRows($sql, [$table]);
57+
}
5058
else
5159
{
5260
$autoIncrement = (bool)$this->getValue("SELECT count(*) FROM sqlite_master where tbl_name='{$table}' and sql like '%autoincrement%'");
@@ -70,9 +78,6 @@ public function describeTable(string $table) : array
7078
*/
7179
public function execute(string $sql, array $input = []) : bool
7280
{
73-
$this->lastParameters = $input;
74-
$this->lastSql = $sql;
75-
7681
return null !== $this->run($sql, $input);
7782
}
7883

@@ -124,10 +129,7 @@ public function executeStatement(\PDOStatement $statement, array $input = []) :
124129
*/
125130
public function getArrayCursor(string $sql = 'select 0 limit 0', array $input = []) : \PHPFUI\ORM\ArrayCursor
126131
{
127-
$this->lastParameters = $input;
128-
$this->lastSql = $sql;
129-
130-
return new \PHPFUI\ORM\ArrayCursor($this->prepare($sql), $input);
132+
return new \PHPFUI\ORM\ArrayCursor($this->getPreparedStatement($sql, $input), $input);
131133
}
132134

133135
/**
@@ -137,10 +139,7 @@ public function getArrayCursor(string $sql = 'select 0 limit 0', array $input =
137139
*/
138140
public function getDataObjectCursor(string $sql = 'select 0 limit 0', array $input = []) : \PHPFUI\ORM\DataObjectCursor
139141
{
140-
$this->lastParameters = $input;
141-
$this->lastSql = $sql;
142-
143-
return new \PHPFUI\ORM\DataObjectCursor($this->prepare($sql), $input);
142+
return new \PHPFUI\ORM\DataObjectCursor($this->getPreparedStatement($sql, $input), $input);
144143
}
145144

146145
public function getDSN() : string
@@ -224,10 +223,7 @@ public function getLastSql() : string
224223
*/
225224
public function getRecordCursor(\PHPFUI\ORM\Record $crud, string $sql = 'select 0 limit 0', array $input = []) : \PHPFUI\ORM\RecordCursor
226225
{
227-
$this->lastParameters = $input;
228-
$this->lastSql = $sql;
229-
230-
return new \PHPFUI\ORM\RecordCursor($crud, $this->prepare($sql), $input);
226+
return new \PHPFUI\ORM\RecordCursor($crud, $this->getPreparedStatement($sql, $input), $input);
231227
}
232228

233229
/**
@@ -237,9 +233,6 @@ public function getRecordCursor(\PHPFUI\ORM\Record $crud, string $sql = 'select
237233
*/
238234
public function getRow(string $sql, array $input = []) : array
239235
{
240-
$this->lastParameters = $input;
241-
$this->lastSql = $sql;
242-
243236
$statement = $this->run($sql, $input);
244237

245238
if (null === $statement)
@@ -267,8 +260,6 @@ public function getRow(string $sql, array $input = []) : array
267260
*/
268261
public function getRows(string $sql, array $input = [], int $fetchType = \PDO::FETCH_ASSOC) : array
269262
{
270-
$this->lastParameters = $input;
271-
$this->lastSql = $sql;
272263
$statement = $this->run($sql, $input);
273264

274265
if (null === $statement)
@@ -288,6 +279,10 @@ public function getTables() : array
288279
{
289280
$rows = $this->getRows('show tables');
290281
}
282+
elseif ($this->postGre)
283+
{
284+
$rows = $this->getRows("SELECT table_name as name FROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'BASE TABLE';");
285+
}
291286
else
292287
{
293288
$rows = $this->getRows('SELECT name FROM sqlite_schema WHERE type="table" AND name NOT LIKE "sqlite_%"');
@@ -309,8 +304,6 @@ public function getTables() : array
309304
*/
310305
public function getValue(string $sql, array $input = []) : string
311306
{
312-
$this->lastParameters = $input;
313-
$this->lastSql = $sql;
314307
$statement = $this->run($sql, $input);
315308

316309
if (null === $statement)
@@ -334,8 +327,6 @@ public function getValue(string $sql, array $input = []) : string
334327
*/
335328
public function getValueArray(string $sql, array $input = []) : array
336329
{
337-
$this->lastParameters = $input;
338-
$this->lastSql = $sql;
339330
$statement = $this->run($sql, $input);
340331

341332
if (null === $statement)
@@ -369,13 +360,26 @@ public function reportErrors() : void
369360
}
370361
}
371362

363+
private function getPreparedStatement(string $sql, array $input) : ?\PDOStatement
364+
{
365+
$this->lastParameters = $input;
366+
367+
if ($this->postGre)
368+
{
369+
$sql = \str_replace('`', '"', $sql);
370+
}
371+
$this->lastSql = $sql;
372+
373+
return $this->prepare($sql);
374+
}
375+
372376
/**
373377
* Runs the query and sets and records errors
374378
*
375379
* @param array<mixed> $input
376380
*/
377-
private function run(string $sql, array $input = []) : ?\PDOStatement
381+
private function run(string $sql, array $input) : ?\PDOStatement
378382
{
379-
return $this->executeStatement($this->prepare($sql), $input);
383+
return $this->executeStatement($this->getPreparedStatement($sql, $input), $input);
380384
}
381385
}

src/PHPFUI/ORM/Record.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ private function privateInsert(bool $updateOnDuplicate, string $ignore = '') : i
907907

908908
if (static::$autoIncrement && $returnValue)
909909
{
910-
$returnValue = (int)\PHPFUI\ORM::lastInsertId(static::$primaryKeys[0]);
910+
$returnValue = (int)\PHPFUI\ORM::lastInsertId(static::$primaryKeys[0], $table);
911911

912912
if ($returnValue)
913913
{

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ protected function getTypeLength(string &$type) : float
5353
if (false !== $start)
5454
{
5555
$precision = \rtrim(\substr($type, $start + 1), ')');
56-
if (str_contains($precision, ','))
56+
57+
if (\str_contains($precision, ','))
5758
{
5859
$parts = \explode(',', $precision);
5960
$precision = ((int)$parts[0]) + 1;

0 commit comments

Comments
 (0)