heliosdb-nano 3.30.0

PostgreSQL-compatible embedded database with TDE + ZKE encryption, HNSW vector search, Product Quantization, git-like branching, time-travel queries, materialized views, row-level security, and 50+ enterprise features
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Procedural Language AST
//!
//! Unified AST for procedural SQL dialects (PL/pgSQL, T-SQL, PL/SQL, DB2 PL).
//! This module provides a common representation that all dialects can be translated to.

use serde::{Deserialize, Serialize};
use crate::DataType;
use crate::sql::LogicalExpr;

/// Procedural SQL dialect
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ProceduralDialect {
    /// PostgreSQL PL/pgSQL
    PlPgSql,
    /// Microsoft T-SQL
    TSql,
    /// Oracle PL/SQL
    PlSql,
    /// IBM DB2 SQL PL
    Db2Pl,
    /// Auto-detected (based on syntax patterns)
    Auto,
}

impl Default for ProceduralDialect {
    fn default() -> Self {
        ProceduralDialect::Auto
    }
}

impl std::fmt::Display for ProceduralDialect {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ProceduralDialect::PlPgSql => write!(f, "PL/pgSQL"),
            ProceduralDialect::TSql => write!(f, "T-SQL"),
            ProceduralDialect::PlSql => write!(f, "PL/SQL"),
            ProceduralDialect::Db2Pl => write!(f, "DB2 SQL PL"),
            ProceduralDialect::Auto => write!(f, "Auto"),
        }
    }
}

/// A procedural code block (BEGIN...END)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProceduralBlock {
    /// Optional label for the block
    pub label: Option<String>,
    /// Variable declarations (DECLARE section)
    pub declarations: Vec<VariableDeclaration>,
    /// Statements in the block
    pub statements: Vec<ProceduralStatement>,
    /// Exception handlers
    pub exception_handlers: Vec<ExceptionHandler>,
}

impl ProceduralBlock {
    pub fn new() -> Self {
        Self {
            label: None,
            declarations: Vec::new(),
            statements: Vec::new(),
            exception_handlers: Vec::new(),
        }
    }
}

impl Default for ProceduralBlock {
    fn default() -> Self {
        Self::new()
    }
}

/// Variable declaration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VariableDeclaration {
    /// Variable name
    pub name: String,
    /// Data type (optional for some dialects)
    pub data_type: Option<DataType>,
    /// Default/initial value
    pub default: Option<LogicalExpr>,
    /// Whether this is a constant
    pub is_constant: bool,
    /// NOT NULL constraint
    pub not_null: bool,
}

/// Procedural statement
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ProceduralStatement {
    /// Variable assignment (SET, :=, =)
    Assignment {
        target: String,
        value: LogicalExpr,
    },

    /// IF statement
    If {
        condition: LogicalExpr,
        then_block: Vec<ProceduralStatement>,
        elsif_branches: Vec<(LogicalExpr, Vec<ProceduralStatement>)>,
        else_block: Option<Vec<ProceduralStatement>>,
    },

    /// CASE statement (searched)
    Case {
        when_branches: Vec<(LogicalExpr, Vec<ProceduralStatement>)>,
        else_block: Option<Vec<ProceduralStatement>>,
    },

    /// CASE statement (simple)
    SimpleCase {
        operand: LogicalExpr,
        when_branches: Vec<(LogicalExpr, Vec<ProceduralStatement>)>,
        else_block: Option<Vec<ProceduralStatement>>,
    },

    /// LOOP statement (infinite)
    Loop {
        label: Option<String>,
        body: Vec<ProceduralStatement>,
    },

    /// WHILE loop
    While {
        label: Option<String>,
        condition: LogicalExpr,
        body: Vec<ProceduralStatement>,
    },

    /// FOR loop (numeric range)
    ForNumeric {
        label: Option<String>,
        variable: String,
        lower_bound: LogicalExpr,
        upper_bound: LogicalExpr,
        step: Option<LogicalExpr>,
        reverse: bool,
        body: Vec<ProceduralStatement>,
    },

    /// FOR loop (cursor/query result)
    ForQuery {
        label: Option<String>,
        record_variable: String,
        query: String,
        body: Vec<ProceduralStatement>,
    },

    /// EXIT (break out of loop)
    Exit {
        label: Option<String>,
        when_condition: Option<LogicalExpr>,
    },

    /// CONTINUE (next iteration)
    Continue {
        label: Option<String>,
        when_condition: Option<LogicalExpr>,
    },

    /// RETURN statement
    Return {
        value: Option<LogicalExpr>,
    },

    /// RETURN NEXT (for set-returning functions)
    ReturnNext {
        value: LogicalExpr,
    },

    /// RETURN QUERY (for set-returning functions)
    ReturnQuery {
        query: String,
    },

    /// RAISE (exception/notice)
    Raise {
        level: RaiseLevel,
        message: Option<LogicalExpr>,
        sqlstate: Option<String>,
        detail: Option<LogicalExpr>,
        hint: Option<LogicalExpr>,
    },

    /// Execute SQL statement
    Execute {
        sql: String,
        into_variables: Vec<String>,
    },

    /// Execute dynamic SQL
    ExecuteDynamic {
        sql_expression: LogicalExpr,
        into_variables: Vec<String>,
        using_parameters: Vec<LogicalExpr>,
    },

    /// Nested block
    Block(ProceduralBlock),

    /// NULL statement (no-op)
    Null,

    /// PRINT (T-SQL)
    Print {
        message: LogicalExpr,
    },

    /// SELECT INTO (fetch into variables)
    SelectInto {
        query: String,
        variables: Vec<String>,
    },

    /// OPEN cursor
    OpenCursor {
        cursor_name: String,
        query: Option<String>,
    },

    /// FETCH from cursor
    FetchCursor {
        cursor_name: String,
        into_variables: Vec<String>,
        direction: FetchDirection,
    },

    /// CLOSE cursor
    CloseCursor {
        cursor_name: String,
    },

    /// CALL procedure
    Call {
        procedure_name: String,
        arguments: Vec<LogicalExpr>,
    },
}

