Embedding the SQL engine in your application
immudb provides you a immutable embedded SQL engine which keeps all history, is tamper-proof and can travel in time.
immudb already provides an embeddable key-value store in the embedded (opens new window) package.
The SQL engine is mounted on top of the embedded key value store, and requires two stores to be initialized, one for the data, and one for the catalog (schema).
Create stores for the data and catalog:
if err != nil {
log.Fatal(err)
}
if err != nil {
}
And now you can create the SQL engine, passing both stores and a key prefix:
Create and use a database:
_, err = engine.ExecStmt("CREATE DATABASE db1")
if err != nil {
}
if err != nil {
log.Fatal(err)
Queries can be executed using QueryStmt
and you can pass a map of parameters to substitute, and whether the engine should wait for indexing:
r, err = engine.QueryStmt("SELECT id, date, creditaccount, debitaccount, amount, description FROM journal WHERE amount > @value", map[string]interface{}{"value": 100}, true)
To iterate over a result set r
, just fetch rows until there are no more entries. Every row has a member you can index to access the column:
And that is all you need. If you need to change options like where things get stored by default, you can do that in the underlying store objects that the SQL engine is using.