SQLite integration

Argentum got SQLite integration:

using sys { log }
using sqliteFfi { Sqlite }
using string;

Sqlite.open("../demo/mydb.sqlite", 2) ? _.query("
   SELECT "id", "name", "avatar"
   FROM "table"
   LIMIT 0, 20
", 0).execute `row log("{}\\
   id={row.intAt(0)}
   name={row.stringAt(1)}
   avatar={row.stringAt(2)}
")

Build & run:

# Compile
./agc -src ../demo -start sqliteDemo -o demo.o

# Link
gcc -no-pie demo.o libag_runtime.a libag_sqlite.a -o demo

#launch the app
./demo

It prints (depending on the actual content of the database):

id=1
name=Andrey
avatar=42

id=2
name=Katya
avatar=11

This code:

  1. Opens mydb.sqlite database in read-write mode.
  2. Checks for success with ?-operator
  3. Creates a query.
  4. Executes this query with a lambda `row ... called on each row.
  5. For each row it extracts data of different types from the row fields.
  6. Frees the query and drops connection to the database.

Other supported features not shown in this demo:

  • Connection can be stored and reused for multiple queries.
  • Queries can have parameters of int, string, blob types.
  • Queries can be stored and executed multiple times, possibly with different parameters.
  • Queries and connections can be created on different threads and result->object mapping can be performed on one thread with passing the resulting objects to the business logic threads.
  • The sqliteFfi is a low level API, you can build any query execution strategies atop of it.

Limitations:

  • Current version of ag_sqlite library is built from the amalgamized C-sources of SQLite that reside in third_party directory right in the argentum project, if newer/different version is needed, replace it there.
  • SQLite lib links to your application as static lib, effectively increasing its size by 1Mb, you can always change the build script to make it so/DLL.

Leave a Reply

Your email address will not be published. Required fields are marked *