Skip to content

Commit dc09178

Browse files
committed
Add PostgreSQL handler
Add new handler and interfaces
1 parent d85cb54 commit dc09178

File tree

14 files changed

+938
-89
lines changed

14 files changed

+938
-89
lines changed

AbstractDB.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ abstract class AbstractDB implements DBInterface
3333
protected $fkData;
3434
protected $joinedTables;
3535

36+
protected string $connKey = "default";
3637

3738
/**
3839
* Build SELECT sql code (The method will be auto called in method build)
@@ -84,13 +85,21 @@ abstract protected function dropView(): self;
8485
*/
8586
abstract protected function showView(): self;
8687

88+
public function setConnKey(?string $key) {
89+
$this->connKey = is_null($key) ? "default" : $key;
90+
}
91+
92+
public function connInst() {
93+
return Connect::getInstance($this->connKey);
94+
}
95+
8796
/**
8897
* Access Mysql DB connection
8998
* @return \mysqli
9099
*/
91100
public function connect()
92101
{
93-
return Connect::getInstance()->DB();
102+
return $this->connInst()->DB();
94103
}
95104

96105
/**
@@ -100,7 +109,7 @@ public function connect()
100109
public function getTable(bool $withAlias = false): string
101110
{
102111
$alias = ($withAlias && !is_null($this->alias)) ? " {$this->alias}" : "";
103-
return Connect::getInstance()->getHandler()->getPrefix() . $this->table . $alias;
112+
return $this->connInst()->getHandler()->getPrefix() . $this->table . $alias;
104113
}
105114

106115
/**
@@ -361,7 +370,7 @@ final protected function extractCamelCase(string $value): array
361370
final protected function buildJoinFromMig(MigrateInterface $mig, string $type): array
362371
{
363372
$joinArr = array();
364-
$prefix = Connect::getInstance()->getHandler()->getPrefix();
373+
$prefix = $this->connInst()->getHandler()->getPrefix();
365374
$main = $this->getMainFKData();
366375
$data = $mig->getData();
367376
$this->mig->mergeData($data);
@@ -391,7 +400,6 @@ final protected function buildJoinFromMig(MigrateInterface $mig, string $type):
391400
return $joinArr;
392401
}
393402

394-
395403
/**
396404
* Build on YB to col sql string part
397405
* @return string|null
@@ -416,7 +424,7 @@ protected function getAllQueryTables(): ?string
416424
*/
417425
final protected function query(string|self $sql, ?string $method = null, array $args = []): array|object|bool|string
418426
{
419-
$query = new Query($sql);
427+
$query = new Query($sql, $this->connInst());
420428
$query->setPluck($this->pluck);
421429
if (!is_null($method)) {
422430
if (method_exists($query, $method)) {

Connect.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33

44
namespace MaplePHP\Query;
55

6+
use InvalidArgumentException;
67
use MaplePHP\Query\Exceptions\ConnectException;
78
use MaplePHP\Query\Interfaces\AttrInterface;
89
use MaplePHP\Query\Interfaces\MigrateInterface;
910
use MaplePHP\Query\Utility\Attr;
1011
use mysqli;
1112

1213
/**
13-
* @method static select(string $string, string|array|MigrateInterface $array)
14+
* @method static select(string $columns, string|array|MigrateInterface $table)
1415
* @method static table(string $string)
1516
*/
1617
class Connect
1718
{
1819
private $handler;
1920
private static array $inst;
21+
public static string $current = "default";
2022
private $db;
2123

2224
private function __construct($handler)
@@ -32,14 +34,15 @@ private function __clone() {
3234
}
3335

3436
/**
35-
* Access query builder intance
37+
* Access query builder instance
3638
* @param string $name
3739
* @param array $arguments
3840
* @return mixed
3941
*/
4042
public static function __callStatic(string $name, array $arguments)
4143
{
4244
$inst = new DB();
45+
$inst->setConnKey(self::$current);
4346
return $inst::$name(...$arguments);
4447
}
4548

@@ -52,12 +55,32 @@ public static function __callStatic(string $name, array $arguments)
5255
public static function setHandler($handler, ?string $key = null): self
5356
{
5457
$key = self::getKey($key);
55-
if(!self::hasInstance($key)) {
56-
self::$inst[$key] = new self($handler);
58+
if(self::hasInstance($key)) {
59+
throw new InvalidArgumentException("A handler is already connected with key \"$key\"!");
5760
}
61+
self::$inst[$key] = new self($handler);
5862
return self::$inst[$key];
5963
}
6064

65+
/**
66+
* Remove a handler
67+
* @param string $key
68+
* @return void
69+
*/
70+
public static function removeHandler(string $key): void
71+
{
72+
if($key === "default") {
73+
throw new InvalidArgumentException("You can not remove the default handler!");
74+
}
75+
if(!self::hasInstance($key)) {
76+
throw new InvalidArgumentException("The handler with with key \"$key\" does not exist!");
77+
}
78+
unset(self::$inst[$key]);
79+
if(self::$current === $key) {
80+
self::$current = "default";
81+
}
82+
}
83+
6184
/**
6285
* Get default instance or secondary instances with key
6386
* @param string|null $key
@@ -68,9 +91,9 @@ public static function getInstance(?string $key = null): self
6891
{
6992
$key = self::getKey($key);
7093
if(!self::hasInstance($key)) {
71-
throw new ConnectException("Connect Error: No Connection Found");
94+
throw new ConnectException("Connection Error: No active connection or connection instance found.");
7295
}
73-
96+
self::$current = $key;
7497
return self::$inst[$key];
7598
}
7699

DB.php

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class DB extends AbstractDB
3535
private $viewName;
3636
private $sql;
3737
private $dynamic;
38+
private ?string $returning = null;
3839
protected ?string $pluck = null;
3940

4041
/**
@@ -52,7 +53,9 @@ public static function __callStatic(string $method, array $args)
5253
$table = array_pop($args);
5354
$inst = self::table($table);
5455
$inst->method = $method;
55-
$prefix = Connect::getInstance()->getHandler()->getPrefix();
56+
$inst->setConnKey(Connect::$current);
57+
$prefix = Connect::getInstance(Connect::$current)->getHandler()->getPrefix();
58+
5659
switch ($inst->method) {
5760
case 'select':
5861
case 'selectView':
@@ -77,9 +80,11 @@ public static function __callStatic(string $method, array $args)
7780
$inst->dynamic = [[$inst, $inst->method], $args];
7881
break;
7982
}
83+
8084
} else {
8185
$inst = new self();
8286
}
87+
8388
return $inst;
8489
}
8590

@@ -151,6 +156,8 @@ public static function table(string|array|MigrateInterface $data): self
151156
$inst->alias = $data['alias'];
152157
$inst->table = $inst->getAttr($data['table'])->enclose(false);
153158
$inst->mig = $mig;
159+
$inst->setConnKey(Connect::$current);
160+
154161
if (is_null($inst->alias)) {
155162
$inst->alias = $inst->table;
156163
}
@@ -179,10 +186,6 @@ public static function withAttr(array|string|int|float $value, ?array $args = nu
179186
return $inst;
180187
}
181188

182-
static function _select($col, $table) {
183-
return self::table($table)->columns($col);
184-
}
185-
186189
/**
187190
* Build SELECT sql code (The method will be auto called in method build)
188191
* @method static __callStatic
@@ -197,10 +200,8 @@ protected function select(): self
197200
$having = $this->buildWhere("HAVING", $this->having);
198201
$order = (!is_null($this->order)) ? " ORDER BY " . implode(",", $this->order) : "";
199202
$limit = $this->buildLimit();
200-
201203
$this->sql = "{$this->explain}SELECT $this->noCache$this->calRows$this->distinct$columns FROM " .
202204
$this->getTable(true) . "$join$where$this->group$having$order$limit$this->union";
203-
204205
return $this;
205206
}
206207

@@ -221,7 +222,7 @@ protected function selectView(): self
221222
protected function insert(): self
222223
{
223224
$this->sql = "{$this->explain}INSERT INTO " . $this->getTable() . " " .
224-
$this->buildInsertSet() . $this->buildDuplicate();
225+
$this->buildInsertSet() . $this->buildDuplicate() . $this->buildReturning();
225226
return $this;
226227
}
227228

@@ -236,7 +237,7 @@ protected function update(): self
236237
$limit = $this->buildLimit();
237238

238239
$this->sql = "{$this->explain}UPDATE " . $this->getTable() . "$join SET " .
239-
$this->buildUpdateSet() . "$where$limit}";
240+
$this->buildUpdateSet() . "$where$limit}" . $this->buildReturning();
240241
return $this;
241242
}
242243

@@ -526,6 +527,17 @@ public function group(...$columns): self
526527
return $this;
527528
}
528529

530+
/**
531+
* Postgre specific function
532+
* @param string $column
533+
* @return $this
534+
*/
535+
public function returning(string $column): self
536+
{
537+
$this->returning = (string)$this->prep($column);
538+
return $this;
539+
}
540+
529541
/**
530542
* Mysql JOIN query (Default: INNER)
531543
* @param string|array|MigrateInterface $table Mysql table name (if array e.g. [TABLE_NAME, ALIAS]) or MigrateInterface instance
@@ -550,7 +562,7 @@ public function join(
550562
throw new DBQueryException("You need to specify the argument 2 (where) value!", 1);
551563
}
552564

553-
$prefix = Connect::getInstance()->getHandler()->getPrefix();
565+
$prefix = $this->connInst()->getHandler()->getPrefix();
554566
$arr = $this->sperateAlias($table);
555567
$table = (string)$this->prep($arr['table'], false);
556568
$alias = (!is_null($arr['alias'])) ? " {$arr['alias']}" : " $table";
@@ -737,6 +749,19 @@ private function buildUpdateSet(?array $arr = null): string
737749
return implode(",", $new);
738750
}
739751

752+
/**
753+
* Will build a returning value that can be fetched with insert id
754+
* This is a PostgreSQL specific function.
755+
* @return string
756+
*/
757+
private function buildReturning(): string
758+
{
759+
if(!is_null($this->returning) && $this->connInst()->getHandler()->getType() === "postgresql") {
760+
return " RETURNING $this->returning";
761+
}
762+
return "";
763+
}
764+
740765
/**
741766
* Build on duplicate sql string part
742767
* @return string
@@ -797,7 +822,7 @@ private function buildLimit(): string
797822

798823
/**
799824
* Used to call method that builds SQL queries
800-
* @throws DBQueryException
825+
* @throws DBQueryException|DBValidationException
801826
*/
802827
final protected function build(): void
803828
{
@@ -816,50 +841,30 @@ final protected function build(): void
816841
/**
817842
* Generate SQL string of current instance/query
818843
* @return string
819-
* @throws DBQueryException
844+
* @throws DBQueryException|DBValidationException
820845
*/
821846
public function sql(): string
822847
{
823848
$this->build();
824849
return $this->sql;
825850
}
826851

827-
/**
828-
* Start Transaction
829-
* @return mixed
830-
* @throws ConnectException
831-
*/
832-
public static function transaction(): mixed
833-
{
834-
return Connect::getInstance()->getHandler()->transaction();
835-
}
836-
837-
/**
838-
* Get return a new generated UUID
839-
* DEPRECATED: Will be moved to Connect for starter
840-
* @throws ConnectException|DBQueryException
841-
*/
842-
public static function getUUID(): ?string
843-
{
844-
$result = Connect::getInstance()->query("SELECT UUID()");
845-
if (is_object($result)) {
846-
if ($result->num_rows > 0) {
847-
$row = $result->fetch_row();
848-
return ($row[0] ?? null);
849-
}
850-
return null;
851-
} else {
852-
throw new DBQueryException(Connect::getInstance()->DB()->error, 1);
853-
}
854-
}
855-
856852
/**
857853
* Get insert AI ID from prev inserted result
858854
* @return int|string
859-
* @throws ConnectException
855+
* @throws ConnectException|DBQueryException
860856
*/
861857
public function insertID(): int|string
862858
{
863-
return Connect::getInstance()->DB()->insert_id;
859+
if($this->connInst()->getHandler()->getType() === "postgresql") {
860+
if(is_null($this->returning)) {
861+
throw new DBQueryException("You need to specify the returning column when using PostgreSQL.");
862+
}
863+
return $this->connInst()->DB()->insert_id($this->returning);
864+
}
865+
if($this->connInst()->getHandler()->getType() === "sqlite") {
866+
return $this->connInst()->DB()->lastInsertRowID();
867+
}
868+
return $this->connInst()->DB()->insert_id;
864869
}
865870
}

0 commit comments

Comments
 (0)