Database store example

Demonstration how to store and fetch data from permanent database, and how to use tied pointer with that.

Compilation

Easiest way to get this example running is to go to examples directory in package root, and run:

$ ./build.sh db_store

build.sh script compiles Ano script to C, copies source files in place and pops up instructions what to do next. Follow them. Check also examples/README for more info.

Preview

; ------------------------------------------------------------------------------
; db_store.ano
;
; To compile:
;
; $ ./build/ano ./examples/db_store/db_store.ano > \
;     engine/dsl_ano.h
; ------------------------------------------------------------------------------
;
@ANO_SCRIPT_NAME		db_store
@ANO_SCRIPT_VERSION		0.0.2
@ANO_SCRIPT_DESCRIPTION	Simple database put/get data handler
;
@ANO_FLAGS_USE_PROTOS		[ ]
@ANO_FLAGS_VAR_NAME_SUBS	[ ]
@ANO_FLAGS_VAR_WARN_UNUSED	[ ]
;
; Copyright © 2016-2026, Jani Salonen <salojan@goto10.co>
; All rights reserved.
;
; Demonstration how to store and fetch data from permanent database, and how
; to use tied pointer with that.
;

main [exit: 0] {
	; You may want to delete .detroit-engine.db file from your home
	; directory after running this example.
	;

	; This is static storage for database data.
	;
	mov storage ("This is enough room for data returned from database.")

	; Tie pointer to storage variable which type is string, this is
	; traditional way to tie variables…
	;
	;mov my_ptr (*0)
	;
	;tie (\
	;	through: "my_ptr",
	;	to: "storage")
	;
	; …and this is preferred, simpler approach to use asterisk in front
	;    of destination variable:
	;
	my_ptr *(storage)

	; Open unencrypted database.
	;
	_hnd = store_open(\
		scope: STORE_SCOPE_LOCAL, \
		db_type: STORE_TYPE_BTREE, \
		file_mode: 0, \
		password: NULL)

	; Save string to database…
	;
	_hnd.put(\
		key: "my_db_key", \
		data: "ABCDE", \
		data_size: 5)

	; …and get it back, it is saved to storage variable which is binded
	;    to my_ptr.
	;
	_hnd.get(\
		key: "my_db_key", \
		data: "my_ptr", \
		data_size: 5)

	; This is identical to above, but data size is not limited…
	;
	;_hnd.get(key: "my_db_key", data: "my_ptr", data_size: 0)

	; …this is also identical, but uses direct variable instead of tied
	;    pointer…
	;_hnd.get(key: "my_db_key", data: "storage", data_size: 5)

	; …and this is identical too, it uses direct variable and no limit
	;    in its size.
	;
	;_hnd.get(key: "my_db_key", data: "storage", data_size: 0)

	; Close database and print what was fetched.
	;
	_hnd.done()

	print "Data is: " . my_ptr . "\n"
}
; ------------------------------------------------------------------------------
; db_store.w2c
;
; To compile:
;
; $ ./build/widget ./examples/db_store/db_store.w2c > \
;     engine/widget_defs.h
; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------
; db_store.m2c
;
; To compile:
;
; $ ./build/menu ./examples/db_store/db_store.m2c > \
;     engine/menu_defs.h
; ------------------------------------------------------------------------------

Copyright © 2026, Jani Salonen <salojan at goto10 piste co>. Piste is finnish word and means dot. All rights reserved.