12-system-resources/06-sqlite-query-lifecycle.protos
- Repository
- guillermomolina/protos
- Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75- Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6- Blob
0ee074baaf0203b0c02ce2c3bdd6efb6f47ad968- Source
- View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.
// DOC024: a practical std:sqlite query and lifecycle lesson. It builds on
// 05-sqlite-protos-first.protos and combines the same public operations, plus
// the LIB030 reuse operations reset() and clearBindings(), into a complete
// table/insert/query/cleanup sequence.
//
// The host must explicitly authorize the optional FFM provider, for example:
// bin/protos --foreign-provider-path <protos-ffm-provider.jar> --allow-library sqlite3=libsqlite3.so.0 <this file>
// Importing std:sqlite alone loads no native library. Behavior without the
// provider is exercised separately by protos/tests/foreign-provider/
// sqlite-missing.protos, which runs with no provider at all; this lesson
// never tries to disable the provider mid-program.
//
// Every assertion passing means the program finishes silently.
Assertions: import("std:test/assertions")
SQLite: import("std:sqlite")
// Step A: open the Connection and establish its cleanup before any SQL work.
db: SQLite.openMemory()
(() => {
// Step B: a command Statement completes on its first step: false, no row.
create: db.prepare("CREATE TABLE measurements (id INTEGER, reading REAL, optional INTEGER, label TEXT);")
(() => {
Assertions.require(create.step() === false)
}).ensure(() => create.close().value())
// Step C: bind one-based parameters and execute. A completed Statement
// stays completed; reset() is the explicit way to run it again. reset()
// keeps the bindings, clearBindings() sets every parameter to SQL NULL.
insert: db.prepare("INSERT INTO measurements VALUES (?1, ?2, ?3, ?4)")
(() => {
insert.bind(1, 1).bind(2, 1.25).bind(3, null).bind(4, "first")
Assertions.require(insert.step() === false)
Assertions.require(insert.step() === false) // not executed again
Assertions.signals(Error, () => insert.bind(1, 2)) // reset first
insert.reset().bind(1, 2).bind(2, 2.5).bind(3, 7).bind(4, "second")
Assertions.require(insert.step() === false)
insert.reset().clearBindings().bind(1, 3).bind(2, 0.75)
Assertions.require(insert.step() === false)
}).ensure(() => insert.close().value())
// Step D: each copies every row into an ordinary Array before calling the
// ordinary Protos Closure, which updates ordinary local variables.
query: db.prepare("SELECT id, reading, optional, label FROM measurements ORDER BY id")
seen: 0
total: 0.0
(() => {
query.each((row) => {
seen = seen + 1
Assertions.require(row[0] == seen)
Assertions.require(Float.recognizes(row[1]))
if(row[0] == 2) {
Assertions.require(row[2] == 7)
Assertions.require(row[3] == "second")
} else {
Assertions.require(row[2] === null)
}
if(row[0] == 3) { Assertions.require(row[3] === null) }
total = total + row[1]
})
}).ensure(() => query.close().value())
Assertions.require(seen == 3)
Assertions.require(total == 4.5)
// Step E: each error case owns a bounded Statement and its own cleanup.
// Ordinary argument errors are plain Errors; SQLite status failures are
// SQLite.SQLiteError with operation and code slots.
// One statement per prepare: a terminator is fine, a second statement is not.
terminated: db.prepare("SELECT ';'; -- a comment after the terminator")
(() => {
Assertions.require(terminated.step())
Assertions.require(terminated.column(0) == ";")
}).ensure(() => terminated.close().value())
Assertions.signals(Error, () => db.prepare("SELECT 1; SELECT 2"))
Assertions.signals(Error, () => db.prepare("SELECT 1\u{0}"))
syntax: Assertions.signals(SQLite.SQLiteError, () => db.prepare("SELECT FROM"))
Assertions.require(syntax.operation == "prepare")
parameters: db.prepare("SELECT ?1")
(() => {
Assertions.signals(Error, () => parameters.bind(0, 1))
// SQLite itself rejects a position beyond the declared parameters.
range: Assertions.signals(SQLite.SQLiteError, () => parameters.bind(2, 1))
Assertions.require(range.operation == "bind")
Assertions.require(Integer.recognizes(range.code))
// No implicit coercion: Fractions and Booleans are not bindable.
Assertions.signals(Error, () => parameters.bind(1, 1 / 2))
Assertions.signals(Error, () => parameters.bind(1, true))
}).ensure(() => parameters.close().value())
rows: db.prepare("SELECT 1")
(() => {
Assertions.signals(Error, () => rows.row())
Assertions.require(rows.step())
Assertions.require(rows.column(0) == 1)
Assertions.require(rows.step() === false)
Assertions.signals(Error, () => rows.row())
Assertions.signals(Error, () => rows.column(0))
}).ensure(() => rows.close().value())
// Step F: close is a permanent cutover. Every close answers a fresh
// terminal Future that observes the same recorded outcome.
retired: db.prepare("SELECT 2")
(() => {
first: retired.close()
Assertions.require(first.value() === retired)
again: retired.close()
Assertions.require(again !== first)
Assertions.require(again.value() === retired)
Assertions.signals(IOLifecycleError, () => retired.step())
Assertions.signals(IOLifecycleError, () => retired.bind(1, 1))
Assertions.signals(IOLifecycleError, () => retired.reset())
Assertions.signals(IOLifecycleError, () => retired.clearBindings())
}).ensure(() => retired.close().value())
}).ensure(() => db.close().value())
// Step F, continued: a fresh Connection whose close retires a still-live
// Statement. Statement cleanup remains in place and is harmless afterwards.
other: SQLite.openMemory()
(() => {
child: other.prepare("SELECT 5")
(() => {
Assertions.require(child.step())
Assertions.require(other.close().value() === other)
Assertions.signals(IOLifecycleError, () => child.column(0))
Assertions.signals(IOLifecycleError, () => other.prepare("SELECT 6"))
Assertions.require(child.close().value() === child)
}).ensure(() => child.close().value())
}).ensure(() => other.close().value())
// Continue with 07-sqlite-durable-transactions.protos for persistent transactions.