Database

Database classes provide persistence for Viper C++ documents using SQLite or remote connections.

When to use: Use Database for simple key-value persistence without history tracking. For the mutation DAG, see Commit Database.

Quick Start

>>> from dsviper import *
>>> MODEL = """
... namespace MyApp {8f14e45f-ceea-467a-9575-1c14b48f0b7e} {
...     concept User;
...     struct Profile { string city; uint16 age; };
...     attachment<User, Profile> profile;
... };
... """
>>> builder = DSMBuilder()
>>> builder.append("model.dsm", MODEL)
>>> report, dsm_defs, defs = builder.parse()
>>> report.has_error()
False
>>> defs.inject(globals())             # MY_APP_T_USER, MY_APP_A_USER_PROFILE, …
>>> key = ValueKey.create(MY_APP_T_USER, "0d2f0e1a-1111-4222-8333-444455556666")
>>> document = Value.create(MY_APP_S_PROFILE, {"city": "Paris", "age": 30})

A Database holds one state, with no history. Writes go in a transaction:

>>> db = Database.create_in_memory()
>>> db.extend_definitions(defs).count()
3
>>> db.begin_transaction()
>>> db.set(MY_APP_A_USER_PROFILE, key, document)
True
>>> db.commit()

get always returns a ValueOptional — never None. Ask is_nil() whether it holds anything, and unwrap() for the document:

>>> result = db.get(MY_APP_A_USER_PROFILE, key)
>>> result.is_nil()
False
>>> Value.dumps(result.unwrap())
{'city': 'Paris', 'age': 30}
>>> db.begin_transaction()
>>> db.delete(MY_APP_A_USER_PROFILE, key)
True
>>> db.commit()
>>> db.get(MY_APP_A_USER_PROFILE, key).is_nil()
True

See also

For detailed examples with concrete attachments, see Database.

Choosing the Right Class

Use Case

Class

Note

Simple CRUD, no history

Database

Creates .vdb files

Need version history

CommitDatabase

See Commit Database

Remote database access

DatabaseRemote

Client-server mode

See also

To copy a Database, or move content to and from a CommitDatabase, see Database Transfer.

Core Classes

dsviper.Database

A Database is a CRUD like transactional database.

dsviper.DatabaseSQLite

A low-level class used to represent a CRUD like database based on SQLite3 through the Databasing interface.

dsviper.DatabaseRemote

Accesses a CRUD like database on a remote repository, through the Databasing interface.

dsviper.Databasing

Abstracts the persistence layer behind a CRUD like database.

Low-Level

dsviper.SQLite

The settings and limits of one database file and of its SQLite3 build.