DbApi now has proper generic types signatured for Model-related functionality.
This commit is contained in:
parent
42960cb96e
commit
652cb178f3
@ -34,7 +34,7 @@ public class DbApi {
|
||||
public void shutdown() { dataSource.shutdown() }
|
||||
|
||||
/// ### Common
|
||||
public def getAll(Class modelClass, String orderClause = null) {
|
||||
public <M extends Model> List<M> getAll(Class<M> 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<UUID> getAllIds(Class modelClass) {
|
||||
public List<UUID> getAllIds(Class<? extends Model> 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<? extends Model> modelClass, UUID id) {
|
||||
public <M extends Model> M getById(Class<M> modelClass, UUID id) {
|
||||
def model = modelClass.newInstance()
|
||||
model.id = id
|
||||
return refresh(model) }
|
||||
|
||||
public List<Model> getByIdLike(Class<? extends Model> modelClass,
|
||||
public <M extends Model> List<M> getByIdLike(Class<M> 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 extends Model> 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 <M extends Model> List<M> getByName(Class<M> 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<String> columns,
|
||||
List<Object> values) {
|
||||
public <M extends Model> List<M> getBy(Class<M> modelClass,
|
||||
List<String> 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<String> columns,
|
||||
List<Object> values) {
|
||||
public <M extends Model> List<M> getLike(Class<M> modelClass,
|
||||
List<String> 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 <M extends Model> List<M> getWhere(Class<M> 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<? extends Model> modelClass1,
|
||||
Class<? extends Model> 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 extends Model> M recordToModel(Class<M> 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 = [:]
|
||||
|
Loading…
Reference in New Issue
Block a user