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-17 18:39:18 +00:00
|
|
|
constructor(config, db, logger) {
|
2019-12-15 02:11:21 +00:00
|
|
|
this.config = config;
|
2019-12-15 16:50:53 +00:00
|
|
|
this.db = db;
|
2019-12-17 18:39:18 +00:00
|
|
|
this.logger = logger;
|
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);
|
2019-12-17 18:38:55 +00:00
|
|
|
this.db.get("SELECT uuid, tag, label FROM library WHERE tag = ? ORDER BY RANDOM() LIMIT 1", tag, (err, row) => {
|
2019-12-15 02:11:21 +00:00
|
|
|
if (typeof row === 'undefined') {
|
2019-12-17 18:39:18 +00:00
|
|
|
this.logger.debug("no matching tag in sqlite backend", { tag: tag });
|
2019-12-15 02:11:21 +00:00
|
|
|
return callback();
|
|
|
|
}
|
2019-12-17 18:39:18 +00:00
|
|
|
this.logger.debug("found a matching tag", { tag: tag });
|
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-17 18:39:18 +00:00
|
|
|
module.exports = function(config, db, logger) {
|
|
|
|
return new SqliteBackend(config, db, logger);
|
2019-12-15 02:11:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// vim:ts=2 sw=2 et:
|