Java Reference
In-Depth Information
void insertProduct(Product p) {
def params = [p.id, p.name, p.price]
sql.execute
'insert into product(id,name,price) values(?,?,?)', params
}
void deleteProduct(int id) {
sql.execute 'delete from product where id=?', id
}
}
By the way, there's one other option available, [ 3 ] but only if the Person class is imple-
mented in Groovy. If so, I can add a constructor to the Person class that handles the case
conversion there:
3 Thanks to Dinko Srkoc on the Groovy Users email list for this helpful suggestion.
class Product {
int id
String name
double price
Person(Map args) {
args.each { k,v ->
setProperty( k.toLowerCase(), v)
}
}
}
With this constructor, the getAllProducts method reduces to
List<Product> getAllProducts() {
sql.rows('select * from product').collect { new Product(it) }
}
It's hard to beat that for elegance.
Going meta
The “elegant” solution in the chapter breaks down if the class attributes use camel case,
which is normal. The corresponding database table entries would then use underscores to
separate the words.
 
Search WWH ::




Custom Search