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

Add file insertion

parent 5237f5a9
......@@ -19,6 +19,7 @@ class IFile
virtual unsigned int duration() = 0;
virtual IShowEpisode* showEpisode() = 0;
virtual int playCount() = 0;
virtual const std::string& mrl() = 0;
virtual std::vector<ILabel*> labels() = 0;
};
......
......@@ -11,7 +11,7 @@ class IMediaLibrary
public:
virtual ~IMediaLibrary() {}
virtual bool initialize( const std::string& dbPath ) = 0;
virtual IFile* addFile( const std::string& path ) = 0;
virtual const std::vector<IFile*>& files() = 0;
};
......
......@@ -19,6 +19,21 @@ File::File( sqlite3* dbConnection, sqlite3_stmt* stmt )
m_albumTrackId = sqlite3_column_int( stmt, 3 );
m_playCount = sqlite3_column_int( stmt, 4 );
m_showEpisodeId = sqlite3_column_int( stmt, 5 );
m_mrl = (const char*)sqlite3_column_text( stmt, 6 );
}
File::File( const std::string& mrl )
: m_dbConnection( NULL )
, m_type( UnknownType )
, m_duration( 0 )
, m_albumTrackId( 0 )
, m_playCount( 0 )
, m_showEpisodeId( 0 )
, m_mrl( mrl )
, m_albumTrack( NULL )
, m_showEpisode( NULL )
, m_labels( NULL )
{
}
bool File::insert( sqlite3* dbConnection )
......@@ -26,12 +41,19 @@ bool File::insert( sqlite3* dbConnection )
assert( m_dbConnection == NULL );
m_dbConnection = dbConnection;
sqlite3_stmt* stmt;
std::string req = "INSERT INTO File VALUES(NULL, ?, ?, ?)";
std::string req = "INSERT INTO File VALUES(NULL, ?, ?, ?, ?, ?, ?)";
if ( sqlite3_prepare_v2( m_dbConnection, req.c_str(), -1, &stmt, NULL ) != SQLITE_OK )
{
std::cerr << "Failed to insert record: " << sqlite3_errmsg( m_dbConnection ) << std::endl;
return false;
}
const char* tmpMrl = strdup( m_mrl.c_str() );
sqlite3_bind_int( stmt, 1, m_type );
sqlite3_bind_int( stmt, 2, m_duration );
sqlite3_bind_int( stmt, 3, m_albumTrackId );
sqlite3_bind_int( stmt, 4, m_playCount );
sqlite3_bind_int( stmt, 5, m_showEpisodeId );
sqlite3_bind_text( stmt, 6, tmpMrl, -1, &free );
if ( sqlite3_step( stmt ) != SQLITE_DONE )
return false;
m_id = sqlite3_last_insert_rowid( m_dbConnection );
......@@ -80,14 +102,21 @@ int File::playCount()
return m_playCount;
}
const std::string& File::mrl()
{
return m_mrl;
}
bool File::createTable(sqlite3* connection)
{
const char* req = "CREATE TABLE IF NOT EXISTS File("
"id_media INTEGER PRIMARY KEY AUTOINCREMENT,"
"type INTEGER, duration UNSIGNED INTEGER,"
"type INTEGER,"
"duration UNSIGNED INTEGER,"
"album_track_id UNSIGNED INTEGER,"
"play_count UNSIGNED INTEGER"
"show_episode_id UNSIGNED INTEGER"
"play_count UNSIGNED INTEGER,"
"show_episode_id UNSIGNED INTEGER,"
"mrl TEXT"
")";
return SqliteTools::createTable( connection, req );
}
......@@ -18,9 +18,11 @@ class File : public IFile
AudioType, // Any kind of audio file, not being an album track
ShowEpisodeType,
AlbumTrackType,
UnknownType,
};
File(sqlite3* dbConnection , sqlite3_stmt* stmt);
File( const std::string& mrl );
bool insert(sqlite3* dbConnection);
......@@ -29,6 +31,7 @@ class File : public IFile
virtual IShowEpisode* showEpisode();
virtual std::vector<ILabel*> labels();
virtual int playCount();
virtual const std::string& mrl();
static bool createTable( sqlite3* connection );
......@@ -42,6 +45,7 @@ class File : public IFile
unsigned int m_albumTrackId;
unsigned int m_playCount;
unsigned int m_showEpisodeId;
std::string m_mrl;
// Auto fetched related properties
Album* m_album;
......
......@@ -36,8 +36,19 @@ const std::vector<IFile*>& MediaLibrary::files()
{
if ( m_files == NULL )
{
const char* req = "SELECT * FROM Files";
const char* req = "SELECT * FROM File";
SqliteTools::fetchAll<File>( m_dbConnection, req, 0, m_files );
}
return *m_files;
}
IFile* MediaLibrary::addFile( const std::string& path )
{
File* f = new File( path );
if ( f->insert( m_dbConnection ) == false )
{
delete f;
return NULL;
}
return f;
}
......@@ -11,6 +11,7 @@ class MediaLibrary : public IMediaLibrary
MediaLibrary();
virtual bool initialize( const std::string& dbPath );
virtual const std::vector<IFile*>& files();
virtual IFile* addFile( const std::string& path );
private:
sqlite3* m_dbConnection;
......
......@@ -34,6 +34,10 @@ TEST_F( MLTest, InsertFile )
IFile* f = ml->addFile( "/dev/null" );
ASSERT_TRUE( f != NULL );
ASSERT_EQ( f->playCount(), 0 );
ASSERT_TRUE( f->albumTrack() == NULL );
ASSERT_TRUE( f->showEpisode() == NULL );
std::vector<IFile*> files = ml->files();
ASSERT_EQ( files.size(), 1u );
ASSERT_EQ( files[0]->mrl(), f->mrl() );
......
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