[ca90cb1] | 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 | */ |
---|
[4693fa6] | 19 | |
---|
| 20 | #include "track.h" |
---|
| 21 | |
---|
| 22 | QString const Track::sTableName = QString("track"); |
---|
[51529bd] | 23 | int const Track::sTableColCount = 3; |
---|
| 24 | const QString Track::CONFERENCEID = "xid_conference"; |
---|
[606c155] | 25 | const QString Track::NAME = "name"; |
---|
[4693fa6] | 26 | |
---|
| 27 | QSqlRecord const Track::sColumns = Track::toRecord(QList<QSqlField>() |
---|
| 28 | << QSqlField("id", QVariant::Int) |
---|
[51529bd] | 29 | << QSqlField(CONFERENCEID, QVariant::Int) |
---|
[606c155] | 30 | << QSqlField(NAME, QVariant::String)); |
---|
[4693fa6] | 31 | |
---|
[606c155] | 32 | class TrackInsertException : OrmSqlException |
---|
| 33 | { |
---|
| 34 | public: |
---|
| 35 | TrackInsertException(const QString& text) : OrmSqlException(text) {} |
---|
| 36 | }; |
---|
| 37 | |
---|
| 38 | int Track::insert() |
---|
| 39 | { |
---|
| 40 | QSqlQuery query; |
---|
[51529bd] | 41 | query.prepare("INSERT INTO " + sTableName + " (" + CONFERENCEID + "," + NAME + ")" + " VALUES " + "(\"" + QString::number(conferenceid()) + "\",\"" + name() + "\")"); |
---|
[606c155] | 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 | |
---|
[141a5c2] | 53 | Track Track::retrieveByName(int conferenceid, QString name) |
---|
[606c155] | 54 | { |
---|
| 55 | QSqlQuery query; |
---|
| 56 | query.prepare( |
---|
| 57 | selectQuery() |
---|
[141a5c2] | 58 | + QString("WHERE %1.xid_conference = :xid_conference and %1.name = :name").arg(sTableName)); |
---|
| 59 | query.bindValue(":xid_conference", conferenceid); |
---|
[606c155] | 60 | query.bindValue(":name", name); |
---|
| 61 | return loadOne(query); |
---|
| 62 | } |
---|
| 63 | |
---|
[4693fa6] | 64 | QList<Track> Track::getAll() |
---|
| 65 | { |
---|
| 66 | QSqlQuery query; |
---|
| 67 | query.prepare(selectQuery()); |
---|
| 68 | return load(query); |
---|
| 69 | } |
---|
| 70 | |
---|
[005e2b7] | 71 | Track Track::retrieve(int id) |
---|
[4693fa6] | 72 | { |
---|
[005e2b7] | 73 | QSqlQuery query; |
---|
| 74 | query.prepare(selectQuery() |
---|
| 75 | + QString("WHERE %1.id = :id").arg(sTableName)); |
---|
| 76 | query.bindValue(":id", id); |
---|
| 77 | return loadOne(query); |
---|
[4693fa6] | 78 | } |
---|
| 79 | |
---|
[005e2b7] | 80 | QString Track::retrieveTrackName(int id) |
---|
[4693fa6] | 81 | { |
---|
[005e2b7] | 82 | Track track = retrieve(id); |
---|
| 83 | return track.name(); |
---|
[4693fa6] | 84 | } |
---|