Skip to content

Commit

Permalink
#89
Browse files Browse the repository at this point in the history
  • Loading branch information
mgramin committed Jan 15, 2019
1 parent 015e524 commit 0cf5ddb
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class SqlResourceType(
}

override fun read(uri: Uri): Sequence<DbResource> {
return selectQuery.select(hashMapOf("uri" to uri))
return selectQuery.execute(hashMapOf("uri" to uri))
.map { o ->
val path = o.entries
.filter { v -> v.key.startsWith("@") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,27 @@ class SqlController(@field:Autowired private val dataSource: DataSource) {

@RequestMapping(value = ["sql"], produces = arrayOf(MediaType.APPLICATION_XML_VALUE))
fun execSql2Xml(@RequestBody sql: String): List<Map<String, Any>> {
return JdbcSelectQuery(dataSource, sql).select().toList()
return JdbcSelectQuery(dataSource, sql).execute(hashMapOf()).toList()
}

@RequestMapping(
value = ["exec"],
method = arrayOf(RequestMethod.POST),
produces = arrayOf(MediaType.APPLICATION_XML_VALUE))
fun execSql2XmlPost(@RequestBody sql: String): List<Map<String, Any>> {
return JdbcSelectQuery(dataSource, sql).select().toList()
return JdbcSelectQuery(dataSource, sql).execute(hashMapOf()).toList()
}

@RequestMapping(value = ["exec"], produces = arrayOf(MediaType.APPLICATION_JSON_VALUE))
fun execSql2Json(@RequestParam("sql") sql: String): List<Map<String, Any>> {
return JdbcSelectQuery(dataSource, sql).select().toList()
return JdbcSelectQuery(dataSource, sql).execute(hashMapOf()).toList()
}

@RequestMapping(
value = ["exec"],
method = arrayOf(RequestMethod.POST),
produces = arrayOf(MediaType.APPLICATION_JSON_VALUE))
fun execSql2JsonPost(@RequestBody sql: String): List<Map<String, Any>> {
return JdbcSelectQuery(dataSource, sql).select().toList()
return JdbcSelectQuery(dataSource, sql).execute(hashMapOf()).toList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,12 @@ package com.github.mgramin.sqlboot.sql.select
*/
interface SelectQuery {

/**
* Execute select query
*
* @return query result
*/
@Deprecated("")
fun select(): Sequence<Map<String, Any>>

/**
* Execute select query with parameters
*
* @return query result
*/
fun select(variables: Map<String, Any>): Sequence<Map<String, Any>> {
return select()
}
fun execute(variables: Map<String, Any>): Sequence<Map<String, Any>>

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ class JdbcSelectQuery(
constructor(dataSource: DataSource, sql: String)
: this(dataSource, sql, "[NULL]", null)

override fun select(): Sequence<Map<String, Any>> {
return getMapStream(sql)
}

override fun select(variables: Map<String, Any>): Sequence<Map<String, Any>> {
return getMapStream(templateGenerator!!.generate(variables))
override fun execute(variables: Map<String, Any>): Sequence<Map<String, Any>> {
return if (sql != null) {
getMapStream(sql)
} else {
getMapStream(templateGenerator!!.generate(variables))
}
}

private fun getMapStream(sqlText: String?): Sequence<Map<String, Any>> {
private fun getMapStream(sqlText: String): Sequence<Map<String, Any>> {
logger.info(sqlText)
val rowSet = JdbcTemplate(dataSource).queryForRowSet(sqlText)
val iterator = object : Iterator<Map<String, Any>> {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ class JdbcSelectQueryTest {
fun select() {
val select = JdbcSelectQuery(this.dataSource!!,
"""select *
| from (select name as "n"
| from (select name as "n"
| , email as "mail"
| from main_schema.users)""".trimMargin())
.select().toList()
.execute(hashMapOf()).toList()
assertEquals(select.toString(),
"[{n=mkyong, [email protected]}, {n=alex, [email protected]}, {n=joel, [email protected]}]")
}
Expand All @@ -65,7 +65,6 @@ class JdbcSelectQueryTest {
| , email as email /* email */
| from main_schema.users""".trimMargin()))
val metadata = selectQuery.columns()
println(metadata)
assertEquals("email", metadata["email"])
assertEquals("name", metadata["name"])
}
Expand Down

0 comments on commit 0cf5ddb

Please sign in to comment.