-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbschema.cpp
57 lines (48 loc) · 1.38 KB
/
dbschema.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
#include "dbschema.h"
DBSchema::DBSchema(QString file, QObject *parent) :
QObject(parent)
{
this->parseJsonFile(file);
}
QStringList DBSchema::tables() const
{
return jsondoc.object().keys();
}
QStringList DBSchema::fields(QString tableName) const
{
QJsonArray table = jsondoc.object().value(tableName).toArray();
QStringList li;
for (int i = 0; i < table.size(); ++i){
QJsonObject field = table.at(i).toObject();
QJsonObject::const_iterator it = field.constBegin();
li.append( it.key() );
}
return li;
}
QString DBSchema::type(QString tableName, QString fieldName) const
{
QJsonArray table = jsondoc.object().value(tableName).toArray();
for (int i = 0; i < table.size(); ++i){
QJsonObject field = table.at(i).toObject();
QJsonObject::const_iterator it = field.constBegin();
if (it.key() == fieldName ){
return it.value().toString();
}
}
return QString();
}
bool DBSchema::isReady() const
{
return (!jsondoc.isNull()) && (!jsondoc.isEmpty());
}
bool DBSchema::parseJsonFile(QString file)
{
QFile loadFile(file);
if (!loadFile.open(QIODevice::ReadOnly)) {
qWarning() << "Couldn't open db schema file: " + file;
return false;
}
QByteArray jsonData = loadFile.readAll();
jsondoc = QJsonDocument::fromJson(jsonData);
return this->isReady();
}