ggsql 0.4.0

A declarative visualization language that extends SQL with powerful data visualization capabilities.
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
//! Safe Rust wrappers around ODBC handles.
//!
//! Provides `Environment`, `Connection`, `Statement`, and `PreparedStatement`
//! types that own ODBC handles and call through the runtime-loaded FFI.

use super::ffi::*;
use crate::{GgsqlError, Result};
use std::sync::OnceLock;

// ============================================================================
// Diagnostic helpers
// ============================================================================

fn extract_diagnostic(handle_type: SqlSmallInt, handle: SqlHandle) -> String {
    let f = fns();
    let mut state = [0u8; 6];
    let mut native_error: SqlInteger = 0;
    let mut buf = vec![0u8; 512];
    let mut text_len: SqlSmallInt = 0;

    let rc = unsafe {
        (f.SQLGetDiagRec)(
            handle_type,
            handle,
            1,
            state.as_mut_ptr(),
            &mut native_error,
            buf.as_mut_ptr(),
            buf.len() as SqlSmallInt,
            &mut text_len,
        )
    };

    if !succeeded(rc) {
        return "Unknown ODBC error (no diagnostic record)".to_string();
    }

    // Retry with larger buffer if truncated
    if text_len as usize >= buf.len() {
        buf.resize(text_len as usize + 1, 0);
        unsafe {
            (f.SQLGetDiagRec)(
                handle_type,
                handle,
                1,
                state.as_mut_ptr(),
                &mut native_error,
                buf.as_mut_ptr(),
                buf.len() as SqlSmallInt,
                &mut text_len,
            );
        }
    }

    let state_str = std::str::from_utf8(&state[..5]).unwrap_or("?????");
    let msg = std::str::from_utf8(&buf[..text_len as usize]).unwrap_or("(invalid UTF-8)");
    format!("[{}] {}", state_str, msg)
}

fn check(rc: SqlReturn, handle_type: SqlSmallInt, handle: SqlHandle, context: &str) -> Result<()> {
    match rc {
        SQL_SUCCESS | SQL_SUCCESS_WITH_INFO => Ok(()),
        SQL_NO_DATA => Ok(()),
        _ => {
            let diag = extract_diagnostic(handle_type, handle);
            Err(GgsqlError::ReaderError(format!("{}: {}", context, diag)))
        }
    }
}

/// Check if the SQLSTATE from the last operation matches a specific code.
fn sqlstate_is(handle_type: SqlSmallInt, handle: SqlHandle, expected: &[u8; 5]) -> bool {
    let f = fns();
    let mut state = [0u8; 6];
    let mut native_error: SqlInteger = 0;
    let mut text_len: SqlSmallInt = 0;
    let rc = unsafe {
        (f.SQLGetDiagRec)(
            handle_type,
            handle,
            1,
            state.as_mut_ptr(),
            &mut native_error,
            std::ptr::null_mut(),
            0,
            &mut text_len,
        )
    };
    succeeded(rc) && state[..5] == expected[..]
}

// ============================================================================
// Environment
// ============================================================================

pub struct Environment {
    handle: SqlHEnv,
}

unsafe impl Send for Environment {}
unsafe impl Sync for Environment {}

impl Environment {
    fn new() -> Result<Self> {
        let f = fns();
        let mut handle = SQL_NULL_HANDLE;
        let rc = unsafe { (f.SQLAllocHandle)(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &mut handle) };
        if !succeeded(rc) {
            return Err(GgsqlError::ReaderError(
                "Failed to allocate ODBC environment handle".into(),
            ));
        }

        let rc = unsafe {
            (f.SQLSetEnvAttr)(
                handle,
                SQL_ATTR_ODBC_VERSION,
                SQL_OV_ODBC3_80 as SqlPointer,
                0,
            )
        };
        check(rc, SQL_HANDLE_ENV, handle, "Failed to set ODBC version")?;

        Ok(Environment { handle })
    }

    pub fn handle(&self) -> SqlHEnv {
        self.handle
    }
}

impl Drop for Environment {
    fn drop(&mut self) {
        let f = fns();
        unsafe { (f.SQLFreeHandle)(SQL_HANDLE_ENV, self.handle) };
    }
}

/// Global ODBC environment (singleton per process).
pub fn odbc_env() -> Result<&'static Environment> {
    static ENV: OnceLock<std::result::Result<Environment, String>> = OnceLock::new();
    let result = ENV.get_or_init(|| Environment::new().map_err(|e| e.to_string()));
    match result {
        Ok(env) => Ok(env),
        Err(e) => Err(GgsqlError::ReaderError(e.clone())),
    }
}

