diff --git a/core/src/main/groovy/com/jdbernard/wdiwtlt/db/DbApi.groovy b/core/src/main/groovy/com/jdbernard/wdiwtlt/db/DbApi.groovy index 952df67..c084367 100644 --- a/core/src/main/groovy/com/jdbernard/wdiwtlt/db/DbApi.groovy +++ b/core/src/main/groovy/com/jdbernard/wdiwtlt/db/DbApi.groovy @@ -34,7 +34,7 @@ public class DbApi { public void shutdown() { dataSource.shutdown() } /// ### Common - public def getAll(Class modelClass, String orderClause = null) { + public List getAll(Class modelClass, String orderClause = null) { def query = new StringBuilder() query.append('SELECT * FROM ') .append(pluralize(nameFromModel(modelClass.simpleName))) @@ -46,19 +46,19 @@ public class DbApi { logger.debug('Selecting models.\n\tSQL: {}', query) return sql.rows(query).collect { recordToModel(modelClass, it) } } - public List getAllIds(Class modelClass) { + public List getAllIds(Class modelClass) { String query = 'SELECT id FROM ' + pluralize(nameFromModel(modelClass.simpleName)) logger.debug('Selecting model ids.\n\tSQL: {}', query) return sql.rows(query).collect { it.id } } - public Model getById(Class modelClass, UUID id) { + public M getById(Class modelClass, UUID id) { def model = modelClass.newInstance() model.id = id return refresh(model) } - public List getByIdLike(Class modelClass, + public List getByIdLike(Class modelClass, String partialId) { String likeVal = partialId + '%' String query = new StringBuilder() @@ -72,7 +72,7 @@ public class DbApi { return sql.rows(query, likeVal) .collect { recordToModel(modelClass, it) } } - public def refresh(def model) { + public M refresh(M model) { def query = new StringBuilder() .append('SELECT * FROM ') .append(pluralize(nameFromModel(model.class.simpleName))) @@ -84,7 +84,7 @@ public class DbApi { if (!result) return null return updateModel(result, model) } - public def getByName(Class modelClass, String name) { + public List getByName(Class modelClass, String name) { def query = new StringBuilder() .append('SELECT * FROM ') .append(pluralize(nameFromModel(modelClass.simpleName))) @@ -94,8 +94,8 @@ public class DbApi { logger.debug('Selecting model.\n\tSQL: {}\n\tPARAMS: {}', query, name) return sql.rows(query, [name]).collect { recordToModel(modelClass, it) } } - public def getBy(Class modelClass, List columns, - List values) { + public List getBy(Class modelClass, + List columns, List values) { def query = new StringBuilder() .append('SELECT * FROM ') .append(pluralize(nameFromModel(modelClass.simpleName))) @@ -107,8 +107,8 @@ public class DbApi { return sql.rows(query, values) .collect { recordToModel(modelClass, it) } } - public def getLike(Class modelClass, List columns, - List values) { + public List getLike(Class modelClass, + List columns, List values) { values = values.collect { "%$it%".toString() } String query = new StringBuilder() .append('SELECT * FROM ') @@ -121,7 +121,7 @@ public class DbApi { return sql.rows(query, values) .collect { recordToModel(modelClass, it) } } - public def getWhere(Class modelClass, Map criteria) { + public List getWhere(Class modelClass, Map criteria) { switch(modelClass) { case Album: return getAlbumsWhere(criteria) case Artist: return getArtistsWhere(criteria) @@ -130,11 +130,11 @@ public class DbApi { case Playlist: return getPlaylistsWhere(criteria) case Tag: return getTagsWhere(criteria) } } - public def save(def model) { + public Model save(Model model) { if (model.id) return update(model) else return create(model) } - public def update(def model) { + public Model update(Model model) { def setClauses = [] def params = [] @@ -144,7 +144,7 @@ public class DbApi { setClauses << '"' + nameFromModel(field.name) + '"= ?' params << field.get(model) } - def query = new StringBuilder() + String query = new StringBuilder() .append('UPDATE ') .append(pluralize(nameFromModel(model.class.simpleName))) .append(' SET ') @@ -159,7 +159,7 @@ public class DbApi { sql.executeUpdate(query, params) return refresh(model) } } - public def create(def model) { + public Model create(Model model) { def columns = [] def params = [] @@ -183,7 +183,7 @@ public class DbApi { sql.executeInsert(query, params) return model } - public def delete(def model) { + public int delete(Model model) { def query = new StringBuilder() .append('DELETE FROM ') .append(pluralize(nameFromModel(model.class.simpleName))) @@ -195,8 +195,8 @@ public class DbApi { sql.execute(query, [model.id]) return sql.updateCount } - public def associate(Class modelClass1, Class modelClass2 , - UUID firstId, UUID secondId) { + public def associate(Class modelClass1, + Class modelClass2 , UUID firstId, UUID secondId) { String linkTable = pluralize(nameFromModel(modelClass1.simpleName)) + '_' + pluralize(nameFromModel(modelClass2.simpleName)) String col1 = nameFromModel(modelClass1.simpleName) + '_id' @@ -218,7 +218,7 @@ public class DbApi { query, params) return sql.execute(query, params) } } } - public def associate(def m1, def m2) { + public def associate(Model m1, Model m2) { return associate(m1.class, m2.class, m1.id, m2.id) } /// ### Album-specific methods @@ -905,7 +905,7 @@ public class DbApi { field.set(model, record[nameFromModel(field.name)]) } return model } - static def recordToModel(Class modelClass, def record) { + static M recordToModel(Class modelClass, def record) { if (record == null) return null def model = modelClass.newInstance() @@ -915,7 +915,7 @@ public class DbApi { return model } - static def modelToRecord(def model) { + static def modelToRecord(Model model) { if (model == null) return null def record = [:]