source: src/sql/sqlengine.cpp @ 27102d5

qt5
Last change on this file since 27102d5 was 27102d5, checked in by fortefr <fortefr@…>, 13 years ago

Temporal virtual_event change

  • Property mode set to 100644
File size: 11.0 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'") \
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"); // not favourite when added
102
103        QString query = QString("INSERT INTO EVENT (xid_conference, id, start, duration, xid_activity, type, language, favourite) 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
124void SqlEngine::addPersonToDB(QHash<QString,QString> &aPerson)
125{
126    QSqlDatabase db = QSqlDatabase::database();
127
128    //TODO: check if the person doesn't exist before inserting
129    if (db.isValid() && db.isOpen())
130    {
131        QString values = QString("'%1', '%2'").arg(aPerson["id"],aPerson["name"]);
132        QString query = QString("INSERT INTO PERSON (id,name) VALUES (%1)").arg(values);
133        QSqlQuery result (query, db);
134        //LOG_AUTOTEST(query);
135
136        values = QString("'%1', '%2', '%3'").arg(aPerson["conference_id"],aPerson["event_id"],aPerson["id"]);
137        query = QString("INSERT INTO EVENT_PERSON (xid_conference,xid_event,xid_person) VALUES (%1)").arg(values);
138        QSqlQuery resultEventPerson (query, db);
139        //LOG_AUTOTEST(query);
140    }
141}
142
143void SqlEngine::addRoomToDB(QHash<QString,QString> &aRoom)
144{
145    QSqlDatabase db = QSqlDatabase::database();
146
147    if (db.isValid() && db.isOpen())
148    {
149        QString queryExist = QString("SELECT id FROM ROOM WHERE name='%1'").arg(aRoom["name"]);
150        QSqlQuery resultExist(queryExist,db);
151        // now we have to check whether ROOM record with 'name' exists or not,
152        // - if it doesn't exist yet, then we have to add that record to 'ROOM' table
153        //   and assign autoincremented 'id' to aRoom
154        // - if it exists, then we need to get its 'id' and assign it to aRoom
155        int roomId = -1;
156        if(resultExist.next()) // ROOM record with 'name' already exists: we need to get its 'id'
157        {
158            roomId = resultExist.value(0).toInt();
159        }
160        else // ROOM record doesn't exist yet, need to create it
161        {
162            QString values = QString("'%1', '%2'").arg(aRoom["name"],aRoom["picture"]);
163            QString query = QString("INSERT INTO ROOM (name,picture) VALUES (%1)").arg(values);
164            QSqlQuery result (query, db);
165            roomId = result.lastInsertId().toInt(); // 'id' is assigned automatically
166            //LOG_AUTOTEST(query);
167        }
168
169        QString values = QString("'%1', '%2', '%3'").arg(aRoom["conference_id"],aRoom["event_id"],QString::number(roomId));
170        QString query = QString("INSERT INTO EVENT_ROOM (xid_conference,xid_event,xid_room) VALUES (%1)").arg(values);
171        QSqlQuery result (query, db);
172        //LOG_AUTOTEST(query);
173    }
174}
175
176void SqlEngine::addLinkToDB(QHash<QString,QString> &aLink)
177{
178    QSqlDatabase db = QSqlDatabase::database();
179
180    //TODO: check if the link doesn't exist before inserting
181    if (db.isValid() && db.isOpen())
182    {
183        QString values = QString("'%1', '%2', '%3', '%4'").arg(aLink["event_id"],aLink["conference_id"],aLink["name"],aLink["url"]);
184        QString query = QString("INSERT INTO LINK (xid_event, xid_conference, name, url) VALUES (%1)").arg(values);
185        QSqlQuery result(query, db);
186        //LOG_AUTOTEST(query);
187    }
188}
189
190bool SqlEngine::createTables(QSqlDatabase &aDatabase)
191{
192    bool result = aDatabase.open();
193
194    if (aDatabase.isValid() && aDatabase.isOpen())
195    {
196        QSqlQuery query(aDatabase);
197
198        query.exec("CREATE TABLE CONFERENCE ( \
199            id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , \
200            title VARCHAR NOT NULL , \
201            subtitle VARCHAR, \
202            venue VARCHAR, \
203            city VARCHAR NOT NULL , \
204            start INTEGER NOT NULL , \
205            end INTEGER NOT NULL , \
206            days INTEGER, \
207            day_change INTEGER, \
208            timeslot_duration INTEGER)");
209
210        query.exec("CREATE TABLE ACTIVITY ( \
211            id INTEGER  PRIMARY KEY AUTOINCREMENT  NOT NULL , \
212            name VARCHAR NOT NULL )");
213
214        query.exec("CREATE TABLE ROOM ( \
215            id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , \
216            name VARCHAR NOT NULL , \
217            picture VARCHAR NOT NULL)");
218
219        query.exec("CREATE TABLE PERSON ( \
220            id INTEGER PRIMARY KEY  NOT NULL , \
221            name VARCHAR NOT NULL)");
222
223        query.exec("CREATE TABLE EVENT ( \
224            xid_conference INTEGER  NOT NULL, \
225            id INTEGER NOT NULL , \
226            start INTEGER NOT NULL , \
227            duration INTEGER NOT NULL , \
228            xid_activity INTEGER NOT NULL REFERENCES ACTIVITY(id), \
229            type VARCHAR, \
230            language VARCHAR, \
231            favourite INTEGER DEFAULT 0, \
232            PRIMARY KEY (xid_conference,id), \
233            FOREIGN KEY(xid_conference) REFERENCES CONFERENCE(id) \
234            FOREIGN KEY(xid_activity) REFERENCES ACTIVITY(id))");
235
236        // TBD Virtual tables compatibility (waiting for Marek). Temporary non virtual VIRTUAL_TABLE below: To be deleted
237/*        query.exec("CREATE VIRTUAL TABLE VIRTUAL_EVENT using fts3 ( \
238            xid_conference INTEGER  NOT NULL, \
239            id INTEGER NOT NULL , \
240            tag VARCHAR,title VARCHAR NOT NULL , \
241            subtitle VARCHAR, \
242            abstract VARCHAR, \
243            description VARCHAR, \
244            PRIMARY KEY (xid_conference,id))");
245*/
246        query.exec("CREATE TABLE VIRTUAL_EVENT ( \
247            xid_conference INTEGER  NOT NULL, \
248            id INTEGER NOT NULL , \
249            tag VARCHAR,title VARCHAR NOT NULL , \
250            subtitle VARCHAR, \
251            abstract VARCHAR, \
252            description VARCHAR, \
253            PRIMARY KEY (xid_conference,id))");
254
255        query.exec("CREATE TABLE EVENT_PERSON ( \
256            xid_conference INTEGER NOT NULL , \
257            xid_event INTEGER NOT NULL , \
258            xid_person INTEGER NOT NULL, \
259            FOREIGN KEY(xid_conference, xid_event) REFERENCES EVENT(xid_conference, id), \
260            FOREIGN KEY(xid_person) REFERENCES PERSON(id))");
261
262        query.exec("CREATE TABLE EVENT_ROOM ( \
263            xid_conference INTEGER NOT NULL , \
264            xid_event INTEGER NOT NULL , \
265            xid_room INTEGER NOT NULL, \
266            FOREIGN KEY(xid_conference, xid_event) REFERENCES EVENT(xid_conference, id), \
267            FOREIGN KEY(xid_room) REFERENCES ROOM(id))");
268
269        query.exec("CREATE TABLE LINK ( \
270            id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL, \
271            xid_conference INTEGER NOT NULL, \
272            xid_event INTEGER NOT NULL, \
273            name VARCHAR, \
274            url VARCHAR NOT NULL, \
275            FOREIGN KEY(xid_conference, xid_event) REFERENCES EVENT(xid_conference, id))");
276    }
277    else
278    {
279        //LOG_WARNING("Database is not opened");
280    }
281
282    return result;
283}
284
Note: See TracBrowser for help on using the repository browser.