diff --git a/CMakeLists.txt b/CMakeLists.txt index 002b2a7da2b361dc34f6ccab8ccb09846621851b..21abb96fb7d2d4b33d3e48fd7ede350b9d895fb0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ list(APPEND SRC_LIST ${HEADERS_LIST} src/File.cpp src/Album.cpp src/Show.cpp + src/Label.cpp ) find_package(Sqlite3 REQUIRED) diff --git a/src/Album.h b/src/Album.h index 47267e59b7674cb08e37d33c63374737ee1b4007..84834f3cbf75e018a221a1d9706d227d3ccea168 100644 --- a/src/Album.h +++ b/src/Album.h @@ -18,7 +18,7 @@ class Album : public IAlbum virtual time_t lastSyncDate(); static bool CreateTable( sqlite3* dbConnection ); - static Album* Fetch( sqlite3* dbConnection, unsigned int albumTrackId) + static Album* Fetch( sqlite3* dbConnection, unsigned int albumTrackId ); protected: sqlite3* m_dbConnection; diff --git a/src/File.cpp b/src/File.cpp index 84dc1e8a23db463d1b495247d08d5388608c54f1..bdcd0288a98fc99ade6552b1ba80c7a8f356ce7e 100644 --- a/src/File.cpp +++ b/src/File.cpp @@ -1,7 +1,9 @@ #include #include "File.h" +#include "Label.h" #include "SqliteTools.h" +#include "Album.h" File::File( sqlite3* dbConnection, sqlite3_stmt* stmt ) : m_dbConnection( dbConnection ) @@ -38,7 +40,7 @@ bool File::insert( sqlite3* dbConnection ) return true; } -IAlbumTrack*File::albumTrack() +IAlbumTrack* File::albumTrack() { if ( m_albumTrack == NULL && m_albumTrackId != 0 ) { @@ -62,11 +64,32 @@ IShowEpisode*File::showEpisode() std::vector File::labels() { + if ( m_labels == NULL ) + { + m_labels = new std::vector; + const char* req = "SELECT * FROM Labels l" + "LEFT JOIN LabelFileRelation lfr ON lfr.id_label = f.id_label " + "WHERE lfr.id_file = ?"; + sqlite3_stmt* stmt; + 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; } bool File::CreateTable(sqlite3* connection) { - std::string req = "CREATE TABLE IF NOT EXISTS File(" + const char* req = "CREATE TABLE IF NOT EXISTS File(" "id_media INTEGER PRIMARY KEY AUTOINCREMENT," "type INTEGER, duration UNSIGNED INTEGER," "album_track_id UNSIGNED INTEGER)"; diff --git a/src/Label.cpp b/src/Label.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6531c752233678c53ef0d6406c6785d44076507 --- /dev/null +++ b/src/Label.cpp @@ -0,0 +1,59 @@ +#include "Label.h" + +#include "SqliteTools.h" + +Label::Label( sqlite3* dbConnection, sqlite3_stmt* stmt ) + : m_dbConnection( dbConnection ) + , m_files( NULL ) +{ + m_id = sqlite3_column_int( stmt, 1 ); + m_name = (const char*)sqlite3_column_text( stmt, 2 ); +} + + +const std::string& Label::name() +{ + return m_name; +} + +std::vector Label::files() +{ + if ( m_files == NULL ) + { + m_files = new std::vector; + const char* req = "SELECT * FROM Files f" + "LEFT JOIN LabelFileRelation lfr ON lfr.id_file = f.id_file " + "WHERE lfr.id_label = ?"; + sqlite3_stmt* stmt; + 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; +} + +bool Label::createTable(sqlite3* dbConnection) +{ + const char* req = "CREATE TABLE IF NOT EXISTS Label(" + "id_label INTEGER PRIMARY KEY AUTO INCREMENT, " + "name TEXT" + ")"; + if ( SqliteTools::CreateTable( dbConnection, req ) ) + return false; + req = "CREATE TABLE IF NOT EXISTS LabelFileRelation(" + "id_label INTEGER," + "id_file INTEGER," + "PRIMARY KEY (id_label, id_file)" + "FOREIGN KEY(id_label) REFERENCES Label(id_label) ON DELETE CASCADE," + "FOREIGN KEY(id_file) REFERENCES File(id_file) ON DELETE CASCADE);"; + return SqliteTools::CreateTable( dbConnection, req ); +} diff --git a/src/Label.h b/src/Label.h new file mode 100644 index 0000000000000000000000000000000000000000..732dd8382d9d8ac63c704dcc0ad5136ad533a8eb --- /dev/null +++ b/src/Label.h @@ -0,0 +1,27 @@ +#ifndef LABEL_H +#define LABEL_H + +#include +#include + +#include "ILabel.h" + +class Label : public ILabel +{ + public: + Label(sqlite3* dbConnection, sqlite3_stmt* stmt); + + public: + virtual const std::string& name(); + virtual std::vector files(); + + static bool createTable( sqlite3* dbConnection ); + + private: + sqlite3* m_dbConnection; + unsigned int m_id; + std::string m_name; + std::vector* m_files; +}; + +#endif // LABEL_H diff --git a/src/MediaLibrary.cpp b/src/MediaLibrary.cpp index 735cdffa636b6a346ee65fabda0a453ffee34674..ac1c68c27f1f355d7aacb8aa0e3fcbaf6aca1da0 100644 --- a/src/MediaLibrary.cpp +++ b/src/MediaLibrary.cpp @@ -7,6 +7,8 @@ MediaLibrary::MediaLibrary() bool MediaLibrary::initialize(const std::string& dbPath) { int res = sqlite3_open( dbPath.c_str(), &m_dbConnection ); + //FIXME: + // PRAGMA foreign_keys = ON; return res == SQLITE_OK; } diff --git a/src/SqliteTools.cpp b/src/SqliteTools.cpp index d079be42a5cccc27fdf2a8f9088dc5b8dfc46e9a..e581ad2bc1fac17323da90b7ed2e63da7da0651f 100644 --- a/src/SqliteTools.cpp +++ b/src/SqliteTools.cpp @@ -2,10 +2,10 @@ #include "SqliteTools.h" -bool SqliteTools::CreateTable( sqlite3 *db, const std::string& request ) +bool SqliteTools::CreateTable( sqlite3 *db, const char* request ) { sqlite3_stmt* stmt; - int res = sqlite3_prepare_v2( db, request.c_str(), -1, &stmt, NULL ); + int res = sqlite3_prepare_v2( db, request, -1, &stmt, NULL ); if ( res != SQLITE_OK ) { std::cerr << "Failed to execute request: " << request << std::endl; diff --git a/src/SqliteTools.h b/src/SqliteTools.h index fc177fcaa12fead9cfb309d985be4e78b02a3c69..e64b03871262d9ab1e224902e9d0a31a2af33866 100644 --- a/src/SqliteTools.h +++ b/src/SqliteTools.h @@ -7,7 +7,7 @@ class SqliteTools { public: - static bool CreateTable(sqlite3* db, const std::string& request ); + static bool CreateTable(sqlite3* db, const char* request ); }; #endif // SQLITETOOLS_H