ORM returns models from save, create, and update. Also additional lookup functions to ORM.

This commit is contained in:
Jonathan Bernard 2016-02-12 17:20:32 -06:00
parent 105a61c11f
commit 1354a92204

View File

@ -115,7 +115,8 @@ public class ORM {
.toString() .toString()
logger.debug("Updating model.\n\tSQL: {}\n\tPARAMS: {}", query, params) logger.debug("Updating model.\n\tSQL: {}\n\tPARAMS: {}", query, params)
return sql.executeUpdate(query, params) } sql.executeUpdate(query, params)
return model }
public def create(def model) { public def create(def model) {
def columns = [] def columns = []
@ -140,7 +141,7 @@ public class ORM {
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 1 } return model }
public def delete(def model) { public def delete(def model) {
def query = new StringBuilder() def query = new StringBuilder()
@ -181,7 +182,10 @@ public class ORM {
/// ### 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> getAlbumByName(String name) { public Album[] getAlbumsByNameAndYear(String name, int year) {
return getBy(Album, ['name', 'year'], [name, year]) }
public List<Album> getAlbumsByName(String name) {
return getByName(Album, name) } return getByName(Album, name) }
public Album getAlbumByNameAndArtistId(String name, int artistId) { public Album getAlbumByNameAndArtistId(String name, int artistId) {
@ -193,11 +197,23 @@ public class ORM {
aa.artist_id = ? aa.artist_id = ?
WHERE al.name = ?""" WHERE al.name = ?"""
def params = [artistId, name] def params = [artistId, name]
logger.debug("Selecting albums.\n\tSQL: {}\n\tPARAMS: {}", logger.debug("Selecting an album.\n\tSQL: {}\n\tPARAMS: {}",
query, params) query, params)
def albums = sql.rows(query, params) return recordToModel(Album, sql.firstRow(query, params)) }
.collect { recordToModel(Album, it) }
return albums ? albums[0] : null } public Album getAlbumByNameAndYearAndArtistId(String name, int year,
int artistId) {
def query = """\
SELECT al.*
FROM albums al JOIN
artists_albums aa ON
al.id = aa.album_id AND
aa.artist_id = ?
WHERE al.name = ? AND al.year = ?"""
def params = [artistId, name, year]
logger.debug("Selecting an album. \n\tSQL: {}\n\tPARAMS: {}",
query, params)
return recordToModel(Album, sql.firstRow(query, params)) }
public List<Album> getAlbumsByArtistId(int artistId) { public List<Album> getAlbumsByArtistId(int artistId) {
def query = """\ def query = """\
@ -221,8 +237,9 @@ public class ORM {
/// ### Artist-specific methods /// ### Artist-specific methods
public Artist getArtistById(int id) { return getById(Artist, id) } public Artist getArtistById(int id) { return getById(Artist, id) }
public List<Artist> getArtistByName(String name) { public Artist getArtistByName(String name) {
return getByName(Artist, name) } def artists = getByName(Artist, name)
if (artists) return artists[0] }
public List<Artist> getArtists() { return getAll(Artist) } public List<Artist> getArtists() { return getAll(Artist) }
public List<Artist> getArtistsByAlbumId(int albumId) { public List<Artist> getArtistsByAlbumId(int albumId) {
@ -301,9 +318,10 @@ public class ORM {
public List<Playlist> getPlaylists() { return getAll(Playlist) } public List<Playlist> getPlaylists() { return getAll(Playlist) }
public Playlist addToPlaylist(Playlist p, MediaFile mf) { public Playlist addToPlaylist(int playlistId, int mediaFileId) {
return withTransaction { return withTransaction {
associate(p, mf) associate(Playlist, MediaFile, p, mf)
def p = getById(Playlist, playlistId)
p.mediaFileCount++ p.mediaFileCount++
update(p) update(p)
return refresh(p) } } return refresh(p) } }