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

Expect std::string in SqliteTools.

So we don't have to call .c_str() every 10 lines
parent 8aeae861
......@@ -88,7 +88,7 @@ AlbumPtr Album::create( sqlite3* dbConnection, const std::string& id3Tag )
auto album = std::make_shared<Album>( id3Tag );
static const std::string& req = "INSERT INTO " + policy::AlbumTable::Name +
"(id_album, id3tag) VALUES(NULL, ?)";
if ( _Cache::insert( dbConnection, album, req.c_str(), id3Tag ) == false )
if ( _Cache::insert( dbConnection, album, req, id3Tag ) == false )
return nullptr;
album->m_dbConnection = dbConnection;
return album;
......
......@@ -50,7 +50,7 @@ AlbumTrackPtr AlbumTrack::create(sqlite3* dbConnection, unsigned int albumId, co
auto self = std::make_shared<AlbumTrack>( name, trackNb, albumId );
static const std::string req = "INSERT INTO " + policy::AlbumTrackTable::Name
+ "(title, track_number, album_id) VALUES(?, ?, ?)";
if ( _Cache::insert( dbConnection, self, req.c_str(), name, trackNb, albumId ) == false )
if ( _Cache::insert( dbConnection, self, req, name, trackNb, albumId ) == false )
return false;
self->m_dbConnection = dbConnection;
return self;
......
......@@ -125,7 +125,7 @@ class Cache
* Create a new instance of the cache class.
*/
template <typename... Args>
static bool insert( sqlite3* dbConnection, std::shared_ptr<IMPL> self, const char* req, const Args&... args )
static bool insert( sqlite3* dbConnection, std::shared_ptr<IMPL> self, const std::string& req, const Args&... args )
{
unsigned int pKey = SqliteTools::insert( dbConnection, req, args... );
if ( pKey == 0 )
......
......@@ -42,7 +42,7 @@ FilePtr File::create( sqlite3* dbConnection, const std::string& mrl )
auto self = std::make_shared<File>( mrl );
static const std::string req = "INSERT INTO " + policy::FileTable::Name +
" VALUES(NULL, ?, ?, ?, ?, ?, ?)";
bool pKey = _Cache::insert( dbConnection, self, req.c_str(), (int)self->m_type, self->m_duration,
bool pKey = _Cache::insert( dbConnection, self, req, (int)self->m_type, self->m_duration,
self->m_albumTrackId, self->m_playCount, self->m_showEpisodeId, self->m_mrl );
if ( pKey == false )
return nullptr;
......@@ -79,7 +79,7 @@ std::vector<std::shared_ptr<ILabel> > File::labels()
static const std::string req = "SELECT l.* FROM " + policy::LabelTable::Name + " l "
"LEFT JOIN LabelFileRelation lfr ON lfr.id_label = l.id_label "
"WHERE lfr.id_file = ?";
SqliteTools::fetchAll<Label>( m_dbConnection, req.c_str(), labels, m_id );
SqliteTools::fetchAll<Label>( m_dbConnection, req, labels, m_id );
return labels;
}
......@@ -109,7 +109,7 @@ bool File::createTable(sqlite3* connection)
"show_episode_id UNSIGNED INTEGER,"
"mrl TEXT UNIQUE ON CONFLICT FAIL"
")";
return SqliteTools::executeRequest( connection, req.c_str() );
return SqliteTools::executeRequest( connection, req );
}
bool File::addLabel( LabelPtr label )
......
......@@ -40,7 +40,7 @@ bool Label::files( std::vector<FilePtr>& files )
static const std::string req = "SELECT f.* FROM " + policy::FileTable::Name + " f "
"LEFT JOIN LabelFileRelation lfr ON lfr.id_file = f.id_file "
"WHERE lfr.id_label = ?";
return SqliteTools::fetchAll<File>( m_dbConnection, req.c_str(), files, m_id );
return SqliteTools::fetchAll<File>( m_dbConnection, req, files, m_id );
}
LabelPtr Label::create( sqlite3* dbConnection, const std::string& name )
......@@ -59,7 +59,7 @@ bool Label::createTable(sqlite3* dbConnection)
"id_label INTEGER PRIMARY KEY AUTOINCREMENT, "
"name TEXT UNIQUE ON CONFLICT FAIL"
")";
if ( SqliteTools::executeRequest( dbConnection, req.c_str() ) == false )
if ( SqliteTools::executeRequest( dbConnection, req ) == false )
return false;
req = "CREATE TABLE IF NOT EXISTS LabelFileRelation("
"id_label INTEGER,"
......@@ -67,7 +67,7 @@ bool Label::createTable(sqlite3* dbConnection)
"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::executeRequest( dbConnection, req.c_str() );
return SqliteTools::executeRequest( dbConnection, req );
}
const std::string&policy::LabelCachePolicy::key( const std::shared_ptr<ILabel> self )
......
......@@ -87,7 +87,7 @@ AlbumPtr MediaLibrary::album( const std::string& id3Tag )
// We can't use Cache helper, since albums are cached by primary keys
static const std::string req = "SELECT * FROM " + policy::AlbumTable::Name +
" WHERE id3tag = ?";
return SqliteTools::fetchOne<Album>( m_dbConnection, req.c_str(), id3Tag );
return SqliteTools::fetchOne<Album>( m_dbConnection, req, id3Tag );
}
AlbumPtr MediaLibrary::createAlbum( const std::string& id3Tag )
......
......@@ -53,7 +53,7 @@ class SqliteTools
* be discarded.
*/
template <typename IMPL, typename INTF, typename... Args>
static bool fetchAll( sqlite3* dbConnection, const char* req, std::vector<std::shared_ptr<INTF> >& results, const Args&... args )
static bool fetchAll( sqlite3* dbConnection, const std::string& req, std::vector<std::shared_ptr<INTF> >& results, const Args&... args )
{
results.clear();
auto stmt = prepareRequest( dbConnection, req, args...);
......@@ -70,7 +70,7 @@ class SqliteTools
}
template <typename T, typename... Args>
static std::shared_ptr<T> fetchOne( sqlite3* dbConnection, const char* req, const Args&... args )
static std::shared_ptr<T> fetchOne( sqlite3* dbConnection, const std::string& req, const Args&... args )
{
std::shared_ptr<T> result;
auto stmt = prepareRequest( dbConnection, req, args... );
......@@ -83,7 +83,7 @@ class SqliteTools
}
template <typename... Args>
static bool executeDelete( sqlite3* dbConnection, const char* req, const Args&... args )
static bool executeDelete( sqlite3* dbConnection, const std::string& req, const Args&... args )
{
if ( executeRequest( dbConnection, req, args... ) == false )
return false;
......@@ -95,7 +95,7 @@ class SqliteTools
* exposed to the caller.
*/
template <typename... Args>
static bool executeRequest( sqlite3* dbConnection, const char* req, const Args&... args )
static bool executeRequest( sqlite3* dbConnection, const std::string& req, const Args&... args )
{
auto stmt = prepareRequest( dbConnection, req, args... );
if ( stmt == nullptr )
......@@ -119,7 +119,7 @@ class SqliteTools
* Returns 0 (which is an invalid sqlite primary key) when insertion fails.
*/
template <typename... Args>
static unsigned int insert( sqlite3* dbConnection, const char* req, const Args&... args )
static unsigned int insert( sqlite3* dbConnection, const std::string& req, const Args&... args )
{
if ( executeRequest( dbConnection, req, args... ) == false )
return 0;
......@@ -128,16 +128,16 @@ class SqliteTools
private:
template <typename... Args>
static StmtPtr prepareRequest( sqlite3* dbConnection, const char* req, const Args&... args )
static StmtPtr prepareRequest( sqlite3* dbConnection, const std::string& req, const Args&... args )
{
return _prepareRequest<1>( dbConnection, req, args... );
}
template <unsigned int>
static StmtPtr _prepareRequest( sqlite3* dbConnection, const char* req )
static StmtPtr _prepareRequest( sqlite3* dbConnection, const std::string& req )
{
sqlite3_stmt* stmt = nullptr;
int res = sqlite3_prepare_v2( dbConnection, req, -1, &stmt, NULL );
int res = sqlite3_prepare_v2( dbConnection, req.c_str(), -1, &stmt, NULL );
if ( res != SQLITE_OK )
{
std::cerr << "Failed to execute request: " << req << std::endl;
......@@ -147,7 +147,7 @@ class SqliteTools
}
template <unsigned int COLIDX, typename T, typename... Args>
static StmtPtr _prepareRequest( sqlite3* dbConnection, const char* req, const T& arg, const Args&... args )
static StmtPtr _prepareRequest( sqlite3* dbConnection, const std::string& req, const T& arg, const Args&... args )
{
auto stmt = _prepareRequest<COLIDX + 1>( dbConnection, req, args... );
Traits<T>::Bind( stmt.get(), COLIDX, arg );
......
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