From 9689547c0dbe053a5c385756914a31c78d6ed71b Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Sat, 12 Mar 2016 20:57:58 -0600 Subject: [PATCH] ORM: Fixed getBookmarksWhere, added removeFromPaylist. --- .../com/jdbernard/wdiwtlt/db/ORM.groovy | 146 ++++++++++++++---- 1 file changed, 119 insertions(+), 27 deletions(-) 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 f0c8a60..9f94eaf 100644 --- a/core/src/main/groovy/com/jdbernard/wdiwtlt/db/ORM.groovy +++ b/core/src/main/groovy/com/jdbernard/wdiwtlt/db/ORM.groovy @@ -330,34 +330,54 @@ public class ORM { public List getBookmarksOrderedByName() { return getAll(Bookmark, 'name ASC') } - public List getBookmarksWhere(Map options) { + public List getBookmarksWhere(Map params) { def query = new StringBuilder() def sqlParams = [] + def orderClauses = [] + def whereClauses = [] query.append('SELECT b.* FROM bookmarks b ') - if (params.size() > 0) query.append(' WHERE ') - if (params.playlistId) { - query.append(' b.playlist_id = ? ') + whereClauses << 'b.playlist_id = ?' sqlParams << params.playlistId } if (params.userCreated != null) { - query.append(' b.user_created = ? ') + whereClauses << 'b.user_created = ?' sqlParams << params.userCreated } if (params.name) { - query.append (' b.name = ? ') + whereClauses << 'b.name = ?' sqlParams << params.name } if (params.mediaFileId) { - query.append(' b.media_file_id = ? ') + whereClauses << 'b.media_file_id = ?' sqlParams << params.mediaFileId } if (params.playIndex) { - query.append(' b.play_index = ? ') + whereClauses << 'b.play_index = ?' sqlParams << params.playIndex } + if (params.lastUsedBefore) { + whereClauses << 'b.last_used < ?' + sqlParams << params.lastUsedBefore } + + if (params.lastUsedAfter) { + whereClauses << 'b.last_used > ?' + sqlParams << params.lastUsedAfter } + + if (params.lastUsedBetween) { + whereClauses << 'b.last_used BETWEEN ? AND ?' + sqlParams.addAll(params.lastUsedBetween) } + + orderClauses << 'b.last_used DESC' + + if (whereClauses) + query.append(' WHERE ').append(whereClauses.join(' AND ')) + + if (orderClauses) + query.append(' ORDER BY ').append(orderClauses.join(', ')) + query = query.toString() logger.debug('Selecting bookmarks.\n\tSQL: {}\n\tPARAMS: {}', query, sqlParams) @@ -389,23 +409,25 @@ public class ORM { def whereClauses = [] query.append('SELECT mf.* FROM media_files mf ') - orderClasess << 'mf.disc_number' - orderClauses << 'mf.track_number' + + if (params.artistId || params.playlistId) { + query.append(' JOIN artists_media_files armf ON ') + .append(' mf.id = armf.media_file_id ') + orderClauses << 'armf.artist_id ASC' } if (params.artistId) { - query.append(' JOIN artists_media_files armf ON ') - .append(' mf.id = armf.media_file_id AND ') - query.append(' armf.artist_id = ? ') - sqlParams << params.artistId - orderClauses << 'armf.artist_id' } + query.append(' AND armf.artist_id = ? ') + sqlParams << params.artistId } + + if (params.albumId || params.artistId || params.playlistId) { + query.append(' JOIN albums_media_files almf ON ') + .append(' mf.id = almf.media_file_id ') + + orderClauses << 'almf.album_id ASC' } if (params.albumId) { - query.append(' JOIN albums_media_files almf ON ') - .append(' mf.id = almf.media_file_id AND ') - .append(' almf.album_id = ? ') - - sqlParams << params.albumId - orderClauses << 'almf.album_id ASC' } + query.append(' AND almf.album_id = ? ') + sqlParams << params.albumId } if (params.playlistId) { query.append(' JOIN playlists_media_files pmf ON ') @@ -442,6 +464,9 @@ public class ORM { sqlParams << tag.id } } + orderClauses << 'mf.disc_number' + orderClauses << 'mf.track_number' + if (whereClauses) query.append(' WHERE ').append(whereClauses.join(' AND ')) @@ -485,6 +510,8 @@ public class ORM { public List getPlaylistsWhere(Map params) { def query = new StringBuilder() def sqlParams = [] + def orderClauses = [] + def whereClauses = [] query.append('SELECT DISTINCT p.* FROM playlists p ') @@ -507,21 +534,38 @@ public class ORM { .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 = ? ') + whereClauses << 'p.user_created = ?' sqlParams << params.userCreated } if (params.name) { - query.append(' p.name = ? ') + whereClauses << 'p.name = ?' sqlParams << params.name } if (params.copiedFromId) { - query.append(' p.copied_from_id = ? ') + whereClauses << 'p.copied_from_id = ?' sqlParams << params.copiedFromId } + if (params.lastUsedBefore) { + whereClauses << 'p.last_used < ?' + sqlParams << params.lastUsedBefore } + + if (params.lastUsedAfter) { + whereClauses << 'p.last_used > ?' + sqlParams << params.lastUsedAfter } + + if (params.lastUsedBetween) { + whereClauses << 'p.last_used BETWEEN ? AND ?' + sqlParams.addAll(params.lastUsedBetween) } + + orderClauses << 'p.last_used DESC' + + if (whereClauses) + query.append(' WHERE ').append(whereClauses.join(' AND ')) + + if (orderClauses) + query.append(' ORDER BY ').append(orderClauses.join(', ')) + query = query.toString() logger.debug('Selecting playlists.\n\tSQL: {}\n\tPARAMS: {}', query, sqlParams) @@ -577,6 +621,7 @@ public class ORM { sql.executeInsert(query, params) } p.mediaFileCount += mediaFileIds.size() + p.lastUsed = new Timestamp(new Date().time) return update(p) } } public Playlist addToPlaylist(int playlistId, int mediaFileId, @@ -601,8 +646,54 @@ public class ORM { sql.executeInsert(query, params) p.mediaFileCount++ + p.lastUsed = new Timestamp(new Date().time) return update(p) } } + public Playlist removeFromPlaylist(int playlistId, int mediaFileId) { + String getPositionQuery = """\ + SELECT position FROM playlists_media_files + WHERE playlist_id = ? AND media_file_id = ?""" + + String delQuery = """\ + DELETE FROM playlists_media_files + WHERE playlist_id = ? AND media_file_id = ?""" + + String reorderPlaylistQuery = """\ + UPDATE playlists_media_files SET position = position - 1 + WHERE playlist_id = ? AND position > ?""" + + String updatePlaylistQuery = """\ + UPDATE playlists SET + media_file_count = (SELECT count(*) + FROM playlists_media_files pmf + WHERE pmf.playlist_id = ?), + last_used = NOW() + WHERE id = ?""" + + withTransaction { + def params = [playlistId, mediaFileId] + logger.debug( + 'Finding media file playlist position.\n\tSQL: {}\n\tPARAMS: {}', + getPositionQuery, params) + int position = sql.firstRow(getPositionQuery, params)[0] + + logger.debug( + 'Removing media file from playlist.\n\tSQL: {}\n\tPARAMS: {}', + delQuery, params) + sql.execute(delQuery, params) + + params = [playlistId, position] + logger.debug('Reording playlist items.\n\tSQL: {}\n\tPARAMS: {}', + reorderPlaylistQuery, params) + sql.execute(reorderPlaylistQuery, params) + + params = [playlistId, playlistId] + logger.debug('Updating playlist.\n\tSQL: {}\n\tPARAMS: {}', + updatePlaylistQuery, params) + sql.executeUpdate(updatePlaylistQuery, params) + + return getById(Playlist, playlistId) } } + public Playlist removeAllFromPlaylist(int playlistId) { return withTransaction { def p = getById(Playlist, playlistId) @@ -614,6 +705,7 @@ public class ORM { query, sqlParams) sql.execute(query, sqlParams) p.mediaFileCount = 0 + p.lastUsed = new Timestamp(new Date().time) return update(p) } } public void removeEmptyPlaylists() {