| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532 |
- package parser
- import "github.com/danfragoso/pizzasql-next/pkg/lexer"
- // Node is the base interface for all AST nodes.
- type Node interface {
- node()
- }
- // Statement represents a SQL statement.
- type Statement interface {
- Node
- stmtNode()
- }
- // Expr represents an expression.
- type Expr interface {
- Node
- exprNode()
- }
- // SetOpType represents a set operation type.
- type SetOpType int
- const (
- SetOpUnion SetOpType = iota
- SetOpUnionAll
- SetOpIntersect
- SetOpExcept
- )
- // CompoundSelect chains two SELECT statements with a set operation.
- type CompoundSelect struct {
- Left *SelectStmt
- Op SetOpType
- Right *SelectStmt // may itself have Compound set for chained ops
- OrderBy []OrderByItem
- Limit Expr
- Offset Expr
- }
- func (c *CompoundSelect) node() {}
- func (c *CompoundSelect) stmtNode() {}
- // SelectStmt represents a SELECT statement.
- type SelectStmt struct {
- Distinct bool
- Columns []SelectColumn
- From []TableRef
- Where Expr
- GroupBy []Expr
- Having Expr
- OrderBy []OrderByItem
- Limit Expr
- Offset Expr
- // Compound chains a set operation onto this SELECT (UNION/INTERSECT/EXCEPT).
- Compound *CompoundSelect
- }
- func (s *SelectStmt) node() {}
- func (s *SelectStmt) stmtNode() {}
- // SelectColumn represents a column in SELECT.
- type SelectColumn struct {
- Expr Expr
- Alias string
- Star bool // true if this is *
- }
- // TableRef represents a table reference.
- type TableRef struct {
- Schema string
- Name string
- Alias string
- Subquery *SelectStmt // for derived tables (SELECT ... FROM (SELECT ...) AS alias)
- Join *JoinClause // for joined tables
- }
- // JoinClause represents a JOIN clause.
- type JoinClause struct {
- Type JoinType
- Table *TableRef
- Condition Expr // ON condition
- Using []string // USING columns
- }
- // JoinType represents the type of JOIN.
- type JoinType int
- const (
- JoinInner JoinType = iota
- JoinLeft
- JoinRight
- JoinFull
- JoinCross
- )
- // OrderByItem represents an ORDER BY item.
- type OrderByItem struct {
- Expr Expr
- Desc bool
- }
- // ConflictAction represents the action to take on conflict.
- type ConflictAction int
- const (
- ConflictAbort ConflictAction = iota // Default
- ConflictReplace // INSERT OR REPLACE
- ConflictIgnore // INSERT OR IGNORE
- ConflictFail // INSERT OR FAIL
- ConflictRollback // INSERT OR ROLLBACK
- )
- // InsertStmt represents an INSERT statement.
- type InsertStmt struct {
- Table *TableRef
- Columns []string
- Values [][]Expr
- Select *SelectStmt // INSERT ... SELECT
- OnConflict ConflictAction // OR REPLACE/IGNORE/etc.
- ConflictTarget []string
- ConflictUpdate []Assignment
- ConflictDoNothing bool
- }
- func (s *InsertStmt) node() {}
- func (s *InsertStmt) stmtNode() {}
- // UpdateStmt represents an UPDATE statement.
- type UpdateStmt struct {
- Table *TableRef
- Set []Assignment
- Where Expr
- }
- func (s *UpdateStmt) node() {}
- func (s *UpdateStmt) stmtNode() {}
- // Assignment represents a SET assignment.
- type Assignment struct {
- Column string
- Value Expr
- }
- // DeleteStmt represents a DELETE statement.
- type DeleteStmt struct {
- Table *TableRef
- Where Expr
- }
- func (s *DeleteStmt) node() {}
- func (s *DeleteStmt) stmtNode() {}
- // CreateTableStmt represents a CREATE TABLE statement.
- type CreateTableStmt struct {
- IfNotExists bool
- Table *TableRef
- Columns []ColumnDef
- Constraints []TableConstraint
- }
- func (s *CreateTableStmt) node() {}
- func (s *CreateTableStmt) stmtNode() {}
- // ColumnDef represents a column definition.
- type ColumnDef struct {
- Name string
- Type DataType
- Constraints []ColumnConstraint
- }
- // DataType represents a SQL data type.
- type DataType struct {
- Name string
- Precision int // for VARCHAR(n), NUMERIC(p,s)
- Scale int // for NUMERIC(p,s)
- }
- // ColumnConstraint represents a column-level constraint.
- type ColumnConstraint struct {
- Type ConstraintType
- Name string // optional constraint name
- Default Expr // for DEFAULT
- RefTable string // for REFERENCES
- RefColumn string // for REFERENCES
- }
- // ConstraintType represents the type of constraint.
- type ConstraintType int
- const (
- ConstraintPrimaryKey ConstraintType = iota
- ConstraintNotNull
- ConstraintUnique
- ConstraintDefault
- ConstraintCheck
- ConstraintForeignKey
- ConstraintAutoIncrement
- )
- // TableConstraint represents a table-level constraint.
- type TableConstraint struct {
- Type ConstraintType
- Name string // optional constraint name
- Columns []string // columns involved
- RefTable string // for FOREIGN KEY
- RefColumns []string // for FOREIGN KEY
- Check Expr // for CHECK
- }
- // DropTableStmt represents a DROP TABLE statement.
- type DropTableStmt struct {
- IfExists bool
- Tables []*TableRef
- }
- func (s *DropTableStmt) node() {}
- func (s *DropTableStmt) stmtNode() {}
- // CreateIndexStmt represents a CREATE INDEX statement.
- type CreateIndexStmt struct {
- IfNotExists bool
- Unique bool
- Name string
- Table string
- Columns []IndexColumn
- }
- func (s *CreateIndexStmt) node() {}
- func (s *CreateIndexStmt) stmtNode() {}
- // IndexColumn represents a column in an index.
- type IndexColumn struct {
- Name string
- Desc bool // true for DESC ordering
- }
- // DropIndexStmt represents a DROP INDEX statement.
- type DropIndexStmt struct {
- IfExists bool
- Name string
- }
- func (s *DropIndexStmt) node() {}
- func (s *DropIndexStmt) stmtNode() {}
- // CreateViewStmt represents a CREATE VIEW statement.
- type CreateViewStmt struct {
- IfNotExists bool
- View *TableRef
- Select *SelectStmt
- }
- func (s *CreateViewStmt) node() {}
- func (s *CreateViewStmt) stmtNode() {}
- // DropViewStmt represents a DROP VIEW statement.
- type DropViewStmt struct {
- IfExists bool
- Views []*TableRef
- }
- func (s *DropViewStmt) node() {}
- func (s *DropViewStmt) stmtNode() {}
- // AlterTableStmt represents an ALTER TABLE statement.
- type AlterTableStmt struct {
- Table string
- Action AlterAction
- }
- func (s *AlterTableStmt) node() {}
- func (s *AlterTableStmt) stmtNode() {}
- // AlterAction represents an ALTER TABLE action.
- type AlterAction interface {
- Node
- alterAction()
- }
- // AddColumnAction represents ADD COLUMN action.
- type AddColumnAction struct {
- Column *ColumnDef
- IfNotExists bool
- }
- func (a *AddColumnAction) node() {}
- func (a *AddColumnAction) alterAction() {}
- // DropColumnAction represents DROP COLUMN action.
- type DropColumnAction struct {
- Column string
- }
- func (a *DropColumnAction) node() {}
- func (a *DropColumnAction) alterAction() {}
- // RenameTableAction represents RENAME TO action.
- type RenameTableAction struct {
- NewName string
- }
- func (a *RenameTableAction) node() {}
- func (a *RenameTableAction) alterAction() {}
- // RenameColumnAction represents RENAME COLUMN action.
- type RenameColumnAction struct {
- OldName string
- NewName string
- }
- func (a *RenameColumnAction) node() {}
- func (a *RenameColumnAction) alterAction() {}
- // AttachStmt represents an ATTACH DATABASE statement.
- type AttachStmt struct {
- FilePath string // Database file path or identifier
- Alias string // Database alias name
- }
- func (s *AttachStmt) node() {}
- func (s *AttachStmt) stmtNode() {}
- // DetachStmt represents a DETACH DATABASE statement.
- type DetachStmt struct {
- Alias string // Database alias to detach
- }
- func (s *DetachStmt) node() {}
- func (s *DetachStmt) stmtNode() {}
- // PragmaStmt represents a PRAGMA statement.
- type PragmaStmt struct {
- Name string // pragma name (e.g., "table_info")
- Arg string // optional argument (e.g., table name)
- Value Expr // optional value for SET pragmas
- }
- func (s *PragmaStmt) node() {}
- func (s *PragmaStmt) stmtNode() {}
- // ExplainStmt represents an EXPLAIN statement.
- type ExplainStmt struct {
- QueryPlan bool // true for EXPLAIN QUERY PLAN
- Statement Statement // the statement being explained
- }
- func (s *ExplainStmt) node() {}
- func (s *ExplainStmt) stmtNode() {}
- // Transaction statements
- // BeginStmt represents a BEGIN TRANSACTION statement.
- type BeginStmt struct {
- // Transaction mode (DEFERRED, IMMEDIATE, EXCLUSIVE) - for future use
- Mode string
- }
- func (s *BeginStmt) node() {}
- func (s *BeginStmt) stmtNode() {}
- // CommitStmt represents a COMMIT statement.
- type CommitStmt struct{}
- func (s *CommitStmt) node() {}
- func (s *CommitStmt) stmtNode() {}
- // RollbackStmt represents a ROLLBACK statement.
- type RollbackStmt struct {
- Savepoint string // for ROLLBACK TO SAVEPOINT
- }
- func (s *RollbackStmt) node() {}
- func (s *RollbackStmt) stmtNode() {}
- // SavepointStmt represents a SAVEPOINT statement.
- type SavepointStmt struct {
- Name string
- }
- func (s *SavepointStmt) node() {}
- func (s *SavepointStmt) stmtNode() {}
- // ReleaseStmt represents a RELEASE SAVEPOINT statement.
- type ReleaseStmt struct {
- Name string
- }
- func (s *ReleaseStmt) node() {}
- func (s *ReleaseStmt) stmtNode() {}
- // Expression types
- // BinaryExpr represents a binary expression.
- type BinaryExpr struct {
- Left Expr
- Op lexer.TokenType
- Right Expr
- }
- func (e *BinaryExpr) node() {}
- func (e *BinaryExpr) exprNode() {}
- // UnaryExpr represents a unary expression.
- type UnaryExpr struct {
- Op lexer.TokenType
- Operand Expr
- }
- func (e *UnaryExpr) node() {}
- func (e *UnaryExpr) exprNode() {}
- // LiteralExpr represents a literal value.
- type LiteralExpr struct {
- Type lexer.TokenType // TokenNumber, TokenString, TokenNULL, TokenTRUE, TokenFALSE
- Value string
- }
- func (e *LiteralExpr) node() {}
- func (e *LiteralExpr) exprNode() {}
- // ColumnRef represents a column reference.
- type ColumnRef struct {
- Table string
- Column string
- }
- func (e *ColumnRef) node() {}
- func (e *ColumnRef) exprNode() {}
- // FunctionCall represents a function call.
- type FunctionCall struct {
- Name string
- Args []Expr
- Distinct bool // for COUNT(DISTINCT x)
- Star bool // for COUNT(*)
- }
- func (e *FunctionCall) node() {}
- func (e *FunctionCall) exprNode() {}
- // SubqueryExpr represents a subquery expression.
- type SubqueryExpr struct {
- Query *SelectStmt
- }
- func (e *SubqueryExpr) node() {}
- func (e *SubqueryExpr) exprNode() {}
- // CaseExpr represents a CASE expression.
- type CaseExpr struct {
- Operand Expr // for CASE operand WHEN...
- Whens []WhenClause
- Else Expr
- }
- func (e *CaseExpr) node() {}
- func (e *CaseExpr) exprNode() {}
- // WhenClause represents a WHEN clause in CASE.
- type WhenClause struct {
- Condition Expr
- Result Expr
- }
- // InExpr represents an IN expression.
- type InExpr struct {
- Left Expr
- Not bool
- Values []Expr // IN (1, 2, 3)
- Subquery *SelectStmt // IN (SELECT ...)
- }
- func (e *InExpr) node() {}
- func (e *InExpr) exprNode() {}
- // BetweenExpr represents a BETWEEN expression.
- type BetweenExpr struct {
- Left Expr
- Not bool
- Low Expr
- High Expr
- }
- func (e *BetweenExpr) node() {}
- func (e *BetweenExpr) exprNode() {}
- // LikeExpr represents a LIKE expression.
- type LikeExpr struct {
- Left Expr
- Not bool
- Pattern Expr
- Escape Expr
- }
- func (e *LikeExpr) node() {}
- func (e *LikeExpr) exprNode() {}
- // IsNullExpr represents an IS NULL expression.
- type IsNullExpr struct {
- Left Expr
- Not bool
- }
- func (e *IsNullExpr) node() {}
- func (e *IsNullExpr) exprNode() {}
- // CastExpr represents a CAST expression.
- type CastExpr struct {
- Expr Expr
- Type DataType
- }
- func (e *CastExpr) node() {}
- func (e *CastExpr) exprNode() {}
- // ExistsExpr represents an EXISTS expression.
- type ExistsExpr struct {
- Subquery *SelectStmt
- }
- func (e *ExistsExpr) node() {}
- func (e *ExistsExpr) exprNode() {}
- // ParenExpr represents a parenthesized expression.
- type ParenExpr struct {
- Expr Expr
- }
- func (e *ParenExpr) node() {}
- func (e *ParenExpr) exprNode() {}
|