source: src/sql/sqlengine.cpp @ 69393c0

qt5
Last change on this file since 69393c0 was 69393c0, checked in by pavelpa <pavelpa@…>, 12 years ago

implemented 'conference' record for accessing info about the conference

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