ast.go 11 KB

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