/// RAISE severity level
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RaiseLevel {
    Debug,
    Log,
    Info,
    Notice,
    Warning,
    Exception,
}

impl Default for RaiseLevel {
    fn default() -> Self {
        RaiseLevel::Exception
    }
}

/// Cursor fetch direction
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum FetchDirection {
    Next,
    Prior,
    First,
    Last,
    Absolute(i64),
    Relative(i64),
}

impl Default for FetchDirection {
    fn default() -> Self {
        FetchDirection::Next
    }
}

/// Exception handler
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExceptionHandler {
    /// Conditions to catch (e.g., "no_data_found", "others", "SQLSTATE '23505'")
    pub conditions: Vec<ExceptionCondition>,
    /// Handler body
    pub body: Vec<ProceduralStatement>,
}

/// Exception condition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ExceptionCondition {
    /// Named condition (e.g., "no_data_found", "division_by_zero")
    Named(String),
    /// SQLSTATE code
    SqlState(String),
    /// OTHERS (catch-all)
    Others,
}

/// Function/Procedure definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoutineDefinition {
    /// Name of the routine
    pub name: String,
    /// Schema/owner (optional)
    pub schema: Option<String>,
    /// Parameters
    pub parameters: Vec<RoutineParameter>,
    /// Return type (None for procedures)
    pub return_type: Option<ReturnType>,
    /// Language (PLPGSQL, SQL, etc.)
    pub language: String,
    /// Volatility (VOLATILE, STABLE, IMMUTABLE)
    pub volatility: Volatility,
    /// Whether it's a SECURITY DEFINER
    pub security_definer: bool,
    /// Procedure/Function body
    pub body: ProceduralBlock,
    /// Original dialect
    pub dialect: ProceduralDialect,
}

/// Routine parameter
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoutineParameter {
    /// Parameter name
    pub name: String,
    /// Data type
    pub data_type: DataType,
    /// Parameter mode (IN, OUT, INOUT)
    pub mode: ParameterMode,
    /// Default value
    pub default: Option<LogicalExpr>,
}

/// Parameter mode
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum ParameterMode {
    #[default]
    In,
    Out,
    InOut,
}

/// Return type for functions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ReturnType {
    /// Single value
    Scalar(DataType),
    /// TABLE (set of rows)
    Table {
        columns: Vec<(String, DataType)>,
    },
    /// SETOF (set of single type)
    SetOf(DataType),
    /// VOID
    Void,
}

/// Function volatility
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum Volatility {
    #[default]
    Volatile,
    Stable,
    Immutable,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;

    #[test]
    fn test_procedural_block() {
        let mut block = ProceduralBlock::new();
        block.label = Some("outer_block".to_string());
        block.declarations.push(VariableDeclaration {
            name: "counter".to_string(),
            data_type: Some(DataType::Int4),
            default: None,
            is_constant: false,
            not_null: false,
        });
        block.statements.push(ProceduralStatement::Null);

        assert_eq!(block.label, Some("outer_block".to_string()));
        assert_eq!(block.declarations.len(), 1);
        assert_eq!(block.statements.len(), 1);
    }

    #[test]
    fn test_dialect_display() {
        assert_eq!(ProceduralDialect::PlPgSql.to_string(), "PL/pgSQL");
        assert_eq!(ProceduralDialect::TSql.to_string(), "T-SQL");
        assert_eq!(ProceduralDialect::PlSql.to_string(), "PL/SQL");
        assert_eq!(ProceduralDialect::Db2Pl.to_string(), "DB2 SQL PL");
    }

    #[test]
    fn test_variable_declaration() {
        let decl = VariableDeclaration {
            name: "my_var".to_string(),
            data_type: Some(DataType::Text),
            default: None,
            is_constant: true,
            not_null: true,
        };

        assert!(decl.is_constant);
        assert!(decl.not_null);
    }
}