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
use std::{
    ffi::{c_void, CStr},
    ops::Deref,
    ptr,
    sync::Arc,
};

use crate::{config::ConfigHandle, connection::ConnectionHandle, ffi};

#[derive(Debug)]
pub struct DatabaseHandle(ffi::duckdb_database);

impl DatabaseHandle {
    /// # Safety
    /// Takes ownership
    pub unsafe fn from_raw(raw: ffi::duckdb_database) -> Arc<Self> {
        Arc::new(Self(raw))
    }
    pub unsafe fn open(path: Option<&CStr>) -> Result<Arc<Self>, String> {
        Self::open_ext(path, None)
    }
    pub fn open_ext(
        path: Option<&CStr>,
        config: Option<&ConfigHandle>,
    ) -> Result<Arc<Self>, String> {
        unsafe {
            let mut db: ffi::duckdb_database = ptr::null_mut();
            let mut err = ptr::null_mut();
            let path = path.map(|p| p.as_ptr()).unwrap_or(ptr::null());
            let config = config.map(|c| **c).unwrap_or(ptr::null_mut());
            let r = ffi::duckdb_open_ext(path, &mut db, config, &mut err);
            if r != ffi::DuckDBSuccess {
                let err_cstr = CStr::from_ptr(err);
                let err_str = err_cstr.to_string_lossy().to_string();
                ffi::duckdb_free(err as _);
                return Err(err_str);
            }
            Ok(Self::from_raw(db))
        }
    }

    pub fn connect(self: &Arc<Self>) -> Result<Arc<ConnectionHandle>, ()> {
        let mut handle = ptr::null_mut();
        let r = unsafe { ffi::duckdb_connect(self.0, &mut handle) };
        if r != ffi::DuckDBSuccess {
            return Err(());
        }
        Ok(unsafe { ConnectionHandle::from_raw(handle, self.clone()) })
    }
    /// # Safety
    /// Force close connection without checking for usage.
    /// Normally you should let Rust handle this.
    pub unsafe fn close(&mut self) {
        ffi::duckdb_close(&mut self.0);
    }
}

impl Drop for DatabaseHandle {
    fn drop(&mut self) {
        unsafe { self.close() }
    }
}

impl Deref for DatabaseHandle {
    type Target = ffi::duckdb_database;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}