mirror of
https://gitee.com/dromara/mayfly-go
synced 2026-01-07 23:15:46 +08:00
feat: sql解析器替换、工单统一由‘我的流程’发起、流程定义支持自定义条件触发审批、资源隐藏编号、model支持物理删除等
This commit is contained in:
1683
server/internal/db/dbm/sqlparser/pgsql/antlr4/PostgreSQLLexer.g4
Normal file
1683
server/internal/db/dbm/sqlparser/pgsql/antlr4/PostgreSQLLexer.g4
Normal file
File diff suppressed because it is too large
Load Diff
2074
server/internal/db/dbm/sqlparser/pgsql/antlr4/PostgreSQLLexer.interp
Normal file
2074
server/internal/db/dbm/sqlparser/pgsql/antlr4/PostgreSQLLexer.interp
Normal file
File diff suppressed because one or more lines are too long
1314
server/internal/db/dbm/sqlparser/pgsql/antlr4/PostgreSQLLexer.tokens
Normal file
1314
server/internal/db/dbm/sqlparser/pgsql/antlr4/PostgreSQLLexer.tokens
Normal file
File diff suppressed because it is too large
Load Diff
5578
server/internal/db/dbm/sqlparser/pgsql/antlr4/PostgreSQLParser.g4
Normal file
5578
server/internal/db/dbm/sqlparser/pgsql/antlr4/PostgreSQLParser.g4
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
1
server/internal/db/dbm/sqlparser/pgsql/antlr4/build.sh
Executable file
1
server/internal/db/dbm/sqlparser/pgsql/antlr4/build.sh
Executable file
@@ -0,0 +1 @@
|
||||
java -jar antlr-4.13.1-complete.jar -Dlanguage=Go -package parser -visitor *.g4
|
||||
4408
server/internal/db/dbm/sqlparser/pgsql/antlr4/postgresql_lexer.go
Normal file
4408
server/internal/db/dbm/sqlparser/pgsql/antlr4/postgresql_lexer.go
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
PostgreSQL grammar.
|
||||
The MIT License (MIT).
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package parser
|
||||
|
||||
import (
|
||||
"unicode"
|
||||
|
||||
"github.com/antlr4-go/antlr/v4"
|
||||
)
|
||||
|
||||
type PostgreSQLLexerBase struct {
|
||||
*antlr.BaseLexer
|
||||
|
||||
stack StringStack
|
||||
}
|
||||
|
||||
func (receiver *PostgreSQLLexerBase) pushTag() {
|
||||
receiver.stack.Push(receiver.GetText())
|
||||
}
|
||||
|
||||
func (receiver *PostgreSQLLexerBase) isTag() bool {
|
||||
if receiver.stack.IsEmpty() {
|
||||
return false
|
||||
}
|
||||
return receiver.GetText() == receiver.stack.PeekOrEmpty()
|
||||
}
|
||||
|
||||
func (receiver *PostgreSQLLexerBase) popTag() {
|
||||
_, _ = receiver.stack.Pop()
|
||||
}
|
||||
|
||||
func (receiver *PostgreSQLLexerBase) checkLA(c int) bool {
|
||||
return receiver.GetInputStream().LA(1) != c
|
||||
}
|
||||
|
||||
func (receiver *PostgreSQLLexerBase) charIsLetter() bool {
|
||||
c := receiver.GetInputStream().LA(-1)
|
||||
return unicode.IsLetter(rune(c))
|
||||
}
|
||||
|
||||
func (receiver *PostgreSQLLexerBase) HandleNumericFail() {
|
||||
index := receiver.GetInputStream().Index() - 2
|
||||
receiver.GetInputStream().Seek(index)
|
||||
receiver.SetType(PostgreSQLLexerIntegral)
|
||||
}
|
||||
|
||||
func (receiver *PostgreSQLLexerBase) HandleLessLessGreaterGreater() {
|
||||
if receiver.GetText() == "<<" {
|
||||
receiver.SetType(PostgreSQLLexerLESS_LESS)
|
||||
}
|
||||
if receiver.GetText() == ">>" {
|
||||
receiver.SetType(PostgreSQLLexerGREATER_GREATER)
|
||||
}
|
||||
}
|
||||
|
||||
func (receiver *PostgreSQLLexerBase) UnterminatedBlockCommentDebugAssert() {
|
||||
//Debug.Assert(InputStream.LA(1) == -1 /*EOF*/);
|
||||
}
|
||||
|
||||
func (receiver *PostgreSQLLexerBase) CheckIfUtf32Letter() bool {
|
||||
codePoint := receiver.GetInputStream().LA(-2)<<8 + receiver.GetInputStream().LA(-1)
|
||||
var c []rune
|
||||
if codePoint < 0x10000 {
|
||||
c = []rune{rune(codePoint)}
|
||||
} else {
|
||||
codePoint -= 0x10000
|
||||
c = []rune{
|
||||
(rune)(codePoint/0x400 + 0xd800),
|
||||
(rune)(codePoint%0x400 + 0xdc00),
|
||||
}
|
||||
}
|
||||
return unicode.IsLetter(c[0])
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
PostgreSQL grammar.
|
||||
The MIT License (MIT).
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package parser
|
||||
|
||||
type PostgreSQLParseError struct {
|
||||
Number int
|
||||
Offset int
|
||||
Line int
|
||||
Column int
|
||||
Message string
|
||||
}
|
||||
185013
server/internal/db/dbm/sqlparser/pgsql/antlr4/postgresql_parser.go
Normal file
185013
server/internal/db/dbm/sqlparser/pgsql/antlr4/postgresql_parser.go
Normal file
File diff suppressed because one or more lines are too long
@@ -0,0 +1,214 @@
|
||||
/*
|
||||
PostgreSQL grammar.
|
||||
The MIT License (MIT).
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package parser
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/antlr4-go/antlr/v4"
|
||||
)
|
||||
|
||||
type PostgreSQLParserBase struct {
|
||||
*antlr.BaseParser
|
||||
|
||||
parseErrors []*PostgreSQLParseError
|
||||
}
|
||||
|
||||
func NewPostgreSQLParserBase(input antlr.TokenStream) *PostgreSQLParserBase {
|
||||
return &PostgreSQLParserBase{
|
||||
BaseParser: antlr.NewBaseParser(input),
|
||||
}
|
||||
}
|
||||
|
||||
func (receiver *PostgreSQLParserBase) GetParsedSqlTree(script string, line int) antlr.ParserRuleContext {
|
||||
parser := getPostgreSQLParser(script)
|
||||
result := parser.Root()
|
||||
for _, err := range parser.parseErrors {
|
||||
receiver.parseErrors = append(receiver.parseErrors, &PostgreSQLParseError{
|
||||
Number: err.Number,
|
||||
Offset: err.Offset,
|
||||
Line: err.Line + line,
|
||||
Column: err.Column,
|
||||
Message: err.Message,
|
||||
})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (receiver *PostgreSQLParserBase) ParseRoutineBody(localContextInterface ICreatefunc_opt_listContext) {
|
||||
localContext, ok := localContextInterface.(*Createfunc_opt_listContext)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
var lang string
|
||||
for _, coi := range localContext.AllCreatefunc_opt_item() {
|
||||
createFuncOptItemContext, ok := coi.(*Createfunc_opt_itemContext)
|
||||
if !ok || createFuncOptItemContext.LANGUAGE() == nil {
|
||||
continue
|
||||
}
|
||||
nonReservedWordOrSConstContextInterface := createFuncOptItemContext.Nonreservedword_or_sconst()
|
||||
if nonReservedWordOrSConstContextInterface == nil {
|
||||
continue
|
||||
}
|
||||
nonReservedWordOrSConstContext, ok := nonReservedWordOrSConstContextInterface.(*Nonreservedword_or_sconstContext)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
nonReservedWordContextInterface := nonReservedWordOrSConstContext.Nonreservedword()
|
||||
if nonReservedWordContextInterface == nil {
|
||||
continue
|
||||
}
|
||||
nonReservedWordContext, ok := nonReservedWordContextInterface.(*NonreservedwordContext)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
identifierInterface := nonReservedWordContext.Identifier()
|
||||
if identifierInterface == nil {
|
||||
continue
|
||||
}
|
||||
identifier, ok := identifierInterface.(*IdentifierContext)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
node := identifier.Identifier()
|
||||
if node == nil {
|
||||
continue
|
||||
}
|
||||
lang = node.GetText()
|
||||
break
|
||||
}
|
||||
if lang == "" {
|
||||
return
|
||||
}
|
||||
|
||||
var funcAs *Createfunc_opt_itemContext
|
||||
for _, coi := range localContext.AllCreatefunc_opt_item() {
|
||||
ctx, ok := coi.(*Createfunc_opt_itemContext)
|
||||
if !ok || ctx.LANGUAGE() == nil {
|
||||
continue
|
||||
}
|
||||
as := ctx.Func_as()
|
||||
if as != nil {
|
||||
funcAs = ctx
|
||||
break
|
||||
}
|
||||
}
|
||||
if funcAs == nil {
|
||||
return
|
||||
}
|
||||
|
||||
funcAsContextInterface := funcAs.Func_as()
|
||||
if funcAsContextInterface == nil {
|
||||
return
|
||||
}
|
||||
funcAsContext, ok := funcAsContextInterface.(*Func_asContext)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
sConstContextInterface := funcAsContext.Sconst(0)
|
||||
if sConstContextInterface == nil {
|
||||
return
|
||||
}
|
||||
sConstContext, ok := sConstContextInterface.(*SconstContext)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
text := GetRoutineBodyString(sConstContext)
|
||||
line := sConstContext.GetStart().GetLine()
|
||||
parser := getPostgreSQLParser(text)
|
||||
switch lang {
|
||||
case "plpgsql":
|
||||
funcAs.Func_as().(*Func_asContext).Definition = parser.Plsqlroot()
|
||||
case "sql":
|
||||
funcAs.Func_as().(*Func_asContext).Definition = parser.Root()
|
||||
}
|
||||
for _, err := range parser.parseErrors {
|
||||
receiver.parseErrors = append(receiver.parseErrors, &PostgreSQLParseError{
|
||||
Number: err.Number,
|
||||
Offset: err.Offset,
|
||||
Line: err.Line + line,
|
||||
Column: err.Column,
|
||||
Message: err.Message,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TrimQuotes(s string) string {
|
||||
if s == "" {
|
||||
return s
|
||||
}
|
||||
return s[1 : len(s)-2]
|
||||
}
|
||||
|
||||
func unquote(s string) string {
|
||||
result := strings.Builder{}
|
||||
length := len(s)
|
||||
index := 0
|
||||
for index < length {
|
||||
c := s[index]
|
||||
result.WriteByte(c)
|
||||
if c == '\'' && index < length-1 && (s[index+1] == '\'') {
|
||||
index++
|
||||
}
|
||||
index++
|
||||
}
|
||||
return result.String()
|
||||
}
|
||||
|
||||
func GetRoutineBodyString(rule *SconstContext) string {
|
||||
if rule.Anysconst() == nil {
|
||||
return ""
|
||||
}
|
||||
anySConstContext := rule.Anysconst().(*AnysconstContext)
|
||||
|
||||
stringConstant := anySConstContext.StringConstant()
|
||||
if stringConstant != nil {
|
||||
return unquote(TrimQuotes(stringConstant.GetText()))
|
||||
}
|
||||
|
||||
unicodeEscapeStringConstant := anySConstContext.UnicodeEscapeStringConstant()
|
||||
if unicodeEscapeStringConstant != nil {
|
||||
return TrimQuotes(unicodeEscapeStringConstant.GetText())
|
||||
}
|
||||
|
||||
escapeStringConstant := anySConstContext.EscapeStringConstant()
|
||||
if escapeStringConstant != nil {
|
||||
return TrimQuotes(escapeStringConstant.GetText())
|
||||
}
|
||||
|
||||
result := strings.Builder{}
|
||||
for _, node := range anySConstContext.AllDollarText() {
|
||||
result.WriteString(node.GetText())
|
||||
}
|
||||
return result.String()
|
||||
}
|
||||
|
||||
func getPostgreSQLParser(script string) *PostgreSQLParser {
|
||||
stream := antlr.NewInputStream(script)
|
||||
lexer := NewPostgreSQLLexer(stream)
|
||||
tokenStream := antlr.NewCommonTokenStream(lexer, 0)
|
||||
parser := NewPostgreSQLParser(tokenStream)
|
||||
errorListener := new(PostgreSQLParserErrorListener)
|
||||
errorListener.grammar = parser
|
||||
parser.AddErrorListener(errorListener)
|
||||
return parser
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
PostgreSQL grammar.
|
||||
The MIT License (MIT).
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package parser
|
||||
|
||||
import "github.com/antlr4-go/antlr/v4"
|
||||
|
||||
type PostgreSQLParserErrorListener struct {
|
||||
grammar *PostgreSQLParser
|
||||
}
|
||||
|
||||
var _ antlr.ErrorListener = &PostgreSQLParserErrorListener{}
|
||||
|
||||
func (receiver PostgreSQLParserErrorListener) SyntaxError(recognizer antlr.Recognizer, offendingSymbol interface{}, line, column int, msg string, e antlr.RecognitionException) {
|
||||
receiver.grammar.parseErrors = append(receiver.grammar.parseErrors, &PostgreSQLParseError{
|
||||
Number: 0,
|
||||
Offset: 0,
|
||||
Line: line,
|
||||
Column: column,
|
||||
Message: msg,
|
||||
})
|
||||
}
|
||||
|
||||
func (receiver PostgreSQLParserErrorListener) ReportAmbiguity(recognizer antlr.Parser, dfa *antlr.DFA, startIndex, stopIndex int, exact bool, ambigAlts *antlr.BitSet, configs *antlr.ATNConfigSet) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
func (receiver PostgreSQLParserErrorListener) ReportAttemptingFullContext(recognizer antlr.Parser, dfa *antlr.DFA, startIndex, stopIndex int, conflictingAlts *antlr.BitSet, configs *antlr.ATNConfigSet) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
func (receiver PostgreSQLParserErrorListener) ReportContextSensitivity(recognizer antlr.Parser, dfa *antlr.DFA, startIndex, stopIndex, prediction int, configs *antlr.ATNConfigSet) {
|
||||
// ignore
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
PostgreSQL grammar.
|
||||
The MIT License (MIT).
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package parser
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrorStackEmpty = errors.New("stack empty")
|
||||
)
|
||||
|
||||
type StringStack struct {
|
||||
items []string
|
||||
}
|
||||
|
||||
func (receiver *StringStack) Push(value string) {
|
||||
receiver.items = append(receiver.items, value)
|
||||
}
|
||||
|
||||
func (receiver *StringStack) Pop() (string, error) {
|
||||
if receiver.IsEmpty() {
|
||||
return "", ErrorStackEmpty
|
||||
}
|
||||
value := receiver.items[0]
|
||||
receiver.items = receiver.items[1:]
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func (receiver *StringStack) PopOrEmpty() string {
|
||||
value, err := receiver.Pop()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func (receiver *StringStack) Peek() (string, error) {
|
||||
if receiver.IsEmpty() {
|
||||
return "", ErrorStackEmpty
|
||||
}
|
||||
return receiver.items[0], nil
|
||||
}
|
||||
|
||||
func (receiver *StringStack) PeekOrEmpty() string {
|
||||
value, err := receiver.Peek()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func (receiver *StringStack) Size() int {
|
||||
return len(receiver.items)
|
||||
}
|
||||
|
||||
func (receiver *StringStack) IsEmpty() bool {
|
||||
return receiver.Size() == 0
|
||||
}
|
||||
60
server/internal/db/dbm/sqlparser/pgsql/pgsql.go
Normal file
60
server/internal/db/dbm/sqlparser/pgsql/pgsql.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package pgsql
|
||||
|
||||
import (
|
||||
"mayfly-go/internal/db/dbm/sqlparser/base"
|
||||
pgparser "mayfly-go/internal/db/dbm/sqlparser/pgsql/antlr4"
|
||||
"mayfly-go/pkg/logx"
|
||||
|
||||
"mayfly-go/internal/db/dbm/sqlparser/sqlstmt"
|
||||
|
||||
"github.com/antlr4-go/antlr/v4"
|
||||
)
|
||||
|
||||
func GetPgsqlParserTree(baseLine int, statement string) (antlr.ParseTree, *antlr.CommonTokenStream, error) {
|
||||
lexer := pgparser.NewPostgreSQLLexer(antlr.NewInputStream(statement))
|
||||
stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
|
||||
parser := pgparser.NewPostgreSQLParser(stream)
|
||||
|
||||
lexerErrorListener := &base.ParseErrorListener{
|
||||
BaseLine: baseLine,
|
||||
}
|
||||
lexer.RemoveErrorListeners()
|
||||
lexer.AddErrorListener(lexerErrorListener)
|
||||
|
||||
parserErrorListener := &base.ParseErrorListener{
|
||||
BaseLine: baseLine,
|
||||
}
|
||||
parser.RemoveErrorListeners()
|
||||
parser.AddErrorListener(parserErrorListener)
|
||||
parser.BuildParseTrees = true
|
||||
|
||||
tree := parser.Root()
|
||||
|
||||
if lexerErrorListener.Err != nil {
|
||||
return nil, nil, lexerErrorListener.Err
|
||||
}
|
||||
|
||||
if parserErrorListener.Err != nil {
|
||||
return nil, nil, parserErrorListener.Err
|
||||
}
|
||||
|
||||
return tree, stream, nil
|
||||
}
|
||||
|
||||
type PgsqlParser struct {
|
||||
}
|
||||
|
||||
func (*PgsqlParser) Parse(stmt string) (stmts []sqlstmt.Stmt, err error) {
|
||||
defer func() {
|
||||
if e := recover(); e != nil {
|
||||
logx.ErrorTrace("postgres sql parser err: ", e)
|
||||
err = e.(error)
|
||||
}
|
||||
}()
|
||||
tree, _, err := GetPgsqlParserTree(1, stmt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return tree.Accept(new(PgsqlVisitor)).([]sqlstmt.Stmt), nil
|
||||
}
|
||||
83
server/internal/db/dbm/sqlparser/pgsql/pgsql_test.go
Normal file
83
server/internal/db/dbm/sqlparser/pgsql/pgsql_test.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package pgsql
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"mayfly-go/internal/db/dbm/sqlparser/sqlstmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParserSimpleSelect(t *testing.T) {
|
||||
parser := new(PgsqlParser)
|
||||
|
||||
sql := `SELECT t.* FROM mayfly.sys_login_log as t where t.id > 0 OFFSET 0 LIMIT 25; select * from tdb where id > 1`
|
||||
stmts, err := parser.Parse(sql)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stmt := stmts[1].(*sqlstmt.SimpleSelectStmt)
|
||||
|
||||
t.Log(stmt.QuerySpecification.Where.GetText())
|
||||
fmt.Println(stmt.QuerySpecification.From.GetText())
|
||||
t.Log(stmts)
|
||||
}
|
||||
|
||||
func TestParserUnionSelect(t *testing.T) {
|
||||
parser := new(PgsqlParser)
|
||||
|
||||
sql := `(select sum(t.age), t.id tid, t1.id2, t1.* from T_DB t join t_db_ins as t1 on t.id = t1.id2 where t.id = 1 AND t1.status=0 and t.id2='9' and t.name in ('name2', 'name3') order by t.id desc) union all (select * from t_db2) OFFSET 0 LIMIT 25;`
|
||||
stmts, err := parser.Parse(sql)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(stmts)
|
||||
}
|
||||
|
||||
func TestParserSingleUpdate(t *testing.T) {
|
||||
parser := new(PgsqlParser)
|
||||
|
||||
sql := `UPDATE test.t_sys_msg t
|
||||
SET
|
||||
recipient_id = 13,
|
||||
t.creator = 'admin4'
|
||||
WHERE
|
||||
t.id = 1;`
|
||||
stmts, err := parser.Parse(sql)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(stmts)
|
||||
}
|
||||
|
||||
func TestParserDelete(t *testing.T) {
|
||||
parser := new(PgsqlParser)
|
||||
|
||||
sql := `Delete from t_sys_msg t
|
||||
WHERE
|
||||
t.id = 1;`
|
||||
stmts, err := parser.Parse(sql)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(stmts)
|
||||
}
|
||||
|
||||
func TestParserInsert(t *testing.T) {
|
||||
parser := new(PgsqlParser)
|
||||
|
||||
sql := `INSERT INTO
|
||||
mayfly_go.t_sys_msg (
|
||||
type,
|
||||
msg,
|
||||
recipient_id,
|
||||
creator_id,
|
||||
create_time,
|
||||
is_deleted
|
||||
)
|
||||
VALUES
|
||||
(1, 'hahaha', 2, 1, '2024-08-26 15:36:27', 0);`
|
||||
stmts, err := parser.Parse(sql)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(stmts)
|
||||
}
|
||||
417
server/internal/db/dbm/sqlparser/pgsql/visitor.go
Normal file
417
server/internal/db/dbm/sqlparser/pgsql/visitor.go
Normal file
@@ -0,0 +1,417 @@
|
||||
package pgsql
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
pgparser "mayfly-go/internal/db/dbm/sqlparser/pgsql/antlr4"
|
||||
"mayfly-go/internal/db/dbm/sqlparser/sqlstmt"
|
||||
|
||||
"github.com/may-fly/cast"
|
||||
)
|
||||
|
||||
type PgsqlVisitor struct {
|
||||
*pgparser.BasePostgreSQLParserVisitor
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitRoot(ctx *pgparser.RootContext) interface{} {
|
||||
if sbc := ctx.Stmtblock(); sbc != nil {
|
||||
return sbc.Accept(v)
|
||||
}
|
||||
return sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitPlsqlroot(ctx *pgparser.PlsqlrootContext) interface{} {
|
||||
return v.VisitChildren(ctx)
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitStmtblock(ctx *pgparser.StmtblockContext) interface{} {
|
||||
if smc := ctx.Stmtmulti(); smc != nil {
|
||||
return smc.Accept(v)
|
||||
}
|
||||
return sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitStmtmulti(ctx *pgparser.StmtmultiContext) interface{} {
|
||||
allSqlStatement := ctx.AllStmt()
|
||||
stmts := make([]sqlstmt.Stmt, 0)
|
||||
for _, sqlStatement := range allSqlStatement {
|
||||
stmts = append(stmts, sqlStatement.Accept(v).(sqlstmt.Stmt))
|
||||
}
|
||||
return stmts
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitStmt(ctx *pgparser.StmtContext) interface{} {
|
||||
if selectstmtCtx := ctx.Selectstmt(); selectstmtCtx != nil {
|
||||
return selectstmtCtx.Accept(v)
|
||||
}
|
||||
if updatestmtCtx := ctx.Updatestmt(); updatestmtCtx != nil {
|
||||
return updatestmtCtx.Accept(v)
|
||||
}
|
||||
if deletestmtCtx := ctx.Deletestmt(); deletestmtCtx != nil {
|
||||
return deletestmtCtx.Accept(v)
|
||||
}
|
||||
if insertstmtC := ctx.Insertstmt(); insertstmtC != nil {
|
||||
return insertstmtC.Accept(v)
|
||||
}
|
||||
if c := ctx.Createdbstmt(); c != nil {
|
||||
cds := new(sqlstmt.CreateDatabase)
|
||||
cds.Node = sqlstmt.NewNode(c.GetParser(), c)
|
||||
return cds
|
||||
}
|
||||
if c := ctx.Createtablespacestmt(); c != nil {
|
||||
cds := new(sqlstmt.CreateTable)
|
||||
cds.Node = sqlstmt.NewNode(c.GetParser(), c)
|
||||
return cds
|
||||
}
|
||||
if c := ctx.Altertablestmt(); c != nil {
|
||||
cds := new(sqlstmt.AlterTable)
|
||||
cds.Node = sqlstmt.NewNode(c.GetParser(), c)
|
||||
return cds
|
||||
}
|
||||
if c := ctx.Dropdbstmt(); c != nil {
|
||||
cds := new(sqlstmt.DropDatabase)
|
||||
cds.Node = sqlstmt.NewNode(c.GetParser(), c)
|
||||
return cds
|
||||
}
|
||||
if c := ctx.Droptablespacestmt(); c != nil {
|
||||
cds := new(sqlstmt.DropTable)
|
||||
cds.Node = sqlstmt.NewNode(c.GetParser(), c)
|
||||
return cds
|
||||
}
|
||||
if explain := ctx.Explainstmt(); explain != nil {
|
||||
otherRead := new(sqlstmt.OtherReadStmt)
|
||||
otherRead.Node = sqlstmt.NewNode(explain.GetParser(), explain)
|
||||
return otherRead
|
||||
}
|
||||
if c := ctx.Variableshowstmt(); c != nil {
|
||||
otherRead := new(sqlstmt.OtherReadStmt)
|
||||
otherRead.Node = sqlstmt.NewNode(c.GetParser(), c)
|
||||
return otherRead
|
||||
}
|
||||
return sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitSelectstmt(ctx *pgparser.SelectstmtContext) interface{} {
|
||||
if spnc := ctx.Select_no_parens(); spnc != nil {
|
||||
return spnc.Accept(v)
|
||||
}
|
||||
selectstmt := new(sqlstmt.SelectStmt)
|
||||
selectstmt.Node = sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
return selectstmt
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitSelect_with_parens(ctx *pgparser.Select_with_parensContext) interface{} {
|
||||
return v.VisitChildren(ctx)
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitSelect_no_parens(ctx *pgparser.Select_no_parensContext) interface{} {
|
||||
if c := ctx.Select_clause(); c == nil {
|
||||
return sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
}
|
||||
|
||||
var limit *sqlstmt.Limit
|
||||
if limitC := ctx.Select_limit(); limitC != nil {
|
||||
limit = limitC.Accept(v).(*sqlstmt.Limit)
|
||||
}
|
||||
if limitC := ctx.Opt_select_limit(); limitC != nil {
|
||||
limit = limitC.Accept(v).(*sqlstmt.Limit)
|
||||
}
|
||||
|
||||
selectClause := ctx.Select_clause()
|
||||
asis := selectClause.AllSimple_select_intersect()
|
||||
// 简单查询
|
||||
if len(asis) == 1 {
|
||||
sss := new(sqlstmt.SimpleSelectStmt)
|
||||
sss.Node = sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
sss.QuerySpecification = ctx.Select_clause().Accept(v).([]*sqlstmt.QuerySpecification)[0]
|
||||
sss.QuerySpecification.Limit = limit
|
||||
return sss
|
||||
}
|
||||
|
||||
uss := new(sqlstmt.UnionSelectStmt)
|
||||
uss.Node = sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
|
||||
allUnion := selectClause.AllUNION()
|
||||
// todo 赋值union信息
|
||||
for _, union := range allUnion {
|
||||
uss.UnionType = union.GetText()
|
||||
}
|
||||
// uss.QuerySpecifications = ctx.Select_clause().Accept(v).([]*sqlstmt.QuerySpecification)
|
||||
uss.Limit = limit
|
||||
return uss
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitSelect_clause(ctx *pgparser.Select_clauseContext) interface{} {
|
||||
qs := make([]*sqlstmt.QuerySpecification, 0)
|
||||
for _, ssi := range ctx.AllSimple_select_intersect() {
|
||||
qs = append(qs, ssi.Accept(v).(*sqlstmt.QuerySpecification))
|
||||
}
|
||||
|
||||
return qs
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitSimple_select_intersect(ctx *pgparser.Simple_select_intersectContext) interface{} {
|
||||
// 只返回一个查询,INTERSECT(交集)暂不支持
|
||||
if spsc := ctx.AllSimple_select_pramary(); spsc != nil {
|
||||
return spsc[0].Accept(v)
|
||||
}
|
||||
return sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitSimple_select_pramary(ctx *pgparser.Simple_select_pramaryContext) interface{} {
|
||||
qs := new(sqlstmt.QuerySpecification)
|
||||
qs.Node = sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
|
||||
if c := ctx.From_clause(); c != nil {
|
||||
qs.From = c.Accept(v).(*sqlstmt.TableSources)
|
||||
}
|
||||
|
||||
if c := ctx.Where_clause(); c != nil {
|
||||
qs.Where = c.A_expr().Accept(v).(sqlstmt.IExpr)
|
||||
}
|
||||
|
||||
return qs
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitSelect_limit(ctx *pgparser.Select_limitContext) interface{} {
|
||||
limit := new(sqlstmt.Limit)
|
||||
limit.Node = sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
|
||||
if lc := ctx.Limit_clause(); lc != nil {
|
||||
if lv := lc.Select_limit_value(); lv != nil {
|
||||
limit.RowCount = cast.ToInt(lv.GetText())
|
||||
}
|
||||
}
|
||||
if oc := ctx.Offset_clause(); oc != nil {
|
||||
if ov := oc.Select_offset_value(); ov != nil {
|
||||
limit.Offset = cast.ToInt(ov.GetText())
|
||||
}
|
||||
}
|
||||
return limit
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitOpt_select_limit(ctx *pgparser.Opt_select_limitContext) interface{} {
|
||||
if slc := ctx.Select_limit(); slc != nil {
|
||||
return slc.Accept(v)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitFrom_clause(ctx *pgparser.From_clauseContext) interface{} {
|
||||
if c := ctx.From_list(); c != nil {
|
||||
return c.Accept(v)
|
||||
}
|
||||
return sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitFrom_list(ctx *pgparser.From_listContext) interface{} {
|
||||
ts := new(sqlstmt.TableSources)
|
||||
ts.Node = sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
|
||||
// ts.StartIndex = ctx.GetStart().GetStart()
|
||||
// ts.StopIndex = ctx.GetStop().GetStop()
|
||||
|
||||
tableSources := make([]sqlstmt.ITableSource, 0)
|
||||
allTableRefCtx := ctx.AllTable_ref()
|
||||
for _, trc := range allTableRefCtx {
|
||||
tableSources = append(tableSources, trc.Accept(v).(sqlstmt.ITableSource))
|
||||
}
|
||||
|
||||
ts.TableSources = tableSources
|
||||
return ts
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitNon_ansi_join(ctx *pgparser.Non_ansi_joinContext) interface{} {
|
||||
return v.VisitChildren(ctx)
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitTable_ref(ctx *pgparser.Table_refContext) interface{} {
|
||||
tableSourceBase := new(sqlstmt.TableSourceBase)
|
||||
tableSourceBase.Node = sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
|
||||
atomTable := new(sqlstmt.AtomTableItem)
|
||||
|
||||
if c := ctx.Relation_expr(); c != nil {
|
||||
tableName := new(sqlstmt.TableName)
|
||||
if qn := c.Qualified_name(); qn != nil {
|
||||
if qc := qn.Colid(); qc != nil {
|
||||
if c := qn.Indirection(); c != nil {
|
||||
tableName.Owner = qc.GetText()
|
||||
tableName.Identifier = sqlstmt.NewIdentifierValue(c.GetText())
|
||||
} else {
|
||||
tableName.Identifier = sqlstmt.NewIdentifierValue(qc.Identifier().GetText())
|
||||
}
|
||||
}
|
||||
}
|
||||
atomTable.TableName = tableName
|
||||
}
|
||||
if c := ctx.Opt_alias_clause(); c != nil {
|
||||
if aliasC := c.Table_alias_clause(); aliasC != nil {
|
||||
atomTable.Alias = aliasC.Table_alias().GetText()
|
||||
}
|
||||
}
|
||||
|
||||
tableSourceBase.TableSourceItem = atomTable
|
||||
return tableSourceBase
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitAlias_clause(ctx *pgparser.Alias_clauseContext) interface{} {
|
||||
return v.VisitChildren(ctx)
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitOpt_alias_clause(ctx *pgparser.Opt_alias_clauseContext) interface{} {
|
||||
return v.VisitChildren(ctx)
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitTable_alias_clause(ctx *pgparser.Table_alias_clauseContext) interface{} {
|
||||
return v.VisitChildren(ctx)
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitFunc_alias_clause(ctx *pgparser.Func_alias_clauseContext) interface{} {
|
||||
return v.VisitChildren(ctx)
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitRelation_expr(ctx *pgparser.Relation_exprContext) interface{} {
|
||||
return v.VisitChildren(ctx)
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitRelation_expr_list(ctx *pgparser.Relation_expr_listContext) interface{} {
|
||||
return v.VisitChildren(ctx)
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitUpdatestmt(ctx *pgparser.UpdatestmtContext) interface{} {
|
||||
updateStmt := new(sqlstmt.UpdateStmt)
|
||||
updateStmt.Node = sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
|
||||
updateStmt.TableSources = v.GetTableSourcesByrelation_expr_opt_alias(ctx.Relation_expr_opt_alias())
|
||||
updateStmt.UpdatedElements = ctx.Set_clause_list().Accept(v).([]*sqlstmt.UpdatedElement)
|
||||
if ec := ctx.Where_or_current_clause().A_expr(); ec != nil {
|
||||
updateStmt.Where = ec.Accept(v).(sqlstmt.IExpr)
|
||||
}
|
||||
|
||||
return updateStmt
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitSet_clause_list(ctx *pgparser.Set_clause_listContext) interface{} {
|
||||
ues := make([]*sqlstmt.UpdatedElement, 0)
|
||||
aucs := ctx.AllSet_clause()
|
||||
for _, auc := range aucs {
|
||||
ues = append(ues, auc.Accept(v).(*sqlstmt.UpdatedElement))
|
||||
}
|
||||
return ues
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitSet_clause(ctx *pgparser.Set_clauseContext) interface{} {
|
||||
updateEle := new(sqlstmt.UpdatedElement)
|
||||
updateEle.Node = sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
|
||||
updateEle.ColumnName = ctx.Set_target().Accept(v).(*sqlstmt.ColumnName)
|
||||
if ac := ctx.A_expr(); ac != nil {
|
||||
updateEle.Value = ac.Accept(v).(sqlstmt.IExpr)
|
||||
}
|
||||
return updateEle
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitSet_target(ctx *pgparser.Set_targetContext) interface{} {
|
||||
columnName := new(sqlstmt.ColumnName)
|
||||
columnName.Node = sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
|
||||
if ic := ctx.Opt_indirection(); ic != nil {
|
||||
if ic.GetText() == "" {
|
||||
columnName.Identifier = sqlstmt.NewIdentifierValue(ctx.Colid().GetText())
|
||||
} else {
|
||||
columnName.Owner = ctx.Colid().GetText()
|
||||
columnName.Identifier = sqlstmt.NewIdentifierValue(ic.GetText())
|
||||
}
|
||||
} else {
|
||||
columnName.Identifier = sqlstmt.NewIdentifierValue(ctx.Colid().GetText())
|
||||
}
|
||||
return columnName
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitSet_target_list(ctx *pgparser.Set_target_listContext) interface{} {
|
||||
return v.VisitChildren(ctx)
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitA_expr(ctx *pgparser.A_exprContext) interface{} {
|
||||
expr := new(sqlstmt.Expr)
|
||||
expr.Node = sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
return expr
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitA_expr_qual(ctx *pgparser.A_expr_qualContext) interface{} {
|
||||
expr := new(sqlstmt.Expr)
|
||||
expr.Node = sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
return expr
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitDeletestmt(ctx *pgparser.DeletestmtContext) interface{} {
|
||||
deletestmt := new(sqlstmt.DeleteStmt)
|
||||
deletestmt.Node = sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
|
||||
deletestmt.TableSources = v.GetTableSourcesByrelation_expr_opt_alias(ctx.Relation_expr_opt_alias())
|
||||
|
||||
if ec := ctx.Where_or_current_clause().A_expr(); ec != nil {
|
||||
deletestmt.Where = ec.Accept(v).(sqlstmt.IExpr)
|
||||
}
|
||||
return deletestmt
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitInsertstmt(ctx *pgparser.InsertstmtContext) interface{} {
|
||||
insertstmt := new(sqlstmt.InsertStmt)
|
||||
insertstmt.Node = sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
insertstmt.TableName = ctx.Insert_target().Accept(v).(*sqlstmt.TableName)
|
||||
return insertstmt
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitInsert_target(ctx *pgparser.Insert_targetContext) interface{} {
|
||||
tableName := new(sqlstmt.TableName)
|
||||
tableName.Node = sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
table := ctx.GetText()
|
||||
if strings.Contains(table, ".") {
|
||||
tableAndOwner := strings.Split(table, ".")
|
||||
tableName.Identifier = sqlstmt.NewIdentifierValue(tableAndOwner[1])
|
||||
tableName.Owner = tableAndOwner[0]
|
||||
} else {
|
||||
tableName.Identifier = sqlstmt.NewIdentifierValue(table)
|
||||
}
|
||||
return tableName
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) VisitInsert_rest(ctx *pgparser.Insert_restContext) interface{} {
|
||||
return v.VisitChildren(ctx)
|
||||
}
|
||||
|
||||
func (v *PgsqlVisitor) GetTableSourcesByrelation_expr_opt_alias(ctx pgparser.IRelation_expr_opt_aliasContext) *sqlstmt.TableSources {
|
||||
tableSources := new(sqlstmt.TableSources)
|
||||
tableSources.Node = sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
|
||||
atomTable := new(sqlstmt.AtomTableItem)
|
||||
atomTable.Node = sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
|
||||
if c := ctx.Relation_expr(); c != nil {
|
||||
tableName := new(sqlstmt.TableName)
|
||||
if qn := c.Qualified_name(); qn != nil {
|
||||
if qc := qn.Colid(); qc != nil {
|
||||
if c := qn.Indirection(); c != nil {
|
||||
tableName.Owner = qc.GetText()
|
||||
tableName.Identifier = sqlstmt.NewIdentifierValue(c.GetText())
|
||||
} else {
|
||||
tableName.Identifier = sqlstmt.NewIdentifierValue(qc.Identifier().GetText())
|
||||
}
|
||||
}
|
||||
}
|
||||
atomTable.TableName = tableName
|
||||
}
|
||||
|
||||
if c := ctx.Colid(); c != nil {
|
||||
atomTable.Alias = c.GetText()
|
||||
}
|
||||
|
||||
tableSourceBase := new(sqlstmt.TableSourceBase)
|
||||
tableSourceBase.Node = sqlstmt.NewNode(ctx.GetParser(), ctx)
|
||||
tableSourceBase.TableSourceItem = atomTable
|
||||
|
||||
tableSources.TableSources = []sqlstmt.ITableSource{tableSourceBase}
|
||||
return tableSources
|
||||
}
|
||||
Reference in New Issue
Block a user