From 95ead1f32fc3d02c11a58bf67be16f93c06c0682 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Sun, 19 Sep 2010 19:23:28 +0200 Subject: Wrote database classes. --- database.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 database.cpp (limited to 'database.cpp') 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(); +} -- cgit v1.2.3