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 KwSongdbSongBook.cpp
22 * @brief A song book of songs from the database.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwSongdbSongBook.h"
31 #include <QStringList>
34 * Constructors + destructor
37 /// Construct a new song book for database insertion.
38 KwSongdbSongBook::KwSongdbSongBook()
40 , m_modifiedFields(Abreviation
)
48 /// Construct from the database.
49 KwSongdbSongBook::KwSongdbSongBook(int id
)
56 // Get the song book data
57 QSqlQuery
query(KwSongdb::self()->database());
58 query
.prepare("SELECT `abreviation`, `name`, `description` "
61 query
.addBindValue(QVariant(id
));
62 KW_SONGDB_QUERY(query
);
67 qDebug() << "first failed, active:" << query
.isActive() << ", select:" << query
.isSelect();
70 m_abreviation
= query
.value(0).toString();
71 m_name
= query
.value(1).toString();
72 m_description
= query
.value(2).toString();
74 // Register with songdb
75 KwSongdb::self()->registerSongBook(this);
79 KwSongdbSongBook::~KwSongdbSongBook()
88 int KwSongdbSongBook::id() const
93 /// Get the abreviation of the song book.
94 QString
KwSongdbSongBook::abreviation() const
99 /// Get the name of the song book.
100 QString
KwSongdbSongBook::name() const
105 /// Get the description of the song book.
106 QString
KwSongdbSongBook::description() const
108 return m_description
;
115 /// Set the abreviation.
116 void KwSongdbSongBook::setAbreviation(const QString
& abreviation
)
118 if (abreviation
!= m_abreviation
)
120 m_modifiedFields
|= Abreviation
;
121 m_abreviation
= abreviation
;
122 /// @todo Update any songdb abreviation hash
127 void KwSongdbSongBook::setName(const QString
& name
)
131 m_modifiedFields
|= Name
;
136 /// Set the description.
137 void KwSongdbSongBook::setDescription(const QString
& description
)
139 if (description
!= m_description
)
141 m_modifiedFields
|= Description
;
142 m_description
= description
;
146 /// Save changes to the song book data.
147 void KwSongdbSongBook::save()
150 QList
<QVariant
> values
;
153 // Straightforward modifications
155 if (m_modifiedFields
.testFlag(Abreviation
))
157 handled
|= Abreviation
;
158 fields
.push_back("`abreviation`");
159 values
.push_back(QVariant(m_abreviation
));
162 if (m_modifiedFields
.testFlag(Name
))
165 fields
.push_back("`name`");
166 values
.push_back(QVariant(m_name
));
169 if (m_modifiedFields
.testFlag(Description
))
171 handled
|= Description
;
172 fields
.push_back("`description`");
173 values
.push_back(QVariant(m_description
));
176 bool insertion
= (m_id
< 0);
177 if (insertion
|| !fields
.isEmpty())
179 QSqlQuery
query(KwSongdb::self()->database());
184 QString qs
= QString::fromAscii("?,").repeated(fields
.size());
186 query
.prepare("INSERT INTO `SongBook` (" + fields
.join(",") + ")"
187 " VALUES (" + qs
+ ")");
191 for (int i
= 0; i
< fields
.size(); ++i
)
195 query
.prepare("UPDATE `SongBook` "
196 "SET " + fields
.join(",") + " "
198 values
.push_back(QVariant(m_id
));
202 foreach (QVariant value
, values
)
204 query
.addBindValue(value
);
208 KW_SONGDB_QUERY(query
);
210 // Update which fields are modified
211 m_modifiedFields
&= ~handled
;
213 // Update relevent objects
216 m_id
= query
.lastInsertId().toInt();
217 KwSongdb::self()->registerSongBook(this);