source: src/mvc/track.cpp @ 005e2b7

qt5
Last change on this file since 005e2b7 was 005e2b7, checked in by korrco <korrco@…>, 12 years ago

caching removed

  • Property mode set to 100644
File size: 1.6 KB
RevLine 
[4693fa6]1/*
[606c155]2 * activity.cpp
[4693fa6]3 *
4 *  Created on: Dec 27, 2009
5 *      Author: Pavol Korinek
6 */
7
8#include "track.h"
[606c155]9#include <QSqlTableModel>
[4693fa6]10
11QString const Track::sTableName = QString("track");
12int const Track::sTableColCount = 2;
[606c155]13const QString Track::NAME = "name";
[4693fa6]14
15QSqlRecord const Track::sColumns = Track::toRecord(QList<QSqlField>()
16    << QSqlField("id", QVariant::Int)
[606c155]17    << QSqlField(NAME, QVariant::String));
[4693fa6]18
[005e2b7]19//QMap<int, Track> Track::mIdToTrack;
[4693fa6]20
[606c155]21class TrackInsertException : OrmSqlException
22{
23public:
24    TrackInsertException(const QString& text) : OrmSqlException(text) {}
25};
26
27int Track::insert()
28{
29    QSqlQuery query;
30    query.prepare("INSERT INTO " + sTableName + " (" + NAME + ")" + " VALUES " + "(\"" + name() + "\")");
31    if (!query.exec())
32    {
33        throw TrackInsertException("Exec Error");
34    }
35    QVariant variant = query.lastInsertId();
36    if (variant.isValid())
37        return variant.toInt();
38    else
39        throw TrackInsertException("Last Insert Id Error");
40}
41
42Track Track::retrieveByName(QString name)
43{
44    QSqlQuery query;
45    query.prepare(
46            selectQuery()
47            + QString("WHERE %1.name = :name").arg(sTableName));
48    query.bindValue(":name", name);
49    return loadOne(query);
50}
51
[4693fa6]52QList<Track> Track::getAll()
53{
54    QSqlQuery query;
55    query.prepare(selectQuery());
56    return load(query);
57}
58
[005e2b7]59Track Track::retrieve(int id)
[4693fa6]60{
[005e2b7]61    QSqlQuery query;
62    query.prepare(selectQuery()
63            + QString("WHERE %1.id = :id").arg(sTableName));
64    query.bindValue(":id", id);
65    return loadOne(query);
[4693fa6]66}
67
[005e2b7]68QString Track::retrieveTrackName(int id)
[4693fa6]69{
[005e2b7]70    Track track = retrieve(id);
71    return track.name();
[4693fa6]72}
Note: See TracBrowser for help on using the repository browser.