alopex_sql/ast/
ddl.rs

1use super::expr::Expr;
2use super::span::{Span, Spanned};
3
4#[derive(Debug, Clone)]
5pub struct CreateTable {
6    pub if_not_exists: bool,
7    pub name: String,
8    pub columns: Vec<ColumnDef>,
9    pub constraints: Vec<TableConstraint>,
10    /// Raw WITH オプション (key, value) の組。
11    pub with_options: Vec<(String, String)>,
12    pub span: Span,
13}
14
15#[derive(Debug, Clone)]
16pub struct ColumnDef {
17    pub name: String,
18    pub data_type: DataType,
19    pub constraints: Vec<ColumnConstraint>,
20    pub span: Span,
21}
22
23#[derive(Debug, Clone)]
24pub enum DataType {
25    Integer,
26    Int,
27    BigInt,
28    Float,
29    Double,
30    Text,
31    Blob,
32    Boolean,
33    Bool,
34    Timestamp,
35    Vector {
36        dimension: u32,
37        metric: Option<VectorMetric>,
38    },
39}
40
41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub enum VectorMetric {
43    Cosine,
44    L2,
45    Inner,
46}
47
48#[derive(Debug, Clone)]
49pub enum ColumnConstraint {
50    NotNull,
51    Null,
52    PrimaryKey,
53    Unique,
54    Default(Expr),
55    /// Span for the constraint keyword/location.
56    WithSpan {
57        kind: Box<ColumnConstraint>,
58        span: Span,
59    },
60}
61
62#[derive(Debug, Clone)]
63pub enum TableConstraint {
64    PrimaryKey { columns: Vec<String>, span: Span },
65}
66
67#[derive(Debug, Clone)]
68pub struct DropTable {
69    pub if_exists: bool,
70    pub name: String,
71    pub span: Span,
72}
73
74#[derive(Debug, Clone)]
75pub struct CreateIndex {
76    pub if_not_exists: bool,
77    pub name: String,
78    pub table: String,
79    pub column: String,
80    pub method: Option<IndexMethod>,
81    pub options: Vec<IndexOption>,
82    pub span: Span,
83}
84
85#[derive(Debug, Clone, Copy, PartialEq, Eq)]
86pub enum IndexMethod {
87    BTree,
88    Hnsw,
89}
90
91#[derive(Debug, Clone)]
92pub struct IndexOption {
93    pub key: String,
94    pub value: String,
95    pub span: Span,
96}
97
98#[derive(Debug, Clone)]
99pub struct DropIndex {
100    pub if_exists: bool,
101    pub name: String,
102    pub span: Span,
103}
104
105impl Spanned for CreateTable {
106    fn span(&self) -> Span {
107        self.span
108    }
109}
110
111impl Spanned for ColumnDef {
112    fn span(&self) -> Span {
113        self.span
114    }
115}
116
117impl Spanned for ColumnConstraint {
118    fn span(&self) -> Span {
119        match self {
120            ColumnConstraint::WithSpan { span, .. } => *span,
121            _ => Span::empty(),
122        }
123    }
124}
125
126impl Spanned for TableConstraint {
127    fn span(&self) -> Span {
128        match self {
129            TableConstraint::PrimaryKey { span, .. } => *span,
130        }
131    }
132}
133
134impl Spanned for DropTable {
135    fn span(&self) -> Span {
136        self.span
137    }
138}
139
140impl Spanned for CreateIndex {
141    fn span(&self) -> Span {
142        self.span
143    }
144}
145
146impl Spanned for IndexOption {
147    fn span(&self) -> Span {
148        self.span
149    }
150}
151
152impl Spanned for DropIndex {
153    fn span(&self) -> Span {
154        self.span
155    }
156}