// ============================================================================
// Connection
// ============================================================================

pub struct Connection {
    handle: SqlHDbc,
}

unsafe impl Send for Connection {}

impl Connection {
    pub fn connect(env: &Environment, conn_str: &str) -> Result<Self> {
        let f = fns();
        let mut handle = SQL_NULL_HANDLE;
        let rc = unsafe { (f.SQLAllocHandle)(SQL_HANDLE_DBC, env.handle(), &mut handle) };
        if !succeeded(rc) {
            return Err(GgsqlError::ReaderError(
                "Failed to allocate ODBC connection handle".into(),
            ));
        }

        let conn_cstr = std::ffi::CString::new(conn_str)
            .map_err(|_| GgsqlError::ReaderError("Connection string contains null byte".into()))?;
        let rc = unsafe {
            (f.SQLDriverConnect)(
                handle,
                std::ptr::null_mut(), // no window handle
                conn_cstr.as_ptr() as *const SqlChar,
                conn_str.len() as SqlSmallInt,
                std::ptr::null_mut(), // no output buffer
                0,
                std::ptr::null_mut(),
                SQL_DRIVER_NOPROMPT,
            )
        };
        if !succeeded(rc) {
            let diag = extract_diagnostic(SQL_HANDLE_DBC, handle);
            unsafe { (f.SQLFreeHandle)(SQL_HANDLE_DBC, handle) };
            return Err(GgsqlError::ReaderError(format!(
                "ODBC connection failed: {}",
                diag
            )));
        }

        Ok(Connection { handle })
    }

    pub fn handle(&self) -> SqlHDbc {
        self.handle
    }

    /// Execute a SQL statement, returning a Statement if it produces a result set.
    pub fn execute(&self, sql: &str) -> Result<Option<Statement>> {
        let f = fns();
        let mut stmt_handle = SQL_NULL_HANDLE;
        let rc = unsafe { (f.SQLAllocHandle)(SQL_HANDLE_STMT, self.handle, &mut stmt_handle) };
        check(
            rc,
            SQL_HANDLE_DBC,
            self.handle,
            "Failed to allocate statement",
        )?;

        let sql_cstr = std::ffi::CString::new(sql)
            .map_err(|_| GgsqlError::ReaderError("SQL string contains null byte".into()))?;
        let rc = unsafe {
            (f.SQLExecDirect)(
                stmt_handle,
                sql_cstr.as_ptr() as *const SqlChar,
                sql.len() as SqlInteger,
            )
        };

        match rc {
            SQL_SUCCESS | SQL_SUCCESS_WITH_INFO => {}
            SQL_NO_DATA => {
                // DDL or statement with no result set
                unsafe { (f.SQLFreeHandle)(SQL_HANDLE_STMT, stmt_handle) };
                return Ok(None);
            }
            _ => {
                let diag = extract_diagnostic(SQL_HANDLE_STMT, stmt_handle);
                unsafe { (f.SQLFreeHandle)(SQL_HANDLE_STMT, stmt_handle) };
                return Err(GgsqlError::ReaderError(format!(
                    "ODBC execute failed: {}",
                    diag
                )));
            }
        }

        // Check if there's a result set
        let mut col_count: SqlSmallInt = 0;
        let rc = unsafe { (f.SQLNumResultCols)(stmt_handle, &mut col_count) };
        check(
            rc,
            SQL_HANDLE_STMT,
            stmt_handle,
            "Failed to get column count",
        )?;

        if col_count == 0 {
            unsafe { (f.SQLFreeHandle)(SQL_HANDLE_STMT, stmt_handle) };
            return Ok(None);
        }

        Ok(Some(Statement {
            handle: stmt_handle,
        }))
    }

