removed the old experiments for templating; so we now just create a StringTemplate object that can be converted to html or used with JDBC queries/statements etc

This commit is contained in:
James Strachan
2012-03-14 16:00:11 +00:00
parent 34c4808c76
commit e2d85427ce
15 changed files with 100 additions and 600 deletions
@@ -1,6 +1,6 @@
package test.kotlin.jdbc.experiment1
import kotlin.template.experiment1.*
import kotlin.template.*
import kotlin.jdbc.*
import kotlin.test.*
import kotlin.util.*
@@ -14,34 +14,50 @@ import java.sql.Connection
import java.sql.PreparedStatement
import java.sql.ResultSet
fun DataSource.update(fn: (PreparedStatementBuilder) -> Unit): Int {
return use{ it.update(fn) }
fun DataSource.update(template: StringTemplate): Int {
return use{ it.update(template) }
}
fun <T> DataSource.query(fn: (PreparedStatementBuilder) -> Unit, resultBlock: (ResultSet) -> T): T {
return use{ it.query(fn, resultBlock) }
fun <T> DataSource.query(template: StringTemplate, resultBlock: (ResultSet) -> T): T {
return use{ it.query(template, resultBlock) }
}
fun Connection.update(fn: (PreparedStatementBuilder) -> Unit): Int {
val preparedStatement = prepare(fn)
fun Connection.update(template: StringTemplate): Int {
val preparedStatement = prepare(template)
return preparedStatement.update()
}
fun <T> Connection.query(fn: (PreparedStatementBuilder) -> Unit, resultBlock: (ResultSet) -> T): T {
val preparedStatement = prepare(fn)
fun <T> Connection.query(template: StringTemplate, resultBlock: (ResultSet) -> T): T {
val preparedStatement = prepare(template)
return preparedStatement.query(resultBlock)
}
fun Connection.prepare(fn: (PreparedStatementBuilder) -> Unit): PreparedStatement {
fun Connection.prepare(template: StringTemplate): PreparedStatement {
val builder = PreparedStatementBuilder(this)
fn(builder)
var constantText = true
template.forEach{
if (constantText) {
if (it == null) {
throw IllegalStateException("No constant checks should be null");
} else {
val text = it.toString()
if (text != null) {
builder.text(text)
}
}
} else {
builder.expression(it)
}
constantText = !constantText
}
return builder.statement()
}
trait Binding {
fun bind(statement: PreparedStatement): Unit
}
class PreparedStatementBuilder(val connection: Connection) {
val sql = StringBuilder()
val tasks = arrayList<Binding>()
@@ -52,6 +68,16 @@ class PreparedStatementBuilder(val connection: Connection) {
sql.append(value)
}
fun expression(value: Any?): Unit {
if (value is String) {
expression(value)
} else if (value is Int) {
expression(value)
} else {
}
}
fun expression(value: String): Unit {
sql.append("?")
tasks.add(object: Binding {
@@ -98,21 +124,17 @@ class JdbcTemplateTest : TestCase() {
// Mimicks the following code
// dataSource.update("insert into foo (id, name) values ($id, $name)")
dataSource.update {
it.text("insert into foo (id, name) values (")
it.expression(id)
it.text(", ")
it.expression(name)
it.text(")")
}
// TODO will use a tuple soon
dataSource.update(
StringTemplate(array("insert into foo (id, name) values (", id, ", ", name, ")"))
)
// Mimicks
// datasource.query("select * from foo where id = $id") { it["name"] }
// datasource.query("select * from foo where id = $id") { it.map{ it["name"] } }
val names = dataSource.query({
it.text("select * from foo where id = ")
it.expression(id)
}) {
val names = dataSource.query(
StringTemplate(array("select * from foo where id = ", id))
) {
it.map{ it["name"] }
}