Commit 6a448852 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Link files & labels

parent d5d2d7a5
...@@ -22,6 +22,7 @@ list(APPEND SRC_LIST ${HEADERS_LIST} ...@@ -22,6 +22,7 @@ list(APPEND SRC_LIST ${HEADERS_LIST}
src/File.cpp src/File.cpp
src/Album.cpp src/Album.cpp
src/Show.cpp src/Show.cpp
src/Label.cpp
) )
find_package(Sqlite3 REQUIRED) find_package(Sqlite3 REQUIRED)
......
...@@ -18,7 +18,7 @@ class Album : public IAlbum ...@@ -18,7 +18,7 @@ class Album : public IAlbum
virtual time_t lastSyncDate(); virtual time_t lastSyncDate();
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;
......
#include <cassert> #include <cassert>
#include "File.h" #include "File.h"
#include "Label.h"
#include "SqliteTools.h" #include "SqliteTools.h"
#include "Album.h"
File::File( sqlite3* dbConnection, sqlite3_stmt* stmt ) File::File( sqlite3* dbConnection, sqlite3_stmt* stmt )
: m_dbConnection( dbConnection ) : m_dbConnection( dbConnection )
...@@ -38,7 +40,7 @@ bool File::insert( sqlite3* dbConnection ) ...@@ -38,7 +40,7 @@ bool File::insert( sqlite3* dbConnection )
return true; return true;
} }
IAlbumTrack*File::albumTrack() IAlbumTrack* File::albumTrack()
{ {
if ( m_albumTrack == NULL && m_albumTrackId != 0 ) if ( m_albumTrack == NULL && m_albumTrackId != 0 )
{ {
...@@ -62,11 +64,32 @@ IShowEpisode*File::showEpisode() ...@@ -62,11 +64,32 @@ IShowEpisode*File::showEpisode()
std::vector<ILabel*> File::labels() std::vector<ILabel*> File::labels()
{ {
if ( m_labels == NULL )
{
m_labels = new std::vector<ILabel*>;
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) 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," "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)";
......
#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<IFile*> Label::files()
{
if ( m_files == NULL )
{
m_files = new std::vector<IFile*>;
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 );
}
#ifndef LABEL_H
#define LABEL_H
#include <sqlite3.h>
#include <string>
#include "ILabel.h"
class Label : public ILabel
{
public:
Label(sqlite3* dbConnection, sqlite3_stmt* stmt);
public:
virtual const std::string& name();
virtual std::vector<IFile*> files();
static bool createTable( sqlite3* dbConnection );
private:
sqlite3* m_dbConnection;
unsigned int m_id;
std::string m_name;
std::vector<IFile*>* m_files;
};
#endif // LABEL_H
...@@ -7,6 +7,8 @@ MediaLibrary::MediaLibrary() ...@@ -7,6 +7,8 @@ MediaLibrary::MediaLibrary()
bool MediaLibrary::initialize(const std::string& dbPath) bool MediaLibrary::initialize(const std::string& dbPath)
{ {
int res = sqlite3_open( dbPath.c_str(), &m_dbConnection ); int res = sqlite3_open( dbPath.c_str(), &m_dbConnection );
//FIXME:
// PRAGMA foreign_keys = ON;
return res == SQLITE_OK; return res == SQLITE_OK;
} }
......
...@@ -2,10 +2,10 @@ ...@@ -2,10 +2,10 @@
#include "SqliteTools.h" #include "SqliteTools.h"
bool SqliteTools::CreateTable( sqlite3 *db, const std::string& request ) bool SqliteTools::CreateTable( sqlite3 *db, const char* request )
{ {
sqlite3_stmt* stmt; 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 ) if ( res != SQLITE_OK )
{ {
std::cerr << "Failed to execute request: " << request << std::endl; std::cerr << "Failed to execute request: " << request << std::endl;
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
class SqliteTools class SqliteTools
{ {
public: public:
static bool CreateTable(sqlite3* db, const std::string& request ); static bool CreateTable(sqlite3* db, const char* request );
}; };
#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