Skip to content

Commit 4c6df5f

Browse files
committed
#89 use Sequence in SelectQuery
1 parent 5ab28f2 commit 4c6df5f

File tree

7 files changed

+43
-68
lines changed

7 files changed

+43
-68
lines changed

src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/ResourceType.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,13 @@ package com.github.mgramin.sqlboot.model.resource_type
2727
import com.fasterxml.jackson.annotation.JsonAutoDetect
2828
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility
2929
import com.fasterxml.jackson.annotation.JsonProperty
30-
import com.github.mgramin.sqlboot.exceptions.BootException
3130
import com.github.mgramin.sqlboot.model.resource.DbResource
3231
import com.github.mgramin.sqlboot.model.uri.Uri
3332
import org.json.JSONException
3433
import org.json.JSONObject
35-
36-
import java.util.HashMap
37-
import java.util.stream.Stream
38-
34+
import java.util.*
3935
import java.util.stream.Collectors.toList
36+
import java.util.stream.Stream
4037

4138
/**
4239
* Resource type e.g. Table, Index, Stored function etc
@@ -82,7 +79,6 @@ interface ResourceType {
8279
/**
8380
* Read resources by uri
8481
*/
85-
@Throws(BootException::class)
8682
fun read(uri: Uri): Stream<DbResource>
8783

8884
@JsonAutoDetect(fieldVisibility = Visibility.ANY)

src/main/kotlin/com/github/mgramin/sqlboot/model/resource_type/impl/sql/SqlResourceType.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,11 @@ import com.github.mgramin.sqlboot.model.resource_type.ResourceType
3131
import com.github.mgramin.sqlboot.model.uri.Uri
3232
import com.github.mgramin.sqlboot.model.uri.impl.DbUri
3333
import com.github.mgramin.sqlboot.sql.select.SelectQuery
34-
35-
import java.util.HashMap
36-
import java.util.LinkedHashMap
37-
import kotlin.collections.Map.Entry
38-
import java.util.stream.Stream
39-
34+
import org.apache.commons.lang3.StringUtils.strip
35+
import java.util.*
4036
import java.util.Optional.ofNullable
4137
import java.util.stream.Collectors.toList
42-
import java.util.stream.Collectors.toMap
43-
import org.apache.commons.lang3.StringUtils.strip
38+
import java.util.stream.Stream
4439

