2024-10-16 17:24:50 +08:00
|
|
|
|
package sqlstmt
|
|
|
|
|
|
|
2026-05-08 20:45:13 +08:00
|
|
|
|
// SelectItemKind SELECT 元素类型
|
|
|
|
|
|
type SelectItemKind int
|
2024-10-16 17:24:50 +08:00
|
|
|
|
|
2026-05-08 20:45:13 +08:00
|
|
|
|
const (
|
|
|
|
|
|
SelectItemStar SelectItemKind = iota
|
|
|
|
|
|
SelectItemColumn
|
|
|
|
|
|
SelectItemExpr
|
|
|
|
|
|
SelectItemFunction
|
2024-10-16 17:24:50 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2026-05-08 20:45:13 +08:00
|
|
|
|
// SelectItem 表示 SELECT 列表中的一个元素
|
|
|
|
|
|
type SelectItem struct {
|
|
|
|
|
|
Kind SelectItemKind
|
|
|
|
|
|
Text string // 原始文本
|
|
|
|
|
|
Alias string
|
|
|
|
|
|
TableAlias string // 对于 t.* 或 t.col,这是表别名
|
|
|
|
|
|
ColumnName string // 对于列引用
|
2024-10-16 17:24:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-08 20:45:13 +08:00
|
|
|
|
// IsStar 判断是否为 *
|
|
|
|
|
|
func (s SelectItem) IsStar() bool {
|
|
|
|
|
|
return s.Kind == SelectItemStar
|
2024-10-16 17:24:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-08 20:45:13 +08:00
|
|
|
|
// JoinKind JOIN 类型
|
|
|
|
|
|
type JoinKind int
|
2024-10-16 17:24:50 +08:00
|
|
|
|
|
2026-05-08 20:45:13 +08:00
|
|
|
|
const (
|
|
|
|
|
|
JoinKindInner JoinKind = iota
|
|
|
|
|
|
JoinKindLeft
|
|
|
|
|
|
JoinKindRight
|
|
|
|
|
|
JoinKindFull
|
|
|
|
|
|
JoinKindCross
|
|
|
|
|
|
JoinKindNatural
|
2024-10-16 17:24:50 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2026-05-08 20:45:13 +08:00
|
|
|
|
// JoinClause JOIN 子句
|
|
|
|
|
|
type JoinClause struct {
|
|
|
|
|
|
Kind JoinKind
|
|
|
|
|
|
Table TableRef
|
|
|
|
|
|
On *Expr
|
|
|
|
|
|
Text string
|
|
|
|
|
|
}
|
2024-10-16 17:24:50 +08:00
|
|
|
|
|
2026-05-08 20:45:13 +08:00
|
|
|
|
// 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 }
|