ffi/sqlite-prepared-numeric.protos
- Repository
- guillermomolina/protos
- Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75- Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6- Blob
9f4d873e95483d04cad012c41732a63ed4dff688- 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 cookbook: query and filter numeric records with prepared parameters.
// Host: --foreign-provider-path <protos-ffm-provider.jar> --allow-library sqlite3=libsqlite3.so.0; Linux x86-64 JVM/JDK25.
// Numeric binds and one INSERT reused through reset()/clearBindings() (LIB030).
Assertions: import("std:test/assertions")
SQLite: import("std:sqlite")
db: SQLite.openMemory()
(() => {
create: db.prepare("CREATE TABLE readings (id INTEGER, value REAL, flag INTEGER)")
(() => { Assertions.require(create.step() === false) }).ensure(() => create.close().value())
// One prepared INSERT is reused. The first execution binds and steps;
// every later one resets, rebinds and steps.
insert: db.prepare("INSERT INTO readings VALUES (?1, ?2, ?3);")
(() => {
insert.bind(1, 4).bind(2, 0.5).bind(3, 20)
Assertions.require(insert.step() === false)
for(reading in Array(Array(2, 2.5, 10), Array(5, 4.0, 30))) {
insert.reset().bind(1, reading[0]).bind(2, reading[1]).bind(3, reading[2])
Assertions.require(insert.step() === false)
}
// clearBindings() here deliberately turns the unbound flag into SQL NULL.
for(reading in Array(Array(1, 1.5), Array(3, 3.25))) {
insert.reset().clearBindings().bind(1, reading[0]).bind(2, reading[1])
Assertions.require(insert.step() === false)
}
}).ensure(() => insert.close().value())
// A numeric predicate; ORDER BY makes the traversal deterministic.
query: db.prepare("SELECT id, value, flag FROM readings WHERE value >= ?1 AND id <= ?2 ORDER BY id")
expected: Array(2, 3, 5)
seen: 0
total: 0.0
(() => {
query.bind(1, 2.0).bind(2, 5)
query.each((row) => {
Assertions.require(Float.recognizes(row[1]))
Assertions.require(row[0] == expected[seen])
seen = seen + 1
total = total + row[1]
if(row[0] == 3) { Assertions.require(row[2] === null) }
if(row[0] == 5) { Assertions.require(row[2] == 30) }
})
}).ensure(() => query.close().value())
Assertions.require(seen == 3)
Assertions.require(total == 9.75)
// Single-row aggregate read with step and column.
missing: db.prepare("SELECT count(*) FROM readings WHERE flag IS NULL")
(() => {
Assertions.require(missing.step())
Assertions.require(missing.column(0) == 2)
Assertions.require(missing.step() === false)
}).ensure(() => missing.close().value())
}).ensure(() => db.close().value())