1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
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. *
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. *
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 ***************************************************************************/
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"
32 #include <QStringList>
35 * Constructors + destructor
38 /// Construct a new song for database insertion.
39 KwSongdbSong::KwSongdbSong()
41 , m_modifiedFields(Name
)
44 , m_versionsLoaded(false)
50 /// Construct from the database.
51 KwSongdbSong::KwSongdbSong(int id
)
56 , m_versionsLoaded(false)
60 QSqlQuery
query(KwSongdb::self()->database());
61 query
.prepare("SELECT `name`, `alternate_name`, `css_style_sheet_id` "
64 query
.addBindValue(QVariant(id
));
65 KW_SONGDB_QUERY(query
);
70 qDebug() << "first failed, active:" << query
.isActive() << ", select:" << query
.isSelect();
73 m_name
= query
.value(0).toString();
74 m_alternateName
= query
.value(1).toString();
76 // Register with songdb
77 KwSongdb::self()->registerSong(this);
81 KwSongdbSong::~KwSongdbSong()
90 int KwSongdbSong::id() const
95 /// Get the name of the song.
96 QString
KwSongdbSong::name() const
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
);
123 m_versionIds
.push_back(query
.value(0).toInt());
124 } while (query
.next());
127 m_versionsLoaded
= true;
129 return KwSongdb::self()->songVersionsByIds(m_versionIds
);
137 void KwSongdbSong::setName(const QString
& name
)
141 m_modifiedFields
|= 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()
160 QList
<QVariant
> values
;
163 // Straightforward modifications
165 if (m_modifiedFields
.testFlag(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());
186 QString qs
= QString::fromAscii("?,").repeated(fields
.size());
188 query
.prepare("INSERT INTO `Song` (" + fields
.join(",") + ")"
189 " VALUES (" + qs
+ ")");
193 for (int i
= 0; i
< fields
.size(); ++i
)
197 query
.prepare("UPDATE `Song` "
198 "SET " + fields
.join(",") + " "
200 values
.push_back(QVariant(m_id
));
204 foreach (QVariant value
, values
)
206 query
.addBindValue(value
);
210 KW_SONGDB_QUERY(query
);
212 // Update which fields are modified
213 m_modifiedFields
&= ~handled
;
215 // Update relevent objects
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());