heliosdb-nano 3.23.2

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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//! TTC (Two-Task Common) Protocol Implementation
//!
//! TTC is Oracle's protocol layer on top of TNS for SQL command execution.
//! This module implements TTC message types and data structures.
//!
//! Reference: Oracle Database Net Services Reference

use bytes::{Buf, BufMut, BytesMut};
use std::io::{self, Cursor};

/// TTC function codes (message types)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum TtcFunction {
    /// Protocol negotiation
    ProtoNeg = 1,
    /// Data type negotiation
    DataTypeNeg = 2,
    /// Open cursor
    OpenCursor = 3,
    /// Parse statement
    Parse = 4,
    /// Execute statement
    Execute = 5,
    /// Fetch rows
    Fetch = 6,
    /// Close cursor
    CloseCursor = 7,
    /// Commit transaction
    Commit = 8,
    /// Rollback transaction
    Rollback = 9,
    /// Describe statement
    Describe = 10,
    /// Define output columns
    Define = 11,
    /// Bind variables
    Bind = 12,
    /// Get server version
    Version = 13,
    /// Logon to database
    Logon = 14,
    /// Logoff from database
    Logoff = 15,
    /// OALL (Oracle All) - generic call
    Oall = 17,
    /// Ping
    Ping = 147,
}

impl TtcFunction {
    /// Convert u8 to TtcFunction
    pub fn from_u8(value: u8) -> Option<Self> {
        match value {
            1 => Some(Self::ProtoNeg),
            2 => Some(Self::DataTypeNeg),
            3 => Some(Self::OpenCursor),
            4 => Some(Self::Parse),
            5 => Some(Self::Execute),
            6 => Some(Self::Fetch),
            7 => Some(Self::CloseCursor),
            8 => Some(Self::Commit),
            9 => Some(Self::Rollback),
            10 => Some(Self::Describe),
            11 => Some(Self::Define),
            12 => Some(Self::Bind),
            13 => Some(Self::Version),
            14 => Some(Self::Logon),
            15 => Some(Self::Logoff),
            17 => Some(Self::Oall),
            147 => Some(Self::Ping),
            _ => None,
        }
    }
}

/// TTC message header
#[derive(Debug, Clone)]
pub struct TtcHeader {
    /// Function code
    pub function: TtcFunction,
    /// Sequence number
    pub seq_num: u8,
    /// Data flags
    pub flags: u8,
}

impl TtcHeader {
    /// TTC header size
    pub const SIZE: usize = 3;

    /// Parse TTC header from bytes
    pub fn parse(data: &[u8]) -> io::Result<Self> {
        if data.len() < Self::SIZE {
            return Err(io::Error::new(
                io::ErrorKind::UnexpectedEof,
                "Insufficient data for TTC header",
            ));
        }

        let mut cursor = Cursor::new(data);
        let function_code = cursor.get_u8();
        let seq_num = cursor.get_u8();
        let flags = cursor.get_u8();

        let function = TtcFunction::from_u8(function_code)
            .ok_or_else(|| {
                io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!("Invalid TTC function code: {}", function_code),
                )
            })?;

        Ok(Self {
            function,
            seq_num,
            flags,
        })
    }

    /// Encode TTC header to bytes
    pub fn encode(&self) -> Vec<u8> {
        let mut buf = BytesMut::with_capacity(Self::SIZE);
        buf.put_u8(self.function as u8);
        buf.put_u8(self.seq_num);
        buf.put_u8(self.flags);
        buf.to_vec()
    }
}

/// TTC message
#[derive(Debug, Clone)]
pub struct TtcMessage {
    /// TTC header
    pub header: TtcHeader,
    /// Message payload
    pub payload: Vec<u8>,
}

impl TtcMessage {
    /// Create a new TTC message
    pub fn new(function: TtcFunction, payload: Vec<u8>) -> Self {
        let header = TtcHeader {
            function,
            seq_num: 0,
            flags: 0,
        };

        Self { header, payload }
    }

    /// Parse TTC message from bytes
    pub fn parse(data: &[u8]) -> io::Result<Self> {
        let header = TtcHeader::parse(data)?;
        let payload = data.get(TtcHeader::SIZE..).unwrap_or(&[]).to_vec();

        Ok(Self { header, payload })
    }

