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

Add label insertion

parent ae0ff399
......@@ -20,6 +20,7 @@ class IFile
virtual IShowEpisode* showEpisode() = 0;
virtual int playCount() = 0;
virtual const std::string& mrl() = 0;
virtual ILabel* addLabel( const std::string& label ) = 0;
virtual std::vector<ILabel*> labels() = 0;
};
......
......@@ -24,6 +24,7 @@ File::File( sqlite3* dbConnection, sqlite3_stmt* stmt )
File::File( const std::string& mrl )
: m_dbConnection( NULL )
, m_id( 0 )
, m_type( UnknownType )
, m_duration( 0 )
, m_albumTrackId( 0 )
......@@ -39,12 +40,12 @@ File::File( const std::string& mrl )
bool File::insert( sqlite3* dbConnection )
{
assert( m_dbConnection == NULL );
m_dbConnection = dbConnection;
assert( m_id == 0 );
sqlite3_stmt* stmt;
std::string req = "INSERT INTO File VALUES(NULL, ?, ?, ?, ?, ?, ?)";
if ( sqlite3_prepare_v2( m_dbConnection, req.c_str(), -1, &stmt, NULL ) != SQLITE_OK )
const char* req = "INSERT INTO File VALUES(NULL, ?, ?, ?, ?, ?, ?)";
if ( sqlite3_prepare_v2( dbConnection, req, -1, &stmt, NULL ) != SQLITE_OK )
{
std::cerr << "Failed to insert record: " << sqlite3_errmsg( m_dbConnection ) << std::endl;
std::cerr << "Failed to insert record: " << sqlite3_errmsg( dbConnection ) << std::endl;
return false;
}
const char* tmpMrl = strdup( m_mrl.c_str() );
......@@ -56,7 +57,8 @@ bool File::insert( sqlite3* dbConnection )
sqlite3_bind_text( stmt, 6, tmpMrl, -1, &free );
if ( sqlite3_step( stmt ) != SQLITE_DONE )
return false;
m_id = sqlite3_last_insert_rowid( m_dbConnection );
m_id = sqlite3_last_insert_rowid( dbConnection );
m_dbConnection = dbConnection;
return true;
}
......@@ -89,8 +91,8 @@ std::vector<ILabel*> File::labels()
{
if ( m_labels == NULL )
{
const char* req = "SELECT * FROM Labels l"
"LEFT JOIN LabelFileRelation lfr ON lfr.id_label = f.id_label "
const char* req = "SELECT l.* FROM Label l "
"LEFT JOIN LabelFileRelation lfr ON lfr.id_label = l.id_label "
"WHERE lfr.id_file = ?";
SqliteTools::fetchAll<Label>( m_dbConnection, req, m_id, m_labels );
}
......@@ -107,10 +109,27 @@ const std::string& File::mrl()
return m_mrl;
}
ILabel* File::addLabel(const std::string& label)
{
Label* l = new Label( label );
if ( l->insert( m_dbConnection ) == false )
{
delete l;
return NULL;
}
l->link( this );
return l;
}
unsigned int File::id() const
{
return m_id;
}
bool File::createTable(sqlite3* connection)
{
const char* req = "CREATE TABLE IF NOT EXISTS File("
"id_media INTEGER PRIMARY KEY AUTOINCREMENT,"
"id_file INTEGER PRIMARY KEY AUTOINCREMENT,"
"type INTEGER,"
"duration UNSIGNED INTEGER,"
"album_track_id UNSIGNED INTEGER,"
......
......@@ -32,7 +32,9 @@ class File : public IFile
virtual std::vector<ILabel*> labels();
virtual int playCount();
virtual const std::string& mrl();
virtual ILabel* addLabel( const std::string &label );
unsigned int id() const;
static bool createTable( sqlite3* connection );
private:
......
......@@ -2,12 +2,22 @@
#include "File.h"
#include "SqliteTools.h"
#include <cassert>
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 );
m_id = sqlite3_column_int( stmt, 0 );
m_name = (const char*)sqlite3_column_text( stmt, 1 );
}
Label::Label( const std::string& name )
: m_dbConnection( NULL )
, m_id( 0 )
, m_name( name )
, m_files( NULL )
{
}
......@@ -20,7 +30,7 @@ std::vector<IFile*> Label::files()
{
if ( m_files == NULL )
{
const char* req = "SELECT * FROM Files f"
const char* req = "SELECT f.* FROM Files f "
"LEFT JOIN LabelFileRelation lfr ON lfr.id_file = f.id_file "
"WHERE lfr.id_label = ?";
SqliteTools::fetchAll<File>( m_dbConnection, req, m_id, m_files );
......@@ -28,6 +38,26 @@ std::vector<IFile*> Label::files()
return *m_files;
}
bool Label::insert( sqlite3* dbConnection )
{
assert( m_dbConnection == NULL );
assert( m_id == 0 );
sqlite3_stmt* stmt;
const char* req = "INSERT INTO Label VALUES(NULL, ?)";
if ( sqlite3_prepare_v2( dbConnection, req, -1, &stmt, NULL ) != SQLITE_OK )
{
std::cerr << "Failed to insert record: " << sqlite3_errmsg( dbConnection ) << std::endl;
return false;
}
char* tmpName = strdup( m_name.c_str() );
sqlite3_bind_text( stmt, 1, tmpName, -1, &free );
if ( sqlite3_step( stmt ) != SQLITE_DONE )
return false;
m_dbConnection = dbConnection;
m_id = sqlite3_last_insert_rowid( dbConnection );
return true;
}
bool Label::createTable(sqlite3* dbConnection)
{
const char* req = "CREATE TABLE IF NOT EXISTS Label("
......@@ -44,3 +74,24 @@ bool Label::createTable(sqlite3* dbConnection)
"FOREIGN KEY(id_file) REFERENCES File(id_file) ON DELETE CASCADE);";
return SqliteTools::createTable( dbConnection, req );
}
bool Label::link(File* file)
{
if ( m_dbConnection == NULL || m_id == 0 )
{
std::cerr << "A label needs to be inserted in database before being linked to a file" << std::endl;
return false;
}
const char* req = "INSERT INTO LabelFileRelation VALUES(?, ?)";
sqlite3_stmt* stmt;
if ( sqlite3_prepare_v2( m_dbConnection, req, -1, &stmt, NULL ) != SQLITE_OK )
{
std::cerr << "Failed to insert record: " << sqlite3_errmsg( m_dbConnection ) << std::endl;
return false;
}
sqlite3_bind_int( stmt, 1, m_id );
sqlite3_bind_int( stmt, 2, file->id() );
bool res = sqlite3_step( stmt ) == SQLITE_DONE;
sqlite3_finalize( stmt );
return res;
}
......@@ -5,18 +5,21 @@
#include <string>
#include "ILabel.h"
class File;
class Label : public ILabel
{
public:
Label(sqlite3* dbConnection, sqlite3_stmt* stmt);
Label( const std::string& name );
public:
virtual const std::string& name();
virtual std::vector<IFile*> files();
bool insert( sqlite3* dbConnection );
static bool createTable( sqlite3* dbConnection );
bool link( File* file );
private:
sqlite3* m_dbConnection;
unsigned int m_id;
......
#include "gtest/gtest.h"
#include "IMediaLibrary.h"
#include "ILabel.h"
class MLTest : public testing::Test
{
......@@ -41,4 +42,25 @@ TEST_F( MLTest, InsertFile )
std::vector<IFile*> files = ml->files();
ASSERT_EQ( files.size(), 1u );
ASSERT_EQ( files[0]->mrl(), f->mrl() );
delete f;
}
TEST_F( MLTest, AddLabel )
{
IFile* f = ml->addFile( "/dev/null" );
ILabel* l1 = f->addLabel( "sea otter" );
ILabel* l2 = f->addLabel( "cony the cone" );
ASSERT_TRUE( l1 != NULL );
ASSERT_TRUE( l2 != NULL );
std::vector<ILabel*> labels = f->labels();
ASSERT_EQ( labels.size(), 2u );
ASSERT_EQ( labels[0]->name(), "sea otter" );
ASSERT_EQ( labels[1]->name(), "cony the cone" );
delete l1;
delete l2;
delete f;
}
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