-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlstructuresync.cpp
More file actions
269 lines (213 loc) · 6.09 KB
/
sqlstructuresync.cpp
File metadata and controls
269 lines (213 loc) · 6.09 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "sqlstructuresync.h"
#include "QFile"
SqlStructureSync::SqlStructureSync(QString db_1,QString db_2)
{
this->db_1 = db_1;
this->db_2 = db_2;
//connect db 1,2
}
bool SqlStructureSync::initDb()
{
originalDb = QSqlDatabase::addDatabase("QSQLITE","c1");
syncToDb = QSqlDatabase::addDatabase("QSQLITE","c2");
originalDb.setDatabaseName(db_1);
syncToDb.setDatabaseName(db_2);
if(QFile::exists(db_1) && QFile::exists(db_2))
{
if(originalDb.open() && syncToDb.open())
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
void SqlStructureSync::startSync()
{
QSqlQuery query_ori(originalDb);
QSqlQuery query_syncTo(syncToDb);
setp_one_sync_table(query_ori,query_syncTo);
setp_two_sync_table_structure(query_ori,query_syncTo);
}
void SqlStructureSync::setp_one_sync_table(QSqlQuery query_ori, QSqlQuery query_sync)
{
QStringList oriTables = originalDb.tables();
QStringList syncTables = syncToDb.tables();
QStringList missingTabel;
for(auto &i:oriTables)
{
if(syncTables.contains(i))
{
;
}
else
{
missingTabel.append(i);
qDebug() << QString("table missing find :").arg(i);
}
}
//在syncTo 里面创建表
if(!missingTabel.empty())
{
QStringList sqls;
QString sql;
QStringList result_sqls_create_tables;
for(auto &i:missingTabel)
{
sql = QString("select sql from sqlite_master where type='table' and name='%1';").arg(i);
sqls.push_back(sql);
}
for(auto &sql:sqls)
{
if(!query_ori.exec(sql))
qDebug() << query_ori.lastError();
query_ori.next();
result_sqls_create_tables.push_back(query_ori.value(0).toString());
}
//sync exec
for(auto &sql:result_sqls_create_tables)
{
qDebug() << sql;
if(!query_sync.exec(sql))
qDebug() << query_sync.lastError();
}
}
}
void SqlStructureSync::setp_two_sync_table_structure(QSqlQuery query_ori, QSqlQuery query_syncTo)
{
QString sql_get_table_info = "PRAGMA table_info(%1);";
QString sql_alter_table = "ALTER TABLE %1 ADD COLUMN %2 %3%4 %5;";//ALTER TABLE FaceControlRecord ADD COLUMN sAlcohol Text;
// query_ori.exec(sql_get_table_info.arg("audio"));
// query_ori.next();
// while(query_ori.next())
// {
// if(query_ori.value(4).toString().isEmpty())
// qDebug() << query_ori.value(4);
// }
//提取主表原始数据
QString sql;
QStringList ori_tables_alter_str;
QStringList sync_tables_alter_str;
QStringList different_tables;
for(auto &table_name:originalDb.tables())
{
sql = sql_get_table_info.arg(table_name);
if(!query_ori.exec(sql))
qDebug() << query_ori.lastError();
if(!query_syncTo.exec(sql))
qDebug() << query_syncTo.lastError();
//遍历表结构 ori alter
while(query_ori.next())
{
//cid | name | type | notnull | dflt_value | pk
QString sql;
sql = sql_alter_table.arg(table_name).arg(query_ori.value(1).toString()).arg(query_ori.value(2).toString());
//not null flag
if(query_ori.value(3).toInt() == 1)
{
sql = sql.arg(" NOT NULL");
}
else
{
sql = sql.arg("");
}
if(query_ori.value(4).toString().isEmpty())
{
sql = sql.arg("");
}
else
{
sql = sql.arg("DEFAULT %1").arg(query_ori.value(4).toString());
}
ori_tables_alter_str.append(sql);
}
//遍历表结构 sync alter
while(query_syncTo.next())
{
//cid | name | type | notnull | dflt_value | pk
QString sql;
sql = sql_alter_table.arg(table_name).arg(query_syncTo.value(1).toString()).arg(query_syncTo.value(2).toString());
if(query_syncTo.value(3).toInt() == 1)
{
sql = sql.arg(" NOT NULL");
}
else
{
sql = sql.arg("");
}
if(query_syncTo.value(4).toString().isEmpty())
{
sql = sql.arg("");
}
else
{
sql = sql.arg("DEFAULT %1").arg(query_syncTo.value(4).toString());
}
sync_tables_alter_str.append(sql);
}
}
int cnt_change = 0;
for(auto &i:ori_tables_alter_str)
{
if(!sync_tables_alter_str.contains(i))
{
cnt_change++;
qDebug() << i;
if(!query_syncTo.exec(i))
qDebug() << query_syncTo.lastError();
}
}
if(cnt_change == 0)
{
qDebug() << "all tables are up to date";
}
else
{
qDebug() << cnt_change << "tables changed";
}
// for(QString &i:ori_tables_alter_str)
// {
// if(i.contains("INT"))
// {
// qDebug() << i;
// }
// }
}
bool SqlStructureSync::outputDatabaseInfo(QSqlDatabase database)
{
QStringList tabels = database.tables();
QStringListIterator itr(tabels);
while (itr.hasNext())
{
QString tabel = itr.next();
qDebug() << QString("表名:") << tabel;
outPutTableInfo(tabel);
}
return true;
}
bool SqlStructureSync::outPutTableInfo(QString tabNmae)
{
QSqlQuery query(originalDb);
QString strTableNmae = tabNmae;
QString str = "PRAGMA table_info(" + strTableNmae + ")";
query.prepare(str);
if (query.exec())
{
while (query.next())
{
qDebug() << QString(QString("%1 %2 %3")).arg(query.value(0).toString()).arg(query.value(1).toString()).arg(query.value(2).toString());
}
}
else
{
qDebug() << query.lastError();
return false;
}
return true;
}