Skip to content

Commit cec33fa

Browse files
committed
add SqlCommand for transactions
1 parent 6b59b3b commit cec33fa

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

src/Ubiquity/db/SqlCommand.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Ubiquity\db;
4+
5+
use Ubiquity\orm\DAO;
6+
7+
class SqlCommand {
8+
9+
public static function executeSQLTransaction(string $activeDbOffset, string $sql): bool {
10+
$db = DAO::getDatabase($activeDbOffset ?? 'default');
11+
if (! $db->isConnected()) {
12+
$db->setDbName('');
13+
$db->connect();
14+
}
15+
if ($db->beginTransaction()) {
16+
try {
17+
$db->execute($sql);
18+
if ($db->inTransaction()) {
19+
$db->commit();
20+
}
21+
return true;
22+
} catch (\Error $e) {
23+
if ($db->inTransaction()) {
24+
$db->rollBack();
25+
}
26+
return false;
27+
}
28+
} else {
29+
$db->execute($sql);
30+
return true;
31+
}
32+
}
33+
}

src/Ubiquity/orm/reverse/DatabaseChecker.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* This class is part of Ubiquity
1515
*
1616
* @author jcheron <[email protected]>
17-
* @version 1.0.1
17+
* @version 1.0.2
1818
* @package Ubiquity.dev
1919
*
2020
*/
@@ -72,7 +72,7 @@ private function _getNonExistingTables() {
7272
return $this->nonExistingTables ??= $this->getNonExistingTables();
7373
}
7474

75-
protected function tableExists(string $table): bool {
75+
public function tableExists(string $table): bool {
7676
return \array_search($table, $this->_getNonExistingTables()) === false;
7777
}
7878

src/Ubiquity/orm/reverse/DatabaseReversor.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Ubiquity\db\reverse\DbGenerator;
55
use Ubiquity\controllers\Startup;
66
use Ubiquity\cache\CacheManager;
7+
use Ubiquity\db\SqlCommand;
78
use Ubiquity\db\utils\DbTypes;
89
use Ubiquity\exceptions\DBException;
910
use Ubiquity\orm\DAO;
@@ -16,7 +17,7 @@
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

Comments
 (0)