4540
/**
4641
* Created by MGramin on 12.07.2017.
@@ -75,12 +70,14 @@ class SqlResourceType(
7570
val name = path[path.size - 1].toString()
7671

7772
val headers = o.entries
78-
.map { strip(it.key, "@") to ofNullable(it.value).orElse("")}
73+
.map { strip(it.key, "@") to ofNullable(it.value).orElse("") }
7974
.toMap()
8075
DbResourceImpl(name, this,
8176
DbUri(this.name(), path.stream().map { it.toString() }.collect(toList())),
8277
headers)
8378
}
79+
.map { o -> o as DbResource }
80+
.toList().stream()
8481
}
8582

8683
override fun metaData(): Map<String, String> {

src/main/kotlin/com/github/mgramin/sqlboot/rest/controllers/SqlController.kt

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@
2424

2525
package com.github.mgramin.sqlboot.rest.controllers
2626

27-
import com.github.mgramin.sqlboot.exceptions.BootException
2827
import com.github.mgramin.sqlboot.sql.select.impl.JdbcSelectQuery
2928
import org.springframework.beans.factory.annotation.Autowired
3029
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
3130
import org.springframework.http.MediaType
3231
import org.springframework.web.bind.annotation.*
33-
import java.util.stream.Collectors.toList
3432
import javax.sql.DataSource
3533

3634
/**
@@ -42,27 +40,28 @@ import javax.sql.DataSource
4240
class SqlController(@field:Autowired private val dataSource: DataSource) {
4341

4442
@RequestMapping(value = ["sql"], produces = arrayOf(MediaType.APPLICATION_XML_VALUE))
45-
@Throws(BootException::class)
4643
fun execSql2Xml(@RequestBody sql: String): List<Map<String, Any>> {
47-
return JdbcSelectQuery(dataSource, sql).select().collect(toList())
44+
return JdbcSelectQuery(dataSource, sql).select().toList()
4845
}
4946

50-
@RequestMapping(value = ["exec"], method = arrayOf(RequestMethod.POST), produces = arrayOf(MediaType.APPLICATION_XML_VALUE))
51-
@Throws(BootException::class)
47+
@RequestMapping(
48+
value = ["exec"],
49+
method = arrayOf(RequestMethod.POST),
50+
produces = arrayOf(MediaType.APPLICATION_XML_VALUE))
5251
fun execSql2XmlPost(@RequestBody sql: String): List<Map<String, Any>> {
53-
return JdbcSelectQuery(dataSource, sql).select().collect(toList())
52+
return JdbcSelectQuery(dataSource, sql).select().toList()
5453
}
5554

5655
@RequestMapping(value = ["exec"], produces = arrayOf(MediaType.APPLICATION_JSON_VALUE))
57-
@Throws(BootException::class)
5856
fun execSql2Json(@RequestParam("sql") sql: String): List<Map<String, Any>> {
59-
return JdbcSelectQuery(dataSource, sql).select().collect(toList())
57+
return JdbcSelectQuery(dataSource, sql).select().toList()
6058
}
6159

62-
@RequestMapping(value = ["exec"], method = arrayOf(RequestMethod.POST), produces = arrayOf(MediaType.APPLICATION_JSON_VALUE))
63-
@Throws(BootException::class)
60+
@RequestMapping(
61+
value = ["exec"],
62+
method = arrayOf(RequestMethod.POST),
63+
produces = arrayOf(MediaType.APPLICATION_JSON_VALUE))
6464
fun execSql2JsonPost(@RequestBody sql: String): List<Map<String, Any>> {
65-
return JdbcSelectQuery(dataSource, sql).select().collect(toList())
65+
return JdbcSelectQuery(dataSource, sql).select().toList()
6666
}
67-
6867
}

src/main/kotlin/com/github/mgramin/sqlboot/sql/select/SelectQuery.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,15 @@ interface SelectQuery {
4343
* @throws BootException SQL exception
4444
*/
4545
@Deprecated("")
46-
@Throws(BootException::class)
47-
fun select(): Stream<Map<String, Any>>
46+
fun select(): Sequence<Map<String, Any>>
4847

