Commit be265d17 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

sqlite: Traits: Provide a specialization for enum types

parent cd934ade
...@@ -81,8 +81,7 @@ std::shared_ptr<Media> Media::create( DBConnection dbConnection, Type type, cons ...@@ -81,8 +81,7 @@ std::shared_ptr<Media> Media::create( DBConnection dbConnection, Type type, cons
static const std::string req = "INSERT INTO " + policy::MediaTable::Name + static const std::string req = "INSERT INTO " + policy::MediaTable::Name +
"(type, mrl, folder_id, last_modification_date, title) VALUES(?, ?, ?, ?, ?)"; "(type, mrl, folder_id, last_modification_date, title) VALUES(?, ?, ?, ?, ?)";
using type_t = std::underlying_type<Type>::type; if ( _Cache::insert( dbConnection, self, req, type, self->m_mrl, sqlite::ForeignKey( folderId ),
if ( _Cache::insert( dbConnection, self, req, static_cast<type_t>( type ), self->m_mrl, sqlite::ForeignKey( folderId ),
self->m_lastModificationDate, self->m_title) == false ) self->m_lastModificationDate, self->m_title) == false )
return nullptr; return nullptr;
self->m_dbConnection = dbConnection; self->m_dbConnection = dbConnection;
...@@ -297,8 +296,7 @@ bool Media::setType( Type type ) ...@@ -297,8 +296,7 @@ bool Media::setType( Type type )
static const std::string req = "UPDATE " + policy::MediaTable::Name static const std::string req = "UPDATE " + policy::MediaTable::Name
+ " SET type = ? WHERE id_media = ?"; + " SET type = ? WHERE id_media = ?";
// We need to convert to an integer representation for the sqlite traits to work properly // We need to convert to an integer representation for the sqlite traits to work properly
using type_t = std::underlying_type<Type>::type; if ( sqlite::Tools::executeUpdate( m_dbConnection, req, type, m_id ) == false )
if ( sqlite::Tools::executeUpdate( m_dbConnection, req, static_cast<type_t>( type ), m_id ) == false )
return false; return false;
m_type = type; m_type = type;
return true; return true;
......
...@@ -166,16 +166,13 @@ std::vector<MediaPtr> MediaLibrary::files() ...@@ -166,16 +166,13 @@ std::vector<MediaPtr> MediaLibrary::files()
std::vector<MediaPtr> MediaLibrary::audioFiles() std::vector<MediaPtr> MediaLibrary::audioFiles()
{ {
static const std::string req = "SELECT * FROM " + policy::MediaTable::Name + " WHERE type = ?"; static const std::string req = "SELECT * FROM " + policy::MediaTable::Name + " WHERE type = ?";
//FIXME: Replace this with template magic in sqlite's traits return Media::fetchAll( m_dbConnection.get(), req, IMedia::Type::AudioType );
using type_t = std::underlying_type<IMedia::Type>::type;
return Media::fetchAll( m_dbConnection.get(), req, static_cast<type_t>( IMedia::Type::AudioType ) );
} }
std::vector<MediaPtr> MediaLibrary::videoFiles() std::vector<MediaPtr> MediaLibrary::videoFiles()
{ {
static const std::string req = "SELECT * FROM " + policy::MediaTable::Name + " WHERE type = ?"; static const std::string req = "SELECT * FROM " + policy::MediaTable::Name + " WHERE type = ?";
using type_t = std::underlying_type<IMedia::Type>::type; return Media::fetchAll( m_dbConnection.get(), req, IMedia::Type::VideoType );
return Media::fetchAll( m_dbConnection.get(), req, static_cast<type_t>( IMedia::Type::VideoType ) );
} }
MediaPtr MediaLibrary::file( const std::string& path ) MediaPtr MediaLibrary::file( const std::string& path )
......
...@@ -113,6 +113,24 @@ struct Traits<std::nullptr_t> ...@@ -113,6 +113,24 @@ struct Traits<std::nullptr_t>
} }
}; };
template <typename T>
struct Traits<T, typename std::enable_if<std::is_enum<
typename std::decay<T>::type
>::value>::type>
{
using type_t = typename std::underlying_type<typename std::decay<T>::type>::type;
static int Bind(sqlite3_stmt* stmt, int pos, T value )
{
return sqlite3_bind_int( stmt, pos, static_cast<type_t>( value ) );
}
static T Load( sqlite3_stmt* stmt, int pos )
{
return static_cast<T>( sqlite3_column_int( stmt, pos ) );
}
};
template <typename T> template <typename T>
struct Traits<T, typename std::enable_if<IsSameDecay<T, int64_t>::value>::type> struct Traits<T, typename std::enable_if<IsSameDecay<T, int64_t>::value>::type>
{ {
......
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