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

A prepared query which can be executed many times.

Constructors

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

A prepared query which can be executed many times.

The 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.

Calling all invalidates any iterators previously returned by calls to iter. Using an invalidated iterator is a bug.

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

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.

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.

Calling execute invalidates any iterators previously returned by calls to iter. Using an invalidated iterator is a bug.

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

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

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, trying to call iter, all, one, execute, or columns, or using iterators which where previously obtained from the finalized query is a bug.

finalize may safely be called multiple times.

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.

Example:

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

Calling iter invalidates any iterators previously returned from this prepared query. Using an invalidated iterator is a bug.

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

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.

next(): IteratorResult<R | O>
one(params?: P): R

Binds the given parameters to the query and returns exactly one row.

If the query does not return exactly one row, this throws an error.

Calling one invalidates any iterators previously returned by calls to iter. Using an invalidated iterator is a bug.

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

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.

oneEntry(params?: P): O

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

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