-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStructure.cpp
246 lines (226 loc) · 10.6 KB
/
Structure.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQueryModel>
#include "SQL/UpdateStru2024-12-30_2025-01-20.sql"
#include "SQL/CreateTables.sql"
#include "SQL/CreateViews.sql"
#include "SQL/CreateBaseData.sql"
#include "SQL/CreateTriggers.sql"
#include "SQL/UpdateTableParams.sql"
#include "PotaUtils.h"
#include "data/Data.h"
bool MainWindow::UpdateDBShema(QString sDBVersion)
{
//QSqlDatabase db = QSqlDatabase::database();
PotaQuery *model = new PotaQuery;
PotaQuery *model2 = new PotaQuery;
PotaQuery *model3 = new PotaQuery;
model->lErr = ui->lDBErr;
bool bNew=false;
bool bResult=true;
QString sResult="";
if (!model->ExecShowErr("PRAGMA foreign_keys = OFF")) {
ui->tbInfoDB->append(tr("Impossible de désactiver les clés étrangères."));
return false;
}
ui->progressBar->setValue(0);
ui->progressBar->setMaximum(0);
ui->progressBar->setFormat("%p%");
ui->progressBar->setVisible(true);
if (sDBVersion == "New" or sDBVersion == "NewWithBaseData")
{
//bNew=true; mettre à false quand debug fait.
ui->progressBar->setFormat("CREATE TABLES %p%");
if (model->ExecMultiShowErr(DynDDL(sDDLTables),";",ui->progressBar)) {
sResult.append("Create tables : ok (").append(DbVersion).append(")\n");
if (sDBVersion == "NewWithBaseData") {
ui->progressBar->setFormat("BASE DATA %p%");
if (!model->ExecMultiShowErr(sSQLBaseData,";",ui->progressBar)) {
ui->tbInfoDB->append(tr("Echec de la création des données de base")+" ("+ui->lVerBDDAttendue->text()+")");
ui->progressBar->setVisible(false);
return false;
}
sResult.append("Create base data : ok\n");
}
sDBVersion = DbVersion;//"2024-12-30";
} else {
ui->tbInfoDB->append(tr("Echec de la création de la BDD vide")+" ("+ui->lVerBDDAttendue->text()+")");
ui->progressBar->setVisible(false);
return false;
}
} else { //Updating an existing db.
//DROP all views
ui->progressBar->setValue(0);
ui->progressBar->setMaximum(0);
ui->progressBar->setFormat("DROP VIEWS %p%");
QString sDropViews="";
model->clear();
model->ExecShowErr("PRAGMA table_list;");
while (model->next()) {
if (model->value("type").toString()=="view"){
sDropViews = sDropViews + "DROP VIEW IF EXISTS \""+model->value("name").toString()+"\";";
}
}
model->ExecMultiShowErr(sDropViews,";",ui->progressBar);
//DROP SQLean functions
ui->progressBar->setValue(0);
ui->progressBar->setMaximum(0);
ui->progressBar->setFormat("DROP SQLean functions %p%");
QSqlDatabase db = QSqlDatabase::database();
QString sDbFile=db.databaseName();
dbClose();
dbOpen(sDbFile,false,true,false);
dbClose();
dbOpen(sDbFile,false,false,false);
if (bResult and(sDBVersion == "2024-12-30")) { //Specific update shema.
ui->progressBar->setValue(0);
ui->progressBar->setMaximum(0);
ui->progressBar->setFormat("Specific update shema %p%");
bResult = model->ExecMultiShowErr(sDDL20250120,";",ui->progressBar);
sResult.append(sDBVersion+" -> 2025-01-20 : "+iif(bResult,"ok","Err").toString()+"\n");
if (bResult) sDBVersion = "2025-01-20";
}
if (bResult) { //Update schema.
ui->progressBar->setValue(0);
ui->progressBar->setMaximum(0);
ui->progressBar->setFormat("Update shema %p%");
QString sUpdateSchema="BEGIN TRANSACTION;";
//Rename old tables.
model->clear();
model->ExecShowErr("PRAGMA table_list;");
while (model->next()) {
if (model->value("type").toString()=="table" and !model->value("name").toString().startsWith("sql"))//No sqlite or sqlean tables.
sUpdateSchema +="DROP TABLE IF EXISTS Temp_"+model->value("name").toString()+";"
"CREATE TABLE Temp_"+model->value("name").toString()+" AS "
"SELECT * FROM "+model->value("name").toString()+";"
"DROP TABLE "+model->value("name").toString()+";";
}
//Create new tables.
sUpdateSchema += DynDDL(sDDLTables);
bResult = model->ExecMultiShowErr(sUpdateSchema,";",ui->progressBar);
if (bResult) {
sUpdateSchema="";
ui->progressBar->setValue(0);
ui->progressBar->setMaximum(0);
ui->progressBar->setFormat("Data transfert %p%");
//Import data from old to new tables.
QString sFieldsList;
model->clear();
model->ExecShowErr("PRAGMA table_list;");
while (model->next()) {
if (model->value("type").toString()=="table" and
!model->value("name").toString().startsWith("Temp_") and
!model->value("name").toString().startsWith("sql")) {
sFieldsList="";
model2->ExecShowErr("PRAGMA table_xinfo("+model->value("name").toString()+");");//New table
while (model2->next()) {
if (model2->value("hidden").toInt()==0) {
model3->ExecShowErr("PRAGMA table_xinfo(Temp_"+model->value("name").toString()+");");//Old table
qDebug() << model->value("name").toString() << " - " << model2->value("name").toString();
while (model3->next()) {
qDebug() << model3->value("name").toString();
if (model3->value("name").toString()==model2->value("name").toString())//Fields exists in old and new tables
sFieldsList +=model2->value("name").toString()+",";
}
}
}
sFieldsList=sFieldsList.removeLast();
sUpdateSchema +="INSERT INTO "+model->value("name").toString()+" ("+sFieldsList+") " //todo: spécifier les champs si pas le même nom.
"SELECT "+sFieldsList+" FROM Temp_"+model->value("name").toString()+";";
}
}
//DROP Temp tables.
model->clear();
model->ExecShowErr("PRAGMA table_list;");
while (model->next()) {
if (model->value("type").toString()=="table" and !model->value("name").toString().startsWith("sql"))
sUpdateSchema +="DROP TABLE IF EXISTS Temp_"+model->value("name").toString()+";";
}
//Update schema.
sUpdateSchema += "COMMIT TRANSACTION;";
bResult = model->ExecMultiShowErr(sUpdateSchema,";",ui->progressBar);
}
sResult.append("Data transfert : "+iif(bResult,"ok","Err").toString()+"\n");
}
}
if (bResult and(sDBVersion == ui->lVerBDDAttendue->text())) { //Tables shema ok.
//if (sResult=="")
// sResult.append("Version : "+sDBVersion+"\n");
if (bResult){
//Create scalar functions
ui->progressBar->setValue(0);
ui->progressBar->setMaximum(0);
ui->progressBar->setFormat("Scalar functions %p%");
bResult = registerScalarFunctions();
sResult.append("Scalar functions : "+iif(bResult,"ok","Err").toString()+"\n");
}
if (bResult){
//Create views
ui->progressBar->setValue(0);
ui->progressBar->setMaximum(0);
ui->progressBar->setFormat("Views %p%");
bResult = model->ExecMultiShowErr(DynDDL(sDDLViews),";",ui->progressBar);
sResult.append("Views : "+iif(bResult,"ok","Err").toString()+"\n");
}
if (bResult){
//Create table valued functions
ui->progressBar->setValue(0);
ui->progressBar->setMaximum(0);
ui->progressBar->setFormat("Table valued functions %p%");
bResult = registerTableValuedFunctions();
sResult.append("Table valued functions : "+iif(bResult,"ok","Err").toString()+"\n");
}
if (bResult){
//Test functions
ui->progressBar->setValue(0);
ui->progressBar->setMaximum(0);
ui->progressBar->setFormat("Function test %p%");
QString sErrFunc = testCustomFunctions();
bResult = (sErrFunc=="");
sResult.append("Function test : "+iif(bResult,"ok","Err ("+sErrFunc+")").toString()+"\n");
}
if (bResult){
//Create triggers
ui->progressBar->setValue(0);
ui->progressBar->setMaximum(0);
ui->progressBar->setFormat("Triggers %p%");
bResult = model->ExecMultiShowErr(sDDLTriggers,";;",ui->progressBar);//";" exists in CREATE TRIGGER statments
sResult.append("Triggers : "+iif(bResult,"ok","Err").toString()+"\n");
}
if (bResult){
//Update params table
ui->progressBar->setValue(0);
ui->progressBar->setMaximum(0);
ui->progressBar->setFormat("Params %p%");
bResult = model->ExecMultiShowErr(sDDLTableParams,";",ui->progressBar);
sResult.append("Params : "+iif(bResult,"ok","Err").toString()+"\n");
}
}
ui->progressBar->setVisible(false);
if (bResult and !model->ExecShowErr("PRAGMA foreign_keys = ON")) {
ui->tbInfoDB->append(tr("Impossible d'activer les clés étrangères."));
return false;
}
if (bResult)
{
if (sDBVersion == ui->lVerBDDAttendue->text())
{
if (!bNew)
MessageDialog(tr("Mise à jour réussie."),
sResult,QStyle::SP_MessageBoxInformation);
return true;
}
else {
ui->tbInfoDB->append(tr("Version de BDD inconnue:")+sDBVersion);
return false;
}
}
else
{
MessageDialog(tr("Echec de la mise à jour."),
sResult,QStyle::SP_MessageBoxCritical);
ui->tbInfoDB->append(tr("Echec de la mise à jour de la version de la BDD ")+" ("+sDBVersion+" > "+ui->lVerBDDAttendue->text()+")");
return false;
}
}