ORM: implemented methods to remove empty albums, artists, and playlists.
This commit is contained in:
parent
33e470c871
commit
49a250b63e
@ -4,6 +4,7 @@ import java.lang.reflect.Modifier
|
|||||||
import java.sql.Connection
|
import java.sql.Connection
|
||||||
import java.sql.PreparedStatement
|
import java.sql.PreparedStatement
|
||||||
import java.sql.ResultSet
|
import java.sql.ResultSet
|
||||||
|
import java.sql.Timestamp
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.regex.Pattern
|
import java.util.regex.Pattern
|
||||||
import javax.persistence.Entity
|
import javax.persistence.Entity
|
||||||
@ -191,12 +192,12 @@ 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 List<Album> getAlbums() { return getAll(Album) }
|
public List<Album> getAlbums() { return getAll(Album) }
|
||||||
|
|
||||||
public List<Album> getAlbumsOrderedByName() {
|
public List<Album> getAlbumsOrderedByName() {
|
||||||
return getAll(Album, 'name ASC') }
|
return getAll(Album, 'name ASC') }
|
||||||
|
|
||||||
@ -207,7 +208,7 @@ public class ORM {
|
|||||||
def query = new StringBuilder()
|
def query = new StringBuilder()
|
||||||
def sqlParams = []
|
def sqlParams = []
|
||||||
|
|
||||||
query.append('SELECT al.* FROM albums al ')
|
query.append('SELECT DISTINCT al.* FROM albums al ')
|
||||||
|
|
||||||
if (params.artistId) {
|
if (params.artistId) {
|
||||||
query.append(' JOIN artists_albums aa ON al.id = aa.album_id ')
|
query.append(' JOIN artists_albums aa ON al.id = aa.album_id ')
|
||||||
@ -244,17 +245,26 @@ public class ORM {
|
|||||||
query, sqlParams)
|
query, sqlParams)
|
||||||
return sql.rows(query, sqlParams).collect { recordToModel(Album, it) } }
|
return sql.rows(query, sqlParams).collect { recordToModel(Album, it) } }
|
||||||
|
|
||||||
public List<Album> removeEmptyAlbums() {
|
public void removeEmptyAlbums() {
|
||||||
throw new UnsupportedOperationException('Not yet implemented.');
|
|
||||||
}
|
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
|
/// ### Artist-specific methods
|
||||||
|
public List<Artist> getArtists() { return getAll(Artist) }
|
||||||
public Artist getArtistById(int id) { return getById(Artist, id) }
|
public Artist getArtistById(int id) { return getById(Artist, id) }
|
||||||
|
|
||||||
public Artist getArtistByName(String name) {
|
public Artist getArtistByName(String name) {
|
||||||
def artists = getByName(Artist, name)
|
def artists = getByName(Artist, name)
|
||||||
if (artists) return artists[0] }
|
if (artists) return artists[0] }
|
||||||
|
|
||||||
public List<Artist> getArtists() { return getAll(Artist) }
|
|
||||||
public List<Artist> getArtistsOrderedByName() {
|
public List<Artist> getArtistsOrderedByName() {
|
||||||
return getAll(Artist, 'name ASC') }
|
return getAll(Artist, 'name ASC') }
|
||||||
|
|
||||||
@ -262,7 +272,7 @@ public class ORM {
|
|||||||
def query = new StringBuilder()
|
def query = new StringBuilder()
|
||||||
def sqlParams = []
|
def sqlParams = []
|
||||||
|
|
||||||
query.append('SELECT ar.* FROM artists ar ')
|
query.append('SELECT DISTINCT ar.* FROM artists ar ')
|
||||||
|
|
||||||
if (params.albumId) {
|
if (params.albumId) {
|
||||||
query.append(' JOIN artists_albums aa ON ar.id = aa.artist_id ')
|
query.append(' JOIN artists_albums aa ON ar.id = aa.artist_id ')
|
||||||
@ -295,13 +305,21 @@ public class ORM {
|
|||||||
public List<Artist> getArtistsLikeName(String name) {
|
public List<Artist> getArtistsLikeName(String name) {
|
||||||
return getLike(Artist, ['name'], [name]) }
|
return getLike(Artist, ['name'], [name]) }
|
||||||
|
|
||||||
public List<Artist> removeEmptyArtists() {
|
|
||||||
throw new UnsupportedOperationException('Not yet implemented.');
|
|
||||||
}
|
|
||||||
|
|
||||||
public def addAlbumArtist(int albumId, int artistId) {
|
public def addAlbumArtist(int albumId, int artistId) {
|
||||||
return associate(Artist, Album, artistId, albumId) }
|
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
|
/// ### Bookmark-specific methods
|
||||||
public Bookmark getBookmarkById(int id) { return getById(Bookmark, id) }
|
public Bookmark getBookmarkById(int id) { return getById(Bookmark, id) }
|
||||||
|
|
||||||
@ -436,7 +454,6 @@ public class ORM {
|
|||||||
return sql.rows(query, sqlParams).collect {
|
return sql.rows(query, sqlParams).collect {
|
||||||
recordToModel(MediaFile, it) } }
|
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) }
|
||||||
|
|
||||||
@ -459,13 +476,12 @@ public class ORM {
|
|||||||
return mf }
|
return mf }
|
||||||
|
|
||||||
/// ### Playlist-specific methods
|
/// ### Playlist-specific methods
|
||||||
|
public List<Playlist> getPlaylists() { return getAll(Playlist) }
|
||||||
public Playlist getPlaylistById(int id) { return getById(Playlist, id) }
|
public Playlist getPlaylistById(int id) { return getById(Playlist, id) }
|
||||||
|
|
||||||
public List<Playlist> getPlaylistByName(String name) {
|
public List<Playlist> getPlaylistByName(String name) {
|
||||||
return getByName(Playlist, name) }
|
return getByName(Playlist, name) }
|
||||||
|
|
||||||
public List<Playlist> getPlaylists() { return getAll(Playlist) }
|
|
||||||
|
|
||||||
public List<Playlist> getPlaylistsWhere(Map params) {
|
public List<Playlist> getPlaylistsWhere(Map params) {
|
||||||
def query = new StringBuilder()
|
def query = new StringBuilder()
|
||||||
def sqlParams = []
|
def sqlParams = []
|
||||||
@ -600,9 +616,17 @@ public class ORM {
|
|||||||
p.mediaFileCount = 0
|
p.mediaFileCount = 0
|
||||||
return update(p) } }
|
return update(p) } }
|
||||||
|
|
||||||
public List<Playlist> removeEmptyPlaylists() {
|
public void removeEmptyPlaylists() {
|
||||||
throw new UnsupportedOperationException('Not yet implemented.');
|
|
||||||
}
|
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
|
/// ### Tag-specific methods
|
||||||
public Tag getTagById(int id) { return getById(Tag, id) }
|
public Tag getTagById(int id) { return getById(Tag, id) }
|
||||||
@ -613,7 +637,7 @@ public class ORM {
|
|||||||
def query = new StringBuilder()
|
def query = new StringBuilder()
|
||||||
def sqlParams = []
|
def sqlParams = []
|
||||||
|
|
||||||
query.append('SELECT t.* FROM tags ')
|
query.append('SELECT DISTINCT t.* FROM tags ')
|
||||||
|
|
||||||
if (params.mediaFileId || params.artistId || params.albumId ||
|
if (params.mediaFileId || params.artistId || params.albumId ||
|
||||||
params.playlistId)
|
params.playlistId)
|
||||||
|
Loading…
Reference in New Issue
Block a user