1 | |
---|
2 | #include <QSqlError> |
---|
3 | #include <QSqlQuery> |
---|
4 | #include <QSqlRecord> |
---|
5 | #include <QVariant> |
---|
6 | #include <QDateTime> |
---|
7 | |
---|
8 | #include <QDir> |
---|
9 | #include "sqlengine.h" |
---|
10 | |
---|
11 | #include <QDebug> |
---|
12 | |
---|
13 | const QString DATE_FORMAT ("yyyy-MM-dd"); |
---|
14 | const QString TIME_FORMAT ("hh:mm"); |
---|
15 | |
---|
16 | SqlEngine::SqlEngine(QObject *aParent) |
---|
17 | : QObject(aParent) |
---|
18 | { |
---|
19 | } |
---|
20 | |
---|
21 | SqlEngine::~SqlEngine() |
---|
22 | { |
---|
23 | } |
---|
24 | |
---|
25 | QString SqlEngine::login(const QString &aDatabaseType, const QString &aDatabaseName) |
---|
26 | { |
---|
27 | QSqlDatabase database = QSqlDatabase::addDatabase(aDatabaseType); |
---|
28 | database.setDatabaseName(aDatabaseName); |
---|
29 | |
---|
30 | bool result = false; |
---|
31 | if(!QFile::exists(aDatabaseName)) // the DB (tables) doesn't exists, and so we have to create one |
---|
32 | { |
---|
33 | // creating empty DB + tables |
---|
34 | // ??? what is the best way of creating new empty DB ??? |
---|
35 | // we can either: |
---|
36 | // - create new DB + tables by issuing corresponding queries (used solution) |
---|
37 | // - create new DB from resource, which contains empty DB with tables |
---|
38 | result = createTables(database); |
---|
39 | } |
---|
40 | |
---|
41 | database.open(); |
---|
42 | |
---|
43 | //LOG_INFO(QString("Opening '%1' database '%2'").arg(aDatabaseType).arg(aDatabaseName)); |
---|
44 | |
---|
45 | return result ? QString() : database.lastError().text(); |
---|
46 | } |
---|
47 | |
---|
48 | void SqlEngine::initialize() |
---|
49 | { |
---|
50 | QString databaseName; |
---|
51 | if(!QDir::home().exists(".fosdem")) |
---|
52 | QDir::home().mkdir(".fosdem"); |
---|
53 | databaseName = QDir::homePath() + "/.fosdem/" + "fosdem.sqlite"; |
---|
54 | |
---|
55 | login("QSQLITE",databaseName); |
---|
56 | } |
---|
57 | |
---|
58 | void SqlEngine::addConferenceToDB(QHash<QString,QString> &aConference) |
---|
59 | { |
---|
60 | QSqlDatabase db = QSqlDatabase::database(); |
---|
61 | |
---|
62 | if (db.isValid() && db.isOpen()) |
---|
63 | { |
---|
64 | QString values = QString("'%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8', '%9', '%10'") \ |
---|
65 | .arg(aConference["id"]) \ |
---|
66 | .arg(aConference["title"]) \ |
---|
67 | .arg(aConference["subtitle"]) \ |
---|
68 | .arg(aConference["venue"]) \ |
---|
69 | .arg(aConference["city"]) \ |
---|
70 | .arg(aConference["start"]) \ |
---|
71 | .arg(aConference["end"]) \ |
---|
72 | .arg(aConference["days"]) \ |
---|
73 | .arg(aConference["day_change"]) \ |
---|
74 | .arg(aConference["timeslot_duration"]); |
---|
75 | |
---|
76 | QString query = QString("INSERT INTO CONFERENCE (id,title,subtitle,venue,city,start,end,days,day_change,timeslot_duration) VALUES (%1)").arg(values); |
---|
77 | QSqlQuery result (query, db); |
---|
78 | //LOG_AUTOTEST(query); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | void SqlEngine::addEventToDB(QHash<QString,QString> &aEvent) |
---|
83 | { |
---|
84 | //LOG_DEBUG(QString("Adding event '%1' to DB").arg(*aEvent)); |
---|
85 | |
---|
86 | QSqlDatabase db = QSqlDatabase::database(); |
---|
87 | |
---|
88 | if (db.isValid() && db.isOpen()) |
---|
89 | { |
---|
90 | // The items of the Event are divided into the two tables EVENT and VIRTUAL_EVENT |
---|
91 | // VIRTUAL_EVENT is for Full-Text-Serach Support |
---|
92 | QTime duration = QTime::fromString(aEvent["duration"],TIME_FORMAT); |
---|
93 | QDateTime startDateTime = QDateTime(QDate::fromString(aEvent["date"],DATE_FORMAT),QTime::fromString(aEvent["start"],TIME_FORMAT)); |
---|
94 | QString values = QString("'%1', '%2', '%3', '%4', '%5', '%6', '%7'") \ |
---|
95 | .arg(aEvent["conference_id"]) \ |
---|
96 | .arg(aEvent["id"]) \ |
---|
97 | .arg(QString::number(startDateTime.toTime_t())) \ |
---|
98 | .arg(QString::number(duration.hour()*3600 + duration.minute()*60 + duration.second())) \ |
---|
99 | .arg("123456") \ |
---|
100 | .arg(aEvent["type"]) \ |
---|
101 | .arg(aEvent["language"]); |
---|
102 | |
---|
103 | QString query = QString("INSERT INTO EVENT (xid_conference, id, start, duration, xid_activity, type, language) VALUES (%1)").arg(values); |
---|
104 | QSqlQuery result (query, db); |
---|
105 | //LOG_AUTOTEST(query); |
---|
106 | |
---|
107 | // add some(text related) Event's items to VIRTUAL_EVENT table |
---|
108 | QString values2 = QString("'%1', '%2', '%3', '%4', '%5', '%6', '%7'") \ |
---|
109 | .arg(aEvent["conference_id"]) \ |
---|
110 | .arg(aEvent["id"]) \ |
---|
111 | .arg(aEvent["tag"]) \ |
---|
112 | .arg(aEvent["title"]) \ |
---|
113 | .arg(aEvent["subtitle"]) \ |
---|
114 | .arg(aEvent["abstract"]) \ |
---|
115 | .arg(aEvent["description"]); |
---|
116 | |
---|
117 | QString query2 = QString("INSERT INTO VIRTUAL_EVENT (xid_conference, id, tag, title, subtitle, abstract, description) VALUES (%1)").arg(values2); |
---|
118 | QSqlQuery result2 (query2, db); |
---|
119 | //LOG_AUTOTEST(query2); |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | void SqlEngine::addPersonToDB(QHash<QString,QString> &aPerson) |
---|
124 | { |
---|
125 | QSqlDatabase db = QSqlDatabase::database(); |
---|
126 | |
---|
127 | //TODO: check if the person doesn't exist before inserting |
---|
128 | if (db.isValid() && db.isOpen()) |
---|
129 | { |
---|
130 | QString values = QString("'%1', '%2'").arg(aPerson["id"],aPerson["name"]); |
---|
131 | QString query = QString("INSERT INTO PERSON (id,name) VALUES (%1)").arg(values); |
---|
132 | QSqlQuery result (query, db); |
---|
133 | //LOG_AUTOTEST(query); |
---|
134 | |
---|
135 | values = QString("'%1', '%2', '%3'").arg(aPerson["conference_id"],aPerson["event_id"],aPerson["id"]); |
---|
136 | query = QString("INSERT INTO EVENT_PERSON (xid_conference,xid_event,xid_person) VALUES (%1)").arg(values); |
---|
137 | QSqlQuery resultEventPerson (query, db); |
---|
138 | //LOG_AUTOTEST(query); |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | void SqlEngine::addRoomToDB(QHash<QString,QString> &aRoom) |
---|
143 | { |
---|
144 | QSqlDatabase db = QSqlDatabase::database(); |
---|
145 | |
---|
146 | if (db.isValid() && db.isOpen()) |
---|
147 | { |
---|
148 | QString queryExist = QString("SELECT id FROM ROOM WHERE name='%1'").arg(aRoom["name"]); |
---|
149 | QSqlQuery resultExist(queryExist,db); |
---|
150 | // now we have to check whether ROOM record with 'name' exists or not, |
---|
151 | // - if it doesn't exist yet, then we have to add that record to 'ROOM' table |
---|
152 | // and assign autoincremented 'id' to aRoom |
---|
153 | // - if it exists, then we need to get its 'id' and assign it to aRoom |
---|
154 | int roomId = -1; |
---|
155 | if(resultExist.next()) // ROOM record with 'name' already exists: we need to get its 'id' |
---|
156 | { |
---|
157 | roomId = resultExist.value(0).toInt(); |
---|
158 | } |
---|
159 | else // ROOM record doesn't exist yet, need to create it |
---|
160 | { |
---|
161 | QString values = QString("'%1', '%2'").arg(aRoom["name"],aRoom["picture"]); |
---|
162 | QString query = QString("INSERT INTO ROOM (name,picture) VALUES (%1)").arg(values); |
---|
163 | QSqlQuery result (query, db); |
---|
164 | roomId = result.lastInsertId().toInt(); // 'id' is assigned automatically |
---|
165 | //LOG_AUTOTEST(query); |
---|
166 | } |
---|
167 | |
---|
168 | QString values = QString("'%1', '%2', '%3'").arg(aRoom["conference_id"],aRoom["event_id"],QString::number(roomId)); |
---|
169 | QString query = QString("INSERT INTO EVENT_ROOM (xid_conference,xid_event,xid_room) VALUES (%1)").arg(values); |
---|
170 | QSqlQuery result (query, db); |
---|
171 | //LOG_AUTOTEST(query); |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | void SqlEngine::addLinkToDB(QHash<QString,QString> &aLink) |
---|
176 | { |
---|
177 | QSqlDatabase db = QSqlDatabase::database(); |
---|
178 | |
---|
179 | //TODO: check if the link doesn't exist before inserting |
---|
180 | if (db.isValid() && db.isOpen()) |
---|
181 | { |
---|
182 | QString values = QString("'%1', '%2', '%3', '%4'").arg(aLink["event_id"],aLink["conference_id"],aLink["name"],aLink["url"]); |
---|
183 | QString query = QString("INSERT INTO LINK (xid_event, xid_conference, name, url) VALUES (%1)").arg(values); |
---|
184 | QSqlQuery result(query, db); |
---|
185 | //LOG_AUTOTEST(query); |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | bool SqlEngine::createTables(QSqlDatabase &aDatabase) |
---|
190 | { |
---|
191 | bool result = aDatabase.open(); |
---|
192 | |
---|
193 | if (aDatabase.isValid() && aDatabase.isOpen()) |
---|
194 | { |
---|
195 | QSqlQuery query(aDatabase); |
---|
196 | |
---|
197 | query.exec("CREATE TABLE CONFERENCE ( \ |
---|
198 | id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , \ |
---|
199 | title VARCHAR NOT NULL , \ |
---|
200 | subtitle VARCHAR, \ |
---|
201 | venue VARCHAR, \ |
---|
202 | city VARCHAR NOT NULL , \ |
---|
203 | start DATETIME NOT NULL , \ |
---|
204 | end DATETIME NOT NULL , \ |
---|
205 | days INTEGER, \ |
---|
206 | day_change DATETIME, \ |
---|
207 | timeslot_duration DATETIME)"); |
---|
208 | |
---|
209 | query.exec("CREATE TABLE ACTIVITY ( \ |
---|
210 | id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , \ |
---|
211 | name VARCHAR NOT NULL )"); |
---|
212 | |
---|
213 | query.exec("CREATE TABLE ROOM ( \ |
---|
214 | id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , \ |
---|
215 | name VARCHAR NOT NULL , \ |
---|
216 | picture VARCHAR NOT NULL)"); |
---|
217 | |
---|
218 | query.exec("CREATE TABLE PERSON ( \ |
---|
219 | id INTEGER PRIMARY KEY NOT NULL , \ |
---|
220 | name VARCHAR NOT NULL)"); |
---|
221 | |
---|
222 | query.exec("CREATE TABLE EVENT ( \ |
---|
223 | xid_conference INTEGER NOT NULL, \ |
---|
224 | id INTEGER NOT NULL , \ |
---|
225 | start INTEGER NOT NULL , \ |
---|
226 | duration INTEGER NOT NULL , \ |
---|
227 | xid_activity INTEGER NOT NULL REFERENCES ACTIVITY(id), \ |
---|
228 | type VARCHAR, \ |
---|
229 | language VARCHAR, \ |
---|
230 | PRIMARY KEY (xid_conference,id), \ |
---|
231 | FOREIGN KEY(xid_conference) REFERENCES CONFERENCE(id) \ |
---|
232 | FOREIGN KEY(xid_activity) REFERENCES ACTIVITY(id))"); |
---|
233 | |
---|
234 | query.exec("CREATE VIRTUAL TABLE VIRTUAL_EVENT using fts3 ( \ |
---|
235 | xid_conference INTEGER NOT NULL, \ |
---|
236 | id INTEGER NOT NULL , \ |
---|
237 | tag VARCHAR,title VARCHAR NOT NULL , \ |
---|
238 | subtitle VARCHAR, \ |
---|
239 | abstract VARCHAR, \ |
---|
240 | description VARCHAR, \ |
---|
241 | PRIMARY KEY (xid_conference,id))"); |
---|
242 | |
---|
243 | query.exec("CREATE TABLE EVENT_PERSON ( \ |
---|
244 | xid_conference INTEGER NOT NULL , \ |
---|
245 | xid_event INTEGER NOT NULL , \ |
---|
246 | xid_person INTEGER NOT NULL, \ |
---|
247 | FOREIGN KEY(xid_conference, xid_event) REFERENCES EVENT(xid_conference, id), \ |
---|
248 | FOREIGN KEY(xid_person) REFERENCES PERSON(id))"); |
---|
249 | |
---|
250 | query.exec("CREATE TABLE EVENT_ROOM ( \ |
---|
251 | xid_conference INTEGER NOT NULL , \ |
---|
252 | xid_event INTEGER NOT NULL , \ |
---|
253 | xid_room INTEGER NOT NULL, \ |
---|
254 | FOREIGN KEY(xid_conference, xid_event) REFERENCES EVENT(xid_conference, id), \ |
---|
255 | FOREIGN KEY(xid_room) REFERENCES ROOM(id))"); |
---|
256 | |
---|
257 | query.exec("CREATE TABLE LINK ( \ |
---|
258 | id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, \ |
---|
259 | xid_conference INTEGER NOT NULL, \ |
---|
260 | xid_event INTEGER NOT NULL, \ |
---|
261 | name VARCHAR, \ |
---|
262 | url VARCHAR NOT NULL, \ |
---|
263 | FOREIGN KEY(xid_conference, xid_event) REFERENCES EVENT(xid_conference, id))"); |
---|
264 | } |
---|
265 | else |
---|
266 | { |
---|
267 | //LOG_WARNING("Database is not opened"); |
---|
268 | } |
---|
269 | |
---|
270 | return result; |
---|
271 | } |
---|
272 | |
---|