4948
/**
5049
* Execute select query with parameters
5150
*
5251
* @return query result
5352
* @throws BootException SQL exception
5453
*/
55-
@Throws(BootException::class)
56-
open fun select(variables: Map<String, Any>): Stream<Map<String, Any>> {
54+
fun select(variables: Map<String, Any>): Sequence<Map<String, Any>> {
5755
return select()
5856
}
5957

@@ -69,7 +67,5 @@ interface SelectQuery {
6967
* @throws BootException SQL exception
7068
*/
7169
@Deprecated("")
72-
@Throws(BootException::class)
7370
fun dbHealth()
74-
7571
}

src/main/kotlin/com/github/mgramin/sqlboot/sql/select/impl/JdbcSelectQuery.kt

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ import java.sql.SQLException
3434
import java.util.AbstractMap.SimpleEntry
3535
import java.util.Arrays.stream
3636
import java.util.Optional.ofNullable
37-
import java.util.Spliterator.ORDERED
38-
import java.util.Spliterators.spliteratorUnknownSize
3937
import java.util.stream.Collectors.toMap
40-
import java.util.stream.Stream
41-
import java.util.stream.StreamSupport.stream
4238
import javax.sql.DataSource
4339

4440
/**
@@ -48,30 +44,28 @@ import javax.sql.DataSource
4844
* @version $Id: f38638fde3d38f83edd4b8a03c570f845c856752 $
4945
* @since 0.1
5046
*/
51-
class JdbcSelectQuery(private val dataSource: DataSource,
52-
private val sql: String?,
53-
private val nullAlias: String,
54-
private val templateGenerator: TemplateGenerator?) : SelectQuery {
47+
class JdbcSelectQuery(
48+
private val dataSource: DataSource,
49+
private val sql: String?,
50+
private val nullAlias: String,
51+
private val templateGenerator: TemplateGenerator?
52+
) : SelectQuery {
5553

56-
constructor(datasource: DataSource,
57-
templateGenerator: TemplateGenerator) : this(datasource, null, "[NULL]", templateGenerator) {
58-
}
54+
constructor(datasource: DataSource, templateGenerator: TemplateGenerator)
55+
: this(datasource, null, "[NULL]", templateGenerator)
5956

60-
constructor(datasource: DataSource,
61-
sql: String) : this(datasource, sql, "[NULL]", null) {
62-
}
57+
constructor(datasource: DataSource, sql: String)
58+
: this(datasource, sql, "[NULL]", null)
6359

64-
@Throws(BootException::class)
65-
override fun select(): Stream<Map<String, Any>> {
60+
override fun select(): Sequence<Map<String, Any>> {
6661
return getMapStream(sql)
6762
}
6863

69-
@Throws(BootException::class)
70-
override fun select(variables: Map<String, Any>): Stream<Map<String, Any>> {
64+
override fun select(variables: Map<String, Any>): Sequence<Map<String, Any>> {
7165
return getMapStream(templateGenerator!!.generate(variables))
7266
}
7367

74-
private fun getMapStream(sqlText: String?): Stream<Map<String, Any>> {
68+
private fun getMapStream(sqlText: String?): Sequence<Map<String, Any>> {
7569
logger.info(sqlText)
7670
val rowSet = JdbcTemplate(dataSource).queryForRowSet(sqlText)
7771
val iterator = object : Iterator<Map<String, Any>> {
@@ -101,7 +95,7 @@ class JdbcSelectQuery(private val dataSource: DataSource,
10195
{ a, b -> a }))
10296
}
10397
}
104-
return stream(spliteratorUnknownSize(iterator, ORDERED), false)
98+
return iterator.asSequence()
10599
}
106100

107101
override fun columns(): Map<String, String> {

src/main/kotlin/com/github/mgramin/sqlboot/sql/select/wrappers/PageWrapper.kt

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,19 @@ import com.github.mgramin.sqlboot.exceptions.BootException
2828
import com.github.mgramin.sqlboot.sql.select.SelectQuery
2929
import java.util.stream.Stream
3030

31-
class PageWrapper
32-
/**
33-
* Ctor.
34-
*
35-
* @param origin
36-
* @param pageNumber
37-
* @param pageSize
38-
*/
39-
(private val origin: SelectQuery,
40-
private val pageNumber: Int,
41-
private val pageSize: Int
31+
class PageWrapper(
32+
private val origin: SelectQuery,
33+
private val pageNumber: Int,
34+
private val pageSize: Int
4235
) : SelectQuery {
4336

4437
val sql = """select *
4538
| from ()""".trimMargin()
4639

4740
@Throws(BootException::class)
48-
override fun select(): Stream<Map<String, Any>> {
41+
override fun select(): Sequence<Map<String, Any>> {
4942
println(sql)
50-
return Stream.empty()
43+
return sequenceOf()
5144
}
5245

5346
override fun columns(): Map<String, String> {

src/test/kotlin/com/github/mgramin/sqlboot/sql/impl/JdbcSelectQueryTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class JdbcSelectQueryTest {
5454
| from (select name as "n"
5555
| , email as "mail"
5656
| from main_schema.users)""".trimMargin())
57-
.select().collect(Collectors.toList())
57+
.select().toList()
5858
println(select[0])
5959
// assertThat(select[0], hasEntry("labrador", "buzz"));
6060
// assertEquals(select.toString(),

0 commit comments

Comments
 (0)