source: src/mvc/track.cpp @ 81e085d

qt5
Last change on this file since 81e085d was 81e085d, checked in by gregor herrmann <gregor@…>, 5 years ago

TrackInsertException?: make error message useful.

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