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

use crate::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))
    }

    /// # Safety
    /// * Ensure all children are closed
    /// * Don't use this object afterwards
    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
    }
}