diff --git a/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/impl/sql/SqlResourceType.kt b/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/impl/sql/SqlResourceType.kt index 4b95cc49..c84a2478 100644 --- a/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/impl/sql/SqlResourceType.kt +++ b/src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/impl/sql/SqlResourceType.kt @@ -53,7 +53,7 @@ class SqlResourceType( } override fun read(uri: Uri): Sequence { - 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("@") } diff --git a/src/main/kotlin/com/github/mgramin/sqlboot/rest/controllers/SqlController.kt b/src/main/kotlin/com/github/mgramin/sqlboot/rest/controllers/SqlController.kt index 767bec8d..f384c48a 100644 --- a/src/main/kotlin/com/github/mgramin/sqlboot/rest/controllers/SqlController.kt +++ b/src/main/kotlin/com/github/mgramin/sqlboot/rest/controllers/SqlController.kt @@ -41,7 +41,7 @@ class SqlController(@field:Autowired private val dataSource: DataSource) { @RequestMapping(value = ["sql"], produces = arrayOf(MediaType.APPLICATION_XML_VALUE)) fun execSql2Xml(@RequestBody sql: String): List> { - return JdbcSelectQuery(dataSource, sql).select().toList() + return JdbcSelectQuery(dataSource, sql).execute(hashMapOf()).toList() } @RequestMapping( @@ -49,12 +49,12 @@ class SqlController(@field:Autowired private val dataSource: DataSource) { method = arrayOf(RequestMethod.POST), produces = arrayOf(MediaType.APPLICATION_XML_VALUE)) fun execSql2XmlPost(@RequestBody sql: String): List> { - 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> { - return JdbcSelectQuery(dataSource, sql).select().toList() + return JdbcSelectQuery(dataSource, sql).execute(hashMapOf()).toList() } @RequestMapping( @@ -62,6 +62,6 @@ class SqlController(@field:Autowired private val dataSource: DataSource) { method = arrayOf(RequestMethod.POST), produces = arrayOf(MediaType.APPLICATION_JSON_VALUE)) fun execSql2JsonPost(@RequestBody sql: String): List> { - return JdbcSelectQuery(dataSource, sql).select().toList() + return JdbcSelectQuery(dataSource, sql).execute(hashMapOf()).toList() } } diff --git a/src/main/kotlin/com/github/mgramin/sqlboot/sql/select/SelectQuery.kt b/src/main/kotlin/com/github/mgramin/sqlboot/sql/select/SelectQuery.kt index ee48e275..3bc2d739 100644 --- a/src/main/kotlin/com/github/mgramin/sqlboot/sql/select/SelectQuery.kt +++ b/src/main/kotlin/com/github/mgramin/sqlboot/sql/select/SelectQuery.kt @@ -33,22 +33,12 @@ package com.github.mgramin.sqlboot.sql.select */ interface SelectQuery { - /** - * Execute select query - * - * @return query result - */ - @Deprecated("") - fun select(): Sequence> - /** * Execute select query with parameters * * @return query result */ - fun select(variables: Map): Sequence> { - return select() - } + fun execute(variables: Map): Sequence> /** * diff --git a/src/main/kotlin/com/github/mgramin/sqlboot/sql/select/impl/JdbcSelectQuery.kt b/src/main/kotlin/com/github/mgramin/sqlboot/sql/select/impl/JdbcSelectQuery.kt index 6b6fd7d3..9f2c4d5e 100644 --- a/src/main/kotlin/com/github/mgramin/sqlboot/sql/select/impl/JdbcSelectQuery.kt +++ b/src/main/kotlin/com/github/mgramin/sqlboot/sql/select/impl/JdbcSelectQuery.kt @@ -54,15 +54,15 @@ class JdbcSelectQuery( constructor(dataSource: DataSource, sql: String) : this(dataSource, sql, "[NULL]", null) - override fun select(): Sequence> { - return getMapStream(sql) - } - - override fun select(variables: Map): Sequence> { - return getMapStream(templateGenerator!!.generate(variables)) + override fun execute(variables: Map): Sequence> { + return if (sql != null) { + getMapStream(sql) + } else { + getMapStream(templateGenerator!!.generate(variables)) + } } - private fun getMapStream(sqlText: String?): Sequence> { + private fun getMapStream(sqlText: String): Sequence> { logger.info(sqlText) val rowSet = JdbcTemplate(dataSource).queryForRowSet(sqlText) val iterator = object : Iterator> { diff --git a/src/main/kotlin/com/github/mgramin/sqlboot/sql/select/wrappers/PageWrapper.kt b/src/main/kotlin/com/github/mgramin/sqlboot/sql/select/wrappers/PageWrapper.kt deleted file mode 100644 index 7f1f067e..00000000 --- a/src/main/kotlin/com/github/mgramin/sqlboot/sql/select/wrappers/PageWrapper.kt +++ /dev/null @@ -1,54 +0,0 @@ -/* - * The MIT License (MIT) - *

- * Copyright (c) 2016-2019 Maksim Gramin - *

- * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - *

- * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - *

- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package com.github.mgramin.sqlboot.sql.select.wrappers - -import com.github.mgramin.sqlboot.exceptions.BootException -import com.github.mgramin.sqlboot.sql.select.SelectQuery - -class PageWrapper( - private val origin: SelectQuery, - private val pageNumber: Int, - private val pageSize: Int -) : SelectQuery { - - val sql = """select * - | from ()""".trimMargin() - - @Throws(BootException::class) - override fun select(): Sequence> { - println(sql) - return sequenceOf() - } - - override fun columns(): Map { - return origin.columns() - } - - @Throws(BootException::class) - override fun dbHealth() { - origin.dbHealth() - } - -} diff --git a/src/test/kotlin/com/github/mgramin/sqlboot/sql/impl/JdbcSelectQueryTest.kt b/src/test/kotlin/com/github/mgramin/sqlboot/sql/impl/JdbcSelectQueryTest.kt index 4d682f25..a172e3c3 100644 --- a/src/test/kotlin/com/github/mgramin/sqlboot/sql/impl/JdbcSelectQueryTest.kt +++ b/src/test/kotlin/com/github/mgramin/sqlboot/sql/impl/JdbcSelectQueryTest.kt @@ -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, mail=mkyong@gmail.com}, {n=alex, mail=alex@yahoo.com}, {n=joel, mail=joel@gmail.com}]") } @@ -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"]) }