4
4
use Ubiquity \db \reverse \DbGenerator ;
5
5
use Ubiquity \controllers \Startup ;
6
6
use Ubiquity \cache \CacheManager ;
7
+ use Ubiquity \db \SqlCommand ;
7
8
use Ubiquity \db \utils \DbTypes ;
8
9
use Ubiquity \exceptions \DBException ;
9
10
use Ubiquity \orm \DAO ;
16
17
* This class is part of Ubiquity
17
18
*
18
19
* @author jcheron <[email protected] >
19
- * @version 1.0.4
20
+ * @version 1.0.5
20
21
* @package Ubiquity.dev
21
22
*
22
23
*/
@@ -28,11 +29,12 @@ class DatabaseReversor {
28
29
29
30
private $ models ;
30
31
31
- private $ databaseMetas ;
32
+ private $ dbOffset ;
32
33
33
34
public function __construct (DbGenerator $ generator , $ databaseOffset = 'default ' ) {
34
35
$ this ->generator = $ generator ;
35
36
$ this ->database = $ databaseOffset ;
37
+ $ this ->dbOffset =$ databaseOffset ;
36
38
$ config =Startup::$ config ;
37
39
$ this ->generator ->setDatabaseWrapper ($ this ->getWrapperInstance ($ config ,$ databaseOffset ));
38
40
}
@@ -68,6 +70,18 @@ public function createDatabase(string $name, bool $createDb = true): void {
68
70
}
69
71
$ this ->generator ->generateManyToManys ();
70
72
}
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
+ }
71
85
72
86
private function getDbName (): ?string {
73
87
$ config = Startup::$ config ;
@@ -87,7 +101,7 @@ public function migrate(): void {
87
101
return ;
88
102
}
89
103
$ tablesToCreate = $ checker ->getNonExistingTables ();
90
- if (\count ($ tablesToCreate ) > 0 ) {
104
+ if (! \empty ($ tablesToCreate )) {
91
105
$ this ->generator ->setTablesToCreate ($ tablesToCreate );
92
106
$ this ->createDatabase ($ dbName , false );
93
107
}
@@ -110,19 +124,19 @@ public function migrate(): void {
110
124
$ this ->generator ->modifyField ($ updatedField ['table ' ],$ updatedField ['name ' ],$ updatedField ['attributes ' ]);
111
125
}
112
126
$ missingPks =$ checker ->checkPrimaryKeys ($ model );
113
- if ( \count ($ missingPks )> 0 ) {
127
+ if (! \empty ($ missingPks )) {
114
128
$ pks =$ missingPks ['primaryKeys ' ];
115
129
$ tablereversor ->addPrimaryKeys ($ this ->generator ,$ pks );
116
130
}
117
131
$ missingFks =$ checker ->checkManyToOne ($ model );
118
- if (\count ($ missingFks )> 0 ){
132
+ if (! \empty ($ missingFks )){
119
133
foreach ($ missingFks as $ fk ){
120
134
$ this ->generator ->addForeignKey ($ fk ['table ' ], $ fk ['column ' ], $ fk ['fkTable ' ], $ fk ['fkId ' ]);
121
135
}
122
136
}
123
137
124
138
$ missingFks =$ checker ->checkManyToMany ($ model );
125
- if (\count ($ missingFks )> 0 ){
139
+ if (! \empty ($ missingFks )){
126
140
foreach ($ missingFks as $ fk ){
127
141
if (!$ this ->generator ->hasToCreateTable ($ fk ['table ' ])) {
128
142
$ this ->checkManyToManyFields ($ checker , $ fk ['table ' ], $ fk ['column ' ],$ newMissingPks );
@@ -142,7 +156,7 @@ private function checkManyToManyFields(DatabaseChecker $checker,string $table,st
142
156
if (!isset ($ originalFieldInfos [$ field ])) {
143
157
$ this ->generator ->addField ($ table , $ field , ['type ' => 'int ' ]);
144
158
}
145
- if (\array_search ($ field ,$ pks )=== false ){
159
+ if (! in_array ($ field , $ pks )){
146
160
$ newMissingPks [$ table ][]=$ field ;
147
161
}
148
162
}
0 commit comments