    /// Prepare a SQL statement for repeated execution with parameters.
    pub fn prepare(&self, sql: &str) -> Result<PreparedStatement> {
        let f = fns();
        let mut stmt_handle = SQL_NULL_HANDLE;
        let rc = unsafe { (f.SQLAllocHandle)(SQL_HANDLE_STMT, self.handle, &mut stmt_handle) };
        check(
            rc,
            SQL_HANDLE_DBC,
            self.handle,
            "Failed to allocate statement",
        )?;

        let sql_cstr = std::ffi::CString::new(sql)
            .map_err(|_| GgsqlError::ReaderError("SQL string contains null byte".into()))?;
        let rc = unsafe {
            (f.SQLPrepare)(
                stmt_handle,
                sql_cstr.as_ptr() as *const SqlChar,
                sql.len() as SqlInteger,
            )
        };
        if !succeeded(rc) {
            let diag = extract_diagnostic(SQL_HANDLE_STMT, stmt_handle);
            unsafe { (f.SQLFreeHandle)(SQL_HANDLE_STMT, stmt_handle) };
            return Err(GgsqlError::ReaderError(format!(
                "ODBC prepare failed: {}",
                diag
            )));
        }

        Ok(PreparedStatement {
            handle: stmt_handle,
        })
    }

    /// Get DBMS name via SQLGetInfo.
    pub fn dbms_name(&self) -> Option<String> {
        let f = fns();
        let mut buf = vec![0u8; 256];
        let mut len: SqlSmallInt = 0;
        let rc = unsafe {
            (f.SQLGetInfo)(
                self.handle,
                SQL_DBMS_NAME,
                buf.as_mut_ptr() as SqlPointer,
                buf.len() as SqlSmallInt,
                &mut len,
            )
        };
        if succeeded(rc) && len > 0 {
            let s = std::str::from_utf8(&buf[..len as usize]).ok()?.to_string();
            Some(s)
        } else {
            None
        }
    }
}

impl Drop for Connection {
    fn drop(&mut self) {
        let f = fns();
        let rc = unsafe { (f.SQLDisconnect)(self.handle) };
        // If there's an open transaction, attempt rollback then retry
        if !succeeded(rc) && sqlstate_is(SQL_HANDLE_DBC, self.handle, b"25000") {
            unsafe {
                (f.SQLEndTran)(SQL_HANDLE_DBC, self.handle, SQL_ROLLBACK);
                (f.SQLDisconnect)(self.handle);
            }
        }
        unsafe { (f.SQLFreeHandle)(SQL_HANDLE_DBC, self.handle) };
    }
}

// ============================================================================
// Statement (result cursor)
// ============================================================================

pub struct Statement {
    handle: SqlHStmt,
}

impl Statement {
    pub fn handle(&self) -> SqlHStmt {
        self.handle
    }

    pub fn num_result_cols(&self) -> Result<usize> {
        let f = fns();
        let mut count: SqlSmallInt = 0;
        let rc = unsafe { (f.SQLNumResultCols)(self.handle, &mut count) };
        check(
            rc,
            SQL_HANDLE_STMT,
            self.handle,
            "Failed to get column count",
        )?;
        Ok(count as usize)
    }

    /// Describe column `col` (1-based).
    /// Returns (name, sql_data_type, column_size, decimal_digits, nullable).
    pub fn describe_col(
        &self,
        col: u16,
    ) -> Result<(String, SqlSmallInt, SqlULen, SqlSmallInt, bool)> {
        let f = fns();
        let mut name_buf = vec![0u8; 256];
        let mut name_len: SqlSmallInt = 0;
        let mut data_type: SqlSmallInt = 0;
        let mut col_size: SqlULen = 0;
        let mut decimal_digits: SqlSmallInt = 0;
        let mut nullable: SqlSmallInt = 0;

        let rc = unsafe {
            (f.SQLDescribeCol)(
                self.handle,
                col,
                name_buf.as_mut_ptr(),
                name_buf.len() as SqlSmallInt,
                &mut name_len,
                &mut data_type,
                &mut col_size,
                &mut decimal_digits,
                &mut nullable,
            )
        };
        check(
            rc,
            SQL_HANDLE_STMT,
            self.handle,
            "Failed to describe column",
        )?;

        let name = std::str::from_utf8(&name_buf[..name_len as usize])
            .unwrap_or("?")
            .to_string();

        Ok((
            name,
            data_type,
            col_size,
            decimal_digits,
            nullable != SQL_NO_NULLS,
        ))
    }

    /// Bind a column buffer for batch fetching (1-based column index).
    pub fn bind_col(
        &self,
        col: u16,
        c_type: SqlSmallInt,
        buffer: SqlPointer,
        buffer_len: SqlLen,
        indicator: *mut SqlLen,
    ) -> Result<()> {
        let f = fns();
        let rc = unsafe { (f.SQLBindCol)(self.handle, col, c_type, buffer, buffer_len, indicator) };
        check(rc, SQL_HANDLE_STMT, self.handle, "Failed to bind column")
    }

