use std::sync::Arc;
use parking_lot::Mutex;
use pyo3::prelude::*;
use crate::cursor::Cursor;
#[pyclass]
pub struct Database {
pub(crate) db: Arc<Mutex<vibesql_storage::Database>>,
}
#[pymethods]
impl Database {
#[new]
pub fn new() -> Self {
Database { db: Arc::new(Mutex::new(vibesql_storage::Database::new())) }
}
fn cursor(&self) -> PyResult<Cursor> {
Cursor::new(Arc::clone(&self.db))
}
fn close(&self) -> PyResult<()> {
Ok(())
}
fn commit(&self) -> PyResult<()> {
Ok(())
}
fn version(&self) -> String {
"vibesql-py 0.1.0".to_string()
}
fn save(&self, path: &str) -> PyResult<()> {
self.db.lock().save(path).map_err(|e| {
pyo3::exceptions::PyIOError::new_err(format!("Failed to save database: {}", e))
})
}
#[staticmethod]
fn load(path: &str) -> PyResult<Database> {
let db = vibesql_storage::Database::load(path).map_err(|e| {
pyo3::exceptions::PyIOError::new_err(format!("Failed to load database: {}", e))
})?;
Ok(Database { db: Arc::new(Mutex::new(db)) })
}
}