    /// Encode TTC message to bytes
    pub fn encode(&self) -> Vec<u8> {
        let mut buf = BytesMut::new();
        buf.extend_from_slice(&self.header.encode());
        buf.extend_from_slice(&self.payload);
        buf.to_vec()
    }
}

/// TTC Parse message data
#[derive(Debug, Clone)]
pub struct TtcParse {
    /// Cursor number
    pub cursor_id: u16,
    /// SQL statement text
    pub sql: String,
    /// Parse options
    pub options: u32,
}

impl TtcParse {
    /// Parse TTC Parse message
    pub fn parse(data: &[u8]) -> io::Result<Self> {
        let mut cursor = Cursor::new(data);

        // Simple parsing - in reality TTC uses a complex wire format
        // This is a simplified version for basic functionality
        let cursor_id = if cursor.remaining() >= 2 {
            cursor.get_u16()
        } else {
            1
        };

        let options = if cursor.remaining() >= 4 {
            cursor.get_u32()
        } else {
            0
        };

        // Remaining bytes are SQL text
        let sql_bytes: Vec<u8> = data.get(cursor.position() as usize..).unwrap_or(&[]).to_vec();
        let sql = String::from_utf8_lossy(&sql_bytes).to_string();

        Ok(Self {
            cursor_id,
            sql,
            options,
        })
    }
}

/// TTC Execute message data
#[derive(Debug, Clone)]
pub struct TtcExecute {
    /// Cursor number
    pub cursor_id: u16,
    /// Number of iterations (for batch operations)
    pub iterations: u32,
    /// Execute options
    pub options: u32,
}

impl TtcExecute {
    /// Parse TTC Execute message
    pub fn parse(data: &[u8]) -> io::Result<Self> {
        let mut cursor = Cursor::new(data);

        let cursor_id = if cursor.remaining() >= 2 {
            cursor.get_u16()
        } else {
            1
        };

        let iterations = if cursor.remaining() >= 4 {
            cursor.get_u32()
        } else {
            1
        };

        let options = if cursor.remaining() >= 4 {
            cursor.get_u32()
        } else {
            0
        };

        Ok(Self {
            cursor_id,
            iterations,
            options,
        })
    }
}

/// TTC Fetch message data
#[derive(Debug, Clone)]
pub struct TtcFetch {
    /// Cursor number
    pub cursor_id: u16,
    /// Number of rows to fetch
    pub num_rows: u32,
}

impl TtcFetch {
    /// Parse TTC Fetch message
    pub fn parse(data: &[u8]) -> io::Result<Self> {
        let mut cursor = Cursor::new(data);

        let cursor_id = if cursor.remaining() >= 2 {
            cursor.get_u16()
        } else {
            1
        };

        let num_rows = if cursor.remaining() >= 4 {
            cursor.get_u32()
        } else {
            1
        };

        Ok(Self {
            cursor_id,
            num_rows,
        })
    }
}

/// TTC Logon message data
#[derive(Debug, Clone)]
pub struct TtcLogon {
    /// Username
    pub username: String,
    /// Password (encrypted or plain)
    pub password: String,
    /// Database name/SID
    pub database: String,
}

impl TtcLogon {
    /// Parse TTC Logon message (simplified)
    pub fn parse(data: &[u8]) -> io::Result<Self> {
        // In reality, TTC logon uses complex encoding with O5LOGON, O7LOGON, etc.
        // This is a simplified version that extracts basic auth info

        let text = String::from_utf8_lossy(data).to_string();

        // Try to extract username/password from connection string
        let username = Self::extract_field(&text, "USER=")
            .or_else(|| Self::extract_field(&text, "UID="))
            .unwrap_or_else(|| "helios".to_string());

        let password = Self::extract_field(&text, "PASSWORD=")
            .or_else(|| Self::extract_field(&text, "PWD="))
            .unwrap_or_else(|| "".to_string());

        let database = Self::extract_field(&text, "SERVICE_NAME=")
            .or_else(|| Self::extract_field(&text, "SID="))
            .unwrap_or_else(|| "heliosdb".to_string());

        Ok(Self {
            username,
            password,
            database,
        })
    }