    /// Set a statement attribute.
    pub fn set_stmt_attr(
        &self,
        attribute: SqlInteger,
        value: SqlPointer,
        string_length: SqlInteger,
    ) -> Result<()> {
        let f = fns();
        let rc = unsafe { (f.SQLSetStmtAttr)(self.handle, attribute, value, string_length) };
        check(
            rc,
            SQL_HANDLE_STMT,
            self.handle,
            "Failed to set statement attribute",
        )
    }

    /// Set up batch fetching with the given batch size.
    /// Returns a mutable reference location for rows_fetched.
    pub fn setup_batch_fetch(&self, batch_size: usize) -> Result<()> {
        // Column-wise binding
        self.set_stmt_attr(SQL_ATTR_ROW_BIND_TYPE, SQL_BIND_BY_COLUMN as SqlPointer, 0)?;
        // Array size
        self.set_stmt_attr(SQL_ATTR_ROW_ARRAY_SIZE, batch_size as SqlPointer, 0)?;
        Ok(())
    }

    /// Set the rows-fetched pointer.
    ///
    /// # Safety
    /// `rows_fetched` must remain valid and pinned for the lifetime of the cursor.
    pub unsafe fn set_rows_fetched_ptr(&self, rows_fetched: *mut SqlULen) -> Result<()> {
        self.set_stmt_attr(SQL_ATTR_ROWS_FETCHED_PTR, rows_fetched as SqlPointer, 0)
    }

    /// Fetch the next batch of rows. Returns the ODBC return code.
    pub fn fetch_raw(&self) -> SqlReturn {
        let f = fns();
        unsafe { (f.SQLFetch)(self.handle) }
    }

    /// Unbind all columns.
    pub fn unbind_cols(&self) -> Result<()> {
        let f = fns();
        let rc = unsafe { (f.SQLFreeStmt)(self.handle, SQL_UNBIND) };
        check(rc, SQL_HANDLE_STMT, self.handle, "Failed to unbind columns")
    }
}

impl Drop for Statement {
    fn drop(&mut self) {
        let f = fns();
        let rc = unsafe { (f.SQLFreeHandle)(SQL_HANDLE_STMT, self.handle) };
        if !succeeded(rc) && !std::thread::panicking() {
            panic!(
                "SQLFreeHandle(STMT) failed: {}",
                extract_diagnostic(SQL_HANDLE_STMT, self.handle)
            );
        }
    }
}

// ============================================================================
// PreparedStatement (for bulk insert)
// ============================================================================

pub struct PreparedStatement {
    handle: SqlHStmt,
}

impl PreparedStatement {
    /// Bind a text parameter (1-based index).
    ///
    /// # Safety
    /// `value_ptr` and `indicator` must remain valid until execute() or reset_params().
    pub unsafe fn bind_text_parameter(
        &self,
        param_num: u16,
        value_ptr: *const u8,
        buffer_len: SqlLen,
        indicator: *mut SqlLen,
    ) -> Result<()> {
        let f = fns();
        let rc = unsafe {
            (f.SQLBindParameter)(
                self.handle,
                param_num,
                SQL_PARAM_INPUT,
                SQL_C_CHAR,
                SQL_VARCHAR,
                buffer_len as SqlULen,
                0,
                value_ptr as SqlPointer,
                buffer_len,
                indicator,
            )
        };
        check(rc, SQL_HANDLE_STMT, self.handle, "Failed to bind parameter")
    }

    pub fn execute(&self) -> Result<()> {
        let f = fns();
        let rc = unsafe { (f.SQLExecute)(self.handle) };
        check(
            rc,
            SQL_HANDLE_STMT,
            self.handle,
            "Failed to execute prepared statement",
        )
    }

    pub fn reset_params(&self) -> Result<()> {
        let f = fns();
        let rc = unsafe { (f.SQLFreeStmt)(self.handle, SQL_RESET_PARAMS) };
        check(
            rc,
            SQL_HANDLE_STMT,
            self.handle,
            "Failed to reset parameters",
        )
    }
}

impl Drop for PreparedStatement {
    fn drop(&mut self) {
        let f = fns();
        let rc = unsafe { (f.SQLFreeHandle)(SQL_HANDLE_STMT, self.handle) };
        if !succeeded(rc) && !std::thread::panicking() {
            panic!(
                "SQLFreeHandle(STMT) failed: {}",
                extract_diagnostic(SQL_HANDLE_STMT, self.handle)
            );
        }
    }
}

