Skip to content

Commit 30e678b

Browse files
committed
1 parent 03b0b28 commit 30e678b

15 files changed

Lines changed: 107 additions & 42 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
package com.github.mgramin.sqlboot.model.connection
2626

2727
class CheckedConnection(private val origin: DbConnection) : DbConnection {
28-
2928
private val health: String
3029

3130
init {
@@ -39,6 +38,8 @@ class CheckedConnection(private val origin: DbConnection) : DbConnection {
3938

4039
override fun name() = origin.name()
4140

41+
override fun dialect() = origin.dialect()
42+
4243
override fun properties() = origin.properties()
4344

4445
override fun health() = health

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

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

3333
fun health(): String
3434

35+
fun dialect(): String
36+
3537
fun getDataSource(): DataSource
3638

3739
fun paginationQueryTemplate(): String

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import javax.sql.DataSource
2828

2929
class FakeDbConnection : DbConnection {
3030

31+
override fun dialect() = "h2"
32+
3133
override fun properties(): Map<String, Any> {
3234
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
3335
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ open class SimpleDbConnection(
4242
@JsonIgnore var password: String? = null,
4343
var driverClassName: String? = null,
4444
var properties: String? = null,
45-
var paginationQueryTemplate: String? = null
45+
var paginationQueryTemplate: String? = null,
46+
var dialect: String? = null
4647
) : DbConnection {
4748

49+
override fun dialect() = this.dialect!!
50+
4851
override fun name() = this.name!!
4952

5053
override fun paginationQueryTemplate() = this.paginationQueryTemplate!!

src/main/kotlin/com/github/mgramin/sqlboot/model/dialect/DbDialectList.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ import org.springframework.stereotype.Service
3333
@ConfigurationProperties(prefix = "conf")
3434
open class DbDialectList(val dialects: List<DbDialect>) {
3535

36-
fun dialect(name: String) = dialects.first()
36+
fun dialect(name: String) = dialects.first { it.name.equals(name, ignoreCase = true) }
3737

3838
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* The MIT License (MIT)
3+
* <p>
4+
* Copyright (c) 2016-2019 Maksim Gramin
5+
* <p>
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
* <p>
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
* <p>
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package com.github.mgramin.sqlboot.model.dialect
26+
27+
// TODO rename to H2Dialect
28+
class FakeDialect : Dialect {
29+
30+
override fun name() = "h2"
31+
32+
override fun paginationQueryTemplate() = "${"$"}{query}"
33+
34+
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ package com.github.mgramin.sqlboot.model.resourcetype.impl
2626

2727
import com.github.mgramin.sqlboot.exceptions.BootException
2828
import com.github.mgramin.sqlboot.model.connection.SimpleDbConnection
29+
import com.github.mgramin.sqlboot.model.dialect.Dialect
2930
import com.github.mgramin.sqlboot.model.resourcetype.Metadata
3031
import com.github.mgramin.sqlboot.model.resourcetype.ResourceType
3132
import com.github.mgramin.sqlboot.model.resourcetype.wrappers.body.BodyWrapper
@@ -43,7 +44,10 @@ import java.nio.charset.StandardCharsets.UTF_8
4344
/**
4445
* Created by MGramin on 11.07.2017.
4546
*/
46-
class FsResourceType(private val dbConnections: List<SimpleDbConnection>) : ResourceType {
47+
class FsResourceType(
48+
private val dbConnections: List<SimpleDbConnection>,
49+
private val dialects: List<Dialect>
50+
) : ResourceType {
4751

4852
private val resourceTypes: List<ResourceType> = walk(dbConnections.first().baseFolder!!.file.path)
4953

@@ -64,7 +68,8 @@ class FsResourceType(private val dbConnections: List<SimpleDbConnection>) : Reso
6468
SqlResourceType(
6569
aliases = listOf(File(it.name()).nameWithoutExtension),
6670
sql = it.content().toString(Charset.defaultCharset()),
67-
connections = dbConnections),
71+
connections = dbConnections,
72+
dialects = dialects),
6873
templateGenerator = GroovyTemplateGenerator("[EMPTY BODY]"))))
6974

7075
@Deprecated("")
@@ -80,7 +85,6 @@ class FsResourceType(private val dbConnections: List<SimpleDbConnection>) : Reso
8085
.filter { it.name().matches(wildcardToRegex(uri)) }
8186
.map { it.read(uri) })
8287

83-
8488
override fun metaData(uri: Uri): List<Metadata> =
8589
resourceTypes
8690
.filter { it.name().matches(wildcardToRegex(uri)) }

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
package com.github.mgramin.sqlboot.model.resourcetype.impl
2626

2727
import com.github.mgramin.sqlboot.model.connection.DbConnection
28+
import com.github.mgramin.sqlboot.model.dialect.Dialect
2829
import com.github.mgramin.sqlboot.model.resource.DbResource
2930
import com.github.mgramin.sqlboot.model.resource.impl.DbResourceImpl
3031
import com.github.mgramin.sqlboot.model.resourcetype.Metadata
@@ -46,7 +47,8 @@ import reactor.core.publisher.Flux
4647
class SqlResourceType(
4748
private val aliases: List<String>,
4849
sql: String,
49-
private val connections: List<DbConnection>
50+
private val connections: List<DbConnection>,
51+
private val dialects: List<Dialect>
5052
) : ResourceType {
5153

5254
private val simpleSelectQuery = SimpleSelectQuery(GroovyTemplateGenerator(sql))
@@ -97,7 +99,9 @@ class SqlResourceType(
9799
simpleSelectQuery,
98100
uri.orderedColumns()),
99101
uri,
100-
connection.paginationQueryTemplate()),
102+
//dialects.first { it.name.equals("h2") }.paginationQueryTemplate!!
103+
dialects.first { it.name() == connection.dialect() }.paginationQueryTemplate()
104+
),
101105
dataSource = connection.getDataSource())
102106
}
103107

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

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

2727
import com.github.mgramin.sqlboot.exceptions.BootException
2828
import com.github.mgramin.sqlboot.model.connection.DbConnectionList
29+
import com.github.mgramin.sqlboot.model.dialect.DbDialectList
2930
import com.github.mgramin.sqlboot.model.resourcetype.Metadata
3031
import com.github.mgramin.sqlboot.model.resourcetype.ResourceType
3132
import com.github.mgramin.sqlboot.model.resourcetype.impl.FsResourceType
@@ -60,10 +61,13 @@ class ApiController {
6061
@Autowired
6162
private lateinit var dbConnectionList: DbConnectionList
6263

64+
@Autowired
65+
private lateinit var dbDialectList: DbDialectList
66+
6367

6468
@RequestMapping(value = ["/api/{connectionName}/types"])
6569
fun types(@PathVariable connectionName: String): List<ResourceType>? {
66-
return FsResourceType(dbConnectionList.getConnectionsByMask(connectionName)).resourceTypes()
70+
return FsResourceType(dbConnectionList.getConnectionsByMask(connectionName), emptyList()).resourceTypes()
6771
}
6872

6973

@@ -88,7 +92,7 @@ class ApiController {
8892
private fun getListResponseEntityHeaders(uri: Uri): ResponseEntity<List<Map<String, Any>>> {
8993
val connections = dbConnectionList.getConnectionsByMask(uri.connection())
9094
try {
91-
val headers = FsResourceType(connections)
95+
val headers = FsResourceType(connections, dbDialectList.dialects)
9296
.read(uri)
9397
.map { it.headers() }
9498
.collectList()
@@ -109,7 +113,8 @@ class ApiController {
109113

110114
private fun responseEntity(uri: Uri): ResponseEntity<List<Metadata>> {
111115
val fsResourceTypes = FsResourceType(
112-
listOf(dbConnectionList.getConnectionByName(uri.connection())))
116+
listOf(dbConnectionList.getConnectionByName(uri.connection())),
117+
emptyList())
113118
val resourceType = fsResourceTypes
114119
.resourceTypes()
115120
.stream()

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ 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
2930
import com.github.mgramin.sqlboot.model.resourcetype.impl.FsResourceType
3031
import io.swagger.models.Info
3132
import io.swagger.models.ModelImpl
@@ -92,7 +93,7 @@ class SwaggerController {
9293

9394
private fun getSwaggerDescription(request: HttpServletRequest, connectionName: String): Swagger {
9495
val fsResourceTypes = FsResourceType(
95-
listOf(dbConnectionList.getConnectionByName(connectionName)))
96+
listOf(dbConnectionList.getConnectionByName(connectionName)), emptyList())
9697
val resourceTypes = fsResourceTypes.resourceTypes()
9798
val swagger = Swagger()
9899

0 commit comments

Comments
 (0)