Added the ability to specify a sorting when using the ORM.getAll function.
This commit is contained in:
parent
8edf542726
commit
99c883dbe5
@ -33,9 +33,15 @@ public class ORM {
|
||||
public void shutdown() { dataSource.shutdown() }
|
||||
|
||||
/// ### Common
|
||||
public def getAll(Class modelClass) {
|
||||
def query = "SELECT * FROM " +
|
||||
pluralize(nameFromModel(modelClass.simpleName))
|
||||
public def getAll(Class modelClass, String orderClause = null) {
|
||||
def query = new StringBuilder()
|
||||
query.append("SELECT * FROM ")
|
||||
.append(pluralize(nameFromModel(modelClass.simpleName)))
|
||||
|
||||
if (orderClause) query.append(" ORDER BY ").append(orderClause)
|
||||
|
||||
query = query.toString(()
|
||||
|
||||
logger.debug("Selecting models.\n\tSQL: {}", query)
|
||||
return sql.rows(query).collect { recordToModel(modelClass, it) } }
|
||||
|
||||
@ -190,6 +196,9 @@ public class ORM {
|
||||
public Album getAlbumById(int id) { return getById(Album, id) }
|
||||
|
||||
public List<Album> getAlbums() { return getAll(Album) }
|
||||
public List<Album> getAlbumsOrderedByName() {
|
||||
return getAll(Album, "name ASC") }
|
||||
|
||||
public List<Album> getAlbumsLikeName(String name) {
|
||||
return getLike(Album, ["name"], [name]) }
|
||||
|
||||
@ -245,6 +254,8 @@ public class ORM {
|
||||
if (artists) return artists[0] }
|
||||
|
||||
public List<Artist> getArtists() { return getAll(Artist) }
|
||||
public List<Artist> getArtistsOrderedByName() {
|
||||
return getAll(Artist, "name ASC") }
|
||||
|
||||
public List<Artist> getArtistsWhere(Map params) {
|
||||
def query = new StringBuilder()
|
||||
@ -297,6 +308,8 @@ public class ORM {
|
||||
return getByName(Bookmark, name) }
|
||||
|
||||
public List<Bookmark> getBookmarks() { return getAll(Bookmark) }
|
||||
public List<Bookmark> getBookmarksOrderedByName() {
|
||||
return getAll(Bookmark, "name ASC") }
|
||||
|
||||
public List<Bookmark> getBookmarksWhere(Map options) {
|
||||
def query = new StringBuilder()
|
||||
@ -354,21 +367,22 @@ public class ORM {
|
||||
def query = new StringBuilder()
|
||||
def sqlParams = []
|
||||
def orderClauses = [] as LinkedList
|
||||
def whereClauses = []
|
||||
|
||||
query.append("SELECT mf.* FROM media_files mf ")
|
||||
|
||||
if (params.artistId) {
|
||||
query.append(" JOIN artists_media_files armf ON ")
|
||||
.append(" mf.id = armf.media_file_id ")
|
||||
query.append(" AND armf.artist_id = ? ")
|
||||
.append(" mf.id = armf.media_file_id AND ")
|
||||
query.append(" armf.artist_id = ? ")
|
||||
sqlParams << params.artistId
|
||||
orderClauses << "armf.artist_id" }
|
||||
|
||||
if (params.albumId) {
|
||||
query.append(" JOIN albums_media_files almf ON ")
|
||||
.append(" mf.id = almf.media_file_id ")
|
||||
.append(" mf.album_id = almf.album_id ")
|
||||
query.append(" AND almf.album_id = ? ")
|
||||
.append(" mf.id = almf.media_file_id AND ")
|
||||
.append(" almf.album_id = ? ")
|
||||
|
||||
sqlParams << params.albumId
|
||||
orderClauses << "almf.album_id ASC"
|
||||
orderClauses << "mf.track_number ASC" }
|
||||
@ -380,29 +394,33 @@ public class ORM {
|
||||
orderClauses.addFirst("pmf.position")
|
||||
sqlParams << params.playlistId }
|
||||
|
||||
if (params.name || params.fileHash || params.filePath ||
|
||||
params.trackNumber) { query.append(" WHERE ") }
|
||||
|
||||
if (params.name) {
|
||||
query.append(" WHERE mf.name = ? ")
|
||||
whereClauses << "mf.name = ?"
|
||||
sqlParams << params.name }
|
||||
|
||||
if (params.trackNumber) {
|
||||
query.append(" WHERE mf.track_number = ? ")
|
||||
query.append("mf.track_number = ?"
|
||||
sqlParams << params.trackNumber }
|
||||
|
||||
if (params.filePath) {
|
||||
query.append(" WHERE mf.file_path = ? ")
|
||||
query.append("mf.file_path = ?"
|
||||
sqlParams << params.filePath }
|
||||
|
||||
if (params.fileHash) {
|
||||
query.append(" WHERE mf.file_hash = ? ")
|
||||
query.append("mf.file_hash = ?"
|
||||
sqlParams << params.fileHash }
|
||||
|
||||
if (whereClauses)
|
||||
query.append(" WHERE ").append(whereClauses.join(" AND ")
|
||||
|
||||
if (orderClauses)
|
||||
query.append(" ORDER BY ").append(orderClauses.join(", ")
|
||||
|
||||
query = query.toString()
|
||||
logger.debug("Selecting artists.\n\tSQL: {}\n\tPARAMS: {}",
|
||||
query, sqlParams)
|
||||
return sql.rows(query, sqlParams).collect { recordToModel(MediaFile, it) } }
|
||||
return sql.rows(query, sqlParams).collect {
|
||||
recordToModel(MediaFile, it) } }
|
||||
|
||||
|
||||
public def associateMediaFileWithAlbum(int mediaFileId, int albumId) {
|
||||
@ -474,6 +492,7 @@ public class ORM {
|
||||
query.append(" p.copied_from_id = ? ")
|
||||
sqlParams << params.copiedFromId }
|
||||
|
||||
query.append(" ORDER BY p.position")
|
||||
query = query.toString()
|
||||
logger.debug("Selecting playlists.\n\tSQL: {}\n\tPARAMS: {}",
|
||||
query, sqlParams)
|
||||
|
Loading…
Reference in New Issue
Block a user