2
0

ast.go 10 KB

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