22
33namespace de \interaapps \ulole \orm ;
44
5+ use de \interaapps \ulole \orm \drivers \Driver ;
6+ use de \interaapps \ulole \orm \drivers \MySQLDriver ;
7+ use de \interaapps \ulole \orm \drivers \PostgresDriver ;
8+ use de \interaapps \ulole \orm \drivers \SQLiteDriver ;
59use de \interaapps \ulole \orm \migration \Blueprint ;
610use PDO ;
711use PDOStatement ;
812
913class Database {
10- private PDO $ connection ;
14+ private Driver $ driver ;
15+
16+ private static $ driverFactories = [];
1117
1218 /**
1319 * @param string $username
@@ -17,61 +23,20 @@ class Database {
1723 * @param int $port
1824 * @param string $driver
1925 */
20- public function __construct (string $ username , string |null $ password = null , string |null $ database = null , string $ host = 'localhost ' , int $ port = 3306 , string $ driver = "mysql " ) {
21- if ($ driver == "sqlite " )
22- $ this ->connection = new PDO ($ driver . ': ' . $ database );
23- else
24- $ this ->connection = new PDO ($ driver . ':host= ' . $ host . ';dbname= ' . $ database , $ username , $ password );
25- }
26-
27- public function getConnection (): PDO {
28- return $ this ->connection ;
26+ public function __construct (string $ username = "" , string |null $ password = null , string |null $ database = null , string $ host = 'localhost ' , int |null $ port = null , string $ driver = "mysql " ) {
27+ $ this ->driver = self ::getDriverFactories ()[$ driver ]($ username , $ password , $ database , $ host , $ port , $ driver );
2928 }
3029
31- public function query ( $ sql ): PDOStatement | bool {
32- return $ this ->connection -> query ( $ sql );
30+ public function create ( string $ name , callable $ callable , bool $ ifNotExists = false ): bool {
31+ return $ this ->driver -> create ( $ name , $ callable );
3332 }
3433
35- public function create (string $ name , $ callable , bool $ ifNotExists = false ): PDOStatement |bool {
36- $ blueprint = new Blueprint ();
37- $ callable ($ blueprint );
38- $ sql = "CREATE TABLE " . ($ ifNotExists ? "IF NOT EXISTS " : "" ) . "` " . $ name . "` ( \n" ;
39- $ sql .= implode (", \n" , $ blueprint ->getQueries (true ));
40- $ sql .= "\n) ENGINE = InnoDB; " ;
41-
42- return $ this ->query ($ sql );
43- }
44-
45- public function edit (string $ name , $ callable ): PDOStatement |bool {
46- $ statement = $ this ->connection ->query ("SHOW COLUMNS FROM " . $ name . "; " );
47- $ existingColumns = [];
48- foreach ($ statement ->fetchAll (\PDO ::FETCH_NUM ) as $ row ) {
49- $ existingColumns [] = $ row [0 ];
50- }
51- $ blueprint = new Blueprint ();
52- $ callable ($ blueprint );
53- $ sql = "ALTER TABLE ` " . $ name . "` " ;
54- $ comma = false ;
55- foreach ($ blueprint ->getQueries () as $ column => $ query ) {
56- if ($ comma )
57- $ sql .= ", " ;
58-
59- if (in_array ($ column , $ existingColumns ))
60- $ sql .= (substr ($ query , 0 , 4 ) === "DROP " ? "" : "CHANGE ` " . $ column . "` " ) . $ query ;
61- else
62- $ sql .= " ADD " . $ query ;
63-
64- if (!$ comma )
65- $ comma = true ;
66- }
67- $ sql .= "; " ;
68-
69- return $ this ->query ($ sql );
34+ public function edit (string $ name , callable $ callable ): bool {
35+ return $ this ->driver ->edit ($ name , $ callable );
7036 }
7137
72-
7338 public function drop (string $ name ): PDOStatement |bool {
74- return $ this ->query ( " DROP TABLE ` " . $ name . " `; " );
39+ return $ this ->driver -> drop ( $ name );
7540 }
7641
7742 public function autoMigrate (): Database {
@@ -85,4 +50,27 @@ public function autoMigrate(): Database {
8550 return $ this ;
8651 }
8752
88- }
53+ public function getDriver (): Driver {
54+ return $ this ->driver ;
55+ }
56+
57+ public static function setDriverFactory (string $ name , callable $ callable ) {
58+ self ::$ driverFactories [$ name ] = $ callable ;
59+ }
60+
61+ public static function getDriverFactories (): array {
62+ return self ::$ driverFactories ;
63+ }
64+ }
65+
66+ Database::setDriverFactory ("mysql " , function (string $ username , string |null $ password , string |null $ database , string $ host , int |null $ port , string $ driver ) : Driver {
67+ return new MySQLDriver (new PDO ($ driver . ':host= ' . $ host . ': ' . ($ port ?? 3306 ) . ';dbname= ' . $ database , $ username , $ password ));
68+ });
69+
70+ Database::setDriverFactory ("pgsql " , function (string $ username , string |null $ password , string |null $ database , string $ host , int |null $ port , string $ driver ) : Driver {
71+ return new PostgresDriver (new PDO ($ driver . ':host= ' . $ host . ';dbname= ' . $ database , $ username , $ password ));
72+ });
73+
74+ Database::setDriverFactory ("sqlite " , function (string $ username , string |null $ password , string |null $ database , string $ host , int |null $ port , string $ driver ) : Driver {
75+ return new SQLiteDriver (new PDO ($ driver . ': ' . $ database ));
76+ });
0 commit comments