Skip to content

Commit 6df93cc

Browse files
committed
1 parent 41e3a26 commit 6df93cc

16 files changed

Lines changed: 78 additions & 39 deletions

File tree

conf/h2/database/schema/table/table.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
````sql
22
select "@schema" /* { "label": "owner", "description": "Owner of the table", "visible": true } */
33
, "@table" /* { "label": "table name", "description": "Name of the table", "visible": true } */
4-
, remarks /* { "label": "remarks", "description": "Explanatory comment on the table", "visible": true } */
4+
, remarks properties
55
, last_modification /* { "label": "last modification", "description": "Last modification" } */
66
, table_type /* { "label": "table type", "description": "Typical types", "visible": true, "values": ['TABLE', 'VIEW', 'SYSTEM TABLE', 'GLOBAL TEMPORARY', 'LOCAL TEMPORARY', 'ALIAS', 'SYNONYM'] } */
77
from (select t.table_schema as "@schema"

src/main/kotlin/com/github/mgramin/sqlboot/exceptions/BootException.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ package com.github.mgramin.sqlboot.exceptions
3333
class BootException : RuntimeException {
3434

3535
val errorCode: Int
36-
get() = field
3736

3837

3938
/**

src/main/kotlin/com/github/mgramin/sqlboot/model/resource/impl/DbResourceImpl.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ import com.github.mgramin.sqlboot.model.uri.Uri
3232
* DB resource without body
3333
*/
3434
class DbResourceImpl(
35-
private val name: String,
36-
@field:Transient private val type: ResourceType,
37-
@field:Transient private val uri: Uri,
38-
private val headers: Map<String, Any>,
39-
private val body: String = ""
35+
private val name: String,
36+
@field:Transient private val type: ResourceType,
37+
@field:Transient private val uri: Uri,
38+
private val headers: Map<String, Any>,
39+
private val body: String = ""
4040
) : DbResource {
4141

4242
override fun name(): String {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ class FsResourceType(private val dbConnections: List<SimpleDbConnection>) : Reso
5454
.filter { it.extension.equals("md", true) || it.extension.equals("sql", true) }
5555
.map { return@map if (it.extension.equals("md", true)) MarkdownFile(it.name, it.readText(UTF_8)) else SimpleFile(it.name, it.readText(UTF_8).toByteArray()) }
5656
.filter { it.content().isNotEmpty() }
57-
.map { createQuery(it) }
57+
.map { createObjectType(it) }
5858
.toList()
5959

60-
private fun createQuery(it: com.github.mgramin.sqlboot.tools.files.file.File) =
60+
private fun createObjectType(it: com.github.mgramin.sqlboot.tools.files.file.File) =
6161
SelectWrapper(
6262
SortWrapper(
6363
BodyWrapper(

src/main/kotlin/com/github/mgramin/sqlboot/model/resourcetype/wrappers/body/BodyWrapper.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ import reactor.core.publisher.Flux
3737
* Created by MGramin on 18.07.2017.
3838
*/
3939
class BodyWrapper(
40-
private val origin: ResourceType,
41-
private val templateGenerator: TemplateGenerator
40+
private val origin: ResourceType,
41+
private val templateGenerator: TemplateGenerator
4242
) : ResourceType {
4343

4444
override fun aliases(): List<String> {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,17 @@ interface SelectQuery {
4040
*/
4141
fun query(): String
4242

43-
4443
/**
4544
* Execute select query with parameters
4645
*
4746
* @return query result
4847
*/
4948
fun execute(variables: Map<String, Any>): Flux<Map<String, Any>>
5049

50+
/**
51+
* Query properties
52+
*/
53+
fun properties(): Map<String, String>
5154

5255
/**
5356
* Select columns

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import reactor.core.publisher.Flux
2929

3030
class FakeSelectQuery : SelectQuery {
3131

32+
override fun properties() = mapOf("dialect" to "h2")
33+
3234
override fun query(): String {
3335
return """select n /* First name */
3436
| , mail /* Personal email */

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

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,45 +29,54 @@ import com.github.mgramin.sqlboot.sql.select.impl.parser.SELECTBaseVisitor
2929
import com.github.mgramin.sqlboot.sql.select.impl.parser.SELECTLexer
3030
import com.github.mgramin.sqlboot.sql.select.impl.parser.SELECTParser
3131
import com.github.mgramin.sqlboot.template.generator.TemplateGenerator
32+
import com.google.gson.Gson
33+
import com.google.gson.reflect.TypeToken
3234
import org.antlr.v4.runtime.ANTLRInputStream
3335
import org.antlr.v4.runtime.CommonTokenStream
34-
import reactor.core.publisher.Flux
3536

3637
class SimpleSelectQuery(private val templateGenerator: TemplateGenerator) : SelectQuery {
3738

38-
override fun query(): String {
39-
return templateGenerator.template()
40-
}
39+
override fun query() = templateGenerator.template()
4140

42-
override fun columns(): Map<String, String> {
43-
return SelectStatementParser(templateGenerator.template())
44-
.parse()
45-
.map { it.name() to it.comment() }
46-
.toMap()
41+
override fun properties(): Map<String, String> {
42+
val comment: String = SelectStatementParser(templateGenerator.template()).comment().replace("/*", "").replace("*/", "")
43+
return try {
44+
Gson().fromJson(comment, object : TypeToken<Map<String, String>>() {}.type)
45+
} catch (e: Exception) {
46+
emptyMap()
47+
}
4748
}
4849

49-
override fun execute(variables: Map<String, Any>): Flux<Map<String, Any>> {
50-
throw RuntimeException("Not allow here")
51-
}
50+
override fun columns() =
51+
SelectStatementParser(templateGenerator.template())
52+
.parse()
53+
.map { it.name() to it.comment() }
54+
.toMap()
55+
56+
override fun execute(variables: Map<String, Any>) = throw RuntimeException("Not allow here")
5257

5358

54-
private class SelectStatementParser(private val sql: String) {
59+
private class SelectStatementParser(sql: String) {
60+
61+
val parser = SELECTParser(CommonTokenStream(SELECTLexer(ANTLRInputStream(sql))))
62+
val selectVisitorCustom = SelectVisitorCustom()
63+
val visit = selectVisitorCustom.visit(parser.select_statement())
64+
65+
fun comment() = selectVisitorCustom.queryComment
5566

5667
fun parse(): List<SelectQuery.Column> {
57-
val parser = SELECTParser(
58-
CommonTokenStream(
59-
SELECTLexer(
60-
ANTLRInputStream(sql))))
61-
val visit = SelectVisitorCustom().visit(parser.select_statement())
6268
return visit as List<SelectQuery.Column>
6369
}
6470
}
6571

6672

6773
private class SelectVisitorCustom : SELECTBaseVisitor<Any>() {
6874

75+
var queryComment: String = ""
76+
6977
override fun visitSelect_statement(ctx: SELECTParser.Select_statementContext): Any {
70-
val columns = ctx.select_row()
78+
queryComment = ctx.query_comment().text
79+
return ctx.select_row()
7180
.map { v ->
7281
SelectQuery.Column(
7382
v.column_alias()?.ID()?.text
@@ -77,7 +86,6 @@ class SimpleSelectQuery(private val templateGenerator: TemplateGenerator) : Sele
7786
} ?: (""), hashMapOf())
7887
}
7988
.toList()
80-
return columns
8189
}
8290
}
8391

src/main/kotlin/com/github/mgramin/sqlboot/sql/select/impl/parser/SELECT.g4

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ WITH_EXPRESSION
6262

6363

6464
select_statement
65-
: (with)*
65+
: query_comment
66+
(with)*
6667
select select_row( COMMA select_row )*
6768
from (schema_name+ DOT)? table_name
6869
.*?
@@ -125,6 +126,9 @@ column_alias
125126
;
126127

127128

129+
query_comment
130+
: MULTIPLE_LINE_COMMENT
131+
;
128132

129133
column_comment
130134
: MULTIPLE_LINE_COMMENT

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class JdbcSelectQuery(
4747
private val nullAlias: String
4848
) : SelectQuery {
4949

50+
override fun properties() = origin.properties()
51+
5052
constructor(origin: SelectQuery, dataSource: DataSource) : this(origin, dataSource, "")
5153

5254
private val logger = LoggerFactory.getLogger(this::class.java)

0 commit comments

Comments
 (0)