ffi/sqlite-durable.protos

Repository
guillermomolina/protos
Commit
2723a55555e7beecee0816bf99d16e3c8c1ae635
Tree
c36054e4e5abd7fc29a039b4e096cac2e8706492
Blob
116bbebd66d66cf686c13400863fa782d55233a1
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.

// Supply one database filename; this example owns/replaces only durable_demo.
// Requires the portable JVM FFM provider and explicit sqlite3 authorization.
Assertions: import("std:test/assertions")
SQLite: import("std:sqlite")
args: process.args()
Assertions.require(args.size() == 1)
path: args[0]
verify: (db) => {
    query: db.prepare("SELECT id, name, payload FROM durable_demo ORDER BY id")
    (() => {
        count: 0
        query.each((row) => {
            count = count + 1
            Assertions.require(row[0] == count)
            expected: "first"
            if(count == 2) { expected = "second" }
            Assertions.require(row[1] == expected)
            Assertions.require(Encoding.Latin1.decode(row[2]) == "\u{0}\u{FF}")
        })
        Assertions.require(count == 2)
    }).ensure(() => query.close().value())
}
db: SQLite.openFile(path)
(() => {
    db.busyTimeout(250)
    db.begin()
    create: db.prepare("CREATE TABLE IF NOT EXISTS durable_demo(id INTEGER PRIMARY KEY, name TEXT, payload BLOB)")
    (() => { Assertions.require(create.step() === false) }).ensure(() => create.close().value())
    clear: db.prepare("DELETE FROM durable_demo")
    (() => { Assertions.require(clear.step() === false) }).ensure(() => clear.close().value())
    insert: db.prepare("INSERT INTO durable_demo VALUES(?1, ?2, ?3)")
    (() => {
        insert.bind(1, 1).bind(2, "first").bind(3, Encoding.Latin1.encode("\u{0}\u{FF}"))
        Assertions.require(insert.step() === false)
        // Reset keeps the BLOB binding; only id and name change.
        insert.reset().bind(1, 2).bind(2, "second")
        Assertions.require(insert.step() === false)
    }).ensure(() => insert.close().value())
    Assertions.require(db.inTransaction())
    db.commit()
    Assertions.require(!db.inTransaction())

    db.begin()
    update: db.prepare("UPDATE durable_demo SET name=?1 WHERE id=?2")
    (() => {
        update.bind(1, "temporary").bind(2, 1)
        Assertions.require(update.step() === false)
    }).ensure(() => update.close().value())
    db.rollback()
    verify(db)
}).ensure(() => db.close().value())

reopened: SQLite.openFile(path, "readOnly")
(() => { verify(reopened) }).ensure(() => reopened.close().value())