Skip to content

Commit

Permalink
feat: implement Symbol.dispose for Statement to support using state…
Browse files Browse the repository at this point in the history
…ments
  • Loading branch information
DjDeveloperr committed Apr 5, 2024
1 parent af6d39e commit 0efd04f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
21 changes: 21 additions & 0 deletions doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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} }`;
}
}

0 comments on commit 0efd04f

Please sign in to comment.