summaryrefslogtreecommitdiff
path: root/database.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'database.cpp')
-rw-r--r--database.cpp81
1 files changed, 81 insertions, 0 deletions
diff --git a/database.cpp b/database.cpp
new file mode 100644
index 0000000..580f5c7
--- /dev/null
+++ b/database.cpp
@@ -0,0 +1,81 @@
+#include "database.h"
+
+Artist::p Artist::get(int id) {
+ QSqlQuery query;
+ query.prepare("SELECT id, name FROM artists WHERE id = :id");
+ query.bindValue(":id", id);
+ query.exec();
+ if(!query.first()) {
+ return Artist::p();
+ }
+ Artist::p p(new Artist);
+ p->_id = query.value(0).toInt();
+ p->_name = query.value(1).toString();
+ return p;
+}
+
+QString Artist::name() {
+ return _name;
+}
+
+Album::p Album::get(int id) {
+ QSqlQuery query;
+ query.prepare("SELECT id, artist_id, name, tracks FROM albums WHERE id = :id");
+ query.bindValue(":id", id);
+ query.exec();
+ if(!query.first()) {
+ return Album::p();
+ }
+ Album::p p(new Album);
+ p->_id = query.value(0).toInt();
+ p->_artist_id = query.value(1).toInt();
+ p->_name = query.value(2).toString();
+ p->_tracks = query.value(3).toInt();
+ return p;
+}
+
+QString Album::name() {
+ return _name;
+}
+
+Artist::p Album::artist() {
+ return Artist::get(_artist_id);
+}
+
+Track::p Track::get(int id) {
+ QSqlQuery query;
+ query.prepare("SELECT id, artist_id, album_id, name, length, num, file_name, file_index FROM tracks WHERE id = :id");
+ query.bindValue(":id", id);
+ query.exec();
+ if(!query.first()) {
+ return Track::p();
+ }
+ Track::p p(new Track);
+ p->_id = query.value(0).toInt();
+ p->_artist_id = query.value(1).toInt();
+ p->_album_id = query.value(2).toInt();
+ p->_name = query.value(3).toString();
+ p->_length = query.value(4).toInt();
+ p->_num = query.value(5).toInt();
+ p->_file_name = query.value(6).toString();
+ p->_file_index = query.value(7).toInt();
+ return p;
+}
+
+QString Track::name() {
+ return _name;
+}
+
+Artist::p Track::artist() {
+ return Artist::get(_artist_id);
+}
+
+Album::p Track::album() {
+ return Album::get(_album_id);
+}
+
+Database::Database() {
+ db = QSqlDatabase::addDatabase("QSQLITE");
+ db.setDatabaseName("ongaku.db");
+ qDebug() << "db.open():" << db.open();
+}