Although not a direct answer/solution to the exercise. I'm bringing this old code block from a different
topic simply to because this thread has no code whatsoever.
-
Here we go, an initial pass at an internal SQL DSL using
Expression Based DSL in Groovy 1.8+ (GEP3)
You can run it
here.
class SQL {
def tables = [:]
def add(table){
[with: {content -> tables[table] = content}]
}
def describe(table){
println tables[table][0]
}
def update(table){
[set: { map -> map.keySet().each { key ->
def col_idx = tables[table][0].indexOf(key);
tables[table][1..-1].eachWithIndex {
item, idx-> item[col_idx] =
(map."$key" instanceof Closure)?map."$key"(item[col_idx]):map."$key"
}}}]
}
def show(table){
println tables[table]
}
}
users = [
["id", "name", "password", "created"],
[1, "john", "doe", new Date()],
[2, "jane", "smith", new Date()],
[3, "foo", "bar", new Date()]
]
sql = new SQL()
sql.with {
add "users" with users
describe "users"
update "users" set name: "baba"
show "users"
update "users" set id : {x -> x * 10}, name : "xterm"
show "users"
}
Visualization (Can't be tested on Web Console)