SQLite compatibility and related fixes
[kworship.git] / kworship / songdb / KwSongdb.cpp
blob9ef301b35420e8e24dcc5e18ed9439de639cbe1b
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 KwSongdb.cpp
22 * @brief A song database manager class.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwSongdb.h"
27 #include "KwSongdbSong.h"
28 #include "KwSongdbVersion.h"
29 #include "KwSongdbSongBook.h"
31 #include <QSqlQuery>
32 #include <QSqlError>
33 #include <QVariant>
36 * Singletonhood
39 /// The one instance of this class.
40 KwSongdb* KwSongdb::s_singleton = 0;
42 /// Get the one instance of this class.
43 KwSongdb* KwSongdb::self()
45 return s_singleton;
49 * Constructors + destructor
52 /// Primary constructor.
53 KwSongdb::KwSongdb(QSqlDatabase& db)
54 : m_database(db)
56 Q_ASSERT(0 == s_singleton);
57 s_singleton = this;
59 /// @todo If any tables are missing, set them up
60 /// @todo MYSQL: "SET GLOBAL sql_mode='ANSI';" to enable || instead of CONCAT
63 /// Destructor.
64 KwSongdb::~KwSongdb()
66 s_singleton = 0;
68 // Delete song versions
70 VersionHash::iterator it;
71 for (it = m_versionsById.begin(); it != m_versionsById.end(); ++it)
73 delete *it;
77 // Delete songs
79 SongHash::iterator it;
80 for (it = m_songsById.begin(); it != m_songsById.end(); ++it)
82 delete *it;
89 * Functionality.
92 #include <QtDebug>
93 bool KwSongdb::handleQuery(QSqlQuery &query, const char *src, int line)
95 bool ok = query.exec();
96 if (!ok)
98 qDebug() << "SQL QUERY FAILED:" << query.lastError().text();
100 else
102 qDebug() << "SQL QUERY SUCCEEDED!";
104 if (true || !ok)
106 if (0 != src && 0 != line)
108 qDebug() << "\tIn " << src << "#" << line;
110 qDebug() << "\tQUERY:" << query.executedQuery();
111 //qDebug() << "\tEXECUTED QUERY:" << query.executedQuery();
112 //qDebug() << "\tORIGINAL QUERY:" << query.lastQuery();
113 qDebug() << "\tWITH BINDINGS:" << query.boundValues();
115 return ok;
119 * Accessors
122 /// Get the database.
123 QSqlDatabase KwSongdb::database()
125 return m_database;
128 /// Get a song by id.
129 KwSongdbSong* KwSongdb::songById(int id)
131 SongHash::iterator it = m_songsById.find(id);
132 if (it != m_songsById.end())
134 return *it;
136 else
138 KwSongdbSong* newSong = new KwSongdbSong(id);
139 return newSong;
143 /** Get a song version by id.
144 * This prefetches the song the version is a part of.
146 KwSongdbVersion* KwSongdb::songVersionById(int id)
148 VersionHash::iterator it = m_versionsById.find(id);
149 if (it != m_versionsById.end())
151 return *it;
153 else
155 KwSongdbVersion* newVersion = new KwSongdbVersion(id);
156 return newVersion;
160 /// Get song versions by ids.
161 QList<KwSongdbVersion*> KwSongdb::songVersionsByIds(const QList<int>& ids)
163 QList<KwSongdbVersion*> list;
164 foreach (int id, ids)
166 KwSongdbVersion* version = songVersionById(id);
167 if (0 != version)
169 list.push_back(version);
172 return list;
175 /// Get song book by id.
176 KwSongdbSongBook* KwSongdb::songBookById(int id)
178 SongBookHash::iterator it = m_songBooksById.find(id);
179 if (it != m_songBooksById.end())
181 return *it;
183 else
185 KwSongdbSongBook* newSongBook = new KwSongdbSongBook(id);
186 return newSongBook;
190 /// Get all song books.
191 QList<KwSongdbSongBook*> KwSongdb::songBooks()
193 // First use a query to get all song book ids
194 QSqlQuery query(m_database);
195 query.prepare("SELECT `id` "
196 "FROM `SongBook`");
197 KW_SONGDB_QUERY(query);
199 // Then load them individually into a list
200 QList<KwSongdbSongBook*> songBooks;
201 if (query.first())
203 do {
204 songBooks.push_back(songBookById(query.value(0).toInt()));
205 } while (query.next());
207 return songBooks;
211 * Mutators
214 /// Register a song object.
215 void KwSongdb::registerSong(KwSongdbSong* song)
217 Q_ASSERT(m_songsById.constFind(song->id()) == m_songsById.constEnd());
218 m_songsById[song->id()] = song;
221 /// Register a version object.
222 void KwSongdb::registerVersion(KwSongdbVersion* version)
224 Q_ASSERT(m_versionsById.constFind(version->id()) == m_versionsById.constEnd());
225 m_versionsById[version->id()] = version;
228 /// Register a song book.
229 void KwSongdb::registerSongBook(KwSongdbSongBook* songBook)
231 Q_ASSERT(m_songBooksById.constFind(songBook->id()) == m_songBooksById.constEnd());
232 m_songBooksById[songBook->id()] = songBook;