Commit 0f6d92e0 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Connecting AlbumTracks & adding some helper for fetching

parent 0f981ae5
...@@ -23,6 +23,7 @@ list(APPEND SRC_LIST ${HEADERS_LIST} ...@@ -23,6 +23,7 @@ list(APPEND SRC_LIST ${HEADERS_LIST}
src/Album.cpp src/Album.cpp
src/Show.cpp src/Show.cpp
src/Label.cpp src/Label.cpp
src/AlbumTrack.cpp
) )
find_package(Sqlite3 REQUIRED) find_package(Sqlite3 REQUIRED)
......
...@@ -17,7 +17,6 @@ class IAlbum ...@@ -17,7 +17,6 @@ class IAlbum
virtual const std::string& shortSummary() = 0; virtual const std::string& shortSummary() = 0;
virtual const std::string& artworkUrl() = 0; virtual const std::string& artworkUrl() = 0;
virtual const std::vector<ITrack*>& tracks() = 0; virtual const std::vector<ITrack*>& tracks() = 0;
virtual const std::string& tvdbId() = 0;
}; };
#endif // IALBUM_H #endif // IALBUM_H
...@@ -21,8 +21,6 @@ class IFile ...@@ -21,8 +21,6 @@ class IFile
virtual IShowEpisode* showEpisode() = 0; virtual IShowEpisode* showEpisode() = 0;
virtual int playCount() = 0; virtual int playCount() = 0;
virtual std::vector<ITrackInformation*> tracks() = 0;
virtual std::vector<ILabel*> labels() = 0; virtual std::vector<ILabel*> labels() = 0;
}; };
......
...@@ -43,26 +43,22 @@ time_t Album::lastSyncDate() ...@@ -43,26 +43,22 @@ time_t Album::lastSyncDate()
return m_lastSyncDate; return m_lastSyncDate;
} }
const std::vector<ITrack*>&Album::tracks()
{
}
bool Album::CreateTable(sqlite3* dbConnection) bool Album::CreateTable(sqlite3* dbConnection)
{ {
std::string req = "CREATE TABLE IF NOT EXISTS Album(" const char* req = "CREATE TABLE IF NOT EXISTS Album("
"id_album INTEGER PRIMARY KEY AUTOINCREMENT," "id_album INTEGER PRIMARY KEY AUTOINCREMENT,"
"name TEXT, UNSIGNED INTEGER release_year, TEXT short_summary," "name TEXT, UNSIGNED INTEGER release_year, TEXT short_summary,"
"TEXT artwork_url, UNSIGNED INTEGER last_sync_date)"; "TEXT artwork_url, UNSIGNED INTEGER last_sync_date)";
return SqliteTools::CreateTable( dbConnection, req ); return SqliteTools::CreateTable( dbConnection, req );
} }
Album* Album::Fetch(sqlite3* dbConnection, unsigned int albumTrackId) Album* Album::fetch(sqlite3* dbConnection, unsigned int albumTrackId)
{ {
const char* req = "SELECT * FROM Album WHERE id_album = ?"; const char* req = "SELECT * FROM Album WHERE id_album = ?";
sqlite3_stmt *stmt; return SqliteTools::fetchOne<Album>( dbConnection, req, albumTrackId );
int res = sqlite3_prepare_v2( dbConnection, req, -1, &stmt, NULL );
if ( res != SQLITE_OK )
return NULL;
sqlite3_bind_int( stmt, 1, albumTrackId );
if ( sqlite3_step( stmt ) != SQLITE_ROW )
return NULL;
Album* album = new Album( dbConnection, stmt );
sqlite3_finalize( stmt );
return album;
} }
...@@ -16,9 +16,10 @@ class Album : public IAlbum ...@@ -16,9 +16,10 @@ class Album : public IAlbum
virtual const std::string& shortSummary(); virtual const std::string& shortSummary();
virtual const std::string& artworkUrl(); virtual const std::string& artworkUrl();
virtual time_t lastSyncDate(); virtual time_t lastSyncDate();
virtual const std::vector<ITrack*>& tracks();
static bool CreateTable( sqlite3* dbConnection ); static bool CreateTable( sqlite3* dbConnection );
static Album* Fetch( sqlite3* dbConnection, unsigned int albumTrackId ); static Album* fetch( sqlite3* dbConnection, unsigned int albumTrackId );
protected: protected:
sqlite3* m_dbConnection; sqlite3* m_dbConnection;
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#include "Album.h" #include "Album.h"
#include "SqliteTools.h" #include "SqliteTools.h"
AlbumTrack::AlbumTrak( sqlite3* dbConnection, sqlite3_stmt* stmt ) AlbumTrack::AlbumTrack( sqlite3* dbConnection, sqlite3_stmt* stmt )
: m_dbConnection( dbConnection ) : m_dbConnection( dbConnection )
, m_album( NULL ) , m_album( NULL )
{ {
...@@ -21,11 +21,26 @@ bool AlbumTrack::createTable(sqlite3* dbConnection) ...@@ -21,11 +21,26 @@ bool AlbumTrack::createTable(sqlite3* dbConnection)
"genre TEXT," "genre TEXT,"
"track_number UNSIGNED INTEGER," "track_number UNSIGNED INTEGER,"
"album_id UNSIGNED INTEGER NOT NULL," "album_id UNSIGNED INTEGER NOT NULL,"
"FOREIGN KEY (album_id) REFERENCES Album(id_album) ON DELETE CASCADE "FOREIGN KEY (album_id) REFERENCES Album(id_album) ON DELETE CASCADE"
")"; ")";
return SqliteTools::CreateTable( dbConnection, req ); return SqliteTools::CreateTable( dbConnection, req );
} }
AlbumTrack* AlbumTrack::fetch(sqlite3* dbConnection, unsigned int idTrack )
{
const char* req = "SELECT * FROM AlbumTrack WHERE id_track = ?";
sqlite3_stmt *stmt;
int res = sqlite3_prepare_v2( dbConnection, req, -1, &stmt, NULL );
if ( res != SQLITE_OK )
return NULL;
sqlite3_bind_int( stmt, 1, idTrack );
if ( sqlite3_step( stmt ) != SQLITE_ROW )
return NULL;
AlbumTrack* albumTrack = new AlbumTrack( dbConnection, stmt );
sqlite3_finalize( stmt );
return albumTrack;
}
const std::string& AlbumTrack::genre() const std::string& AlbumTrack::genre()
{ {
return m_genre; return m_genre;
......
...@@ -11,14 +11,16 @@ class Album; ...@@ -11,14 +11,16 @@ class Album;
class AlbumTrack : public IAlbumTrack class AlbumTrack : public IAlbumTrack
{ {
public: public:
AlbumTrack(); AlbumTrack( sqlite3* dbConnection, sqlite3_stmt* stmt );
static bool createTable( sqlite3* dbConnection );
virtual const std::string&genre(); virtual const std::string&genre();
virtual const std::string&title(); virtual const std::string&title();
virtual unsigned int trackNumber(); virtual unsigned int trackNumber();
virtual IAlbum*album(); virtual IAlbum*album();
static bool createTable( sqlite3* dbConnection );
static AlbumTrack* fetch( sqlite3* dbConnection, unsigned int idTrack );
private: private:
sqlite3* m_dbConnection; sqlite3* m_dbConnection;
unsigned int m_id; unsigned int m_id;
......
#include <cassert> #include <cassert>
#include "AlbumTrack.h"
#include "File.h" #include "File.h"
#include "Label.h" #include "Label.h"
#include "SqliteTools.h" #include "SqliteTools.h"
...@@ -15,6 +16,7 @@ File::File( sqlite3* dbConnection, sqlite3_stmt* stmt ) ...@@ -15,6 +16,7 @@ File::File( sqlite3* dbConnection, sqlite3_stmt* stmt )
m_type = (Type)sqlite3_column_int( stmt, 1 ); m_type = (Type)sqlite3_column_int( stmt, 1 );
m_duration = sqlite3_column_int( stmt, 2 ); m_duration = sqlite3_column_int( stmt, 2 );
m_albumTrackId = sqlite3_column_int( stmt, 3 ); m_albumTrackId = sqlite3_column_int( stmt, 3 );
m_playCount = sqlite3_column_int( stmt, 4 );
} }
File::File() File::File()
...@@ -44,7 +46,8 @@ IAlbumTrack* File::albumTrack() ...@@ -44,7 +46,8 @@ IAlbumTrack* File::albumTrack()
{ {
if ( m_albumTrack == NULL && m_albumTrackId != 0 ) if ( m_albumTrack == NULL && m_albumTrackId != 0 )
{ {
m_albumTrack = AlbumTrack::fetch( m_albumTrackId ); const char* req = "SELECT * FROM AlbumTrack WHERE id_track = ?";
m_albumTrack = SqliteTools::fetchOne<AlbumTrack>( m_dbConnection, req, m_albumTrackId );
} }
return m_albumTrack; return m_albumTrack;
} }
...@@ -66,32 +69,25 @@ std::vector<ILabel*> File::labels() ...@@ -66,32 +69,25 @@ std::vector<ILabel*> File::labels()
{ {
if ( m_labels == NULL ) if ( m_labels == NULL )
{ {
m_labels = new std::vector<ILabel*>;
const char* req = "SELECT * FROM Labels l" const char* req = "SELECT * FROM Labels l"
"LEFT JOIN LabelFileRelation lfr ON lfr.id_label = f.id_label " "LEFT JOIN LabelFileRelation lfr ON lfr.id_label = f.id_label "
"WHERE lfr.id_file = ?"; "WHERE lfr.id_file = ?";
sqlite3_stmt* stmt; SqliteTools::fetchAll<Label>( m_dbConnection, req, m_id, m_labels );
int res = sqlite3_prepare_v2( m_dbConnection, req, -1, &stmt, NULL );
if ( res != SQLITE_OK )
return *m_labels;
sqlite3_bind_int( stmt, 1, m_id );
res = sqlite3_step( stmt );
while ( res == SQLITE_ROW )
{
ILabel* l = new Label( m_dbConnection, stmt );
m_labels->push_back( l );
res = sqlite3_step( stmt );
}
sqlite3_finalize( stmt );
} }
return *m_labels; return *m_labels;
} }
int File::playCount()
{
}
bool File::CreateTable(sqlite3* connection) bool File::CreateTable(sqlite3* connection)
{ {
const char* req = "CREATE TABLE IF NOT EXISTS File(" const char* req = "CREATE TABLE IF NOT EXISTS File("
"id_media INTEGER PRIMARY KEY AUTOINCREMENT," "id_media INTEGER PRIMARY KEY AUTOINCREMENT,"
"type INTEGER, duration UNSIGNED INTEGER," "type INTEGER, duration UNSIGNED INTEGER,"
"album_track_id UNSIGNED INTEGER)"; "album_track_id UNSIGNED INTEGER,"
"play_count UNSIGNED INTEGER)";
return SqliteTools::CreateTable( connection, req ); return SqliteTools::CreateTable( connection, req );
} }
...@@ -30,7 +30,8 @@ class File : public IFile ...@@ -30,7 +30,8 @@ class File : public IFile
virtual unsigned int duration(); virtual unsigned int duration();
virtual IShowEpisode* showEpisode(); virtual IShowEpisode* showEpisode();
virtual std::vector<ILabel*> labels(); virtual std::vector<ILabel*> labels();
virtual int playCount();
static bool CreateTable( sqlite3* connection ); static bool CreateTable( sqlite3* connection );
private: private:
...@@ -41,6 +42,7 @@ class File : public IFile ...@@ -41,6 +42,7 @@ class File : public IFile
Type m_type; Type m_type;
unsigned int m_duration; unsigned int m_duration;
unsigned int m_albumTrackId; unsigned int m_albumTrackId;
unsigned int m_playCount;
// Auto fetched related properties // Auto fetched related properties
Album* m_album; Album* m_album;
......
#include "Label.h" #include "Label.h"
#include "File.h"
#include "SqliteTools.h" #include "SqliteTools.h"
Label::Label( sqlite3* dbConnection, sqlite3_stmt* stmt ) Label::Label( sqlite3* dbConnection, sqlite3_stmt* stmt )
...@@ -20,23 +20,10 @@ std::vector<IFile*> Label::files() ...@@ -20,23 +20,10 @@ std::vector<IFile*> Label::files()
{ {
if ( m_files == NULL ) if ( m_files == NULL )
{ {
m_files = new std::vector<IFile*>;
const char* req = "SELECT * FROM Files f" const char* req = "SELECT * FROM Files f"
"LEFT JOIN LabelFileRelation lfr ON lfr.id_file = f.id_file " "LEFT JOIN LabelFileRelation lfr ON lfr.id_file = f.id_file "
"WHERE lfr.id_label = ?"; "WHERE lfr.id_label = ?";
sqlite3_stmt* stmt; SqliteTools::fetchAll<File>( m_dbConnection, req, m_id, m_files );
int res = sqlite3_prepare_v2( m_dbConnection, req, -1, &stmt, NULL );
if ( res != SQLITE_OK )
return ;
sqlite3_bind_int( stmt, 1, m_id );
res = sqlite3_step( stmt );
while ( res == SQLITE_ROW )
{
IFile* f = new File( m_dbConnection, stmt );
m_files->push_back( f );
res = sqlite3_step( stmt );
}
sqlite3_finalize( stmt );
} }
return *m_files; return *m_files;
} }
......
...@@ -52,7 +52,7 @@ const std::string& Show::tvdbId() ...@@ -52,7 +52,7 @@ const std::string& Show::tvdbId()
bool Show::CreateTable(sqlite3* dbConnection) bool Show::CreateTable(sqlite3* dbConnection)
{ {
std::string req = "CREATE TABLE IF NOT EXISTS Show(" const char* req = "CREATE TABLE IF NOT EXISTS Show("
"id_show INTEGER PRIMARY KEY AUTOINCREMENT," "id_show INTEGER PRIMARY KEY AUTOINCREMENT,"
"name TEXT, UNSIGNED INTEGER release_year, TEXT short_summary," "name TEXT, UNSIGNED INTEGER release_year, TEXT short_summary,"
"TEXT artwork_url, UNSIGNED INTEGER last_sync_date, TEXT tvdb_id)"; "TEXT artwork_url, UNSIGNED INTEGER last_sync_date, TEXT tvdb_id)";
......
...@@ -3,11 +3,50 @@ ...@@ -3,11 +3,50 @@
#include <sqlite3.h> #include <sqlite3.h>
#include <string> #include <string>
#include <vector>
class SqliteTools class SqliteTools
{ {
public: public:
static bool CreateTable(sqlite3* db, const char* request ); static bool CreateTable(sqlite3* db, const char* request );
template <typename T, typename U>
static bool fetchAll( sqlite3* dbConnection, const char* req, unsigned int foreignKey, std::vector<U*>*& results)
{
results = new std::vector<U*>;
sqlite3_stmt* stmt;
int res = sqlite3_prepare_v2( dbConnection, req, -1, &stmt, NULL );
if ( res != SQLITE_OK )
return false;
sqlite3_bind_int( stmt, 1, foreignKey );
res = sqlite3_step( stmt );
while ( res == SQLITE_ROW )
{
U* l = new T( dbConnection, stmt );
results->push_back( l );
res = sqlite3_step( stmt );
}
sqlite3_finalize( stmt );
return true;
}
template <typename T>
static T* fetchOne( sqlite3* dbConnection, const char* req, unsigned int primaryKey )
{
T* result = NULL;
sqlite3_stmt *stmt;
int res = sqlite3_prepare_v2( dbConnection, req, -1, &stmt, NULL );
if ( res != SQLITE_OK )
return result;
sqlite3_bind_int( stmt, 1, primaryKey );
if ( sqlite3_step( stmt ) != SQLITE_ROW )
return result;
result = new T( dbConnection, stmt );
sqlite3_finalize( stmt );
return result;
}
}; };
#endif // SQLITETOOLS_H #endif // SQLITETOOLS_H
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment