From 2010aa7d4686291da9ef2503f171961e7757c5b2 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Mon, 15 Feb 2016 16:31:32 -0600 Subject: [PATCH] Added *Where functions for playlists, bookmarks, tags. Bugfixes. --- .../com/jdbernard/wdiwtlt/MediaLibrary.groovy | 8 +- .../com/jdbernard/wdiwtlt/db/ORM.groovy | 127 +++++++++++++++++- 2 files changed, 127 insertions(+), 8 deletions(-) diff --git a/core/src/main/groovy/com/jdbernard/wdiwtlt/MediaLibrary.groovy b/core/src/main/groovy/com/jdbernard/wdiwtlt/MediaLibrary.groovy index 535c32b..8744b26 100644 --- a/core/src/main/groovy/com/jdbernard/wdiwtlt/MediaLibrary.groovy +++ b/core/src/main/groovy/com/jdbernard/wdiwtlt/MediaLibrary.groovy @@ -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 diff --git a/core/src/main/groovy/com/jdbernard/wdiwtlt/db/ORM.groovy b/core/src/main/groovy/com/jdbernard/wdiwtlt/db/ORM.groovy index e110514..9def073 100644 --- a/core/src/main/groovy/com/jdbernard/wdiwtlt/db/ORM.groovy +++ b/core/src/main/groovy/com/jdbernard/wdiwtlt/db/ORM.groovy @@ -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 getBookmarks() { return getAll(Bookmark) } + public List 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 getPlaylists() { return getAll(Playlist) } + public List 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 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") } }