@@ -29,45 +29,54 @@ import com.github.mgramin.sqlboot.sql.select.impl.parser.SELECTBaseVisitor
2929import com.github.mgramin.sqlboot.sql.select.impl.parser.SELECTLexer
3030import com.github.mgramin.sqlboot.sql.select.impl.parser.SELECTParser
3131import com.github.mgramin.sqlboot.template.generator.TemplateGenerator
32+ import com.google.gson.Gson
33+ import com.google.gson.reflect.TypeToken
3234import org.antlr.v4.runtime.ANTLRInputStream
3335import org.antlr.v4.runtime.CommonTokenStream
34- import reactor.core.publisher.Flux
3536
3637class 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
0 commit comments