Added generic query methods for Albums, Artists, and Media Files.

This commit is contained in:
Jonathan Bernard 2016-02-13 23:28:30 -06:00
parent 67ea1535a1
commit 321b201cab

View File

@ -40,7 +40,7 @@ public class ORM {
return sql.rows(query).collect { recordToModel(modelClass, it) } } return sql.rows(query).collect { recordToModel(modelClass, it) } }
public def getById(Class modelClass, int id) { public def getById(Class modelClass, int id) {
def model = modelClass.instantiate() def model = modelClass.newInstance()
model.id = id model.id = id
return refresh(model) } return refresh(model) }
@ -52,7 +52,7 @@ public class ORM {
.toString() .toString()
logger.debug("Selecting model.\n\tSQL: {}\n\tPARAMS: {}", query, model.id) 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) { public def getByName(Class modelClass, String name) {
def query = new StringBuilder() def query = new StringBuilder()
@ -62,7 +62,7 @@ public class ORM {
.toString() .toString()
logger.debug("Selecting model.\n\tSQL: {}\n\tPARAMS: {}", query, name) 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, public def getBy(Class modelClass, List<String> columns,
List<Object> values) { List<Object> values) {
@ -75,7 +75,7 @@ public class ORM {
logger.debug("Selecting models.\n\tSQL: {}\n\tPARAMS: {}", query, values) logger.debug("Selecting models.\n\tSQL: {}\n\tPARAMS: {}", query, values)
return sql.rows(query, values) return sql.rows(query, values)
.collect { recordToModel(it, modelClass) } } .collect { recordToModel(modelClass, it) } }
public def getLike(Class modelClass, List<String> columns, public def getLike(Class modelClass, List<String> columns,
List<Object> values) { List<Object> values) {
@ -89,7 +89,7 @@ public class ORM {
logger.debug("Selecting models.\n\tSQL: {}\n\tPARAMS: {}", query, values) logger.debug("Selecting models.\n\tSQL: {}\n\tPARAMS: {}", query, values)
return sql.rows(query, values) return sql.rows(query, values)
.collect { recordToModel(it, modelClass) } } .collect { recordToModel(modelClass, it) } }
public def save(def model) { public def save(def model) {
if (model.id > 0) return update(model) if (model.id > 0) return update(model)
@ -150,11 +150,13 @@ public class ORM {
.append("WHERE id = ?") .append("WHERE id = ?")
.toString() .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]) sql.execute(query, [model.id])
return sql.updateCount } 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)) + String linkTable = pluralize(nameFromModel(modelClass1.simpleName)) +
"_" + pluralize(nameFromModel(modelClass2.simpleName)) "_" + pluralize(nameFromModel(modelClass2.simpleName))
String col1 = nameFromModel(modelClass1.simpleName) + "_id" String col1 = nameFromModel(modelClass1.simpleName) + "_id"
@ -179,58 +181,56 @@ public class ORM {
public def associate(def m1, def m2) { public def associate(def m1, def m2) {
return associate(m1.class, m2.class, m1.id, m2.id) } return associate(m1.class, m2.class, m1.id, m2.id) }
/// ### Album-specific methods /// ### Album-specific methods
// =======================================================================
public Album getAlbumById(int id) { return getById(Album, id) } 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> getAlbums() { return getAll(Album) }
public List<Album> getAlbumsLikeName(String name) { public List<Album> getAlbumsLikeName(String name) {
return getLike(Album, ["name"], [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() { public List<Album> removeEmptyAlbums() {
throw new UnsupportedOperationException("Not yet implemented."); throw new UnsupportedOperationException("Not yet implemented.");
} }
@ -242,16 +242,40 @@ public class ORM {
if (artists) return artists[0] } if (artists) return artists[0] }
public List<Artist> getArtists() { return getAll(Artist) } public List<Artist> getArtists() { return getAll(Artist) }
public List<Artist> getArtistsByAlbumId(int albumId) {
var query = """\ public List<Artist> getArtistsWhere(Map params) {
SELECT ar.* def query = new StringBuilder()
FROM artists ar JOIN def sqlParams = []
artists_albums aa ON
ar.id = aa.artist_id AND query.append("SELECT ar.* FROM artists ar ")
aa.album_id = ?"""
logger.debug("Selecting artists.\n\tSQL: {}\n\tPARAMS: {}", query, artistId) if (params.albumId) {
return sql.rows(query, [albumId]) query.append(" JOIN artists_albums aa ON ar.id = aa.artist_id ")
.collect { recordToModel(Artist, it) } } 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) { public List<Artist> getArtistsLikeName(String name) {
return getLike(Artist, ["name"], [name]) } return getLike(Artist, ["name"], [name]) }
@ -289,6 +313,55 @@ public class ORM {
def files = getBy(MediaFile, ["file_path"], [filePath]) def files = getBy(MediaFile, ["file_path"], [filePath])
return files ? files[0] : null } 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) { public def associateMediaFileWithAlbum(int mediaFileId, int albumId) {
return associate(Album, MediaFile, albumId, mediaFileId) } return associate(Album, MediaFile, albumId, mediaFileId) }