13 const QString DATE_FORMAT ("yyyy-MM-dd");
14 const QString TIME_FORMAT ("hh:mm");
16 SqlEngine::SqlEngine(QObject *aParent)
21 SqlEngine::~SqlEngine()
25 QString SqlEngine::login(const QString &aDatabaseType, const QString &aDatabaseName)
27 QSqlDatabase database = QSqlDatabase::addDatabase(aDatabaseType);
28 database.setDatabaseName(aDatabaseName);
31 if(!QFile::exists(aDatabaseName)) // the DB (tables) doesn't exists, and so we have to create one
33 // creating empty DB + tables
34 // ??? what is the best way of creating new empty DB ???
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);
43 //LOG_INFO(QString("Opening '%1' database '%2'").arg(aDatabaseType).arg(aDatabaseName));
45 return result ? QString() : database.lastError().text();
48 void SqlEngine::initialize()
51 if(!QDir::home().exists(".fosdem"))
52 QDir::home().mkdir(".fosdem");
53 databaseName = QDir::homePath() + "/.fosdem/" + "fosdem.sqlite";
54 login("QSQLITE",databaseName);
57 void SqlEngine::addConferenceToDB(QHash<QString,QString> &aConference)
59 QSqlDatabase db = QSqlDatabase::database();
61 if (db.isValid() && db.isOpen())
63 QString values = QString("'%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8', '%9', '%10'") \
64 .arg(aConference["id"]) \
65 .arg(aConference["title"]) \
66 .arg(aConference["subtitle"]) \
67 .arg(aConference["venue"]) \
68 .arg(aConference["city"]) \
69 .arg(QDateTime(QDate::fromString(aConference["start"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t()) \
70 .arg(QDateTime(QDate::fromString(aConference["end"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t()) \
71 .arg(aConference["days"]) \
72 .arg(-QTime::fromString(aConference["day_change"],TIME_FORMAT).secsTo(QTime(0,0))) \
73 .arg(-QTime::fromString(aConference["timeslot_duration"],TIME_FORMAT).secsTo(QTime(0,0)));
75 QString query = QString("INSERT INTO CONFERENCE (id,title,subtitle,venue,city,start,end,days,day_change,timeslot_duration) VALUES (%1)").arg(values);
76 QSqlQuery result (query, db);
77 //LOG_AUTOTEST(query);
81 void SqlEngine::addEventToDB(QHash<QString,QString> &aEvent)
83 //LOG_DEBUG(QString("Adding event '%1' to DB").arg(*aEvent));
85 QSqlDatabase db = QSqlDatabase::database();
87 if (db.isValid() && db.isOpen())
89 // track/activity has to be handled as the first, since it is necessary to get
90 // track ID from the ACTIVITY table, or to create new ACTIVITY record
91 // and get the ID from newly created record
92 QString queryExist = QString("SELECT id FROM activity WHERE name='%1'").arg(aEvent["track"]);
93 QSqlQuery resultExist(queryExist,db);
94 // now we have to check whether ACTIVITY record with 'name' exists or not,
95 // - if it doesn't exist yet, then we have to add that record to 'ACTIVITY' table
96 // and assign autoincremented 'id' to aActivity
97 // - if it exists, then we need to get its 'id' and assign it to aRoom
99 if(resultExist.next()) // ACTIVITY record with 'name' already exists: we need to get its 'id'
101 actId = resultExist.value(0).toInt();
103 else // ACTIVITY record doesn't exist yet, need to create it
105 QString values = QString("'%1'").arg(aEvent["track"]);
106 QString query = QString("INSERT INTO activity (name) VALUES (%1)").arg(values);
107 QSqlQuery result (query, db);
108 actId = result.lastInsertId().toInt(); // 'id' is assigned automatically
111 // The items of the Event are divided into the two tables EVENT and VIRTUAL_EVENT
112 // VIRTUAL_EVENT is for Full-Text-Serach Support
113 QDateTime startDateTime = QDateTime(QDate::fromString(aEvent["date"],DATE_FORMAT),QTime::fromString(aEvent["start"],TIME_FORMAT));
114 QString values = QString("'%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8', '%9'") \
115 .arg(aEvent["conference_id"]) \
117 .arg(QString::number(startDateTime.toTime_t())) \
118 .arg(-QTime::fromString(aEvent["duration"],TIME_FORMAT).secsTo(QTime(0,0))) \
119 .arg(QString::number(actId)) \
120 .arg(aEvent["type"]) \
121 .arg(aEvent["language"]) \
125 QString query = QString("INSERT INTO EVENT (xid_conference, id, start, duration, xid_activity, type, language, favourite, alarm) VALUES (%1)").arg(values);
126 QSqlQuery result (query, db);
127 //LOG_AUTOTEST(query);
129 // add some(text related) Event's items to VIRTUAL_EVENT table
130 QString values2 = QString("'%1', '%2', '%3', ? , ? , ? , ? ") \
131 .arg(aEvent["conference_id"]) \
135 QString query2 = QString("INSERT INTO VIRTUAL_EVENT (xid_conference, id, tag, title, subtitle, abstract, description) VALUES (%1)").arg(values2);
138 result2.prepare(query2);
139 result2.bindValue(0,aEvent["title"]);
140 result2.bindValue(1,aEvent["subtitle"]);
141 result2.bindValue(2,aEvent["abstract"]);
142 result2.bindValue(3,aEvent["description"]);
144 //LOG_AUTOTEST(query2);
149 void SqlEngine::addPersonToDB(QHash<QString,QString> &aPerson)
151 QSqlDatabase db = QSqlDatabase::database();
153 //TODO: check if the person doesn't exist before inserting
154 if (db.isValid() && db.isOpen())
156 QString values = QString("'%1', '%2'").arg(aPerson["id"],aPerson["name"]);
157 QString query = QString("INSERT INTO PERSON (id,name) VALUES (%1)").arg(values);
158 QSqlQuery result (query, db);
159 //LOG_AUTOTEST(query);
161 values = QString("'%1', '%2', '%3'").arg(aPerson["conference_id"],aPerson["event_id"],aPerson["id"]);
162 query = QString("INSERT INTO EVENT_PERSON (xid_conference,xid_event,xid_person) VALUES (%1)").arg(values);
163 QSqlQuery resultEventPerson (query, db);
164 //LOG_AUTOTEST(query);
168 void SqlEngine::addRoomToDB(QHash<QString,QString> &aRoom)
170 QSqlDatabase db = QSqlDatabase::database();
172 if (db.isValid() && db.isOpen())
174 QString queryExist = QString("SELECT id FROM ROOM WHERE name='%1'").arg(aRoom["name"]);
175 QSqlQuery resultExist(queryExist,db);
176 // now we have to check whether ROOM record with 'name' exists or not,
177 // - if it doesn't exist yet, then we have to add that record to 'ROOM' table
178 // and assign autoincremented 'id' to aRoom
179 // - if it exists, then we need to get its 'id' and assign it to aRoom
181 if(resultExist.next()) // ROOM record with 'name' already exists: we need to get its 'id'
183 roomId = resultExist.value(0).toInt();
185 else // ROOM record doesn't exist yet, need to create it
187 QString values = QString("'%1', '%2'").arg(aRoom["name"],aRoom["picture"]);
188 QString query = QString("INSERT INTO ROOM (name,picture) VALUES (%1)").arg(values);
189 QSqlQuery result (query, db);
190 roomId = result.lastInsertId().toInt(); // 'id' is assigned automatically
191 //LOG_AUTOTEST(query);
194 QString values = QString("'%1', '%2', '%3'").arg(aRoom["conference_id"],aRoom["event_id"],QString::number(roomId));
195 QString query = QString("INSERT INTO EVENT_ROOM (xid_conference,xid_event,xid_room) VALUES (%1)").arg(values);
196 QSqlQuery result (query, db);
197 //LOG_AUTOTEST(query);
201 void SqlEngine::addLinkToDB(QHash<QString,QString> &aLink)
203 QSqlDatabase db = QSqlDatabase::database();
205 //TODO: check if the link doesn't exist before inserting
206 if (db.isValid() && db.isOpen())
208 QString values = QString("'%1', '%2', '%3', '%4'").arg(aLink["event_id"],aLink["conference_id"],aLink["name"],aLink["url"]);
209 QString query = QString("INSERT INTO LINK (xid_event, xid_conference, name, url) VALUES (%1)").arg(values);
210 QSqlQuery result(query, db);
211 //LOG_AUTOTEST(query);
215 bool SqlEngine::createTables(QSqlDatabase &aDatabase)
217 bool result = aDatabase.open();
219 if (aDatabase.isValid() && aDatabase.isOpen())
221 QSqlQuery query(aDatabase);
223 query.exec("CREATE TABLE CONFERENCE ( \
224 id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , \
225 title VARCHAR NOT NULL , \
228 city VARCHAR NOT NULL , \
229 start INTEGER NOT NULL , \
230 end INTEGER NOT NULL , \
232 day_change INTEGER, \
233 timeslot_duration INTEGER)");
235 query.exec("CREATE TABLE ACTIVITY ( \
236 id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , \
237 name VARCHAR NOT NULL )");
239 query.exec("CREATE TABLE ROOM ( \
240 id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , \
241 name VARCHAR NOT NULL , \
242 picture VARCHAR NOT NULL)");
244 query.exec("CREATE TABLE PERSON ( \
245 id INTEGER PRIMARY KEY NOT NULL , \
246 name VARCHAR NOT NULL)");
248 query.exec("CREATE TABLE EVENT ( \
249 xid_conference INTEGER NOT NULL, \
250 id INTEGER NOT NULL , \
251 start INTEGER NOT NULL , \
252 duration INTEGER NOT NULL , \
253 xid_activity INTEGER NOT NULL REFERENCES ACTIVITY(id), \
256 favourite INTEGER DEFAULT 0, \
257 alarm INTEGER DEFAULT 0, \
258 PRIMARY KEY (xid_conference,id), \
259 FOREIGN KEY(xid_conference) REFERENCES CONFERENCE(id) \
260 FOREIGN KEY(xid_activity) REFERENCES ACTIVITY(id))");
263 // TBD: MAEMO Virtual tables compatibility (waiting for Marek).
264 // MAEMO sqlite Qt driver doesn't provide FTS support by default - use the following HACK
265 query.exec("CREATE TABLE VIRTUAL_EVENT ( \
266 xid_conference INTEGER NOT NULL, \
267 id INTEGER NOT NULL , \
268 tag VARCHAR,title VARCHAR NOT NULL , \
271 description VARCHAR, \
272 PRIMARY KEY (xid_conference,id))");
274 query.exec("CREATE VIRTUAL TABLE VIRTUAL_EVENT using fts3 ( \
275 xid_conference INTEGER NOT NULL, \
276 id INTEGER NOT NULL , \
277 tag VARCHAR,title VARCHAR NOT NULL , \
280 description VARCHAR, \
281 PRIMARY KEY (xid_conference,id))");
284 query.exec("CREATE TABLE EVENT_PERSON ( \
285 xid_conference INTEGER NOT NULL , \
286 xid_event INTEGER NOT NULL , \
287 xid_person INTEGER NOT NULL, \
288 FOREIGN KEY(xid_conference, xid_event) REFERENCES EVENT(xid_conference, id), \
289 FOREIGN KEY(xid_person) REFERENCES PERSON(id))");
291 query.exec("CREATE TABLE EVENT_ROOM ( \
292 xid_conference INTEGER NOT NULL , \
293 xid_event INTEGER NOT NULL , \
294 xid_room INTEGER NOT NULL, \
295 FOREIGN KEY(xid_conference, xid_event) REFERENCES EVENT(xid_conference, id), \
296 FOREIGN KEY(xid_room) REFERENCES ROOM(id))");
298 query.exec("CREATE TABLE LINK ( \
299 id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, \
300 xid_conference INTEGER NOT NULL, \
301 xid_event INTEGER NOT NULL, \
303 url VARCHAR NOT NULL, \
304 FOREIGN KEY(xid_conference, xid_event) REFERENCES EVENT(xid_conference, id))");
308 //LOG_WARNING("Database is not opened");
314 int SqlEngine::searchEvent(int aConferenceId, const QList<QString> &aColumns, const QString &aKeyword)
316 QSqlDatabase db = QSqlDatabase::database();
318 if ( !db.isValid() || !db.isOpen())
321 QString query = QString(
322 "DROP TABLE IF EXISTS SEARCH_EVENT;"
323 "CREATE TEMP TABLE SEARCH_EVENT ( xid_conference INTEGER NOT NULL, id INTEGER NOT NULL );"
324 "INSERT INTO SEARCH_EVENT ( xid_conference, id) "
325 "SELECT xid_conference, id FROM EVENT AS e INNER JOIN VIRTUAL_EVENT AS ve USING (xid_conference, id) "
326 "WHERE xid_conference = %1 AND (").arg( aConferenceId );
329 foreach (QString str, aColumns){
330 query += QString("%1 LIKE '\%%2\%' OR ").arg( aColumns.at(i++), aKeyword );
332 query.chop( QString(" OR ").length() );
333 query += QString(");");
335 qDebug() << "\nSQL: " << query;
339 if( db.lastError().isValid() && db.lastError().number() != 0 ){
340 qDebug() << "SQL ERR: " << db.lastError().number() << ", " << db.lastError().text();
344 qDebug() << "SQL OK.\n";