@@ -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