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. --- SConstruct | 2 +- database.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ database.h | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.cpp | 7 ++++++ 4 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 database.cpp create mode 100644 database.h diff --git a/SConstruct b/SConstruct index f05633e..ba87eff 100644 --- a/SConstruct +++ b/SConstruct @@ -26,7 +26,7 @@ if GetOption('profiling'): Export('env') -env.EnableQt4Modules(['QtCore', 'QtGui'], debug = False) +env.EnableQt4Modules(['QtCore', 'QtGui', 'QtSql'], debug = False) env.Uic4(Glob('gui/*.ui')) 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(); +} diff --git a/database.h b/database.h new file mode 100644 index 0000000..55cd301 --- /dev/null +++ b/database.h @@ -0,0 +1,73 @@ +#ifndef DATABASE_H +#define DATABASE_H + +#include +#include + +class Artist { + private: + int _id; + QString _name; + + public: + typedef QSharedPointer p; + static p get(int id); + + QString name(); + + QUrl url(); +}; + +class Album { + private: + int _id; + int _artist_id; + QString _name; + int _tracks; + + public: + typedef QSharedPointer p; + static p get(int id); + + QString name(); + Artist::p artist(); + int tracks(); + + QUrl url(); +}; + +class Track { + private: + int _id; + int _artist_id; + int _album_id; + QString _name; + int _length; + int _num; + QString _file_name; + int _file_index; + + public: + typedef QSharedPointer p; + static p get(int id); + + QString name(); + Artist::p artist(); + Album::p album(); + int length(); + int num(); + QString file_name(); + int file_index(); + + QUrl url(); +}; + +class Database { + private: + QSqlDatabase db; + + public: + Database(); +}; + +#endif diff --git a/main.cpp b/main.cpp index 1c1bd63..87fbcde 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ #include +#include "database.h" #include "player.h" #include "gui/mainwindow.h" @@ -10,6 +11,12 @@ int main(int argc, char** argv) { qapp.setStyleSheet("file:///default.qss"); + Database* database = new Database(); + + Track::p t = Track::get(1); + + qDebug() << t->album()->artist()->name() << " - " << t->name() << " (" << t->album()->name() << ")"; + Player* player = new Player(); MainWindow* mainwindow = new MainWindow(); -- cgit v1.2.3