mirror of
https://gitee.com/dromara/mayfly-go
synced 2026-05-20 09:55:25 +08:00
refactor: 移除antlr4减小包体积&ai助手优化
This commit is contained in:
@@ -1,187 +1,41 @@
|
||||
package sqlstmt
|
||||
|
||||
type (
|
||||
IExpr interface {
|
||||
INode
|
||||
|
||||
isExpr()
|
||||
}
|
||||
|
||||
Expr struct {
|
||||
*Node
|
||||
}
|
||||
|
||||
ExprLogical struct {
|
||||
Expr
|
||||
|
||||
Operator string
|
||||
Exprs []IExpr
|
||||
}
|
||||
|
||||
ExprPredicate struct {
|
||||
Expr
|
||||
|
||||
Predicate IPredicate
|
||||
}
|
||||
)
|
||||
|
||||
func (*Expr) isExpr() {}
|
||||
|
||||
type (
|
||||
IPredicate interface {
|
||||
INode
|
||||
|
||||
isPredicate()
|
||||
}
|
||||
|
||||
Predicate struct {
|
||||
*Node
|
||||
}
|
||||
|
||||
PredicateBinaryComparison struct {
|
||||
Predicate
|
||||
|
||||
Left IPredicate
|
||||
Right IPredicate
|
||||
ComparisonOperator string
|
||||
}
|
||||
|
||||
PredicateIn struct {
|
||||
Predicate
|
||||
|
||||
InPredicate IPredicate
|
||||
Exprs []IExpr
|
||||
SelectStmt ISelectStmt
|
||||
}
|
||||
|
||||
PredicateExprAtom struct {
|
||||
Predicate
|
||||
|
||||
ExprAtom IExprAtom
|
||||
}
|
||||
|
||||
PredicateLike struct {
|
||||
Predicate
|
||||
|
||||
InPredicate IPredicate
|
||||
Exprs []IExpr
|
||||
SelectStmt ISelectStmt
|
||||
}
|
||||
)
|
||||
|
||||
func (*Predicate) isPredicate() {}
|
||||
|
||||
type (
|
||||
IExprAtom interface {
|
||||
INode
|
||||
|
||||
isExprAtom()
|
||||
}
|
||||
|
||||
ExprAtom struct {
|
||||
*Node
|
||||
}
|
||||
|
||||
ExprAtomFunctionCall struct {
|
||||
*Node
|
||||
}
|
||||
|
||||
ExprAtomConstant struct {
|
||||
ExprAtom
|
||||
|
||||
Constant *Constant
|
||||
}
|
||||
|
||||
ExprAtomColumnName struct {
|
||||
ExprAtom
|
||||
|
||||
ColumnName *ColumnName
|
||||
}
|
||||
)
|
||||
|
||||
func (*ExprAtom) isExprAtom() {}
|
||||
|
||||
type (
|
||||
ITableSource interface {
|
||||
INode
|
||||
|
||||
isTableSource()
|
||||
}
|
||||
|
||||
TableSource struct {
|
||||
*Node
|
||||
}
|
||||
|
||||
TableSources struct {
|
||||
*Node
|
||||
|
||||
TableSources []ITableSource
|
||||
}
|
||||
|
||||
TableSourceBase struct {
|
||||
TableSource
|
||||
|
||||
TableSourceItem ITableSourceItem
|
||||
JoinParts []IJoinPart
|
||||
}
|
||||
|
||||
ITableSourceItem interface {
|
||||
INode
|
||||
}
|
||||
|
||||
TableSourceItem struct {
|
||||
*Node
|
||||
}
|
||||
|
||||
AtomTableItem struct {
|
||||
TableSourceItem
|
||||
|
||||
TableName *TableName // 表名
|
||||
Alias string // 别名
|
||||
}
|
||||
|
||||
// SubqueryTableItem 表示子查询表项,如 (SELECT * FROM table1) AS alias
|
||||
SubqueryTableItem struct {
|
||||
TableSourceItem
|
||||
|
||||
SubQuery ISelectStmt
|
||||
Alias string
|
||||
}
|
||||
)
|
||||
|
||||
func (*TableSource) isTableSource() {}
|
||||
|
||||
type (
|
||||
Constant struct {
|
||||
*Node
|
||||
|
||||
Value string
|
||||
}
|
||||
|
||||
FullId struct {
|
||||
*Node
|
||||
|
||||
Uids []string
|
||||
}
|
||||
)
|
||||
|
||||
type ColumnName struct {
|
||||
*Node
|
||||
|
||||
Owner string
|
||||
Identifier *IdentifierValue
|
||||
NestedObjectAttrs []string
|
||||
// TableRef 表示表引用
|
||||
type TableRef struct {
|
||||
Schema string // 数据库/模式名
|
||||
Name string // 表名
|
||||
Alias string // 别名
|
||||
}
|
||||
|
||||
type TableName struct {
|
||||
*Node
|
||||
|
||||
Owner string
|
||||
Identifier *IdentifierValue
|
||||
// FullName 返回完整表名(含 schema)
|
||||
func (t TableRef) FullName() string {
|
||||
if t.Schema != "" {
|
||||
return t.Schema + "." + t.Name
|
||||
}
|
||||
return t.Name
|
||||
}
|
||||
|
||||
type (
|
||||
FuncCall interface {
|
||||
INode
|
||||
}
|
||||
)
|
||||
// Expr 表示表达式(WHERE、ON、HAVING 等)
|
||||
type Expr struct {
|
||||
Text string // 原始表达式文本
|
||||
}
|
||||
|
||||
// Limit LIMIT/OFFSET
|
||||
type Limit struct {
|
||||
Count int
|
||||
Offset int
|
||||
Text string
|
||||
}
|
||||
|
||||
// OrderByItem ORDER BY 项
|
||||
type OrderByItem struct {
|
||||
Text string
|
||||
Desc bool
|
||||
}
|
||||
|
||||
// Assignment SET 赋值
|
||||
type Assignment struct {
|
||||
Column string
|
||||
Value *Expr
|
||||
Text string
|
||||
}
|
||||
|
||||
@@ -1,53 +1,9 @@
|
||||
package sqlstmt
|
||||
|
||||
type (
|
||||
IDdlStmt interface {
|
||||
isDdl()
|
||||
}
|
||||
|
||||
DdlStmt struct {
|
||||
*Node
|
||||
}
|
||||
|
||||
CreateDatabase struct {
|
||||
DdlStmt
|
||||
}
|
||||
|
||||
CreateTable struct {
|
||||
DdlStmt
|
||||
}
|
||||
|
||||
CreateIndex struct {
|
||||
DdlStmt
|
||||
}
|
||||
|
||||
AlterTable struct {
|
||||
DdlStmt
|
||||
}
|
||||
|
||||
AlterDatabase struct {
|
||||
DdlStmt
|
||||
}
|
||||
|
||||
DropDatabase struct {
|
||||
DdlStmt
|
||||
}
|
||||
|
||||
DropIndex struct {
|
||||
DdlStmt
|
||||
}
|
||||
|
||||
DropTable struct {
|
||||
DdlStmt
|
||||
}
|
||||
|
||||
DropView struct {
|
||||
DdlStmt
|
||||
}
|
||||
)
|
||||
|
||||
func (d *DdlStmt) isDdl() {}
|
||||
|
||||
func IsDDL(node INode) bool {
|
||||
return true
|
||||
// DdlStmt DDL 语句
|
||||
type DdlStmt struct {
|
||||
Base
|
||||
DdlKind string // CREATE_TABLE, DROP_TABLE, ALTER_TABLE 等
|
||||
}
|
||||
|
||||
func (*DdlStmt) StmtKind() Kind { return KindDdl }
|
||||
|
||||
@@ -1,16 +1,10 @@
|
||||
package sqlstmt
|
||||
|
||||
type (
|
||||
IDeleteStmt interface {
|
||||
isDelete()
|
||||
}
|
||||
// DeleteStmt DELETE 语句
|
||||
type DeleteStmt struct {
|
||||
Base
|
||||
Tables []TableRef // 删除的表
|
||||
Where *Expr
|
||||
}
|
||||
|
||||
DeleteStmt struct {
|
||||
*Node
|
||||
|
||||
TableSources *TableSources
|
||||
Where IExpr
|
||||
}
|
||||
)
|
||||
|
||||
func (*DeleteStmt) isDelete() {}
|
||||
func (*DeleteStmt) StmtKind() Kind { return KindDelete }
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
package sqlstmt
|
||||
|
||||
type (
|
||||
IInsertStmt interface {
|
||||
isInsert()
|
||||
}
|
||||
// InsertStmt INSERT 语句
|
||||
type InsertStmt struct {
|
||||
Base
|
||||
Table TableRef
|
||||
Columns []string
|
||||
Values string // VALUES 部分原始文本
|
||||
}
|
||||
|
||||
InsertStmt struct {
|
||||
*Node
|
||||
|
||||
TableName *TableName
|
||||
}
|
||||
)
|
||||
|
||||
func (*InsertStmt) isInsert() {}
|
||||
func (*InsertStmt) StmtKind() Kind { return KindInsert }
|
||||
|
||||
8
server/internal/db/dbm/sqlparser/sqlstmt/other.go
Normal file
8
server/internal/db/dbm/sqlparser/sqlstmt/other.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package sqlstmt
|
||||
|
||||
// OtherStmt 其他语句
|
||||
type OtherStmt struct {
|
||||
Base
|
||||
}
|
||||
|
||||
func (*OtherStmt) StmtKind() Kind { return KindOther }
|
||||
@@ -1,76 +0,0 @@
|
||||
package sqlstmt
|
||||
|
||||
import "strings"
|
||||
|
||||
type QuoteChar struct {
|
||||
StartDelimiter string
|
||||
EndDelimiter string
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap value with quote character.
|
||||
*
|
||||
* - value value to be wrapped
|
||||
* @return wrapped value
|
||||
*/
|
||||
func (qc *QuoteChar) Wrap(value string) string {
|
||||
return qc.StartDelimiter + value + qc.EndDelimiter
|
||||
}
|
||||
|
||||
/**
|
||||
* Unwrap value with quote character.
|
||||
*
|
||||
* - value value to be unwrapped
|
||||
* @return unwrapped value
|
||||
*/
|
||||
func (qc *QuoteChar) Unwrap(value string) string {
|
||||
if qc.IsWrapped(value) {
|
||||
return value[len(qc.StartDelimiter) : len(value)-len(qc.EndDelimiter)]
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
/**
|
||||
* Is wrapped by quote character.
|
||||
*
|
||||
* - value value to be judged
|
||||
* @return is wrapped or not
|
||||
*/
|
||||
func (qc *QuoteChar) IsWrapped(value string) bool {
|
||||
return strings.HasPrefix(value, qc.StartDelimiter) && strings.HasSuffix(value, qc.EndDelimiter)
|
||||
}
|
||||
|
||||
func NewQuoteChar(startDelimiter, endDelimiter string) *QuoteChar {
|
||||
return &QuoteChar{
|
||||
StartDelimiter: startDelimiter,
|
||||
EndDelimiter: endDelimiter,
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
BACK_QUOTE = NewQuoteChar("`", "`")
|
||||
SINGLE_QUOTE = NewQuoteChar("'", "'")
|
||||
QUOTE = NewQuoteChar("\"", "\"")
|
||||
BRACKETS = NewQuoteChar("[", "]")
|
||||
PARENTHESES = NewQuoteChar("(", ")")
|
||||
NONE = NewQuoteChar("", "")
|
||||
|
||||
BY_FIRST_CHAR = map[string]*QuoteChar{
|
||||
BACK_QUOTE.StartDelimiter: BACK_QUOTE,
|
||||
SINGLE_QUOTE.StartDelimiter: SINGLE_QUOTE,
|
||||
QUOTE.StartDelimiter: QUOTE,
|
||||
BRACKETS.StartDelimiter: BRACKETS,
|
||||
PARENTHESES.StartDelimiter: PARENTHESES,
|
||||
}
|
||||
)
|
||||
|
||||
func GetQuoteChar(value string) *QuoteChar {
|
||||
if value == "" {
|
||||
return NONE
|
||||
}
|
||||
if qc := BY_FIRST_CHAR[value[0:1]]; qc != nil {
|
||||
return qc
|
||||
} else {
|
||||
return NONE
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package sqlstmt
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestQuoteCharUnwrap(t *testing.T) {
|
||||
value := "`hehehe`"
|
||||
qc := GetQuoteChar(value)
|
||||
if qc != BACK_QUOTE {
|
||||
t.Fatal("quote char should be BACK_QUOTE")
|
||||
}
|
||||
oriValue := qc.Unwrap(value)
|
||||
t.Log(oriValue)
|
||||
}
|
||||
@@ -1,135 +1,69 @@
|
||||
package sqlstmt
|
||||
|
||||
type (
|
||||
ISelectStmt interface {
|
||||
INode
|
||||
// SelectItemKind SELECT 元素类型
|
||||
type SelectItemKind int
|
||||
|
||||
isSelect()
|
||||
}
|
||||
|
||||
SelectStmt struct {
|
||||
*Node
|
||||
}
|
||||
|
||||
QuerySpecification struct {
|
||||
*Node
|
||||
|
||||
SelectElements *SelectElements
|
||||
From *TableSources
|
||||
Where IExpr
|
||||
Limit *Limit
|
||||
}
|
||||
|
||||
SimpleSelectStmt struct {
|
||||
SelectStmt
|
||||
|
||||
QuerySpecification *QuerySpecification
|
||||
}
|
||||
|
||||
UnionSelectStmt struct {
|
||||
SelectStmt
|
||||
|
||||
UnionType string
|
||||
QuerySpecification *QuerySpecification
|
||||
QueryExpr *QueryExpr
|
||||
UnionStmts []*UnionStmt
|
||||
Limit *Limit
|
||||
}
|
||||
|
||||
// 圆括号查询
|
||||
ParenthesisSelect struct {
|
||||
SelectStmt
|
||||
|
||||
QueryExpr *QueryExpr
|
||||
}
|
||||
|
||||
QueryExpr struct {
|
||||
*Node
|
||||
|
||||
QuerySpecification *QuerySpecification
|
||||
QueryExpr *QueryExpr
|
||||
}
|
||||
const (
|
||||
SelectItemStar SelectItemKind = iota
|
||||
SelectItemColumn
|
||||
SelectItemExpr
|
||||
SelectItemFunction
|
||||
)
|
||||
|
||||
func (*SelectStmt) isSelect() {}
|
||||
|
||||
// var _ (ISelectStmt) = (*SimpleSelectStmt)(nil)
|
||||
// var _ (ISelectStmt) = (*SelectStmt)(nil)
|
||||
// var _ (ISelectStmt) = (*ParenthesisSelect)(nil)
|
||||
|
||||
type (
|
||||
SelectElements struct {
|
||||
*Node
|
||||
|
||||
Star string // 查询所有
|
||||
Elements []ISelectElement
|
||||
}
|
||||
|
||||
ISelectElement interface {
|
||||
INode
|
||||
}
|
||||
|
||||
SelectStarElement struct {
|
||||
*Node
|
||||
|
||||
FullId string
|
||||
}
|
||||
|
||||
SelectColumnElement struct {
|
||||
*Node
|
||||
|
||||
ColumnName *ColumnName
|
||||
Alias string
|
||||
}
|
||||
|
||||
SelectFunctionElement struct {
|
||||
*Node
|
||||
|
||||
Alias string
|
||||
}
|
||||
)
|
||||
|
||||
type From struct {
|
||||
TableSource *ITableSource
|
||||
// SelectItem 表示 SELECT 列表中的一个元素
|
||||
type SelectItem struct {
|
||||
Kind SelectItemKind
|
||||
Text string // 原始文本
|
||||
Alias string
|
||||
TableAlias string // 对于 t.* 或 t.col,这是表别名
|
||||
ColumnName string // 对于列引用
|
||||
}
|
||||
|
||||
type Limit struct {
|
||||
*Node
|
||||
|
||||
RowCount int
|
||||
Offset int
|
||||
// IsStar 判断是否为 *
|
||||
func (s SelectItem) IsStar() bool {
|
||||
return s.Kind == SelectItemStar
|
||||
}
|
||||
|
||||
type (
|
||||
IJoinPart interface {
|
||||
INode
|
||||
}
|
||||
// JoinKind JOIN 类型
|
||||
type JoinKind int
|
||||
|
||||
JoinPart struct {
|
||||
*Node
|
||||
|
||||
TableSourceItem ITableSourceItem
|
||||
}
|
||||
|
||||
InnerJoin struct {
|
||||
JoinPart
|
||||
}
|
||||
|
||||
OuterJoin struct {
|
||||
JoinPart
|
||||
}
|
||||
|
||||
NaturalJoin struct {
|
||||
JoinPart
|
||||
}
|
||||
const (
|
||||
JoinKindInner JoinKind = iota
|
||||
JoinKindLeft
|
||||
JoinKindRight
|
||||
JoinKindFull
|
||||
JoinKindCross
|
||||
JoinKindNatural
|
||||
)
|
||||
|
||||
type (
|
||||
UnionStmt struct {
|
||||
*Node
|
||||
// JoinClause JOIN 子句
|
||||
type JoinClause struct {
|
||||
Kind JoinKind
|
||||
Table TableRef
|
||||
On *Expr
|
||||
Text string
|
||||
}
|
||||
|
||||
UnionType string
|
||||
QuerySpecification *QuerySpecification
|
||||
QueryExpr *QueryExpr
|
||||
}
|
||||
)
|
||||
// UnionClause UNION 子句
|
||||
type UnionClause struct {
|
||||
Select *SelectStmt
|
||||
All bool
|
||||
Text string
|
||||
}
|
||||
|
||||
// SelectStmt SELECT 语句(含 UNION、子查询等)
|
||||
type SelectStmt struct {
|
||||
Base
|
||||
Distinct bool
|
||||
Items []SelectItem
|
||||
From []TableRef
|
||||
Joins []JoinClause
|
||||
Where *Expr
|
||||
GroupBy []string
|
||||
Having *Expr
|
||||
OrderBy []OrderByItem
|
||||
Limit *Limit
|
||||
Unions []UnionClause
|
||||
}
|
||||
|
||||
func (*SelectStmt) StmtKind() Kind { return KindSelect }
|
||||
|
||||
@@ -1,79 +1,32 @@
|
||||
package sqlstmt
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
// Kind 表示 SQL 语句种类
|
||||
type Kind string
|
||||
|
||||
"github.com/antlr4-go/antlr/v4"
|
||||
const (
|
||||
KindSelect Kind = "SELECT"
|
||||
KindInsert Kind = "INSERT"
|
||||
KindUpdate Kind = "UPDATE"
|
||||
KindDelete Kind = "DELETE"
|
||||
KindDdl Kind = "DDL"
|
||||
KindWith Kind = "WITH"
|
||||
KindOther Kind = "OTHER"
|
||||
)
|
||||
|
||||
type INode interface {
|
||||
// Stmt 是所有 SQL 语句的接口
|
||||
type Stmt interface {
|
||||
GetText() string
|
||||
|
||||
// GetStartIndex() int
|
||||
// GetStopIndex() int
|
||||
StmtKind() Kind
|
||||
}
|
||||
|
||||
type Node struct {
|
||||
// startIndex int
|
||||
// stopIndex int
|
||||
|
||||
parser antlr.Parser
|
||||
ruleContext antlr.RuleContext
|
||||
// Base 提供基础实现
|
||||
type Base struct {
|
||||
Text string // 原始 SQL 文本
|
||||
}
|
||||
|
||||
func (n *Node) GetText() string {
|
||||
if n == nil || n.ruleContext == nil || n.parser == nil {
|
||||
func (b *Base) GetText() string {
|
||||
if b == nil {
|
||||
return ""
|
||||
}
|
||||
return n.parser.GetTokenStream().GetTextFromRuleContext(n.ruleContext)
|
||||
}
|
||||
|
||||
// func (n *Node) GetStartIndex() int {
|
||||
// return n.startIndex
|
||||
// }
|
||||
|
||||
// func (n *Node) GetStopIndex() int {
|
||||
// return n.stopIndex
|
||||
// }
|
||||
|
||||
func NewNode(parser antlr.Parser, ruleContext antlr.RuleContext) *Node {
|
||||
return &Node{
|
||||
parser: parser,
|
||||
ruleContext: ruleContext,
|
||||
}
|
||||
}
|
||||
|
||||
// func NewNode(startIndex, stopIndex int) *Node {
|
||||
// return &Node{
|
||||
// startIndex: startIndex,
|
||||
// stopIndex: stopIndex,
|
||||
// }
|
||||
// }
|
||||
|
||||
// func GetText(sqlstmts string, node INode) string {
|
||||
// return sqlstmts[node.GetStartIndex() : node.GetStopIndex()+1]
|
||||
// }
|
||||
|
||||
type Stmt interface {
|
||||
INode
|
||||
}
|
||||
|
||||
type SqlStmt struct {
|
||||
*Node
|
||||
}
|
||||
|
||||
type DmlStmt struct {
|
||||
SqlStmt
|
||||
}
|
||||
|
||||
type OtherReadStmt struct {
|
||||
*Node
|
||||
}
|
||||
|
||||
type WithStmt struct {
|
||||
*Node
|
||||
}
|
||||
|
||||
func IsSelectStmt(stmt Stmt) bool {
|
||||
return reflect.TypeOf(stmt).AssignableTo(reflect.TypeOf(&SelectStmt{}))
|
||||
return b.Text
|
||||
}
|
||||
|
||||
@@ -1,26 +1,11 @@
|
||||
package sqlstmt
|
||||
|
||||
type (
|
||||
IUpdateStmt interface {
|
||||
INode
|
||||
|
||||
isUpdate()
|
||||
}
|
||||
|
||||
UpdateStmt struct {
|
||||
*Node
|
||||
|
||||
TableSources *TableSources
|
||||
UpdatedElements []*UpdatedElement
|
||||
Where IExpr
|
||||
}
|
||||
)
|
||||
|
||||
func (*UpdateStmt) isUpdate() {}
|
||||
|
||||
type UpdatedElement struct {
|
||||
*Node
|
||||
|
||||
ColumnName *ColumnName
|
||||
Value IExpr
|
||||
// UpdateStmt UPDATE 语句
|
||||
type UpdateStmt struct {
|
||||
Base
|
||||
Tables []TableRef // 更新的表(支持多表)
|
||||
Set []Assignment // SET 子句
|
||||
Where *Expr
|
||||
}
|
||||
|
||||
func (*UpdateStmt) StmtKind() Kind { return KindUpdate }
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
package sqlstmt
|
||||
|
||||
import "strings"
|
||||
|
||||
type IdentifierValue struct {
|
||||
Value string
|
||||
QuoteChar *QuoteChar
|
||||
}
|
||||
|
||||
func NewIdentifierValue(value string) *IdentifierValue {
|
||||
value = strings.TrimPrefix(value, ".")
|
||||
qc := GetQuoteChar(value)
|
||||
if qc == NONE {
|
||||
return &IdentifierValue{
|
||||
Value: value,
|
||||
QuoteChar: qc,
|
||||
}
|
||||
}
|
||||
return &IdentifierValue{
|
||||
Value: qc.Unwrap(value),
|
||||
QuoteChar: qc,
|
||||
}
|
||||
}
|
||||
8
server/internal/db/dbm/sqlparser/sqlstmt/with.go
Normal file
8
server/internal/db/dbm/sqlparser/sqlstmt/with.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package sqlstmt
|
||||
|
||||
// WithStmt WITH 语句
|
||||
type WithStmt struct {
|
||||
Base
|
||||
}
|
||||
|
||||
func (*WithStmt) StmtKind() Kind { return KindWith }
|
||||
Reference in New Issue
Block a user