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 ***************************************************************************/
22 * @brief A song database manager class.
23 * @author James Hogan <james@albanarts.com>
27 #include "KwSongdbSong.h"
28 #include "KwSongdbVersion.h"
29 #include "KwSongdbSongBook.h"
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()
49 * Constructors + destructor
52 /// Primary constructor.
53 KwSongdb::KwSongdb(QSqlDatabase
& db
)
56 Q_ASSERT(0 == s_singleton
);
59 /// @todo If any tables are missing, set them up
60 /// @todo MYSQL: "SET GLOBAL sql_mode='ANSI';" to enable || instead of CONCAT
68 // Delete song versions
70 VersionHash::iterator it
;
71 for (it
= m_versionsById
.begin(); it
!= m_versionsById
.end(); ++it
)
79 SongHash::iterator it
;
80 for (it
= m_songsById
.begin(); it
!= m_songsById
.end(); ++it
)
93 bool KwSongdb::handleQuery(QSqlQuery
&query
, const char *src
, int line
)
95 bool ok
= query
.exec();
98 qDebug() << "SQL QUERY FAILED:" << query
.lastError().text();
102 qDebug() << "SQL QUERY SUCCEEDED!";
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();
122 /// Get the database.
123 QSqlDatabase
KwSongdb::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())
138 KwSongdbSong
* newSong
= new KwSongdbSong(id
);
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())
155 KwSongdbVersion
* newVersion
= new KwSongdbVersion(id
);
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
);
169 list
.push_back(version
);
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())
185 KwSongdbSongBook
* newSongBook
= new KwSongdbSongBook(id
);
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` "
197 KW_SONGDB_QUERY(query
);
199 // Then load them individually into a list
200 QList
<KwSongdbSongBook
*> songBooks
;
204 songBooks
.push_back(songBookById(query
.value(0).toInt()));
205 } while (query
.next());
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
;