File: memdbloader.cpp

package info (click to toggle)
openrpt 3.3.7-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,220 kB
  • ctags: 4,263
  • sloc: cpp: 31,332; ansic: 12,175; xml: 7,401; sh: 376; sql: 32; makefile: 21
file content (183 lines) | stat: -rw-r--r-- 4,431 bytes parent folder | download | duplicates (5)
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
#include <QFile>
#include <QDomElement>
#include <QSqlQuery>
#include <QSqlDatabase>
#include <QSqlDriver>
#include <QSqlField>
#include <QVariant>

#include "memdbloader.h"

const QString memDbName = ":memory:";


MemDbLoader::MemDbLoader()
{
}

MemDbLoader::~MemDbLoader()
{
}

QString MemDbLoader::createMemoryDB(QString id)
{
	if(!QSqlDatabase::isDriverAvailable("QSQLITE")) {
		return "SQLITE plugin not available - data can't be loaded";
	}
	if(id.isEmpty() || !QSqlDatabase::contains(id)) {
		QSqlDatabase db = id.isEmpty() ? QSqlDatabase::addDatabase("QSQLITE") : QSqlDatabase::addDatabase("QSQLITE", id);
		db.setDatabaseName(memDbName); 
	}
	return "";
}

// Load the DB from a file
bool MemDbLoader::load(const QString &filename, QSqlDatabase db)
{
    QFile * f = new QFile(filename);

    QDomDocument doc = QDomDocument();
    QString errMsg;
    int errLine, errCol;

    if(!doc.setContent(f,&errMsg,&errLine,&errCol)) {
        _lastError = QString().sprintf("Encountered and error while parsing %s\n\n\t%s (Line %d Column %d)",filename.toLatin1().data(),errMsg.toLatin1().data(),errLine,errCol);
        return false;
    }

    QDomElement root = doc.documentElement();

    return load(root, db);
}

// Load the DB from a DomElement
bool MemDbLoader::load(const QDomElement & elemSource, QSqlDatabase db)
{
    _lastError = "";
    
    // Login to the SQLITE memory DB

	if(db.isOpen()) {
		db.close();
		db.open();
	}

	if(db.databaseName() != memDbName) {
		_lastError = createMemoryDB();
		if(!_lastError.isEmpty()) {
			return false;
		}
		db = QSqlDatabase::database();
		if(!db.open()) {
			_lastError = "Error opening QSQLITE memory database";
			return false;
		}  
	}
	_db = db;

    QDomNodeList nlist = elemSource.childNodes();
    for(int i = 0; i < nlist.count(); i++ ) {
        QDomElement it = nlist.item(i).toElement();
        if(it.tagName()=="table") {
            parseTable(it.toElement());
        }
    }
    return _lastError.isEmpty();
}

void MemDbLoader::parseTable(const QDomElement & elemSource)
{
    _tableName = elemSource.attribute("name");
    _columnNames.clear();

    QDomNodeList nlist = elemSource.childNodes();
    for(int i = 0; i < nlist.count(); i++ ) {
        QDomElement it = nlist.item(i).toElement();
        if(it.tagName()=="columns") {
            parseColumns(it);
        }
        else if(it.tagName()=="record") {
            parseRecord(it);
        }
    }
}

void MemDbLoader::parseColumns(const QDomElement & elemSource)
{
    if(_tableName.isEmpty()) {
        _lastError = "Error: missing table name";
        return;
    }
    
    QString	myQuery = "CREATE TABLE " + _tableName + " (";

    QDomNodeList nlist = elemSource.childNodes();
    bool firstCol = true;
    for(int i = 0; i < nlist.count(); i++ ) {
        QDomElement it = nlist.item(i).toElement();
        if(it.tagName()=="column") {
            QString colName = it.text();
            QString colType = it.attribute("type");
            if(colType.isEmpty()) {
                colType = "TEXT";
            }
            if(!firstCol) {
                myQuery += ", ";
            }
            firstCol = false;

            myQuery += colName + " " + colType;
            _columnNames << colName;
        }
    }
    
    myQuery += ")";
    
    QSqlQuery	query(_db);

    if(!query.exec(myQuery)) {
        _lastError = "Failed Query: " + myQuery;
        return;
    }    
}

void MemDbLoader::parseRecord(const QDomElement & elemSource)
{
    QString	myQuery = "INSERT INTO " + _tableName + " (";
    
    bool firstCol = true;
    foreach (QString val, _columnNames) {
        if(!firstCol) {
            myQuery += ", ";
        }
        firstCol = false;
        myQuery += val;
    }

    myQuery += ") VALUES (";

    QSqlQuery	query(_db);

    QDomNodeList nlist = elemSource.childNodes();
    firstCol = true;
    for(int i = 0; i < nlist.count(); i++ ) {
        QDomElement it = nlist.item(i).toElement();
        if(it.tagName()=="val") {
            if(!firstCol) {
                myQuery += ", ";
            }
            firstCol = false;

            QSqlField field(_columnNames.at(i), QVariant::String);
            field.setValue(it.text());
            myQuery += query.driver()->formatValue (field); 
        }
    }
    
    myQuery += ")";
    
    if(!query.exec(myQuery)) {
        _lastError = "Failed Query: " + myQuery;
        return;
    }
}