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() }
|
public void shutdown() { dataSource.shutdown() }
|
||||||
|
|
||||||
/// ### Common
|
/// ### Common
|
||||||
public def getAll(Class modelClass) {
|
public def getAll(Class modelClass, String orderClause = null) {
|
||||||
def query = "SELECT * FROM " +
|
def query = new StringBuilder()
|
||||||
pluralize(nameFromModel(modelClass.simpleName))
|
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)
|
logger.debug("Selecting models.\n\tSQL: {}", query)
|
||||||
return sql.rows(query).collect { recordToModel(modelClass, it) } }
|
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 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() {
|
||||||
|
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]) }
|
||||||
|
|
||||||
@ -245,6 +254,8 @@ public class ORM {
|
|||||||
if (artists) return artists[0] }
|
if (artists) return artists[0] }
|
||||||
|
|
||||||
public List<Artist> getArtists() { return getAll(Artist) }
|
public List<Artist> getArtists() { return getAll(Artist) }
|
||||||
|
public List<Artist> getArtistsOrderedByName() {
|
||||||
|
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()
|
||||||
@ -297,6 +308,8 @@ public class ORM {
|
|||||||
return getByName(Bookmark, name) }
|
return getByName(Bookmark, name) }
|
||||||
|
|
||||||
public List<Bookmark> getBookmarks() { return getAll(Bookmark) }
|
public List<Bookmark> getBookmarks() { return getAll(Bookmark) }
|
||||||
|
public List<Bookmark> getBookmarksOrderedByName() {
|
||||||
|
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()
|
||||||
@ -354,21 +367,22 @@ public class ORM {
|
|||||||
def query = new StringBuilder()
|
def query = new StringBuilder()
|
||||||
def sqlParams = []
|
def sqlParams = []
|
||||||
def orderClauses = [] as LinkedList
|
def orderClauses = [] as LinkedList
|
||||||
|
def whereClauses = []
|
||||||
|
|
||||||
query.append("SELECT mf.* FROM media_files mf ")
|
query.append("SELECT mf.* FROM media_files mf ")
|
||||||
|
|
||||||
if (params.artistId) {
|
if (params.artistId) {
|
||||||
query.append(" JOIN artists_media_files armf ON ")
|
query.append(" JOIN artists_media_files armf ON ")
|
||||||
.append(" mf.id = armf.media_file_id ")
|
.append(" mf.id = armf.media_file_id AND ")
|
||||||
query.append(" AND armf.artist_id = ? ")
|
query.append(" armf.artist_id = ? ")
|
||||||
sqlParams << params.artistId
|
sqlParams << params.artistId
|
||||||
orderClauses << "armf.artist_id" }
|
orderClauses << "armf.artist_id" }
|
||||||
|
|
||||||
if (params.albumId) {
|
if (params.albumId) {
|
||||||
query.append(" JOIN albums_media_files almf ON ")
|
query.append(" JOIN albums_media_files almf ON ")
|
||||||
.append(" mf.id = almf.media_file_id ")
|
.append(" mf.id = almf.media_file_id AND ")
|
||||||
.append(" mf.album_id = almf.album_id ")
|
.append(" almf.album_id = ? ")
|
||||||
query.append(" AND almf.album_id = ? ")
|
|
||||||
sqlParams << params.albumId
|
sqlParams << params.albumId
|
||||||
orderClauses << "almf.album_id ASC"
|
orderClauses << "almf.album_id ASC"
|
||||||
orderClauses << "mf.track_number ASC" }
|
orderClauses << "mf.track_number ASC" }
|
||||||
@ -380,29 +394,33 @@ public class ORM {
|
|||||||
orderClauses.addFirst("pmf.position")
|
orderClauses.addFirst("pmf.position")
|
||||||
sqlParams << params.playlistId }
|
sqlParams << params.playlistId }
|
||||||
|
|
||||||
if (params.name || params.fileHash || params.filePath ||
|
|
||||||
params.trackNumber) { query.append(" WHERE ") }
|
|
||||||
|
|
||||||
if (params.name) {
|
if (params.name) {
|
||||||
query.append(" WHERE mf.name = ? ")
|
whereClauses << "mf.name = ?"
|
||||||
sqlParams << params.name }
|
sqlParams << params.name }
|
||||||
|
|
||||||
if (params.trackNumber) {
|
if (params.trackNumber) {
|
||||||
query.append(" WHERE mf.track_number = ? ")
|
query.append("mf.track_number = ?"
|
||||||
sqlParams << params.trackNumber }
|
sqlParams << params.trackNumber }
|
||||||
|
|
||||||
if (params.filePath) {
|
if (params.filePath) {
|
||||||
query.append(" WHERE mf.file_path = ? ")
|
query.append("mf.file_path = ?"
|
||||||
sqlParams << params.filePath }
|
sqlParams << params.filePath }
|
||||||
|
|
||||||
if (params.fileHash) {
|
if (params.fileHash) {
|
||||||
query.append(" WHERE mf.file_hash = ? ")
|
query.append("mf.file_hash = ?"
|
||||||
sqlParams << params.fileHash }
|
sqlParams << params.fileHash }
|
||||||
|
|
||||||
|
if (whereClauses)
|
||||||
|
query.append(" WHERE ").append(whereClauses.join(" AND ")
|
||||||
|
|
||||||
|
if (orderClauses)
|
||||||
|
query.append(" ORDER BY ").append(orderClauses.join(", ")
|
||||||
|
|
||||||
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(MediaFile, it) } }
|
return sql.rows(query, sqlParams).collect {
|
||||||
|
recordToModel(MediaFile, it) } }
|
||||||
|
|
||||||
|
|
||||||
public def associateMediaFileWithAlbum(int mediaFileId, int albumId) {
|
public def associateMediaFileWithAlbum(int mediaFileId, int albumId) {
|
||||||
@ -474,6 +492,7 @@ public class ORM {
|
|||||||
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user