Skip to content

Commit adc3114

Browse files
committed
1 parent eb9e4dd commit adc3114

9 files changed

Lines changed: 56 additions & 38 deletions

File tree

src/main/kotlin/com/github/mgramin/sqlboot/model/connection/CheckedConnection.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ class CheckedConnection(private val origin: DbConnection) : DbConnection {
4444

4545
override fun health() = health
4646

47-
override fun paginationQueryTemplate() = origin.paginationQueryTemplate()
48-
4947
override fun getDataSource() = origin.getDataSource()
5048

5149
}

src/main/kotlin/com/github/mgramin/sqlboot/model/connection/DbConnection.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ interface DbConnection {
3636

3737
fun getDataSource(): DataSource
3838

39-
fun paginationQueryTemplate(): String
40-
4139
fun properties(): Map<String, Any>
4240

4341
}

src/main/kotlin/com/github/mgramin/sqlboot/model/connection/FakeDbConnection.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,4 @@ class FakeDbConnection : DbConnection {
4646
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
4747
}
4848

49-
override fun paginationQueryTemplate(): String {
50-
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
51-
}
52-
5349
}

src/main/kotlin/com/github/mgramin/sqlboot/model/connection/SimpleDbConnection.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ open class SimpleDbConnection(
5050

5151
override fun name() = this.name!!
5252

53-
override fun paginationQueryTemplate() = this.paginationQueryTemplate!!
54-
5553
override fun health() = "UNKNOWN"
5654

5755
private var dataSource: DataSource? = null

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import com.github.mgramin.sqlboot.sql.select.impl.SimpleSelectQuery
3737
import com.github.mgramin.sqlboot.sql.select.wrappers.JdbcSelectQuery
3838
import com.github.mgramin.sqlboot.sql.select.wrappers.OrderedSelectQuery
3939
import com.github.mgramin.sqlboot.sql.select.wrappers.PaginatedSelectQuery
40+
import com.github.mgramin.sqlboot.sql.select.wrappers.RestSelectQuery
4041
import com.github.mgramin.sqlboot.template.generator.impl.GroovyTemplateGenerator
4142
import org.apache.commons.lang3.StringUtils.strip
4243
import reactor.core.publisher.Flux
@@ -93,16 +94,25 @@ class SqlResourceType(
9394
Metadata("database", """{"label": "Database", "description": "Database name", "visible": true}""")
9495

9596
private fun createQuery(uri: Uri, connection: DbConnection): SelectQuery {
96-
return JdbcSelectQuery(
97-
PaginatedSelectQuery(
98-
OrderedSelectQuery(
99-
simpleSelectQuery,
100-
uri.orderedColumns()),
101-
uri,
102-
//dialects.first { it.name.equals("h2") }.paginationQueryTemplate!!
103-
dialects.first { it.name() == connection.dialect() }.paginationQueryTemplate()
104-
),
105-
dataSource = connection.getDataSource())
97+
val properties = simpleSelectQuery.properties()
98+
val baseQuery = PaginatedSelectQuery(
99+
OrderedSelectQuery(
100+
simpleSelectQuery,
101+
uri.orderedColumns()),
102+
uri,
103+
dialects.first { it.name() == connection.dialect() }.paginationQueryTemplate()
104+
)
105+
return if (properties["executor"] == "http") {
106+
RestSelectQuery(
107+
baseQuery,
108+
endpoint = "http://5.8.181.165:8082"
109+
)
110+
} else {
111+
JdbcSelectQuery(
112+
baseQuery,
113+
dataSource = connection.getDataSource()
114+
)
115+
}
106116
}
107117

108118
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ package com.github.mgramin.sqlboot.rest.controllers
2626

2727
import com.fasterxml.jackson.core.JsonProcessingException
2828
import com.github.mgramin.sqlboot.model.connection.DbConnectionList
29-
import com.github.mgramin.sqlboot.model.dialect.DbDialectList
3029
import com.github.mgramin.sqlboot.model.resourcetype.impl.FsResourceType
3130
import io.swagger.models.Info
3231
import io.swagger.models.ModelImpl

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class RestSelectQuery(
1414
private val endpoint: String
1515
) : SelectQuery {
1616

17+
override fun properties() = origin.properties()
18+
1719
override fun query() = origin.query()
1820

1921
override fun columns() = origin.columns()
@@ -22,7 +24,7 @@ class RestSelectQuery(
2224
val client: List<Map<String, Any>>? = WebClient
2325
.create(endpoint)
2426
.get()
25-
.uri("/exec?query=${origin.query()}")
27+
.uri("/exec?query=${origin.query()}".replace("{", "[").replace("}", "]"))
2628
.accept(MediaType.APPLICATION_JSON)
2729
.retrieve()
2830
.bodyToMono(String::class.java)

src/main/resources/application.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ conf:
44
- name: h2
55
paginationQueryTemplate: >
66
${query} offset ${uri.pageSize()*(uri.pageNumber()-1)} limit ${uri.pageSize()}
7+
- name: postgres
8+
paginationQueryTemplate: >
9+
${query}
710
811
connections:
912
- name: h2
13+
dialect: h2
1014
baseFolder: file:conf/h2/md/database
1115
url: jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'classpath:schema.sql';
1216
driverClassName: org.h2.Driver
@@ -16,6 +20,7 @@ conf:
1620
"description": "Embedded db for unit tests only"
1721
}
1822
- name: dev
23+
dialect: h2
1924
baseFolder: file:conf/h2/md/database
2025
url: jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'classpath:schema.sql';
2126
driverClassName: org.h2.Driver
@@ -26,6 +31,7 @@ conf:
2631
"css_class": "fas fa-fw fa-2x fa-tools"
2732
}
2833
- name: test
34+
dialect: h2
2935
baseFolder: file:conf/h2/md/database
3036
url: jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'classpath:schema.sql';
3137
driverClassName: org.h2.Driver
@@ -36,6 +42,7 @@ conf:
3642
"css_class": "fas fa-fw fa-2x fa-bug"
3743
}
3844
- name: prod
45+
dialect: h2
3946
baseFolder: file:conf/h2/md/database
4047
url: jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'classpath:schema.sql';
4148
driverClassName: org.h2.Driver

src/test/kotlin/com/github/mgramin/sqlboot/model/resourcetype/impl/SqlResourceTypeTest.kt

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ package com.github.mgramin.sqlboot.model.resourcetype.impl
2727
import com.github.mgramin.sqlboot.model.connection.SimpleDbConnection
2828
import com.github.mgramin.sqlboot.model.dialect.FakeDialect
2929
import com.github.mgramin.sqlboot.model.uri.impl.DbUri
30+
import com.github.mgramin.sqlboot.model.uri.impl.FakeUri
3031
import org.junit.jupiter.api.Assertions.assertEquals
3132
import org.junit.jupiter.api.Test
3233
import org.junit.jupiter.api.extension.ExtendWith
@@ -56,13 +57,13 @@ class SqlResourceTypeTest {
5657

5758
@Test
5859
fun name() {
59-
val name = SqlResourceType(arrayListOf("table", "tbl", "t"), "", listOf(db), emptyList()).name()
60+
val name = SqlResourceType(listOf("table", "tbl", "t"), "", listOf(db), listOf(FakeDialect())).name()
6061
assertEquals("table", name)
6162
}
6263

6364
@Test
6465
fun aliases() {
65-
val aliases = SqlResourceType(arrayListOf("table", "tbl", "t"), "", listOf(db), emptyList()).aliases()
66+
val aliases = SqlResourceType(listOf("table", "tbl", "t"), "", listOf(db), listOf(FakeDialect())).aliases()
6667
assertEquals(arrayListOf("table", "tbl", "t"), aliases)
6768
}
6869

@@ -72,14 +73,8 @@ class SqlResourceTypeTest {
7273
| from (select table_schema as "@schema"
7374
| , table_name as "@table"
7475
| from information_schema.tables)""".trimMargin()
75-
val type =
76-
SqlResourceType(
77-
aliases = arrayListOf("table"),
78-
sql = sql,
79-
connections = listOf(db),
80-
dialects = listOf(FakeDialect()))
8176
StepVerifier
82-
.create(type.read(DbUri("table/m.column")))
77+
.create(createSqlResourceType(sql).read(FakeUri()))
8378
.expectNextCount(46)
8479
.verifyComplete()
8580
}
@@ -91,18 +86,26 @@ class SqlResourceTypeTest {
9186
| , table_name as "@table"
9287
| , column_name as "@column"
9388
| from information_schema.columns)""".trimMargin()
94-
val type =
95-
SqlResourceType(
96-
aliases = arrayListOf("column"),
97-
sql = sql,
98-
connections = listOf(db),
99-
dialects = listOf(FakeDialect()))
10089
StepVerifier
101-
.create(type.read(DbUri("column/main_schema.users")))
90+
.create(createSqlResourceType(sql).read(FakeUri()))
10291
.expectNextCount(347)
10392
.verifyComplete()
10493
}
10594

95+
@Test
96+
fun read3() {
97+
val sql = """/* { "executor": "http" } */
98+
|select pid as "@pid"
99+
| , name
100+
| , total_size
101+
| from processes
102+
| limit 5""".trimMargin()
103+
StepVerifier
104+
.create(createSqlResourceType(sql).read(DbUri("prod/process")))
105+
.expectNextCount(5)
106+
.verifyComplete()
107+
}
108+
106109
@Test
107110
fun path() {
108111
val sql = """select @schema
@@ -125,4 +128,11 @@ class SqlResourceTypeTest {
125128
fun metaData() {
126129
}
127130

131+
private fun createSqlResourceType(sql: String) =
132+
SqlResourceType(
133+
aliases = arrayListOf("table"),
134+
sql = sql,
135+
connections = listOf(db),
136+
dialects = listOf(FakeDialect()))
137+
128138
}

0 commit comments

Comments
 (0)