2
0

ast.go 12 KB

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