SQLite compatibility and related fixes
[kworship.git] / kworship / songdb / KwSongdbSongBook.cpp
blob4a8039f2dfae13e0de24acf158cb831c572a8ba4
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 KwSongdbSongBook.cpp
22 * @brief A song book of songs from the database.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwSongdbSongBook.h"
27 #include "KwSongdb.h"
29 #include <QSqlQuery>
30 #include <QVariant>
31 #include <QStringList>
34 * Constructors + destructor
37 /// Construct a new song book for database insertion.
38 KwSongdbSongBook::KwSongdbSongBook()
39 : m_id(-1)
40 , m_modifiedFields(Abreviation)
41 , m_abreviation()
42 , m_name()
43 , m_description()
47 #include <QtDebug>
48 /// Construct from the database.
49 KwSongdbSongBook::KwSongdbSongBook(int id)
50 : m_id(id)
51 , m_modifiedFields(0)
52 , m_abreviation()
53 , m_name()
54 , m_description()
56 // Get the song book data
57 QSqlQuery query(KwSongdb::self()->database());
58 query.prepare("SELECT `abreviation`, `name`, `description` "
59 "FROM `SongBook` "
60 "WHERE `id` = ?");
61 query.addBindValue(QVariant(id));
62 KW_SONGDB_QUERY(query);
64 // Copy the data
65 if (!query.first())
67 qDebug() << "first failed, active:" << query.isActive() << ", select:" << query.isSelect();
68 exit(1);
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);
78 /// Destructor.
79 KwSongdbSongBook::~KwSongdbSongBook()
84 * Accessors
87 /// Get the id.
88 int KwSongdbSongBook::id() const
90 return m_id;
93 /// Get the abreviation of the song book.
94 QString KwSongdbSongBook::abreviation() const
96 return m_abreviation;
99 /// Get the name of the song book.
100 QString KwSongdbSongBook::name() const
102 return m_name;
105 /// Get the description of the song book.
106 QString KwSongdbSongBook::description() const
108 return m_description;
112 * Mutators
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
126 /// Set the name.
127 void KwSongdbSongBook::setName(const QString& name)
129 if (name != m_name)
131 m_modifiedFields |= Name;
132 m_name = 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()
149 QStringList fields;
150 QList<QVariant> values;
151 Fields handled;
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))
164 handled |= 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());
181 if (insertion)
183 // Insert a new row
184 QString qs = QString::fromAscii("?,").repeated(fields.size());
185 qs.chop(1);
186 query.prepare("INSERT INTO `SongBook` (" + fields.join(",") + ")"
187 " VALUES (" + qs + ")");
189 else
191 for (int i = 0; i < fields.size(); ++i)
193 fields[i] += "=?";
195 query.prepare("UPDATE `SongBook` "
196 "SET " + fields.join(",") + " "
197 "WHERE id = ?");
198 values.push_back(QVariant(m_id));
201 // Add bind values
202 foreach (QVariant value, values)
204 query.addBindValue(value);
207 // Execute query
208 KW_SONGDB_QUERY(query);
210 // Update which fields are modified
211 m_modifiedFields &= ~handled;
213 // Update relevent objects
214 if (insertion)
216 m_id = query.lastInsertId().toInt();
217 KwSongdb::self()->registerSongBook(this);