ORM changed double-quotes to single-quotes in strings with no interpolation.

This commit is contained in:
Jonathan Bernard 2016-03-10 10:40:04 -06:00
parent 976eb97b07
commit 107e48a1d0

View File

@ -35,14 +35,14 @@ public class ORM {
/// ### Common /// ### Common
public def getAll(Class modelClass, String orderClause = null) { public def getAll(Class modelClass, String orderClause = null) {
def query = new StringBuilder() def query = new StringBuilder()
query.append("SELECT * FROM ") query.append('SELECT * FROM ')
.append(pluralize(nameFromModel(modelClass.simpleName))) .append(pluralize(nameFromModel(modelClass.simpleName)))
if (orderClause) query.append(" ORDER BY ").append(orderClause) if (orderClause) query.append(' ORDER BY ').append(orderClause)
query = query.toString(() query = query.toString()
logger.debug("Selecting models.\n\tSQL: {}", query) logger.debug('Selecting models.\n\tSQL: {}', query)
return sql.rows(query).collect { recordToModel(modelClass, it) } } return sql.rows(query).collect { recordToModel(modelClass, it) } }
public def getById(Class modelClass, int id) { public def getById(Class modelClass, int id) {
@ -52,36 +52,36 @@ public class ORM {
public def refresh(def model) { public def refresh(def model) {
def query = new StringBuilder() def query = new StringBuilder()
.append("SELECT * FROM ") .append('SELECT * FROM ')
.append(pluralize(nameFromModel(model.class.simpleName))) .append(pluralize(nameFromModel(model.class.simpleName)))
.append(" WHERE id = ?") .append(' WHERE id = ?')
.toString() .toString()
logger.debug("Selecting model.\n\tSQL: {}\n\tPARAMS: {}", query, model.id) logger.debug('Selecting model.\n\tSQL: {}\n\tPARAMS: {}', query, model.id)
def result = sql.firstRow(query, [model.id]) def result = sql.firstRow(query, [model.id])
if (!result) return null if (!result) return null
return updateModel(result, model) } return updateModel(result, model) }
public def getByName(Class modelClass, String name) { public def getByName(Class modelClass, String name) {
def query = new StringBuilder() def query = new StringBuilder()
.append("SELECT * FROM ") .append('SELECT * FROM ')
.append(pluralize(nameFromModel(modelClass.simpleName))) .append(pluralize(nameFromModel(modelClass.simpleName)))
.append(" WHERE name = ?") .append(' WHERE name = ?')
.toString() .toString()
logger.debug("Selecting model.\n\tSQL: {}\n\tPARAMS: {}", query, name) logger.debug('Selecting model.\n\tSQL: {}\n\tPARAMS: {}', query, name)
return sql.rows(query, [name]).collect { recordToModel(modelClass, it) } } return sql.rows(query, [name]).collect { recordToModel(modelClass, it) } }
public def getBy(Class modelClass, List<String> columns, public def getBy(Class modelClass, List<String> columns,
List<Object> values) { List<Object> values) {
def query = new StringBuilder() def query = new StringBuilder()
.append("SELECT * FROM ") .append('SELECT * FROM ')
.append(pluralize(nameFromModel(modelClass.simpleName))) .append(pluralize(nameFromModel(modelClass.simpleName)))
.append(" WHERE ") .append(' WHERE ')
.append(columns.collect { it + " = ?" }.join(' AND ')) .append(columns.collect { it + ' = ?' }.join(' AND '))
.toString() .toString()
logger.debug("Selecting models.\n\tSQL: {}\n\tPARAMS: {}", query, values) logger.debug('Selecting models.\n\tSQL: {}\n\tPARAMS: {}', query, values)
return sql.rows(query, values) return sql.rows(query, values)
.collect { recordToModel(modelClass, it) } } .collect { recordToModel(modelClass, it) } }
@ -89,13 +89,13 @@ public class ORM {
List<Object> values) { List<Object> values) {
values = values.collect { "%$it%".toString() } values = values.collect { "%$it%".toString() }
String query = new StringBuilder() String query = new StringBuilder()
.append("SELECT * FROM ") .append('SELECT * FROM ')
.append(pluralize(nameFromModel(modelClass.simpleName))) .append(pluralize(nameFromModel(modelClass.simpleName)))
.append(" WHERE ") .append(' WHERE ')
.append(columns.collect { """"$it" LIKE ?"""}.join(' AND ')) .append(columns.collect { """"$it" LIKE ?"""}.join(' AND '))
.toString() .toString()
logger.debug("Selecting models.\n\tSQL: {}\n\tPARAMS: {}", query, values) logger.debug('Selecting models.\n\tSQL: {}\n\tPARAMS: {}', query, values)
return sql.rows(query, values) return sql.rows(query, values)
.collect { recordToModel(modelClass, it) } } .collect { recordToModel(modelClass, it) } }
@ -114,16 +114,17 @@ public class ORM {
params << field.get(model) } params << field.get(model) }
def query = new StringBuilder() def query = new StringBuilder()
.append("UPDATE ") .append('UPDATE ')
.append(pluralize(nameFromModel(model.class.simpleName))) .append(pluralize(nameFromModel(model.class.simpleName)))
.append(" SET ") .append(' SET ')
.append(setClauses.join(', ')) .append(setClauses.join(', '))
.append(" WHERE id = ") .append(' WHERE id = ?')
.append(model.id)
.toString() .toString()
params << model.id
return withTransaction { return withTransaction {
logger.debug("Updating model.\n\tSQL: {}\n\tPARAMS: {}", query, params) logger.debug('Updating model.\n\tSQL: {}\n\tPARAMS: {}', query, params)
sql.executeUpdate(query, params) sql.executeUpdate(query, params)
return refresh(model) } } return refresh(model) } }
@ -140,26 +141,26 @@ public class ORM {
params << field.get(model) } params << field.get(model) }
def query= new StringBuilder() def query= new StringBuilder()
.append("INSERT INTO ") .append('INSERT INTO ')
.append(pluralize(nameFromModel(model.class.simpleName))) .append(pluralize(nameFromModel(model.class.simpleName)))
.append(" (") .append(' (')
.append(columns.join(', ')) .append(columns.join(', '))
.append(") VALUES (") .append(') VALUES (')
.append((1..columns.size()).collect { '?' }.join(', ')) .append((1..columns.size()).collect { '?' }.join(', '))
.append(")").toString() .append(')').toString()
logger.debug("Creating model.\n\tSQL: {}\n\tPARAMS: {}", query, params) logger.debug('Creating model.\n\tSQL: {}\n\tPARAMS: {}', query, params)
model.id = sql.executeInsert(query, params)[0][0] model.id = sql.executeInsert(query, params)[0][0]
return model } return model }
public def delete(def model) { public def delete(def model) {
def query = new StringBuilder() def query = new StringBuilder()
.append("DELETE FROM ") .append('DELETE FROM ')
.append(pluralize(nameFromModel(model.class.simpleName))) .append(pluralize(nameFromModel(model.class.simpleName)))
.append("WHERE id = ?") .append('WHERE id = ?')
.toString() .toString()
logger.debug("Deleting model.\n\tSQL: {}\n\tPARAMS: {}", logger.debug('Deleting model.\n\tSQL: {}\n\tPARAMS: {}',
query, model.id) query, model.id)
sql.execute(query, [model.id]) sql.execute(query, [model.id])
return sql.updateCount } return sql.updateCount }
@ -167,9 +168,9 @@ public class ORM {
public def associate(Class modelClass1, Class modelClass2 , public def associate(Class modelClass1, Class modelClass2 ,
Integer firstId, Integer secondId) { Integer firstId, Integer secondId) {
String linkTable = pluralize(nameFromModel(modelClass1.simpleName)) + String linkTable = pluralize(nameFromModel(modelClass1.simpleName)) +
"_" + pluralize(nameFromModel(modelClass2.simpleName)) '_' + pluralize(nameFromModel(modelClass2.simpleName))
String col1 = nameFromModel(modelClass1.simpleName) + "_id" String col1 = nameFromModel(modelClass1.simpleName) + '_id'
String col2 = nameFromModel(modelClass2.simpleName) + "_id" String col2 = nameFromModel(modelClass2.simpleName) + '_id'
return withTransaction { return withTransaction {
def query = """\ def query = """\
@ -178,12 +179,12 @@ public class ORM {
def params = [firstId, secondId] def params = [firstId, secondId]
// Look first for an existing association before creating one. // Look first for an existing association before creating one.
logger.debug("Selecting association.\n\tSQL: {}\n\tPARAMS: {}", logger.debug('Selecting association.\n\tSQL: {}\n\tPARAMS: {}',
query, params) query, params)
if (sql.firstRow(query, params)) { return 0 } if (sql.firstRow(query, params)) { return 0 }
else { else {
query = """INSERT INTO $linkTable ("$col1", "$col2") VALUES (?, ?)""" query = """INSERT INTO $linkTable ("$col1", "$col2") VALUES (?, ?)"""
logger.debug("Creating association.\n\tSQL: {}\n\tPARAMS: {}", logger.debug('Creating association.\n\tSQL: {}\n\tPARAMS: {}',
query, params) query, params)
return sql.execute(query, params) } } } return sql.execute(query, params) } } }
@ -197,54 +198,54 @@ public class ORM {
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') }
public List<Album> getAlbumsLikeName(String name) { public List<Album> getAlbumsLikeName(String name) {
return getLike(Album, ["name"], [name]) } return getLike(Album, ['name'], [name]) }
public List<Album> getAlbumsWhere(Map params) { public List<Album> getAlbumsWhere(Map params) {
def query = new StringBuilder() def query = new StringBuilder()
def sqlParams = [] def sqlParams = []
query.append("SELECT al.* FROM albums al ") query.append('SELECT 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 ')
query.append(" AND aa.artist_id = ? ") query.append(' AND aa.artist_id = ? ')
sqlParams << params.artistId } sqlParams << params.artistId }
if (params.mediaFileId || params.playlistId) { if (params.mediaFileId || params.playlistId) {
query.append(" JOIN albums_media_files amf ON ") query.append(' JOIN albums_media_files amf ON ')
query.append(" al.id = amf.album_id ") query.append(' al.id = amf.album_id ')
if (params.mediaFileId) { if (params.mediaFileId) {
query.append(" AND amf.media_file_id = ? ") query.append(' AND amf.media_file_id = ? ')
sqlParams << params.mediaFileId } } sqlParams << params.mediaFileId } }
if (params.playlistId) { if (params.playlistId) {
query.append(" JOIN playlists_media_files pmf ON ") query.append(' JOIN playlists_media_files pmf ON ')
query.append(" amf.media_file_id = pmf.media_file_id AND ") query.append(' amf.media_file_id = pmf.media_file_id AND ')
query.append(" pmf.playlist_id = ?") query.append(' pmf.playlist_id = ?')
sqlParams << params.playlistId } sqlParams << params.playlistId }
if (params.name || params.year) { query.append(" WHERE ") } if (params.name || params.year) { query.append(' WHERE ') }
if (params.year) { if (params.year) {
query.append(" al.year = ? ") query.append(' al.year = ? ')
sqlParams << params.year } sqlParams << params.year }
if (params.name) { if (params.name) {
if (params.year) query.append(" AND ") if (params.year) query.append(' AND ')
query.append(" al.name = ? ") query.append(' al.name = ? ')
sqlParams << params.name } sqlParams << params.name }
query = query.toString() query = query.toString()
logger.debug("Selecting albums.\n\tSQL: {}\n\tPARAMS: {}", logger.debug('Selecting albums.\n\tSQL: {}\n\tPARAMS: {}',
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 List<Album> removeEmptyAlbums() {
throw new UnsupportedOperationException("Not yet implemented."); throw new UnsupportedOperationException('Not yet implemented.');
} }
/// ### Artist-specific methods /// ### Artist-specific methods
@ -255,47 +256,47 @@ public class ORM {
public List<Artist> getArtists() { return getAll(Artist) } 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') }
public List<Artist> getArtistsWhere(Map params) { public List<Artist> getArtistsWhere(Map params) {
def query = new StringBuilder() def query = new StringBuilder()
def sqlParams = [] def sqlParams = []
query.append("SELECT ar.* FROM artists ar ") query.append('SELECT 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 ')
query.append(" AND aa.album_id = ? ") query.append(' AND aa.album_id = ? ')
sqlParams << params.albumId } sqlParams << params.albumId }
if (params.mediaFileId || params.playlistId) { if (params.mediaFileId || params.playlistId) {
query.append(" JOIN artists_media_files amf ON ") query.append(' JOIN artists_media_files amf ON ')
query.append(" ar.id = amf.artist_id ") query.append(' ar.id = amf.artist_id ')
if (params.mediaFileId) { if (params.mediaFileId) {
query.append(" AND amf.media_file_id = ? ") query.append(' AND amf.media_file_id = ? ')
sqlParams << params.mediaFileId } } sqlParams << params.mediaFileId } }
if (params.playlistId) { if (params.playlistId) {
query.append(" JOIN playlists_media_files pmf ON ") query.append(' JOIN playlists_media_files pmf ON ')
query.append(" amf.media_file_id = pmf.media_file_id AND ") query.append(' amf.media_file_id = pmf.media_file_id AND ')
query.append(" pmf.playlist_id = ?") query.append(' pmf.playlist_id = ?')
sqlParams << params.playlistId } sqlParams << params.playlistId }
if (params.name) { if (params.name) {
query.append(" WHERE ar.name = ? ") query.append(' WHERE ar.name = ? ')
sqlParams << params.name } sqlParams << params.name }
query = query.toString() query = query.toString()
logger.debug("Selecting artists.\n\tSQL: {}\n\tPARAMS: {}", logger.debug('Selecting artists.\n\tSQL: {}\n\tPARAMS: {}',
query, sqlParams) query, sqlParams)
return sql.rows(query, sqlParams).collect { recordToModel(Artist, it) } } return sql.rows(query, sqlParams).collect { recordToModel(Artist, it) } }
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() { public List<Artist> removeEmptyArtists() {
throw new UnsupportedOperationException("Not yet implemented."); throw new UnsupportedOperationException('Not yet implemented.');
} }
public def addAlbumArtist(int albumId, int artistId) { public def addAlbumArtist(int albumId, int artistId) {
@ -309,38 +310,38 @@ public class ORM {
public List<Bookmark> getBookmarks() { return getAll(Bookmark) } public List<Bookmark> getBookmarks() { return getAll(Bookmark) }
public List<Bookmark> getBookmarksOrderedByName() { public List<Bookmark> getBookmarksOrderedByName() {
return getAll(Bookmark, "name ASC") } return getAll(Bookmark, 'name ASC') }
public List<Bookmark> getBookmarksWhere(Map options) { public List<Bookmark> getBookmarksWhere(Map options) {
def query = new StringBuilder() def query = new StringBuilder()
def sqlParams = [] def sqlParams = []
query.append("SELECT b.* FROM bookmarks b ") query.append('SELECT b.* FROM bookmarks b ')
if (params.size() > 0) query.append(" WHERE ") if (params.size() > 0) query.append(' WHERE ')
if (params.playlistId) { if (params.playlistId) {
query.append(" b.playlist_id = ? ") query.append(' b.playlist_id = ? ')
sqlParams << params.playlistId } sqlParams << params.playlistId }
if (params.userCreated != null) { if (params.userCreated != null) {
query.append(" b.user_created = ? ") query.append(' b.user_created = ? ')
sqlParams << params.userCreated } sqlParams << params.userCreated }
if (params.name) { if (params.name) {
query.append (" b.name = ? ") query.append (' b.name = ? ')
sqlParams << params.name } sqlParams << params.name }
if (params.mediaFileId) { if (params.mediaFileId) {
query.append(" b.media_file_id = ? ") query.append(' b.media_file_id = ? ')
sqlParams << params.mediaFileId } sqlParams << params.mediaFileId }
if (params.playIndex) { if (params.playIndex) {
query.append(" b.play_index = ? ") query.append(' b.play_index = ? ')
sqlParams << params.playIndex } sqlParams << params.playIndex }
query = query.toString() query = query.toString()
logger.debug("Selecting bookmarks.\n\tSQL: {}\n\tPARAMS: {}", logger.debug('Selecting bookmarks.\n\tSQL: {}\n\tPARAMS: {}',
query, sqlParams) query, sqlParams)
return sql.rows(query, sqlParams) return sql.rows(query, sqlParams)
.collect { recordToModel(Bookmark, it) } } .collect { recordToModel(Bookmark, it) } }
@ -357,10 +358,10 @@ public class ORM {
public List<MediaFile> getMediaFiles() { return getAll(MediaFile) } public List<MediaFile> getMediaFiles() { return getAll(MediaFile) }
public List<MediaFile> getMediaFilesLikeName(String name) { public List<MediaFile> getMediaFilesLikeName(String name) {
return getLike(MediaFile, ["name"], [name]) } return getLike(MediaFile, ['name'], [name]) }
public MediaFile getMediaFileByFilePath(String filePath) { public MediaFile getMediaFileByFilePath(String filePath) {
def files = getBy(MediaFile, ["file_path"], [filePath]) def files = getBy(MediaFile, ['file_path'], [filePath])
return files ? files[0] : null } return files ? files[0] : null }
public List<MediaFile> getMediaFilesWhere(Map params) { public List<MediaFile> getMediaFilesWhere(Map params) {
@ -399,35 +400,33 @@ public class ORM {
sqlParams << params.name } sqlParams << params.name }
if (params.trackNumber) { if (params.trackNumber) {
query.append('mf.track_number = ?' query.append('mf.track_number = ?')
sqlParams << params.trackNumber } sqlParams << params.trackNumber }
if (params.filePath) { if (params.filePath) {
query.append('mf.file_path = ?' query.append('mf.file_path = ?')
sqlParams << params.filePath } sqlParams << params.filePath }
if (params.fileHash) { if (params.fileHash) {
query.append('mf.file_hash = ?' query.append('mf.file_hash = ?')
sqlParams << params.fileHash } sqlParams << params.fileHash }
if (params.tags) { if (params.tags) {
String L = "mft${idx}":
String T = "t${idx}":
tags.eachWithIndex { tag, idx -> tags.eachWithIndex { tag, idx ->
String L = "mft${idx}";
query.append(" JOIN media_files_tags $L ON ") query.append(" JOIN media_files_tags $L ON ")
.append(" $L.media_file_id = md.id ") .append(" ${L}.media_file_id = mf.id AND ${L}.tag_id = ?")
.append(" JOIN tags $L.tag_id = $T.id AND $T.name = ?")
sqlParams << tag } } sqlParams << tag.id } }
if (whereClauses) if (whereClauses)
query.append(' WHERE ').append(whereClauses.join(' AND ') query.append(' WHERE ').append(whereClauses.join(' AND '))
if (orderClauses) if (orderClauses)
query.append(' ORDER BY ').append(orderClauses.join(', ') query.append(' ORDER BY ').append(orderClauses.join(', '))
query = query.toString() query = query.toString()
logger.debug("Selecting artists.\n\tSQL: {}\n\tPARAMS: {}", logger.debug('Selecting media files.\n\tSQL: {}\n\tPARAMS: {}',
query, sqlParams) query, sqlParams)
return sql.rows(query, sqlParams).collect { return sql.rows(query, sqlParams).collect {
recordToModel(MediaFile, it) } } recordToModel(MediaFile, it) } }
@ -440,14 +439,14 @@ public class ORM {
return associate(Album, MediaFile, artistId, mediaFileId) } return associate(Album, MediaFile, artistId, mediaFileId) }
public def incrementPlayCount(int mediaFileId) { public def incrementPlayCount(int mediaFileId) {
def query = "UPDATE media_files SET play_count = play_count + 1 WHERE ID = ?" def query = 'UPDATE media_files SET play_count = play_count + 1 WHERE ID = ?'
def params = [mediaFileId] def params = [mediaFileId]
logger.debug("Updating media file.\n\tSQL: {}\n\tPARAMS: {}", query, params) logger.debug('Updating media file.\n\tSQL: {}\n\tPARAMS: {}', query, params)
sql.executeUpdate(query, params) sql.executeUpdate(query, params)
query = "SELECT play_count FROM media_files WHERE id = ?" query = 'SELECT play_count FROM media_files WHERE id = ?'
logger.debug("Selecting media file play count.\n\tSQL: {}\n\tPARAMS: {}", query, params) logger.debug('Selecting media file play count.\n\tSQL: {}\n\tPARAMS: {}', query, params)
return sql.firstRow(query, params)[0] } return sql.firstRow(query, params)[0] }
public MediaFile incrementPlayCount(MediaFile mf) { public MediaFile incrementPlayCount(MediaFile mf) {
@ -466,45 +465,44 @@ public class ORM {
def query = new StringBuilder() def query = new StringBuilder()
def sqlParams = [] def sqlParams = []
query.append("SELECT p.* FROM playlists p ") query.append('SELECT DISTINCT p.* FROM playlists p ')
if (params.albumId || params.artistId || params.mediaFileId) { if (params.albumId || params.artistId || params.mediaFileId) {
query.append(" JOIN playlists_media_files pmf ON ") query.append(' JOIN playlists_media_files pmf ON ')
.append(" pmf.media_file_id = mf.id ") } .append(' pmf.media_file_id = mf.id ') }
if (params.mediaFileId) { if (params.mediaFileId) {
query.append(" AND mf.media_file_id = ? ") query.append(' AND mf.media_file_id = ? ')
sqlParam << params.mediaFileId } sqlParam << params.mediaFileId }
if (params.albumId) { if (params.albumId) {
query.append(" JOIN albums_media_files almf ON ") query.append(' JOIN albums_media_files almf ON ')
.append(" mf.album_id = almf.album_id AND almf.album_id = ? ") .append(' mf.album_id = almf.album_id AND almf.album_id = ? ')
sqlParams << params.albumId } sqlParams << params.albumId }
if (params.artistId) { if (params.artistId) {
query.append(" JOIN arists_media_files armf ON ") query.append(' JOIN arists_media_files armf ON ')
.append(" mf.artist_id = armf.artist_id AND ") .append(' mf.artist_id = armf.artist_id AND ')
.append(" armf.artist_id = ? ") .append(' armf.artist_id = ? ')
sqlParams << params.artistId } sqlParams << params.artistId }
if (params.userCreated != null || params.name || params.copiedFromId) if (params.userCreated != null || params.name || params.copiedFromId)
query.append(" WHERE ") query.append(' WHERE ')
if (params.userCreated != null) { if (params.userCreated != null) {
query.append(" p.user_created = ? ") query.append(' p.user_created = ? ')
sqlParams << params.userCreated } sqlParams << params.userCreated }
if (params.name) { if (params.name) {
query.append(" p.name = ? ") query.append(' p.name = ? ')
sqlParams << params.name } sqlParams << params.name }
if (params.copiedFromId) { if (params.copiedFromId) {
query.append(" p.copied_from_id = ? ") query.append(' p.copied_from_id = ? ')
sqlParams << params.copiedFromId } sqlParams << params.copiedFromId }
query.append(" ORDER BY p.position")
query = query.toString() query = query.toString()
logger.debug("Selecting playlists.\n\tSQL: {}\n\tPARAMS: {}", logger.debug('Selecting playlists.\n\tSQL: {}\n\tPARAMS: {}',
query, sqlParams) query, sqlParams)
return sql.rows(query, sqlParams) return sql.rows(query, sqlParams)
.collect { recordToModel(Playlist, it) } } .collect { recordToModel(Playlist, it) } }
@ -522,16 +520,16 @@ public class ORM {
def p = getById(Playlist, playlistId) def p = getById(Playlist, playlistId)
if (!p) return null if (!p) return null
String query = String query =
"DELETE FROM playlists_media_files WHERE playlist_id = ?" 'DELETE FROM playlists_media_files WHERE playlist_id = ?'
def sqlParams = [playlistId] def sqlParams = [playlistId]
logger.debug("Clearing playlist.\n\tSQL: {}\n\tPARAMS: {}", logger.debug('Clearing playlist.\n\tSQL: {}\n\tPARAMS: {}',
query, sqlParams) query, sqlParams)
sql.execute(query, sqlParams) sql.execute(query, sqlParams)
p.mediaFileCount = 0 p.mediaFileCount = 0
return update(p) } } return update(p) } }
public List<Playlist> removeEmptyPlaylists() { public List<Playlist> removeEmptyPlaylists() {
throw new UnsupportedOperationException("Not yet implemented."); throw new UnsupportedOperationException('Not yet implemented.');
} }
/// ### Tag-specific methods /// ### Tag-specific methods
@ -541,43 +539,43 @@ public class ORM {
def query = new StringBuilder() def query = new StringBuilder()
def sqlParams = [] def sqlParams = []
query.append("SELECT t.* FROM tags ") query.append('SELECT t.* FROM tags ')
if (params.mediaFileId || params.artistId || params.albumId || if (params.mediaFileId || params.artistId || params.albumId ||
params.playlistId) params.playlistId)
query.append(" JOIN media_files_tags mft ON t.id = mft.tag_id") query.append(' JOIN media_files_tags mft ON t.id = mft.tag_id')
if (params.mediaFileId) { if (params.mediaFileId) {
query.append(" AND mft.media_file_id = ? ") query.append(' AND mft.media_file_id = ? ')
sqlParams << params.mediaFileId } sqlParams << params.mediaFileId }
if (params.playlistId) { if (params.playlistId) {
query.append(" JOIN playlists_media_files pmf ON ") query.append(' JOIN playlists_media_files pmf ON ')
.append(" mft.media_file_id = pmf.media_file_id AND ") .append(' mft.media_file_id = pmf.media_file_id AND ')
.append(" pmf.playlist_id = ? ") .append(' pmf.playlist_id = ? ')
sqlParams << params.playlistId } sqlParams << params.playlistId }
if (params.artistId) { if (params.artistId) {
query.append(" JOIN artists_media_files armf ON ") query.append(' JOIN artists_media_files armf ON ')
.append(" mft.media_file_id = armf.media_file_id AND ") .append(' mft.media_file_id = armf.media_file_id AND ')
.append(" armf.artist_id = ? ") .append(' armf.artist_id = ? ')
sqlParams << params.artistId } sqlParams << params.artistId }
if (params.albumId) { if (params.albumId) {
query.append(" JOIN albums_media_files almf ON ") query.append(' JOIN albums_media_files almf ON ')
.append(" mft.media_file_id = almf.media_file_id AND ") .append(' mft.media_file_id = almf.media_file_id AND ')
.append(" almf.album_id = ? ") .append(' almf.album_id = ? ')
sqlParams << params.albumId } sqlParams << params.albumId }
if (params.name) { if (params.name) {
query.append(" WHERE t.name = ? ") query.append(' WHERE t.name = ? ')
sqlParams << params.name } } sqlParams << params.name } }
/// ### Utility functions /// ### Utility functions
public def withTransaction(Closure c) { public def withTransaction(Closure c) {
try { sql.execute("BEGIN TRANSACTION"); return c() } try { sql.execute('BEGIN TRANSACTION'); return c() }
finally { sql.execute("COMMIT") } finally { sql.execute('COMMIT') } }
}
public static String nameToModel(String name) { public static String nameToModel(String name) {
def pts = name.toLowerCase().split('_') def pts = name.toLowerCase().split('_')
return pts.length == 1 ? pts[0] : return pts.length == 1 ? pts[0] :
@ -587,7 +585,7 @@ public class ORM {
return UPPERCASE_PATTERN.matcher(name). return UPPERCASE_PATTERN.matcher(name).
replaceAll(/$1_$2/).toLowerCase() } replaceAll(/$1_$2/).toLowerCase() }
public static String pluralize(String name) { return name + "s" } public static String pluralize(String name) { return name + 's' }
static def updateModel(def record, def model) { static def updateModel(def record, def model) {
getInstanceFields(model.class).each { field -> getInstanceFields(model.class).each { field ->