    fn extract_field(text: &str, field_name: &str) -> Option<String> {
        let text_upper = text.to_uppercase();
        if let Some(start) = text_upper.find(field_name) {
            let start = start + field_name.len();
            let end = text_upper[start..]
                .find([')', ' ', ';'])
                .map(|pos| start + pos)
                .unwrap_or(text.len());
            return Some(text[start..end].to_string());
        }
        None
    }
}

/// TTC Response builder
pub struct TtcResponseBuilder {
    buffer: BytesMut,
}

impl TtcResponseBuilder {
    /// Create a new response builder
    pub fn new() -> Self {
        Self {
            buffer: BytesMut::new(),
        }
    }

    /// Write a response header
    pub fn write_header(&mut self, function: TtcFunction) {
        self.buffer.put_u8(function as u8);
        self.buffer.put_u8(0); // seq_num
        self.buffer.put_u8(0); // flags
    }

    /// Write a row data marker
    pub fn write_row_header(&mut self, num_columns: u16) {
        self.buffer.put_u8(0x15); // Row data marker
        self.buffer.put_u16(num_columns);
    }

    /// Write a column value (text format)
    pub fn write_column(&mut self, value: &str) {
        let bytes = value.as_bytes();
        self.buffer.put_u16(bytes.len() as u16);
        self.buffer.extend_from_slice(bytes);
    }

    /// Write a NULL column
    pub fn write_null_column(&mut self) {
        self.buffer.put_u16(0xFFFF); // NULL marker
    }

    /// Write an error response
    pub fn write_error(&mut self, code: &str, message: &str) {
        self.buffer.put_u8(0x04); // Error marker
        self.buffer.extend_from_slice(code.as_bytes());
        self.buffer.put_u8(0x00);
        self.buffer.extend_from_slice(message.as_bytes());
        self.buffer.put_u8(0x00);
    }

    /// Write end-of-fetch marker
    pub fn write_end_of_fetch(&mut self) {
        self.buffer.put_u8(0x08); // End of fetch marker
    }

    /// Write command complete marker
    pub fn write_command_complete(&mut self, rows_affected: u64) {
        self.buffer.put_u8(0x06); // Command complete marker
        self.buffer.put_u64(rows_affected);
    }

    /// Build the final response
    pub fn build(self) -> Vec<u8> {
        self.buffer.to_vec()
    }
}

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

/// Oracle data type codes (simplified)
#[allow(dead_code)]
pub mod oracle_types {
    pub const VARCHAR2: u8 = 1;
    pub const NUMBER: u8 = 2;
    pub const LONG: u8 = 8;
    pub const DATE: u8 = 12;
    pub const RAW: u8 = 23;
    pub const LONG_RAW: u8 = 24;
    pub const CHAR: u8 = 96;
    pub const BINARY_FLOAT: u8 = 100;
    pub const BINARY_DOUBLE: u8 = 101;
    pub const CLOB: u8 = 112;
    pub const BLOB: u8 = 113;
    pub const TIMESTAMP: u8 = 180;
    pub const TIMESTAMP_TZ: u8 = 181;
}

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

    #[test]
    fn test_ttc_header_parse() {
        let data = vec![
            0x04, // Parse function
            0x00, // seq_num
            0x00, // flags
        ];

        let header = TtcHeader::parse(&data).unwrap();
        assert_eq!(header.function, TtcFunction::Parse);
    }

    #[test]
    fn test_ttc_message_encode_decode() {
        let payload = vec![1, 2, 3, 4];
        let msg = TtcMessage::new(TtcFunction::Execute, payload.clone());

        let encoded = msg.encode();
        let decoded = TtcMessage::parse(&encoded).unwrap();

        assert_eq!(decoded.header.function, TtcFunction::Execute);
        assert_eq!(decoded.payload, payload);
    }

    #[test]
    fn test_response_builder() {
        let mut builder = TtcResponseBuilder::new();
        builder.write_header(TtcFunction::Execute);
        builder.write_command_complete(5);

        let response = builder.build();
        assert!(!response.is_empty());
    }
}