diff --git a/doc.md b/doc.md index 9808091..499e0d6 100644 --- a/doc.md +++ b/doc.md @@ -146,6 +146,15 @@ retrieve the results, and more. ```ts const stmt = db.prepare("SELECT * FROM foo WHERE bar = ? AND baz = ?"); + +// or with a using statement + +{ + using stmt = db.prepare("SELECT * FROM foo WHERE bar = ? AND baz = ?"); + // use stmt +} + +// automatically disposed ``` For more details on binding parameters, see @@ -251,6 +260,18 @@ this method. stmt.finalize(); ``` +You can also use `using` statement to automatically free the statement once the +scope ends. + +```ts +{ + using stmt = db.prepare("SELECT * FROM foo WHERE bar = ? AND baz = ?"); + stmt.run("bar", "baz"); +} + +// stmt is automatically finalized here +``` + ## Setting fixed parameters To set fixed parameters for a statement, use the `bind()` method. This method diff --git a/src/statement.ts b/src/statement.ts index 6109855..f8b8294 100644 --- a/src/statement.ts +++ b/src/statement.ts @@ -785,4 +785,12 @@ export class Statement { } sqlite3_reset(this.#handle); } + + [Symbol.dispose](): void { + this.finalize(); + } + + [Symbol.for("Deno.customInspect")](): string { + return `Statement { ${this.expandedSql} }`; + } }