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 e8eaa9e..f0c8a60 100644 --- a/core/src/main/groovy/com/jdbernard/wdiwtlt/db/ORM.groovy +++ b/core/src/main/groovy/com/jdbernard/wdiwtlt/db/ORM.groovy @@ -4,6 +4,7 @@ import java.lang.reflect.Modifier import java.sql.Connection import java.sql.PreparedStatement import java.sql.ResultSet +import java.sql.Timestamp import java.text.SimpleDateFormat import java.util.regex.Pattern import javax.persistence.Entity @@ -191,12 +192,12 @@ 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 List getAlbums() { return getAll(Album) } + public List getAlbumsOrderedByName() { return getAll(Album, 'name ASC') } @@ -207,7 +208,7 @@ public class ORM { def query = new StringBuilder() def sqlParams = [] - query.append('SELECT al.* FROM albums al ') + query.append('SELECT DISTINCT al.* FROM albums al ') if (params.artistId) { query.append(' JOIN artists_albums aa ON al.id = aa.album_id ') @@ -244,17 +245,26 @@ public class ORM { query, sqlParams) return sql.rows(query, sqlParams).collect { recordToModel(Album, it) } } - public List removeEmptyAlbums() { - throw new UnsupportedOperationException('Not yet implemented.'); - } + public void removeEmptyAlbums() { + + String query = """\ + DELETE FROM albums WHERE id IN ( + SELECT DISTINCT al.id + FROM albums al LEFT OUTER JOIN albums_media_files almf ON + al.id = almf.album_id + WHERE almf.album_id IS NULL)""" + + logger.debug('Deleting empty albums.\n\tSQL: {}', query) + sql.execute(query) } /// ### Artist-specific methods + public List getArtists() { return getAll(Artist) } public Artist getArtistById(int id) { return getById(Artist, id) } + public Artist getArtistByName(String name) { def artists = getByName(Artist, name) if (artists) return artists[0] } - public List getArtists() { return getAll(Artist) } public List getArtistsOrderedByName() { return getAll(Artist, 'name ASC') } @@ -262,7 +272,7 @@ public class ORM { def query = new StringBuilder() def sqlParams = [] - query.append('SELECT ar.* FROM artists ar ') + query.append('SELECT DISTINCT ar.* FROM artists ar ') if (params.albumId) { query.append(' JOIN artists_albums aa ON ar.id = aa.artist_id ') @@ -295,13 +305,21 @@ public class ORM { public List getArtistsLikeName(String name) { return getLike(Artist, ['name'], [name]) } - public List removeEmptyArtists() { - throw new UnsupportedOperationException('Not yet implemented.'); - } - public def addAlbumArtist(int albumId, int artistId) { return associate(Artist, Album, artistId, albumId) } + public void removeEmptyArtists() { + + String query = """\ + DELETE FROM artists WHERE id IN ( + SELECT DISTINCT ar.id + FROM artists ar LEFT OUTER JOIN artists_media_files armf ON + ar.id = armf.artist_id + WHERE armf.artist_id IS NULL)""" + + logger.debug('Deleting empty artists.\n\tSQL: {}', query) + sql.execute(query) } + /// ### Bookmark-specific methods public Bookmark getBookmarkById(int id) { return getById(Bookmark, id) } @@ -436,7 +454,6 @@ public class ORM { return sql.rows(query, sqlParams).collect { recordToModel(MediaFile, it) } } - public def associateMediaFileWithAlbum(int mediaFileId, int albumId) { return associate(Album, MediaFile, albumId, mediaFileId) } @@ -459,13 +476,12 @@ public class ORM { return mf } /// ### Playlist-specific methods + public List getPlaylists() { return getAll(Playlist) } public Playlist getPlaylistById(int id) { return getById(Playlist, id) } public List getPlaylistByName(String name) { return getByName(Playlist, name) } - public List getPlaylists() { return getAll(Playlist) } - public List getPlaylistsWhere(Map params) { def query = new StringBuilder() def sqlParams = [] @@ -600,9 +616,17 @@ public class ORM { p.mediaFileCount = 0 return update(p) } } - public List removeEmptyPlaylists() { - throw new UnsupportedOperationException('Not yet implemented.'); - } + public void removeEmptyPlaylists() { + + String query = """\ + DELETE FROM playlists WHERE id IN ( + SELECT DISTINCT p.id + FROM playlists p LEFT OUTER JOIN playlists_media_files pmf ON + p.id = pmf.playlist_id + WHERE pmf.playlist_id IS NULL)""" + + logger.debug('Deleting empty playlists.\n\tSQL: {}', query) + sql.execute(query) } /// ### Tag-specific methods public Tag getTagById(int id) { return getById(Tag, id) } @@ -613,7 +637,7 @@ public class ORM { def query = new StringBuilder() def sqlParams = [] - query.append('SELECT t.* FROM tags ') + query.append('SELECT DISTINCT t.* FROM tags ') if (params.mediaFileId || params.artistId || params.albumId || params.playlistId)