2
0

ast.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. package parser
  2. import "github.com/danfragoso/pizzasql-next/pkg/lexer"
  3. // Node is the base interface for all AST nodes.
  4. type Node interface {
  5. node()
  6. }
  7. // Statement represents a SQL statement.
  8. type Statement interface {
  9. Node
  10. stmtNode()
  11. }
  12. // Expr represents an expression.
  13. type Expr interface {
  14. Node
  15. exprNode()
  16. }
  17. // SetOpType represents a set operation type.
  18. type SetOpType int
  19. const (
  20. SetOpUnion SetOpType = iota
  21. SetOpUnionAll
  22. SetOpIntersect
  23. SetOpExcept
  24. )
  25. // CompoundSelect chains two SELECT statements with a set operation.
  26. type CompoundSelect struct {
  27. Left *SelectStmt
  28. Op SetOpType
  29. Right *SelectStmt // may itself have Compound set for chained ops
  30. OrderBy []OrderByItem
  31. Limit Expr
  32. Offset Expr
  33. }
  34. func (c *CompoundSelect) node() {}
  35. func (c *CompoundSelect) stmtNode() {}
  36. // SelectStmt represents a SELECT statement.
  37. type SelectStmt struct {
  38. Distinct bool
  39. Columns []SelectColumn
  40. From []TableRef
  41. Where Expr
  42. GroupBy []Expr
  43. Having Expr
  44. OrderBy []OrderByItem
  45. Limit Expr
  46. Offset Expr
  47. // Compound chains a set operation onto this SELECT (UNION/INTERSECT/EXCEPT).
  48. Compound *CompoundSelect
  49. }
  50. func (s *SelectStmt) node() {}
  51. func (s *SelectStmt) stmtNode() {}
  52. // SelectColumn represents a column in SELECT.
  53. type SelectColumn struct {
  54. Expr Expr
  55. Alias string
  56. Star bool // true if this is *
  57. TableStar string // table name/alias if this is a qualified wildcard (table.*)
  58. }
  59. // TableRef represents a table reference.
  60. type TableRef struct {
  61. Schema string
  62. Name string
  63. Alias string
  64. Subquery *SelectStmt // for derived tables (SELECT ... FROM (SELECT ...) AS alias)
  65. Join *JoinClause // for joined tables
  66. }
  67. // JoinClause represents a JOIN clause.
  68. type JoinClause struct {
  69. Type JoinType
  70. Table *TableRef
  71. Condition Expr // ON condition
  72. Using []string // USING columns
  73. }
  74. // JoinType represents the type of JOIN.
  75. type JoinType int
  76. const (
  77. JoinInner JoinType = iota
  78. JoinLeft
  79. JoinRight
  80. JoinFull
  81. JoinCross
  82. )
  83. // OrderByItem represents an ORDER BY item.
  84. type OrderByItem struct {
  85. Expr Expr
  86. Desc bool
  87. }
  88. // ConflictAction represents the action to take on conflict.
  89. type ConflictAction int
  90. const (
  91. ConflictAbort ConflictAction = iota // Default
  92. ConflictReplace // INSERT OR REPLACE
  93. ConflictIgnore // INSERT OR IGNORE
  94. ConflictFail // INSERT OR FAIL
  95. ConflictRollback // INSERT OR ROLLBACK
  96. )
  97. // InsertStmt represents an INSERT statement.
  98. type InsertStmt struct {
  99. Table *TableRef
  100. Columns []string
  101. Values [][]Expr
  102. Select *SelectStmt // INSERT ... SELECT
  103. OnConflict ConflictAction // OR REPLACE/IGNORE/etc.
  104. ConflictTarget []string
  105. ConflictUpdate []Assignment
  106. ConflictDoNothing bool
  107. }
  108. func (s *InsertStmt) node() {}
  109. func (s *InsertStmt) stmtNode() {}
  110. // UpdateStmt represents an UPDATE statement.
  111. type UpdateStmt struct {
  112. Table *TableRef
  113. Set []Assignment
  114. Where Expr
  115. }
  116. func (s *UpdateStmt) node() {}
  117. func (s *UpdateStmt) stmtNode() {}
  118. // Assignment represents a SET assignment.
  119. type Assignment struct {
  120. Column string
  121. Value Expr
  122. }
  123. // DeleteStmt represents a DELETE statement.
  124. type DeleteStmt struct {
  125. Table *TableRef
  126. Where Expr
  127. }
  128. func (s *DeleteStmt) node() {}
  129. func (s *DeleteStmt) stmtNode() {}
  130. // CreateTableStmt represents a CREATE TABLE statement.
  131. type CreateTableStmt struct {
  132. IfNotExists bool
  133. Table *TableRef
  134. Columns []ColumnDef
  135. Constraints []TableConstraint
  136. }
  137. func (s *CreateTableStmt) node() {}
  138. func (s *CreateTableStmt) stmtNode() {}
  139. // ColumnDef represents a column definition.
  140. type ColumnDef struct {
  141. Name string
  142. Type DataType
  143. Constraints []ColumnConstraint
  144. }
  145. // DataType represents a SQL data type.
  146. type DataType struct {
  147. Name string
  148. Precision int // for VARCHAR(n), NUMERIC(p,s)
  149. Scale int // for NUMERIC(p,s)
  150. }
  151. // ColumnConstraint represents a column-level constraint.
  152. type ColumnConstraint struct {
  153. Type ConstraintType
  154. Name string // optional constraint name
  155. Default Expr // for DEFAULT
  156. RefTable string // for REFERENCES
  157. RefColumn string // for REFERENCES
  158. }
  159. // ConstraintType represents the type of constraint.
  160. type ConstraintType int
  161. const (
  162. ConstraintPrimaryKey ConstraintType = iota
  163. ConstraintNotNull
  164. ConstraintUnique
  165. ConstraintDefault
  166. ConstraintCheck
  167. ConstraintForeignKey
  168. ConstraintAutoIncrement
  169. )
  170. // TableConstraint represents a table-level constraint.
  171. type TableConstraint struct {
  172. Type ConstraintType
  173. Name string // optional constraint name
  174. Columns []string // columns involved
  175. RefTable string // for FOREIGN KEY
  176. RefColumns []string // for FOREIGN KEY
  177. Check Expr // for CHECK
  178. }
  179. // DropTableStmt represents a DROP TABLE statement.
  180. type DropTableStmt struct {
  181. IfExists bool
  182. Tables []*TableRef
  183. }
  184. func (s *DropTableStmt) node() {}
  185. func (s *DropTableStmt) stmtNode() {}
  186. // CreateIndexStmt represents a CREATE INDEX statement.
  187. type CreateIndexStmt struct {
  188. IfNotExists bool
  189. Unique bool
  190. Name string
  191. Table string
  192. Columns []IndexColumn
  193. }
  194. func (s *CreateIndexStmt) node() {}
  195. func (s *CreateIndexStmt) stmtNode() {}
  196. // IndexColumn represents a column in an index.
  197. type IndexColumn struct {
  198. Name string
  199. Desc bool // true for DESC ordering
  200. }
  201. // DropIndexStmt represents a DROP INDEX statement.
  202. type DropIndexStmt struct {
  203. IfExists bool
  204. Name string
  205. }
  206. func (s *DropIndexStmt) node() {}
  207. func (s *DropIndexStmt) stmtNode() {}
  208. // CreateViewStmt represents a CREATE VIEW statement.
  209. type CreateViewStmt struct {
  210. IfNotExists bool
  211. View *TableRef
  212. Select *SelectStmt
  213. }
  214. func (s *CreateViewStmt) node() {}
  215. func (s *CreateViewStmt) stmtNode() {}
  216. // DropViewStmt represents a DROP VIEW statement.
  217. type DropViewStmt struct {
  218. IfExists bool
  219. Views []*TableRef
  220. }
  221. func (s *DropViewStmt) node() {}
  222. func (s *DropViewStmt) stmtNode() {}
  223. // AlterTableStmt represents an ALTER TABLE statement.
  224. type AlterTableStmt struct {
  225. Table string
  226. Action AlterAction
  227. }
  228. func (s *AlterTableStmt) node() {}
  229. func (s *AlterTableStmt) stmtNode() {}
  230. // AlterAction represents an ALTER TABLE action.
  231. type AlterAction interface {
  232. Node
  233. alterAction()
  234. }
  235. // AddColumnAction represents ADD COLUMN action.
  236. type AddColumnAction struct {
  237. Column *ColumnDef
  238. IfNotExists bool
  239. }
  240. func (a *AddColumnAction) node() {}
  241. func (a *AddColumnAction) alterAction() {}
  242. // DropColumnAction represents DROP COLUMN action.
  243. type DropColumnAction struct {
  244. Column string
  245. }
  246. func (a *DropColumnAction) node() {}
  247. func (a *DropColumnAction) alterAction() {}
  248. // RenameTableAction represents RENAME TO action.
  249. type RenameTableAction struct {
  250. NewName string
  251. }
  252. func (a *RenameTableAction) node() {}
  253. func (a *RenameTableAction) alterAction() {}
  254. // RenameColumnAction represents RENAME COLUMN action.
  255. type RenameColumnAction struct {
  256. OldName string
  257. NewName string
  258. }
  259. func (a *RenameColumnAction) node() {}
  260. func (a *RenameColumnAction) alterAction() {}
  261. // AttachStmt represents an ATTACH DATABASE statement.
  262. type AttachStmt struct {
  263. FilePath string // Database file path or identifier
  264. Alias string // Database alias name
  265. }
  266. func (s *AttachStmt) node() {}
  267. func (s *AttachStmt) stmtNode() {}
  268. // DetachStmt represents a DETACH DATABASE statement.
  269. type DetachStmt struct {
  270. Alias string // Database alias to detach
  271. }
  272. func (s *DetachStmt) node() {}
  273. func (s *DetachStmt) stmtNode() {}
  274. // PragmaStmt represents a PRAGMA statement.
  275. type PragmaStmt struct {
  276. Name string // pragma name (e.g., "table_info")
  277. Arg string // optional argument (e.g., table name)
  278. Value Expr // optional value for SET pragmas
  279. }
  280. func (s *PragmaStmt) node() {}
  281. func (s *PragmaStmt) stmtNode() {}
  282. // ExplainStmt represents an EXPLAIN statement.
  283. type ExplainStmt struct {
  284. QueryPlan bool // true for EXPLAIN QUERY PLAN
  285. Statement Statement // the statement being explained
  286. }
  287. func (s *ExplainStmt) node() {}
  288. func (s *ExplainStmt) stmtNode() {}
  289. // Transaction statements
  290. // BeginStmt represents a BEGIN TRANSACTION statement.
  291. type BeginStmt struct {
  292. // Transaction mode (DEFERRED, IMMEDIATE, EXCLUSIVE) - for future use
  293. Mode string
  294. }
  295. func (s *BeginStmt) node() {}
  296. func (s *BeginStmt) stmtNode() {}
  297. // CommitStmt represents a COMMIT statement.
  298. type CommitStmt struct{}
  299. func (s *CommitStmt) node() {}
  300. func (s *CommitStmt) stmtNode() {}
  301. // RollbackStmt represents a ROLLBACK statement.
  302. type RollbackStmt struct {
  303. Savepoint string // for ROLLBACK TO SAVEPOINT
  304. }
  305. func (s *RollbackStmt) node() {}
  306. func (s *RollbackStmt) stmtNode() {}
  307. // SavepointStmt represents a SAVEPOINT statement.
  308. type SavepointStmt struct {
  309. Name string
  310. }
  311. func (s *SavepointStmt) node() {}
  312. func (s *SavepointStmt) stmtNode() {}
  313. // ReleaseStmt represents a RELEASE SAVEPOINT statement.
  314. type ReleaseStmt struct {
  315. Name string
  316. }
  317. func (s *ReleaseStmt) node() {}
  318. func (s *ReleaseStmt) stmtNode() {}
  319. // Expression types
  320. // BinaryExpr represents a binary expression.
  321. type BinaryExpr struct {
  322. Left Expr
  323. Op lexer.TokenType
  324. Right Expr
  325. }
  326. func (e *BinaryExpr) node() {}
  327. func (e *BinaryExpr) exprNode() {}
  328. // UnaryExpr represents a unary expression.
  329. type UnaryExpr struct {
  330. Op lexer.TokenType
  331. Operand Expr
  332. }
  333. func (e *UnaryExpr) node() {}
  334. func (e *UnaryExpr) exprNode() {}
  335. // LiteralExpr represents a literal value.
  336. type LiteralExpr struct {
  337. Type lexer.TokenType // TokenNumber, TokenString, TokenNULL, TokenTRUE, TokenFALSE
  338. Value string
  339. }
  340. func (e *LiteralExpr) node() {}
  341. func (e *LiteralExpr) exprNode() {}
  342. // ColumnRef represents a column reference.
  343. type ColumnRef struct {
  344. Table string
  345. Column string
  346. }
  347. func (e *ColumnRef) node() {}
  348. func (e *ColumnRef) exprNode() {}
  349. // FunctionCall represents a function call.
  350. type FunctionCall struct {
  351. Name string
  352. Args []Expr
  353. Distinct bool // for COUNT(DISTINCT x)
  354. Star bool // for COUNT(*)
  355. }
  356. func (e *FunctionCall) node() {}
  357. func (e *FunctionCall) exprNode() {}
  358. // SubqueryExpr represents a subquery expression.
  359. type SubqueryExpr struct {
  360. Query *SelectStmt
  361. }
  362. func (e *SubqueryExpr) node() {}
  363. func (e *SubqueryExpr) exprNode() {}
  364. // CaseExpr represents a CASE expression.
  365. type CaseExpr struct {
  366. Operand Expr // for CASE operand WHEN...
  367. Whens []WhenClause
  368. Else Expr
  369. }
  370. func (e *CaseExpr) node() {}
  371. func (e *CaseExpr) exprNode() {}
  372. // WhenClause represents a WHEN clause in CASE.
  373. type WhenClause struct {
  374. Condition Expr
  375. Result Expr
  376. }
  377. // InExpr represents an IN expression.
  378. type InExpr struct {
  379. Left Expr
  380. Not bool
  381. Values []Expr // IN (1, 2, 3)
  382. Subquery *SelectStmt // IN (SELECT ...)
  383. }
  384. func (e *InExpr) node() {}
  385. func (e *InExpr) exprNode() {}
  386. // BetweenExpr represents a BETWEEN expression.
  387. type BetweenExpr struct {
  388. Left Expr
  389. Not bool
  390. Low Expr
  391. High Expr
  392. }
  393. func (e *BetweenExpr) node() {}
  394. func (e *BetweenExpr) exprNode() {}
  395. // LikeExpr represents a LIKE expression.
  396. type LikeExpr struct {
  397. Left Expr
  398. Not bool
  399. Pattern Expr
  400. Escape Expr
  401. }
  402. func (e *LikeExpr) node() {}
  403. func (e *LikeExpr) exprNode() {}
  404. // IsNullExpr represents an IS NULL expression.
  405. type IsNullExpr struct {
  406. Left Expr
  407. Not bool
  408. }
  409. func (e *IsNullExpr) node() {}
  410. func (e *IsNullExpr) exprNode() {}
  411. // CastExpr represents a CAST expression.
  412. type CastExpr struct {
  413. Expr Expr
  414. Type DataType
  415. }
  416. func (e *CastExpr) node() {}
  417. func (e *CastExpr) exprNode() {}
  418. // ExistsExpr represents an EXISTS expression.
  419. type ExistsExpr struct {
  420. Subquery *SelectStmt
  421. }
  422. func (e *ExistsExpr) node() {}
  423. func (e *ExistsExpr) exprNode() {}
  424. // ParenExpr represents a parenthesized expression.
  425. type ParenExpr struct {
  426. Expr Expr
  427. }
  428. func (e *ParenExpr) node() {}
  429. func (e *ParenExpr) exprNode() {}