Lib sqlite
Console program88 lines of .st193 lines of generated C
name=apple n=1 (2 columns) name=fig n=2 (2 columns) name=pear n=3 (2 columns) error 1: no such column: nope
On this page
The .st files#
intent.st
structop {
type_app {
exec = {
lang = "C"
standard = "c11"
}
}
lib_provider {
C_lang = {
source = "StructOp/c"
version = "@latest"
}
Db = {
source = "C/sqlite3"
version = "@latest"
}
}
}main.st
# SQLite through the provider structop bind writes: open a database in memory,
# fill a table, list its rows through a callback, and report a failing query.
ability "value" "db" {
type = Sqlite3
}
ability "value" "rc" {
type = Int32
}
data "value" "rc" {
of = ability.value.rc
}
ability "value" "err" {
type = String
size = 128
}
data "value" "err" {
of = ability.value.err
}
ability "sqlite3_open" "Open" {
filename = ":memory:"
target = ability.value.db
result = ability.value.rc
}
ability "sqlite3_exec" "Fill" {
db = ability.value.db
sql = "CREATE TABLE fruit(name TEXT, n INT); INSERT INTO fruit VALUES ('pear', 3), ('apple', 1), ('fig', 2);"
result = ability.value.rc
}
ability "sqlite3_exec" "List" {
db = ability.value.db
sql = "SELECT name, n FROM fruit ORDER BY n"
callback = [ability.print.Row]
}
data "sqlite3_exec_callback" "Row" {
of = ability.sqlite3_exec.List
}
ability "print" "Row" {
message = "${data.sqlite3_exec_callback.Row.columns[0]}=${data.sqlite3_exec_callback.Row.values[0]} ${data.sqlite3_exec_callback.Row.columns[1]}=${data.sqlite3_exec_callback.Row.values[1]} (${data.sqlite3_exec_callback.Row.count} columns)"
}
ability "sqlite3_exec" "Broken" {
db = ability.value.db
sql = "SELECT nope FROM fruit"
errmsg = ability.value.err
result = ability.value.rc
}
ability "if" "Failed" {
condition = data.value.rc.current == const.SQLITE_ERROR
then = [ability.print.Error]
}
ability "print" "Error" {
message = "error ${data.value.rc.current}: ${data.value.err.current}"
}
ability "sqlite3_close" "Close" {
db = ability.value.db
}Input, plan and generated C#
bind/sqlite3.bind.st · hints for structop bind
bind/sqlite3.bind.st · hints for structop bind
bind "sqlite3" {
header = "sqlite3.h"
package = "sqlite3"
prefix = ["sqlite3_", "SQLITE_"]
}
function "sqlite3_exec" {
names = ["db"]
out_text = { errmsg = "sqlite3_free" }
arg_names = { callback = ["", "count", "values", "columns"] }
}
function "sqlite3_close" {
names = ["db"]
}structop plan
structop plan
# program.app will be created
+ program.app (.structop/build/app)
+ ability.value.db
+ ability.value.rc
+ ability.value.err
+ ability.sqlite3_open.Open
+ ability.sqlite3_exec.Fill
+ ability.sqlite3_exec.List
+ ability.print.Row
+ ability.sqlite3_exec.Broken
+ ability.if.Failed
+ ability.print.Error
+ ability.sqlite3_close.Close
Plan: 1 to add, 0 to change, 0 to destroy.Generated main.c
To inspect this generated file, select C in intent.st, initialize the project and export its source.
Export locally
structop init
structop apply -source -no-runTry it
structop bind bind/sqlite3.bind.st # from the repository root
cd lib-sqlite # a folder holding the .st files above
structop init
structop plan
structop apply