mirror of
https://github.com/aljazceru/turso.git
synced 2026-02-22 00:15:20 +01:00
bindings/javascript: Improve error when prepare() called after close()
This commit is contained in:
2
bindings/javascript/index.d.ts
vendored
2
bindings/javascript/index.d.ts
vendored
@@ -11,6 +11,8 @@ export declare class Database {
|
||||
constructor(path: string)
|
||||
/** Returns whether the database is in memory-only mode. */
|
||||
get memory(): boolean
|
||||
/** Returns whether the database connection is open. */
|
||||
get open(): boolean
|
||||
/**
|
||||
* Executes a batch of SQL statements.
|
||||
*
|
||||
|
||||
@@ -87,6 +87,10 @@ class Database {
|
||||
* @param {string} sql - The SQL statement string to prepare.
|
||||
*/
|
||||
prepare(sql) {
|
||||
if (!this.open) {
|
||||
throw new TypeError("The database connection is not open");
|
||||
}
|
||||
|
||||
if (!sql) {
|
||||
throw new RangeError("The supplied SQL string contains no statements");
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ pub struct Database {
|
||||
io: Arc<dyn turso_core::IO>,
|
||||
conn: Arc<turso_core::Connection>,
|
||||
is_memory: bool,
|
||||
is_open: RefCell<bool>,
|
||||
}
|
||||
|
||||
#[napi]
|
||||
@@ -76,6 +77,7 @@ impl Database {
|
||||
io,
|
||||
conn,
|
||||
is_memory,
|
||||
is_open: RefCell::new(true),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -85,6 +87,12 @@ impl Database {
|
||||
self.is_memory
|
||||
}
|
||||
|
||||
/// Returns whether the database connection is open.
|
||||
#[napi(getter)]
|
||||
pub fn open(&self) -> bool {
|
||||
*self.is_open.borrow()
|
||||
}
|
||||
|
||||
/// Executes a batch of SQL statements.
|
||||
///
|
||||
/// # Arguments
|
||||
@@ -167,6 +175,7 @@ impl Database {
|
||||
/// `Ok(())` if the database is closed successfully.
|
||||
#[napi]
|
||||
pub fn close(&self) -> Result<()> {
|
||||
*self.is_open.borrow_mut() = false;
|
||||
// Database close is handled automatically when dropped
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -87,6 +87,10 @@ class Database {
|
||||
* @param {string} sql - The SQL statement string to prepare.
|
||||
*/
|
||||
prepare(sql) {
|
||||
if (!this.open) {
|
||||
throw new TypeError("The database connection is not open");
|
||||
}
|
||||
|
||||
if (!sql) {
|
||||
throw new RangeError("The supplied SQL string contains no statements");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user