Added *Where functions for playlists, bookmarks, tags. Bugfixes.

This commit is contained in:
Jonathan Bernard 2016-02-15 16:31:32 -06:00
parent a17589d540
commit 2010aa7d46
2 changed files with 127 additions and 8 deletions

View File

@ -94,7 +94,9 @@ public class MediaLibrary {
public def getByIdOrName(Class modelClass, String input) {
def match
if (safeToInteger(input)) match = [orm.getById(modelClass, safeToInteger(input))]
if (safeToInteger(input)) {
match = orm.getById(modelClass, safeToInteger(input))
if (match) match = [match] }
else {
match = orm.getByName(modelClass, input)
if (!match) match = orm.getLike(modelClass, ["name"], [input]) }
@ -159,11 +161,11 @@ public class MediaLibrary {
album = artists.inject(null, { foundAlbum, artist ->
if (foundAlbum) return foundAlbum
def cur = orm.getAlbumsWhere(
name: albumName, artistId: artistId)
name: albumName, artistId: artist.id)
return cur ? cur[0] : null })
if (!album) {
def cur = org.getAlbumsWhere(name: albumName)
def cur = orm.getAlbumsWhere(name: albumName)
album = cur ? cur[0] : 0 } }
// We still can't find the album at all. We'll need to create it

View File

@ -52,7 +52,9 @@ public class ORM {
.toString()
logger.debug("Selecting model.\n\tSQL: {}\n\tPARAMS: {}", query, model.id)
return updateModel(sql.firstRow(query, [model.id]), model) }
def result = sql.firstRow(query, [model.id])
if (!result) return null
return updateModel(result, model) }
public def getByName(Class modelClass, String name) {
def query = new StringBuilder()
@ -213,7 +215,7 @@ public class ORM {
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) }
sqlParams << params.playlistId }
if (params.name || params.year) { query.append(" WHERE ") }
@ -266,7 +268,7 @@ public class ORM {
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) }
sqlParams << params.playlistId }
if (params.name) {
query.append(" WHERE ar.name = ? ")
@ -295,6 +297,40 @@ public class ORM {
public List<Bookmark> getBookmarks() { return getAll(Bookmark) }
public List<Bookmark> getBookmarksWhere(Map options) {
def query = new StringBuilder()
def sqlParams = []
query.append("SELECT b.* FROM bookmarks b ")
if (params.size() > 0) query.append(" WHERE ")
if (params.playlistId) {
query.append(" b.playlist_id = ? ")
sqlParams << params.playlistId }
if (params.userCreated != null) {
query.append(" b.user_created = ? ")
sqlParams << params.userCreated }
if (params.name) {
query.append (" b.name = ? ")
sqlParams << params.name }
if (params.mediaFileId) {
query.append(" b.media_file_id = ? ")
sqlParams << params.mediaFileId }
if (params.playIndex) {
query.append(" b.play_index = ? ")
sqlParams << params.playIndex }
query = query.toString()
logger.debug("Selecting bookmarks.\n\tSQL: {}\n\tPARAMS: {}",
query, sqlParams)
return sql.rows(query, sqlParams)
.collect { recordToModel(Bookmark, it) } }
/// ### Image-specific methods
public Image getImageById(int id) { return getById(Image, id) }
@ -335,7 +371,7 @@ public class ORM {
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) }
sqlParams << params.playlistId }
if (params.name || params.fileHash || params.filePath ||
params.trackNumber) { query.append(" WHERE ") }
@ -391,6 +427,52 @@ public class ORM {
public List<Playlist> getPlaylists() { return getAll(Playlist) }
public List<Playlist> getPlaylistsWhere(Map params) {
def query = new StringBuilder()
def sqlParams = []
query.append("SELECT p.* FROM playlists p ")
if (params.albumId || params.artistId || params.mediaFileId) {
query.append(" JOIN playlists_media_files pmf ON ")
.append(" pmf.media_file_id = mf.id ") }
if (params.mediaFileId) {
query.append(" AND mf.media_file_id = ? ")
sqlParam << params.mediaFileId }
if (params.albumId) {
query.append(" JOIN albums_media_files almf ON ")
.append(" mf.album_id = almf.album_id AND almf.album_id = ? ")
sqlParams << params.albumId }
if (params.artistId) {
query.append(" JOIN arists_media_files armf ON ")
.append(" mf.artist_id = armf.artist_id AND ")
.append(" armf.artist_id = ? ")
sqlParams << params.artistId }
if (params.userCreated != null || params.name || params.copiedFromId)
query.append(" WHERE ")
if (params.userCreated != null) {
query.append(" p.user_created = ? ")
sqlParams << params.userCreated }
if (params.name) {
query.append(" p.name = ? ")
sqlParams << params.name }
if (params.copiedFromId) {
query.append(" p.copied_from_id = ? ")
sqlParams << params.copiedFromId }
query = query.toString()
logger.debug("Selecting playlists.\n\tSQL: {}\n\tPARAMS: {}",
query, sqlParams)
return sql.rows(query, sqlParams)
.collect { recordToModel(Playlist, it) } }
public Playlist addToPlaylist(int playlistId, int mediaFileId) {
return withTransaction {
associate(Playlist, MediaFile, p, mf)
@ -406,9 +488,44 @@ public class ORM {
/// ### Tag-specific methods
public Tag getTagById(int id) { return getById(Tag, id) }
public Tag getTagByName(String name) { return getByName(Tag, name)[0] }
public List<Tag> getTagsWhere(Map params) {
def query = new StringBuilder()
def sqlParams = []
query.append("SELECT t.* FROM tags ")
if (params.mediaFileId || params.artistId || params.albumId ||
params.playlistId)
query.append(" JOIN media_files_tags mft ON t.id = mft.tag_id")
if (params.mediaFileId) {
query.append(" AND mft.media_file_id = ? ")
sqlParams << params.mediaFileId }
if (params.playlistId) {
query.append(" JOIN playlists_media_files pmf ON ")
.append(" mft.media_file_id = pmf.media_file_id AND ")
.append(" pmf.playlist_id = ? ")
sqlParams << params.playlistId }
if (params.artistId) {
query.append(" JOIN artists_media_files armf ON ")
.append(" mft.media_file_id = armf.media_file_id AND ")
.append(" armf.artist_id = ? ")
sqlParams << params.artistId }
if (params.albumId) {
query.append(" JOIN albums_media_files almf ON ")
.append(" mft.media_file_id = almf.media_file_id AND ")
.append(" almf.album_id = ? ")
sqlParams << params.albumId }
if (params.name) {
query.append(" WHERE t.name = ? ")
sqlParams << params.name } }
/// ### Utility functions
public void withTransaction(Closure c) {
public void withTransaction(Closure c) {
try { sql.execute("BEGIN TRANSACTION"); c() }
finally { sql.execute("COMMIT") }
}