2019-12-15 02:11:21 +00:00
"use strict" ;
2019-12-15 17:14:55 +00:00
const Track = require ( './track' ) ;
2019-12-15 02:11:21 +00:00
class SqliteBackend {
2019-12-15 16:50:53 +00:00
constructor ( config , db ) {
2019-12-15 02:11:21 +00:00
this . config = config ;
2019-12-15 16:50:53 +00:00
this . db = db ;
2019-12-15 02:11:21 +00:00
}
find ( tag , callback ) {
2019-12-17 15:22:58 +00:00
this . db . run ( "INSERT INTO tags (tag) VALUES (?)" , tag ) ;
this . db . get ( "SELECT uuid, tag, label FROM library WHERE tag = ? AND _ROWID_ >= (abs(random()) % (SELECT max(_ROWID_) FROM tags)) LIMIT 1" , tag , ( err , row ) => {
2019-12-15 02:11:21 +00:00
if ( typeof row === 'undefined' ) {
return callback ( ) ;
}
2019-12-15 17:14:55 +00:00
callback ( new Track ( row [ 'tag' ] , row [ 'uuid' ] , row [ 'label' ] ) ) ;
2019-12-15 02:11:21 +00:00
} ) ;
}
}
2019-12-15 16:50:53 +00:00
module . exports = function ( config , db ) {
return new SqliteBackend ( config , db ) ;
2019-12-15 02:11:21 +00:00
} ;
// vim:ts=2 sw=2 et: