Skip to main content
Go to Latest
class PreparedQuery
import { PreparedQuery } from "https://deno.land/x/sqlite@v3.5.0/mod.ts";

A prepared query which can be executed many times.

Constructors

new
PreparedQuery(
wasm: Wasm,
stmt: StatementPtr,
openStatements: Set<StatementPtr>,
)

This constructor should never be used directly. Instead a prepared query can be obtained by calling DB.prepareQuery.

Type Parameters

optional
R extends Row = Row
optional
O extends RowObject = RowObject

Properties

private
_finalized: boolean
private
_iterKv: boolean
private
_openStatements: Set<StatementPtr>
private
optional
_rowKeys: Array<string>
private
_status: number
private
_stmt: StatementPtr
private
_wasm: Wasm

Methods

private
getQueryRow(): R
private
makeRowObject(row: Row): O
private
startQuery(params?: P)
all(params?: P): Array<R>

Binds the given parameters to the query and returns an array containing all resulting rows.

Examples

const query = db.prepareQuery<[number, string]>("SELECT id, name FROM people");
const rows = query.all();
// [[1, "Peter"], ...]

To avoid SQL injection, user-provided values should always be passed to the database through a query parameter.

const query = db.prepareQuery("SELECT id FROM people WHERE name = ?");
preparedQuery.all([name]);
const query = db.prepareQuery("SELECT id FROM people WHERE name = :name");
preparedQuery.all({ name });

See QueryParameterSet for documentation on how values can be bound to SQL statements.

See QueryParameter for documentation on how values are returned from the database.

allEntries(params?: P): Array<O>

Like all except each row is returned as an object containing key-value pairs.

Examples

const query = db.prepareQuery<_, { id: number, name: string }>("SELECT id, name FROM people");
const rows = query.all();
// [{ id: 1, name: "Peter" }, ...]

Returns the column names for the query results.

This method returns an array of objects, where each object has the following properties:

Property Value
name the result of sqlite3_column_name
originName the result of sqlite3_column_origin_name
tableName the result of sqlite3_column_table_name
execute(params?: P)

Binds the given parameters to the query and executes the query, ignoring any rows which might be returned.

Using this method is more efficient when the rows returned by a query are not needed or the query does not return any rows.

Examples

const query = db.prepareQuery<_, _, [string]>("INSERT INTO people (name) VALUES (?)");
query.execute(["Peter"]);
const query = db.prepareQuery<_, _, { name: string }>("INSERT INTO people (name) VALUES (:name)");
query.execute({ name: "Peter" });

See QueryParameterSet for documentation on how values can be bound to SQL statements.

See QueryParameter for documentation on how values are returned from the database.

Closes the prepared query. This must be called once the query is no longer needed to avoid leaking resources.

After a prepared query has been finalized, calls to iter, all, first, execute, or columns will fail.

Using iterators which were previously returned from the finalized query will fail.

finalize may safely be called multiple times.

first(params?: P): R | undefined

Binds the given parameters to the query and returns the first resulting row or undefined when there are no rows returned by the query.

Examples

const query = db.prepareQuery<[number, string]>("SELECT id, name FROM people");
const person = query.first();
// [1, "Peter"]
const query = db.prepareQuery("SELECT id, name FROM people WHERE name = ?");
const person = query.first(["not a name"]);
// undefined

To avoid SQL injection, user-provided values should always be passed to the database through a query parameter.

const query = db.prepareQuery("SELECT id FROM people WHERE name = ?");
preparedQuery.first([name]);
const query = db.prepareQuery("SELECT id FROM people WHERE name = :name");
preparedQuery.first({ name });

See QueryParameterSet for documentation on how values can be bound to SQL statements.

See QueryParameter for documentation on how values are returned from the database.

firstEntry(params?: P): O | undefined

Like first except the row is returned as an object containing key-value pairs.

Examples

const query = db.prepareQuery<_, { id: number, name: string }>("SELECT id, name FROM people");
const person = query.first();
// { id: 1, name: "Peter" }
iter(params?: P): RowsIterator<R>

Binds the given parameters to the query and returns an iterator over rows.

Using an iterator avoids loading all returned rows into memory and hence allows to process a large number of rows.

Calling iter, all, or first invalidates any iterators previously returned from this prepared query.

Examples

const query = db.prepareQuery<[number, string]>("SELECT id, name FROM people");
for (const [id, name] of query.iter()) {
  // ...
}

To avoid SQL injection, user-provided values should always be passed to the database through a query parameter.

const query = db.prepareQuery("SELECT id FROM people WHERE name = ?");
preparedQuery.iter([name]);
const query = db.prepareQuery("SELECT id FROM people WHERE name = :name");
preparedQuery.iter({ name });

See QueryParameterSet for documentation on how values can be bound to SQL statements.

See QueryParameter for documentation on how values are returned from the database.

iterEntries(params?: P): RowsIterator<O>

Like iter except each row is returned as an object containing key-value pairs.

Examples

const query = db.prepareQuery<_, { id: number, name: string }>("SELECT id, name FROM people");
for (const { id, name } of query.iter()) {
  // ...
}
next(): IteratorResult<R | O>
one(params?: P): R

Deprecated: prefer first.

oneEntry(params?: P): O

Deprecated: prefer firstEntry.

[Symbol.iterator](): RowsIterator<R | O>