source: src/sql/sqlengine.cpp @ f9db452

qt5
Last change on this file since f9db452 was b6cd05c, checked in by pavelpa <pavelpa@…>, 13 years ago

added 'alarm' columnt to the 'EVENT' table to signalize
that the event has/hasn't alarm set

  • Property mode set to 100644
File size: 11.1 KB
Line 
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
13const QString DATE_FORMAT ("yyyy-MM-dd");
14const QString TIME_FORMAT ("hh:mm");
15
16SqlEngine::SqlEngine(QObject *aParent)
17    : QObject(aParent)
18{
19}
20
21SqlEngine::~SqlEngine()
22{
23}
24
25QString 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
48void SqlEngine::initialize()
49{
50    QString databaseName;
51    if(!QDir::home().exists(".fosdem"))
52        QDir::home().mkdir(".fosdem");
53    databaseName = QDir::homePath() + "/.fosdem/" + "fosdem.sqlite";
54qDebug()<<databaseName;
55    login("QSQLITE",databaseName);
56}
57
58void 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(QDateTime(QDate::fromString(aConference["start"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t()) \
71                         .arg(QDateTime(QDate::fromString(aConference["end"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t()) \
72                         .arg(aConference["days"]) \
73                         .arg(-QTime::fromString(aConference["day_change"],TIME_FORMAT).secsTo(QTime(0,0))) \
74                         .arg(-QTime::fromString(aConference["timeslot_duration"],TIME_FORMAT).secsTo(QTime(0,0)));
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
82void 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        QDateTime startDateTime = QDateTime(QDate::fromString(aEvent["date"],DATE_FORMAT),QTime::fromString(aEvent["start"],TIME_FORMAT));
93        QString values = QString("'%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8', '%9'") \
94                         .arg(aEvent["conference_id"]) \
95                         .arg(aEvent["id"]) \
96                         .arg(QString::number(startDateTime.toTime_t())) \
97                         .arg(-QTime::fromString(aEvent["duration"],TIME_FORMAT).secsTo(QTime(0,0))) \
98                         .arg("123456") \
99                         .arg(aEvent["type"]) \
100                         .arg(aEvent["language"]) \
101                         .arg("0") \
102                         .arg("0");
103
104        QString query = QString("INSERT INTO EVENT (xid_conference, id, start, duration, xid_activity, type, language, favourite, alarm) VALUES (%1)").arg(values);
105        QSqlQuery result (query, db);
106        //LOG_AUTOTEST(query);
107
108        // add some(text related) Event's items to VIRTUAL_EVENT table
109        QString values2 = QString("'%1', '%2', '%3', ? , ? , ? , ? ") \
110                          .arg(aEvent["conference_id"]) \
111                          .arg(aEvent["id"]) \
112                          .arg(aEvent["tag"]);
113
114        QString query2 = QString("INSERT INTO VIRTUAL_EVENT (xid_conference, id, tag, title, subtitle, abstract, description) VALUES (%1)").arg(values2);
115
116        QSqlQuery result2;
117        result2.prepare(query2);
118        result2.bindValue(0,aEvent["title"]);
119        result2.bindValue(1,aEvent["subtitle"]);
120        result2.bindValue(2,aEvent["abstract"]);
121        result2.bindValue(3,aEvent["description"]);
122        result2.exec();
123        //LOG_AUTOTEST(query2);
124    }
125}
126
127
128void SqlEngine::addPersonToDB(QHash<QString,QString> &aPerson)
129{
130    QSqlDatabase db = QSqlDatabase::database();
131
132    //TODO: check if the person doesn't exist before inserting
133    if (db.isValid() && db.isOpen())
134    {
135        QString values = QString("'%1', '%2'").arg(aPerson["id"],aPerson["name"]);
136        QString query = QString("INSERT INTO PERSON (id,name) VALUES (%1)").arg(values);
137        QSqlQuery result (query, db);
138        //LOG_AUTOTEST(query);
139
140        values = QString("'%1', '%2', '%3'").arg(aPerson["conference_id"],aPerson["event_id"],aPerson["id"]);
141        query = QString("INSERT INTO EVENT_PERSON (xid_conference,xid_event,xid_person) VALUES (%1)").arg(values);
142        QSqlQuery resultEventPerson (query, db);
143        //LOG_AUTOTEST(query);
144    }
145}
146
147void SqlEngine::addRoomToDB(QHash<QString,QString> &aRoom)
148{
149    QSqlDatabase db = QSqlDatabase::database();
150
151    if (db.isValid() && db.isOpen())
152    {
153        QString queryExist = QString("SELECT id FROM ROOM WHERE name='%1'").arg(aRoom["name"]);
154        QSqlQuery resultExist(queryExist,db);
155        // now we have to check whether ROOM record with 'name' exists or not,
156        // - if it doesn't exist yet, then we have to add that record to 'ROOM' table
157        //   and assign autoincremented 'id' to aRoom
158        // - if it exists, then we need to get its 'id' and assign it to aRoom
159        int roomId = -1;
160        if(resultExist.next()) // ROOM record with 'name' already exists: we need to get its 'id'
161        {
162            roomId = resultExist.value(0).toInt();
163        }
164        else // ROOM record doesn't exist yet, need to create it
165        {
166            QString values = QString("'%1', '%2'").arg(aRoom["name"],aRoom["picture"]);
167            QString query = QString("INSERT INTO ROOM (name,picture) VALUES (%1)").arg(values);
168            QSqlQuery result (query, db);
169            roomId = result.lastInsertId().toInt(); // 'id' is assigned automatically
170            //LOG_AUTOTEST(query);
171        }
172
173        QString values = QString("'%1', '%2', '%3'").arg(aRoom["conference_id"],aRoom["event_id"],QString::number(roomId));
174        QString query = QString("INSERT INTO EVENT_ROOM (xid_conference,xid_event,xid_room) VALUES (%1)").arg(values);
175        QSqlQuery result (query, db);
176        //LOG_AUTOTEST(query);
177    }
178}
179
180void SqlEngine::addLinkToDB(QHash<QString,QString> &aLink)
181{
182    QSqlDatabase db = QSqlDatabase::database();
183
184    //TODO: check if the link doesn't exist before inserting
185    if (db.isValid() && db.isOpen())
186    {
187        QString values = QString("'%1', '%2', '%3', '%4'").arg(aLink["event_id"],aLink["conference_id"],aLink["name"],aLink["url"]);
188        QString query = QString("INSERT INTO LINK (xid_event, xid_conference, name, url) VALUES (%1)").arg(values);
189        QSqlQuery result(query, db);
190        //LOG_AUTOTEST(query);
191    }
192}
193
194bool SqlEngine::createTables(QSqlDatabase &aDatabase)
195{
196    bool result = aDatabase.open();
197
198    if (aDatabase.isValid() && aDatabase.isOpen())
199    {
200        QSqlQuery query(aDatabase);
201
202        query.exec("CREATE TABLE CONFERENCE ( \
203            id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , \
204            title VARCHAR NOT NULL , \
205            subtitle VARCHAR, \
206            venue VARCHAR, \
207            city VARCHAR NOT NULL , \
208            start INTEGER NOT NULL , \
209            end INTEGER NOT NULL , \
210            days INTEGER, \
211            day_change INTEGER, \
212            timeslot_duration INTEGER)");
213
214        query.exec("CREATE TABLE ACTIVITY ( \
215            id INTEGER  PRIMARY KEY AUTOINCREMENT  NOT NULL , \
216            name VARCHAR NOT NULL )");
217
218        query.exec("CREATE TABLE ROOM ( \
219            id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , \
220            name VARCHAR NOT NULL , \
221            picture VARCHAR NOT NULL)");
222
223        query.exec("CREATE TABLE PERSON ( \
224            id INTEGER PRIMARY KEY  NOT NULL , \
225            name VARCHAR NOT NULL)");
226
227        query.exec("CREATE TABLE EVENT ( \
228            xid_conference INTEGER  NOT NULL, \
229            id INTEGER NOT NULL , \
230            start INTEGER NOT NULL , \
231            duration INTEGER NOT NULL , \
232            xid_activity INTEGER NOT NULL REFERENCES ACTIVITY(id), \
233            type VARCHAR, \
234            language VARCHAR, \
235            favourite INTEGER DEFAULT 0, \
236            alarm INTEGER DEFAULT 0, \
237            PRIMARY KEY (xid_conference,id), \
238            FOREIGN KEY(xid_conference) REFERENCES CONFERENCE(id) \
239            FOREIGN KEY(xid_activity) REFERENCES ACTIVITY(id))");
240
241#ifdef MAEMO
242        // TBD: MAEMO Virtual tables compatibility (waiting for Marek).
243        // MAEMO sqlite Qt driver doesn't provide FTS support by default - use the following HACK
244        query.exec("CREATE TABLE VIRTUAL_EVENT ( \
245            xid_conference INTEGER  NOT NULL, \
246            id INTEGER NOT NULL , \
247            tag VARCHAR,title VARCHAR NOT NULL , \
248            subtitle VARCHAR, \
249            abstract VARCHAR, \
250            description VARCHAR, \
251            PRIMARY KEY (xid_conference,id))");
252#else
253        query.exec("CREATE VIRTUAL TABLE VIRTUAL_EVENT using fts3 ( \
254            xid_conference INTEGER  NOT NULL, \
255            id INTEGER NOT NULL , \
256            tag VARCHAR,title VARCHAR NOT NULL , \
257            subtitle VARCHAR, \
258            abstract VARCHAR, \
259            description VARCHAR, \
260            PRIMARY KEY (xid_conference,id))");
261#endif
262
263        query.exec("CREATE TABLE EVENT_PERSON ( \
264            xid_conference INTEGER NOT NULL , \
265            xid_event INTEGER NOT NULL , \
266            xid_person INTEGER NOT NULL, \
267            FOREIGN KEY(xid_conference, xid_event) REFERENCES EVENT(xid_conference, id), \
268            FOREIGN KEY(xid_person) REFERENCES PERSON(id))");
269
270        query.exec("CREATE TABLE EVENT_ROOM ( \
271            xid_conference INTEGER NOT NULL , \
272            xid_event INTEGER NOT NULL , \
273            xid_room INTEGER NOT NULL, \
274            FOREIGN KEY(xid_conference, xid_event) REFERENCES EVENT(xid_conference, id), \
275            FOREIGN KEY(xid_room) REFERENCES ROOM(id))");
276
277        query.exec("CREATE TABLE LINK ( \
278            id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL, \
279            xid_conference INTEGER NOT NULL, \
280            xid_event INTEGER NOT NULL, \
281            name VARCHAR, \
282            url VARCHAR NOT NULL, \
283            FOREIGN KEY(xid_conference, xid_event) REFERENCES EVENT(xid_conference, id))");
284    }
285    else
286    {
287        //LOG_WARNING("Database is not opened");
288    }
289
290    return result;
291}
292
Note: See TracBrowser for help on using the repository browser.