Loading of song data from zionworx
[kworship.git] / kworship / songdb / KwSongdbSong.cpp
blob6ad471cb517424eebb81ab70adae80bca8eea9f0
1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
5 * KWorship is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * KWorship is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with KWorship. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 /**
21 * @file KwSongdbSong.cpp
22 * @brief A song from the database.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwSongdbSong.h"
27 #include "KwSongdbVersion.h"
28 #include "KwSongdb.h"
30 #include <QSqlQuery>
31 #include <QVariant>
32 #include <QStringList>
35 * Constructors + destructor
38 /// Construct a new song for database insertion.
39 KwSongdbSong::KwSongdbSong()
40 : m_id(-1)
41 , m_modifiedFields(Name)
42 , m_name()
43 , m_alternateName()
44 , m_versionsLoaded(false)
45 , m_versionIds()
49 /// Construct from the database.
50 KwSongdbSong::KwSongdbSong(int id)
51 : m_id(id)
52 , m_modifiedFields(0)
53 , m_name()
54 , m_alternateName()
55 , m_versionsLoaded(false)
56 , m_versionIds()
58 // Get the song data
59 QSqlQuery query(KwSongdb::self()->database());
60 query.prepare("SELECT `name`, `alternate_name`, `css_style_sheet_id` "
61 "FROM `Song` "
62 "WHERE `id` = ?");
63 query.addBindValue(QVariant(id));
64 bool worked = query.exec();
65 Q_ASSERT(worked);
67 // Copy the data
68 Q_ASSERT(query.first());
69 m_name = query.value(0).toString();
70 m_alternateName = query.value(1).toString();
72 // Register with songdb
73 KwSongdb::self()->registerSong(this);
76 /// Destructor.
77 KwSongdbSong::~KwSongdbSong()
82 * Accessors
85 /// Get the id.
86 int KwSongdbSong::id() const
88 return m_id;
91 /// Get the name of the song.
92 QString KwSongdbSong::name() const
94 return m_name;
97 /// Get the alternate name of the song.
98 QString KwSongdbSong::alternateName() const
100 return m_alternateName;
103 /// Get list of song versions.
104 QList<KwSongdbVersion*> KwSongdbSong::versions()
106 if (!m_versionsLoaded)
108 // Get the version ids
109 QSqlQuery query(KwSongdb::self()->database());
110 query.prepare("SELECT `id` "
111 "FROM `SongVersion` "
112 "WHERE `song_id` = ?");
113 query.addBindValue(QVariant(m_id));
114 bool worked = query.exec();
115 Q_ASSERT(worked);
117 if (query.first())
119 do {
120 m_versionIds.push_back(query.value(0).toInt());
121 } while (query.next());
124 m_versionsLoaded = true;
126 return KwSongdb::self()->songVersionsByIds(m_versionIds);
130 * Mutators
133 /// Set the name.
134 void KwSongdbSong::setName(const QString& name)
136 if (name != m_name)
138 m_modifiedFields |= Name;
139 m_name = name;
143 /// Set the alternate name.
144 void KwSongdbSong::setAlternateName(const QString& name)
146 if (name != m_alternateName)
148 m_modifiedFields |= AlternateName;
149 m_alternateName = name;
153 /// Save changes to the song data.
154 void KwSongdbSong::save()
156 QStringList fields;
157 QList<QVariant> values;
158 Fields handled;
160 // Straightforward modifications
162 if (m_modifiedFields.testFlag(Name))
164 handled |= Name;
165 fields.push_back("`name`=?");
166 values.push_back(QVariant(m_name));
168 if (m_modifiedFields.testFlag(AlternateName))
170 handled |= AlternateName;
171 fields.push_back("`alternate_name`=?");
172 values.push_back(QVariant(m_alternateName));
175 bool insertion = (m_id < 0);
176 if (insertion || !fields.isEmpty())
178 QSqlQuery query(KwSongdb::self()->database());
180 if (insertion)
182 // Insert a new row
183 query.prepare("INSERT INTO `Song` "
184 "SET " + fields.join(","));
186 else
188 query.prepare("UPDATE `Song` "
189 "SET " + fields.join(",") + " "
190 "WHERE id = ?");
191 values.push_back(QVariant(m_id));
194 // Add bind values
195 foreach (QVariant value, values)
197 query.addBindValue(value);
200 // Execute query
201 bool worked = query.exec();
202 Q_ASSERT(worked);
204 // Update which fields are modified
205 m_modifiedFields &= ~handled;
207 // Update relevent objects
208 if (insertion)
210 m_id = query.lastInsertId().toInt();
211 KwSongdb::self()->registerSong(this);
216 /// Register a version object.
217 void KwSongdbSong::registerVersion(KwSongdbVersion* version)
219 m_versionIds.push_back(version->id());