Database: sqlite default to appdata kworship.db
[kworship.git] / kworship / songdb / KwSongdbSong.cpp
blobcb7f10ccdb14d3aff858fe11049bd33a4fd1ff98
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 #include <QtDebug>
50 /// Construct from the database.
51 KwSongdbSong::KwSongdbSong(int id)
52 : m_id(id)
53 , m_modifiedFields(0)
54 , m_name()
55 , m_alternateName()
56 , m_versionsLoaded(false)
57 , m_versionIds()
59 // Get the song data
60 QSqlQuery query(KwSongdb::self()->database());
61 query.prepare("SELECT `name`, `alternate_name`, `css_style_sheet_id` "
62 "FROM `Song` "
63 "WHERE `id` = ?");
64 query.addBindValue(QVariant(id));
65 KW_SONGDB_QUERY(query);
67 // Copy the data
68 if (!query.first())
70 qDebug() << "first failed, active:" << query.isActive() << ", select:" << query.isSelect();
71 exit(1);
73 m_name = query.value(0).toString();
74 m_alternateName = query.value(1).toString();
76 // Register with songdb
77 KwSongdb::self()->registerSong(this);
80 /// Destructor.
81 KwSongdbSong::~KwSongdbSong()
86 * Accessors
89 /// Get the id.
90 int KwSongdbSong::id() const
92 return m_id;
95 /// Get the name of the song.
96 QString KwSongdbSong::name() const
98 return m_name;
101 /// Get the alternate name of the song.
102 QString KwSongdbSong::alternateName() const
104 return m_alternateName;
107 /// Get list of song versions.
108 QList<KwSongdbVersion*> KwSongdbSong::versions()
110 if (!m_versionsLoaded)
112 // Get the version ids
113 QSqlQuery query(KwSongdb::self()->database());
114 query.prepare("SELECT `id` "
115 "FROM `SongVersion` "
116 "WHERE `song_id` = ?");
117 query.addBindValue(QVariant(m_id));
118 KW_SONGDB_QUERY(query);
120 if (query.first())
122 do {
123 m_versionIds.push_back(query.value(0).toInt());
124 } while (query.next());
127 m_versionsLoaded = true;
129 return KwSongdb::self()->songVersionsByIds(m_versionIds);
133 * Mutators
136 /// Set the name.
137 void KwSongdbSong::setName(const QString& name)
139 if (name != m_name)
141 m_modifiedFields |= Name;
142 m_name = name;
146 /// Set the alternate name.
147 void KwSongdbSong::setAlternateName(const QString& name)
149 if (name != m_alternateName)
151 m_modifiedFields |= AlternateName;
152 m_alternateName = name;
156 /// Save changes to the song data.
157 void KwSongdbSong::save()
159 QStringList fields;
160 QList<QVariant> values;
161 Fields handled;
163 // Straightforward modifications
165 if (m_modifiedFields.testFlag(Name))
167 handled |= Name;
168 fields.push_back("`name`");
169 values.push_back(QVariant(m_name));
171 if (m_modifiedFields.testFlag(AlternateName))
173 handled |= AlternateName;
174 fields.push_back("`alternate_name`");
175 values.push_back(QVariant(m_alternateName));
178 bool insertion = (m_id < 0);
179 if (insertion || !fields.isEmpty())
181 QSqlQuery query(KwSongdb::self()->database());
183 if (insertion)
185 // Insert a new row
186 QString qs = QString::fromAscii("?,").repeated(fields.size());
187 qs.chop(1);
188 query.prepare("INSERT INTO `Song` (" + fields.join(",") + ")"
189 " VALUES (" + qs + ")");
191 else
193 for (int i = 0; i < fields.size(); ++i)
195 fields[i] += "=?";
197 query.prepare("UPDATE `Song` "
198 "SET " + fields.join(",") + " "
199 "WHERE id = ?");
200 values.push_back(QVariant(m_id));
203 // Add bind values
204 foreach (QVariant value, values)
206 query.addBindValue(value);
209 // Execute query
210 KW_SONGDB_QUERY(query);
212 // Update which fields are modified
213 m_modifiedFields &= ~handled;
215 // Update relevent objects
216 if (insertion)
218 m_id = query.lastInsertId().toInt();
219 KwSongdb::self()->registerSong(this);
224 /// Register a version object.
225 void KwSongdbSong::registerVersion(KwSongdbVersion* version)
227 m_versionIds.push_back(version->id());