44use Ubiquity \db \reverse \DbGenerator ;
55use Ubiquity \controllers \Startup ;
66use Ubiquity \cache \CacheManager ;
7+ use Ubiquity \db \SqlCommand ;
78use Ubiquity \db \utils \DbTypes ;
89use Ubiquity \exceptions \DBException ;
910use Ubiquity \orm \DAO ;
1617 * This class is part of Ubiquity
1718 *
1819 * @author jcheron <[email protected] > 19- * @version 1.0.4
20+ * @version 1.0.5
2021 * @package Ubiquity.dev
2122 *
2223 */
@@ -28,11 +29,12 @@ class DatabaseReversor {
2829
2930 private $ models ;
3031
31- private $ databaseMetas ;
32+ private $ dbOffset ;
3233
3334 public function __construct (DbGenerator $ generator , $ databaseOffset = 'default ' ) {
3435 $ this ->generator = $ generator ;
3536 $ this ->database = $ databaseOffset ;
37+ $ this ->dbOffset =$ databaseOffset ;
3638 $ config =Startup::$ config ;
3739 $ this ->generator ->setDatabaseWrapper ($ this ->getWrapperInstance ($ config ,$ databaseOffset ));
3840 }
@@ -68,6 +70,18 @@ public function createDatabase(string $name, bool $createDb = true): void {
6870 }
6971 $ this ->generator ->generateManyToManys ();
7072 }
73+
74+ public function generateTablesForModels (?array $ models =null ,bool $ execute =false ): bool {
75+ if (isset ($ models )) {
76+ $ this ->setModels ($ models );
77+ }
78+ $ this ->createDatabase ('' , false );
79+ if ($ execute ) {
80+ $ script =\implode ('; ' , $ this ->getScript ());
81+ return SqlCommand::executeSQLTransaction ($ this ->dbOffset ,$ script );
82+ }
83+ return true ;
84+ }
7185
7286 private function getDbName (): ?string {
7387 $ config = Startup::$ config ;
@@ -87,7 +101,7 @@ public function migrate(): void {
87101 return ;
88102 }
89103 $ tablesToCreate = $ checker ->getNonExistingTables ();
90- if (\count ($ tablesToCreate ) > 0 ) {
104+ if (! \empty ($ tablesToCreate )) {
91105 $ this ->generator ->setTablesToCreate ($ tablesToCreate );
92106 $ this ->createDatabase ($ dbName , false );
93107 }
@@ -110,19 +124,19 @@ public function migrate(): void {
110124 $ this ->generator ->modifyField ($ updatedField ['table ' ],$ updatedField ['name ' ],$ updatedField ['attributes ' ]);
111125 }
112126 $ missingPks =$ checker ->checkPrimaryKeys ($ model );
113- if ( \count ($ missingPks )> 0 ) {
127+ if (! \empty ($ missingPks )) {
114128 $ pks =$ missingPks ['primaryKeys ' ];
115129 $ tablereversor ->addPrimaryKeys ($ this ->generator ,$ pks );
116130 }
117131 $ missingFks =$ checker ->checkManyToOne ($ model );
118- if (\count ($ missingFks )> 0 ){
132+ if (! \empty ($ missingFks )){
119133 foreach ($ missingFks as $ fk ){
120134 $ this ->generator ->addForeignKey ($ fk ['table ' ], $ fk ['column ' ], $ fk ['fkTable ' ], $ fk ['fkId ' ]);
121135 }
122136 }
123137
124138 $ missingFks =$ checker ->checkManyToMany ($ model );
125- if (\count ($ missingFks )> 0 ){
139+ if (! \empty ($ missingFks )){
126140 foreach ($ missingFks as $ fk ){
127141 if (!$ this ->generator ->hasToCreateTable ($ fk ['table ' ])) {
128142 $ this ->checkManyToManyFields ($ checker , $ fk ['table ' ], $ fk ['column ' ],$ newMissingPks );
@@ -142,7 +156,7 @@ private function checkManyToManyFields(DatabaseChecker $checker,string $table,st
142156 if (!isset ($ originalFieldInfos [$ field ])) {
143157 $ this ->generator ->addField ($ table , $ field , ['type ' => 'int ' ]);
144158 }
145- if (\array_search ($ field ,$ pks )=== false ){
159+ if (! in_array ($ field , $ pks )){
146160 $ newMissingPks [$ table ][]=$ field ;
147161 }
148162 }
0 commit comments