// ============================================================================
// ODBC catalog function helpers
// ============================================================================

/// Execute SQLTables and return the result as a Statement cursor.
pub fn sql_tables(
    conn: &Connection,
    catalog: Option<&str>,
    schema: Option<&str>,
    table: Option<&str>,
    table_type: Option<&str>,
) -> Result<Statement> {
    let f = fns();
    let mut stmt_handle = SQL_NULL_HANDLE;
    let rc = unsafe { (f.SQLAllocHandle)(SQL_HANDLE_STMT, conn.handle(), &mut stmt_handle) };
    check(
        rc,
        SQL_HANDLE_DBC,
        conn.handle(),
        "Failed to allocate statement for SQLTables",
    )?;

    let (cat_cs, cat_len) = str_to_odbc_cstring(catalog)?;
    let (sch_cs, sch_len) = str_to_odbc_cstring(schema)?;
    let (tbl_cs, tbl_len) = str_to_odbc_cstring(table)?;
    let (typ_cs, typ_len) = str_to_odbc_cstring(table_type)?;

    let rc = unsafe {
        (f.SQLTables)(
            stmt_handle,
            cstring_ptr(&cat_cs),
            cat_len,
            cstring_ptr(&sch_cs),
            sch_len,
            cstring_ptr(&tbl_cs),
            tbl_len,
            cstring_ptr(&typ_cs),
            typ_len,
        )
    };
    if !succeeded(rc) {
        let diag = extract_diagnostic(SQL_HANDLE_STMT, stmt_handle);
        unsafe { (f.SQLFreeHandle)(SQL_HANDLE_STMT, stmt_handle) };
        return Err(GgsqlError::ReaderError(format!(
            "SQLTables failed: {}",
            diag
        )));
    }

    Ok(Statement {
        handle: stmt_handle,
    })
}

/// Execute SQLColumns and return the result as a Statement cursor.
pub fn sql_columns(
    conn: &Connection,
    catalog: Option<&str>,
    schema: Option<&str>,
    table: Option<&str>,
    column: Option<&str>,
) -> Result<Statement> {
    let f = fns();
    let mut stmt_handle = SQL_NULL_HANDLE;
    let rc = unsafe { (f.SQLAllocHandle)(SQL_HANDLE_STMT, conn.handle(), &mut stmt_handle) };
    check(
        rc,
        SQL_HANDLE_DBC,
        conn.handle(),
        "Failed to allocate statement for SQLColumns",
    )?;

    let (cat_cs, cat_len) = str_to_odbc_cstring(catalog)?;
    let (sch_cs, sch_len) = str_to_odbc_cstring(schema)?;
    let (tbl_cs, tbl_len) = str_to_odbc_cstring(table)?;
    let (col_cs, col_len) = str_to_odbc_cstring(column)?;

    let rc = unsafe {
        (f.SQLColumns)(
            stmt_handle,
            cstring_ptr(&cat_cs),
            cat_len,
            cstring_ptr(&sch_cs),
            sch_len,
            cstring_ptr(&tbl_cs),
            tbl_len,
            cstring_ptr(&col_cs),
            col_len,
        )
    };
    if !succeeded(rc) {
        let diag = extract_diagnostic(SQL_HANDLE_STMT, stmt_handle);
        unsafe { (f.SQLFreeHandle)(SQL_HANDLE_STMT, stmt_handle) };
        return Err(GgsqlError::ReaderError(format!(
            "SQLColumns failed: {}",
            diag
        )));
    }

    Ok(Statement {
        handle: stmt_handle,
    })
}

fn str_to_odbc_cstring(s: Option<&str>) -> Result<(Option<std::ffi::CString>, SqlSmallInt)> {
    match s {
        Some(s) => {
            let cs = std::ffi::CString::new(s).map_err(|_| {
                GgsqlError::ReaderError("ODBC catalog argument contains null byte".into())
            })?;
            let len = s.len() as SqlSmallInt;
            Ok((Some(cs), len))
        }
        None => Ok((None, 0)),
    }
}

fn cstring_ptr(cs: &Option<std::ffi::CString>) -> *const SqlChar {
    match cs {
        Some(cs) => cs.as_ptr() as *const SqlChar,
        None => std::ptr::null(),
    }
}