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 = 3; |
---|
24 | const QString Track::CONFERENCEID = "xid_conference"; |
---|
25 | const QString Track::NAME = "name"; |
---|
26 | |
---|
27 | QSqlRecord const Track::sColumns = Track::toRecord(QList<QSqlField>() |
---|
28 | << QSqlField("id", QVariant::Int) |
---|
29 | << QSqlField(CONFERENCEID, QVariant::Int) |
---|
30 | << QSqlField(NAME, QVariant::String)); |
---|
31 | |
---|
32 | class TrackInsertException : OrmSqlException |
---|
33 | { |
---|
34 | public: |
---|
35 | TrackInsertException(const QString& text) : OrmSqlException(text) {} |
---|
36 | }; |
---|
37 | |
---|
38 | int Track::insert() |
---|
39 | { |
---|
40 | QSqlQuery query; |
---|
41 | query.prepare("INSERT INTO " + sTableName + " (" + CONFERENCEID + "," + NAME + ")" + " VALUES " + "(\"" + QString::number(conferenceid()) + "\",\"" + name() + "\")"); |
---|
42 | if (!query.exec()) |
---|
43 | { |
---|
44 | throw TrackInsertException("Exec Error"); |
---|
45 | } |
---|
46 | QVariant variant = query.lastInsertId(); |
---|
47 | if (variant.isValid()) |
---|
48 | return variant.toInt(); |
---|
49 | else |
---|
50 | throw TrackInsertException("Last Insert Id Error"); |
---|
51 | } |
---|
52 | |
---|
53 | Track Track::retrieveByName(QString name) |
---|
54 | { |
---|
55 | QSqlQuery query; |
---|
56 | query.prepare( |
---|
57 | selectQuery() |
---|
58 | + QString("WHERE %1.name = :name").arg(sTableName)); |
---|
59 | query.bindValue(":name", name); |
---|
60 | return loadOne(query); |
---|
61 | } |
---|
62 | |
---|
63 | QList<Track> Track::getAll() |
---|
64 | { |
---|
65 | QSqlQuery query; |
---|
66 | query.prepare(selectQuery()); |
---|
67 | return load(query); |
---|
68 | } |
---|
69 | |
---|
70 | Track Track::retrieve(int id) |
---|
71 | { |
---|
72 | QSqlQuery query; |
---|
73 | query.prepare(selectQuery() |
---|
74 | + QString("WHERE %1.id = :id").arg(sTableName)); |
---|
75 | query.bindValue(":id", id); |
---|
76 | return loadOne(query); |
---|
77 | } |
---|
78 | |
---|
79 | QString Track::retrieveTrackName(int id) |
---|
80 | { |
---|
81 | Track track = retrieve(id); |
---|
82 | return track.name(); |
---|
83 | } |
---|