Added generic query methods for Albums, Artists, and Media Files.
This commit is contained in:
parent
67ea1535a1
commit
321b201cab
@ -40,7 +40,7 @@ public class ORM {
|
||||
return sql.rows(query).collect { recordToModel(modelClass, it) } }
|
||||
|
||||
public def getById(Class modelClass, int id) {
|
||||
def model = modelClass.instantiate()
|
||||
def model = modelClass.newInstance()
|
||||
model.id = id
|
||||
return refresh(model) }
|
||||
|
||||
@ -52,7 +52,7 @@ public class ORM {
|
||||
.toString()
|
||||
|
||||
logger.debug("Selecting model.\n\tSQL: {}\n\tPARAMS: {}", query, model.id)
|
||||
return updateModel(sql.firstRow(query, [id]), model) }
|
||||
return updateModel(sql.firstRow(query, [model.id]), model) }
|
||||
|
||||
public def getByName(Class modelClass, String name) {
|
||||
def query = new StringBuilder()
|
||||
@ -62,7 +62,7 @@ public class ORM {
|
||||
.toString()
|
||||
|
||||
logger.debug("Selecting model.\n\tSQL: {}\n\tPARAMS: {}", query, name)
|
||||
return sql.rows(query, [name]).collect { recordToModel(it, modelClass) } }
|
||||
return sql.rows(query, [name]).collect { recordToModel(modelClass, it) } }
|
||||
|
||||
public def getBy(Class modelClass, List<String> columns,
|
||||
List<Object> values) {
|
||||
@ -75,7 +75,7 @@ public class ORM {
|
||||
|
||||
logger.debug("Selecting models.\n\tSQL: {}\n\tPARAMS: {}", query, values)
|
||||
return sql.rows(query, values)
|
||||
.collect { recordToModel(it, modelClass) } }
|
||||
.collect { recordToModel(modelClass, it) } }
|
||||
|
||||
public def getLike(Class modelClass, List<String> columns,
|
||||
List<Object> values) {
|
||||
@ -89,7 +89,7 @@ public class ORM {
|
||||
|
||||
logger.debug("Selecting models.\n\tSQL: {}\n\tPARAMS: {}", query, values)
|
||||
return sql.rows(query, values)
|
||||
.collect { recordToModel(it, modelClass) } }
|
||||
.collect { recordToModel(modelClass, it) } }
|
||||
|
||||
public def save(def model) {
|
||||
if (model.id > 0) return update(model)
|
||||
@ -150,11 +150,13 @@ public class ORM {
|
||||
.append("WHERE id = ?")
|
||||
.toString()
|
||||
|
||||
logger.debug("Deleting model.\n\tSQL: {}\n\tPARAMS: {}", query, model.id)
|
||||
logger.debug("Deleting model.\n\tSQL: {}\n\tPARAMS: {}",
|
||||
query, model.id)
|
||||
sql.execute(query, [model.id])
|
||||
return sql.updateCount }
|
||||
|
||||
public def associate(Class modelClass1, Class modelClass2 , Integer firstId, Integer secondId) {
|
||||
public def associate(Class modelClass1, Class modelClass2 ,
|
||||
Integer firstId, Integer secondId) {
|
||||
String linkTable = pluralize(nameFromModel(modelClass1.simpleName)) +
|
||||
"_" + pluralize(nameFromModel(modelClass2.simpleName))
|
||||
String col1 = nameFromModel(modelClass1.simpleName) + "_id"
|
||||
@ -179,58 +181,56 @@ public class ORM {
|
||||
public def associate(def m1, def m2) {
|
||||
return associate(m1.class, m2.class, m1.id, m2.id) }
|
||||
|
||||
|
||||
/// ### Album-specific methods
|
||||
// =======================================================================
|
||||
public Album getAlbumById(int id) { return getById(Album, id) }
|
||||
|
||||
public Album[] getAlbumsByNameAndYear(String name, int year) {
|
||||
return getBy(Album, ['name', 'year'], [name, year]) }
|
||||
|
||||
public List<Album> getAlbumsByName(String name) {
|
||||
return getByName(Album, name) }
|
||||
|
||||
public Album getAlbumByNameAndArtistId(String name, int artistId) {
|
||||
def query = """\
|
||||
SELECT al.*
|
||||
FROM albums al JOIN
|
||||
artists_albums aa ON
|
||||
al.id = aa.album_id AND
|
||||
aa.artist_id = ?
|
||||
WHERE al.name = ?"""
|
||||
def params = [artistId, name]
|
||||
logger.debug("Selecting an album.\n\tSQL: {}\n\tPARAMS: {}",
|
||||
query, params)
|
||||
return recordToModel(Album, sql.firstRow(query, params)) }
|
||||
|
||||
public Album getAlbumByNameAndYearAndArtistId(String name, int year,
|
||||
int artistId) {
|
||||
def query = """\
|
||||
SELECT al.*
|
||||
FROM albums al JOIN
|
||||
artists_albums aa ON
|
||||
al.id = aa.album_id AND
|
||||
aa.artist_id = ?
|
||||
WHERE al.name = ? AND al.year = ?"""
|
||||
def params = [artistId, name, year]
|
||||
logger.debug("Selecting an album. \n\tSQL: {}\n\tPARAMS: {}",
|
||||
query, params)
|
||||
return recordToModel(Album, sql.firstRow(query, params)) }
|
||||
|
||||
public List<Album> getAlbumsByArtistId(int artistId) {
|
||||
def query = """\
|
||||
SELECT al.*
|
||||
FROM albums al JOIN
|
||||
artists_albums aa ON
|
||||
al.id = aa.album_id AND
|
||||
aa.artist_id = ?"""
|
||||
|
||||
logger.debug("Selecting albums.\n\tSQL: {}\n\tPARAMS: {}", query, artistId)
|
||||
return sql.rows(query, [artistId])
|
||||
.collect { recordToModel(Album, it) } }
|
||||
|
||||
public List<Album> getAlbums() { return getAll(Album) }
|
||||
public List<Album> getAlbumsLikeName(String name) {
|
||||
return getLike(Album, ["name"], [name]) }
|
||||
|
||||
public List<Album> getAlbumsWhere(Map params) {
|
||||
def query = new StringBuilder()
|
||||
def sqlParams = []
|
||||
|
||||
query.append("SELECT al.* FROM albums al ")
|
||||
|
||||
if (params.artistId) {
|
||||
query.append(" JOIN artists_albums aa ON al.id = aa.album_id ")
|
||||
query.append(" AND aa.artist_id = ? ")
|
||||
sqlParams << params.artistId }
|
||||
|
||||
if (params.mediaFileId || params.playlistId) {
|
||||
query.append(" JOIN albums_media_files amf ON ")
|
||||
query.append(" al.id = amf.album_id ")
|
||||
|
||||
if (params.mediaFileId) {
|
||||
query.append(" AND amf.media_file_id = ? ")
|
||||
sqlParams << params.mediaFileId } }
|
||||
|
||||
if (params.playlistId) {
|
||||
query.append(" JOIN playlists_media_files pmf ON ")
|
||||
query.append(" amf.media_file_id = pmf.media_file_id AND ")
|
||||
query.append(" pmf.playlist_id = ?")
|
||||
sqlParams.append(params.playlistId) }
|
||||
|
||||
if (params.name || params.year) { query.append(" WHERE ") }
|
||||
|
||||
if (params.year) {
|
||||
query.append(" al.year = ? ")
|
||||
sqlParams << params.year }
|
||||
|
||||
if (params.name) {
|
||||
if (params.year) query.append(" AND ")
|
||||
query.append(" al.name = ? ")
|
||||
sqlParams << params.name }
|
||||
|
||||
query = query.toString()
|
||||
logger.debug("Selecting albums.\n\tSQL: {}\n\tPARAMS: {}",
|
||||
query, sqlParams)
|
||||
return sql.rows(query, sqlParams).collect { recordToModel(Album, it) } }
|
||||
|
||||
public List<Album> removeEmptyAlbums() {
|
||||
throw new UnsupportedOperationException("Not yet implemented.");
|
||||
}
|
||||
@ -242,16 +242,40 @@ public class ORM {
|
||||
if (artists) return artists[0] }
|
||||
|
||||
public List<Artist> getArtists() { return getAll(Artist) }
|
||||
public List<Artist> getArtistsByAlbumId(int albumId) {
|
||||
var query = """\
|
||||
SELECT ar.*
|
||||
FROM artists ar JOIN
|
||||
artists_albums aa ON
|
||||
ar.id = aa.artist_id AND
|
||||
aa.album_id = ?"""
|
||||
logger.debug("Selecting artists.\n\tSQL: {}\n\tPARAMS: {}", query, artistId)
|
||||
return sql.rows(query, [albumId])
|
||||
.collect { recordToModel(Artist, it) } }
|
||||
|
||||
public List<Artist> getArtistsWhere(Map params) {
|
||||
def query = new StringBuilder()
|
||||
def sqlParams = []
|
||||
|
||||
query.append("SELECT ar.* FROM artists ar ")
|
||||
|
||||
if (params.albumId) {
|
||||
query.append(" JOIN artists_albums aa ON ar.id = aa.artist_id ")
|
||||
query.append(" AND aa.album_id = ? ")
|
||||
sqlParams << params.albumId }
|
||||
|
||||
if (params.mediaFileId || params.playlistId) {
|
||||
query.append(" JOIN artists_media_files amf ON ")
|
||||
query.append(" ar.id = amf.artist_id ")
|
||||
|
||||
if (params.mediaFileId) {
|
||||
query.append(" AND amf.media_file_id = ? ")
|
||||
sqlParams << params.mediaFileId } }
|
||||
|
||||
if (params.playlistId) {
|
||||
query.append(" JOIN playlists_media_files pmf ON ")
|
||||
query.append(" amf.media_file_id = pmf.media_file_id AND ")
|
||||
query.append(" pmf.playlist_id = ?")
|
||||
sqlParams.append(params.playlistId) }
|
||||
|
||||
if (params.name) {
|
||||
query.append(" WHERE ar.name = ? ")
|
||||
sqlParams << params.name }
|
||||
|
||||
query = query.toString()
|
||||
logger.debug("Selecting artists.\n\tSQL: {}\n\tPARAMS: {}",
|
||||
query, sqlParams)
|
||||
return sql.rows(query, sqlParams).collect { recordToModel(Artist, it) } }
|
||||
|
||||
public List<Artist> getArtistsLikeName(String name) {
|
||||
return getLike(Artist, ["name"], [name]) }
|
||||
@ -289,6 +313,55 @@ public class ORM {
|
||||
def files = getBy(MediaFile, ["file_path"], [filePath])
|
||||
return files ? files[0] : null }
|
||||
|
||||
public List<MediaFile> getMediaFilesWhere(Map params) {
|
||||
def query = new StringBuilder()
|
||||
def sqlParams = []
|
||||
|
||||
query.append("SELECT mf.* FROM media_files mf ")
|
||||
|
||||
if (params.artistId) {
|
||||
query.append(" JOIN artists_media_files armf ON ")
|
||||
.append(" mf.id = armf.media_file_id ")
|
||||
query.append(" AND armf.artist_id = ? ")
|
||||
sqlParams << params.artistId }
|
||||
|
||||
if (params.albumId) {
|
||||
query.append(" JOIN albums_media_files almf ON ")
|
||||
.append(" al.id = almf.album_id ")
|
||||
query.append(" AND almf.album_id = ? ")
|
||||
sqlParams << params.albumId }
|
||||
|
||||
if (params.playlistId) {
|
||||
query.append(" JOIN playlists_media_files pmf ON ")
|
||||
query.append(" pmf.media_file_id = mf.id AND ")
|
||||
query.append(" pmf.playlist_id = ?")
|
||||
sqlParams.append(params.playlistId) }
|
||||
|
||||
if (params.name || params.fileHash || params.filePath ||
|
||||
params.trackNumber) { query.append(" WHERE ") }
|
||||
|
||||
if (params.name) {
|
||||
query.append(" WHERE mf.name = ? ")
|
||||
sqlParams << params.name }
|
||||
|
||||
if (params.trackNumber) {
|
||||
query.append(" WHERE mf.track_number = ? ")
|
||||
sqlParams << params.trackNumber }
|
||||
|
||||
if (params.filePath) {
|
||||
query.append(" WHERE mf.file_path = ? ")
|
||||
sqlParams << params.filePath }
|
||||
|
||||
if (params.fileHash) {
|
||||
query.append(" WHERE mf.file_hash = ? ")
|
||||
sqlParams << params.fileHash }
|
||||
|
||||
query = query.toString()
|
||||
logger.debug("Selecting artists.\n\tSQL: {}\n\tPARAMS: {}",
|
||||
query, sqlParams)
|
||||
return sql.rows(query, sqlParams).collect { recordToModel(MediaFile, it) } }
|
||||
|
||||
|
||||
public def associateMediaFileWithAlbum(int mediaFileId, int albumId) {
|
||||
return associate(Album, MediaFile, albumId, mediaFileId) }
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user