55{
66 protected $ attributes = array ();
77
8- private static $ connection = null ;
8+ private static $ config = 'database.php ' ;
9+
10+ protected $ connection = 'default ' ;
911
1012 private $ data = null ;
1113
@@ -25,6 +27,8 @@ class DB
2527
2628 private $ params = array ();
2729
30+ private static $ pool = array ();
31+
2832 private $ rowCount = null ;
2933
3034 private $ select = null ;
@@ -37,7 +41,7 @@ class DB
3741
3842 public function __construct ()
3943 {
40- $ this ->connect ();
44+ $ this ->mount ();
4145 }
4246
4347 protected function buildInsert (array $ data , $ modifiers =null , $ upsert =false ): string
@@ -164,28 +168,9 @@ protected function buildWhere(): string
164168 return join (' AND ' , $ tmp );
165169 }
166170
167- protected function connect (): void
168- {
169- if (is_object (self ::$ connection ))
171+ public static function config (string $ filename )
170172 {
171- $ this ->dbh = self ::$ connection ;
172- return ;
173- }
174-
175- $ dsn = $ _ENV ['PHP_ORM_DSN ' ];
176- $ user = $ _ENV ['PHP_ORM_USER ' ];
177- $ password = $ _ENV ['PHP_ORM_PSWD ' ];
178-
179- try {
180- $ this ->dbh = new \PDO ($ dsn , $ user , $ password );
181- $ this ->dbh ->setAttribute (\PDO ::ATTR_ERRMODE , \PDO ::ERRMODE_EXCEPTION );
182- $ this ->dbh ->setAttribute (\PDO ::ATTR_EMULATE_PREPARES ,true );
183-
184- self ::$ connection = $ this ->dbh ;
185-
186- } catch (\PDOException $ e ) {
187- echo 'Connection failed: ' . $ e ->getMessage ();
188- }
173+ self ::$ config = $ filename ;
189174 }
190175
191176 public function count (): ?int
@@ -395,21 +380,44 @@ public function leftJoin(string $table, string $conditions): DB
395380 return $ this ;
396381 }
397382
398- public function rightJoin ( string $ table , string $ conditions ): DB
383+ public function limit ( int $ rowCount = null , int $ offset = null ): DB
399384 {
400- $ this ->join [] = array (
401- 'type ' => 'RIGHT ' ,
402- 'table ' => $ table ,
403- 'conditions ' => $ conditions ,
404- );
385+ $ this ->rowCount = $ rowCount ;
386+ $ this ->offset = $ offset ;
405387
406388 return $ this ;
407389 }
408390
409- public function limit ( int $ rowCount = null , int $ offset = null ): DB
391+ protected function mount ( ): DB
410392 {
411- $ this ->rowCount = $ rowCount ;
412- $ this ->offset = $ offset ;
393+ if (array_key_exists ($ this ->connection , self ::$ pool )
394+ && is_object (self ::$ pool [$ this ->connection ])
395+ && self ::$ pool [$ this ->connection ] instanceof \PDO
396+ )
397+ {
398+ $ this ->dbh = self ::$ pool [$ this ->connection ];
399+ return $ this ;
400+ }
401+
402+ $ database = include self ::$ config ;
403+ if (!isset ($ database [$ this ->connection ]))
404+ {
405+ throw new \Exception ("Connection not found. " );
406+ }
407+ $ opts = $ database [$ this ->connection ];
408+
409+ $ configuration = new Configuration (
410+ $ opts ['username ' ],
411+ $ opts ['password ' ],
412+ $ opts ['database ' ],
413+ $ opts ['host ' ],
414+ $ opts ['port ' ],
415+ $ opts ['driver ' ],
416+ $ opts ['charset ' ],
417+ $ opts ['collation ' ]);
418+ $ connection = new Connection ($ configuration );
419+ $ this ->dbh = $ connection ->getDbh ();
420+ self ::$ pool [$ this ->connection ] = $ this ->dbh ;
413421
414422 return $ this ;
415423 }
@@ -451,6 +459,17 @@ public function reset(): DB
451459 return $ this ;
452460 }
453461
462+ public function rightJoin (string $ table , string $ conditions ): DB
463+ {
464+ $ this ->join [] = array (
465+ 'type ' => 'RIGHT ' ,
466+ 'table ' => $ table ,
467+ 'conditions ' => $ conditions ,
468+ );
469+
470+ return $ this ;
471+ }
472+
454473 public function select (string $ value =null ): DB
455474 {
456475 $ this ->select = $ value ;
0 commit comments