[svn-upgrade] new version fosdem-schedule (0.4.1+svn262+ds)
[debian/fosdem-schedule.git] / src / mvc / track.cpp
1 /*
2  * Copyright (C) 2010 Ixonos Plc.
3  *
4  * This file is part of fosdem-schedule.
5  *
6  * fosdem-schedule is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation, either version 2 of the License, or (at your option)
9  * any later version.
10  *
11  * fosdem-schedule is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * fosdem-schedule.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "track.h"
21
22 QString const Track::sTableName = QString("track");
23 int const Track::sTableColCount = 2;
24 const QString Track::NAME = "name";
25
26 QSqlRecord const Track::sColumns = Track::toRecord(QList<QSqlField>()
27     << QSqlField("id", QVariant::Int)
28     << QSqlField(NAME, QVariant::String));
29
30 class TrackInsertException : OrmSqlException
31 {
32 public:
33     TrackInsertException(const QString& text) : OrmSqlException(text) {}
34 };
35
36 int Track::insert()
37 {
38     QSqlQuery query;
39     query.prepare("INSERT INTO " + sTableName + " (" + NAME + ")" + " VALUES " + "(\"" + name() + "\")");
40     if (!query.exec())
41     {
42         throw TrackInsertException("Exec Error");
43     }
44     QVariant variant = query.lastInsertId();
45     if (variant.isValid())
46         return variant.toInt();
47     else
48         throw TrackInsertException("Last Insert Id Error");
49 }
50
51 Track Track::retrieveByName(QString name)
52 {
53     QSqlQuery query;
54     query.prepare(
55             selectQuery()
56             + QString("WHERE %1.name = :name").arg(sTableName));
57     query.bindValue(":name", name);
58     return loadOne(query);
59 }
60
61 QList<Track> Track::getAll()
62 {
63     QSqlQuery query;
64     query.prepare(selectQuery());
65     return load(query);
66 }
67
68 Track Track::retrieve(int id)
69 {
70     QSqlQuery query;
71     query.prepare(selectQuery()
72             + QString("WHERE %1.id = :id").arg(sTableName));
73     query.bindValue(":id", id);
74     return loadOne(query);
75 }
76
77 QString Track::retrieveTrackName(int id)
78 {
79     Track track = retrieve(id);
80     return track.name();
81 }