Entity Framework reference
Every method of RingDatabase and RingEntityDb, the schema format, the query specification, hooks and limits.
Ring
aSchema = [
[ "TableName",
[ [ "Column", "type", isKey, [ ["option", value], ... ] ], ... ],
[ ["tableOption", value], ... ] # optional
],
...
]
| Part | Values |
|---|
| column type | int, long, double, decimal, string (or text), bool, datetime, bytes |
isKey | 1 for a key column; several give a composite key. A single int/long key is generated by the database |
| column options | required, nullable, unique, index, maxlength, default, references, ondelete, convert |
| table options | ["filter", condition] — a global query filter |
Creating a database#
Ring
oDb = new RingDatabase(connection, aSchema) # rows as [name, value] pairs
oDb = new RingEntityDb(connection, aSchema) # real entity objects
The connection starts with the provider name (inmemory:, sqlserver:, postgres:, mysql:) or is a SQLite connection string.
RingDatabase#
Rows#
| Method | Returns |
|---|
insert(table, values) | the key (a list for a composite key). values is [["Name","Ann"],["Age",31]] |
byKey(table, key) | the row, or "". The row is tracked: after raw SQL, call discard() to read the new values |
all(table) | all rows |
where(table, condition, params) | rows matching a SQL condition with {0} parameters (SQLite) |
update(table, key, values) | 1 when the row exists, else 0 |
remove(table, key) | 1 when the row existed, else 0 |
count(table) | the number of rows |
dbValue(row, column) | a column of a row ("" for null) |
Queries#
| Method | Does |
|---|
query(table, spec) | rows matching a query specification |
countWhere(table, cond) | number of rows that match ([] for all) |
sum, min, max, average(table, column, cond) | aggregates |
groupCount(table, column, cond) | [[value, count], ...] |
sqlRows(sql, params) | rows of any SELECT as [column, value] lists |
execute(sql, params) | runs a statement, returns the rows affected |
scalar(sql) | the first value of the first row |
Relationships#
| Method | Returns |
|---|
children(parentTable, key, childTable) | the rows of the child table that belong to the parent |
parent(childTable, key, parentTable) | the parent row, or "" |
include(table, relatedTable) | [[row, children-or-parent], ...] in two queries |
Transactions and saving#
| Method | Does |
|---|
begin(), commit(), rollback() | a transaction; an error in commit() undoes everything |
inTransaction() | true while a transaction is active |
autoSave(flag), save() | batch changes; pendingChanges() lists them |
discard() | forget changes that were not saved |
insertAsync, allAsync, whereAsync, countAsync, saveAsync | return a Task; dbWait(task) returns the result |
Schema and migrations#
| Method | Does |
|---|
migrate() | creates missing tables, columns and indexes; returns the list of changes (SQLite) |
apply(name, sql) | runs a SQL change once; 1 when applied now, 0 when applied before |
applied() | names of the applied changes |
columnInfo(table, column) | [["name",..],["type",..],["key",..],["nullable",..],["required",..],["unique",..],["index",..],["maxlength",..]] |
Hooks#
| Method | Does |
|---|
onSaving(dnCallback("f")) | f(aArgs) runs before changes are written; aArgs[1] is [[table, state, row], ...] (state: Added, Modified, Deleted). Return a text to cancel the save. |
onSaved(dnCallback("f")) | aArgs[1] is the number of rows written |
onLog(dnCallback("f")) | aArgs[1] is the text of each SQL command |
pendingChanges() | changes waiting to be saved: [[table, state, row], ...] |
Other#
| Method | Does |
|---|
context() | the real EF DbContext (a wrapped object) |
close() | dispose and release the database file |
RingEntityDb#
| Method | Does |
|---|
newEntity(table) | a new object of the generated class |
entityType(table) | the .NET Type |
properties(table) | the property names: columns first, then navigations (Author, Books) |
add(entity) | adds the entity (and the entities it refers to); returns the key |
remove(entity) | removes it |
find(table, key) | the tracked entity, or "" |
query(table, spec) | tracked entities; the specification also accepts ["include",["Author","Other.Nested"]] |
all(table), count(table, cond) | all entities, number of entities |
getValue(entity, property), setValue(entity, property, value) | read or write a property (dates are texts, bytes are hex) |
toRow(entity), fill(entity, values) | columns as [name, value] pairs |
save(), autoSave(flag), discard() | saving |
begin(), commit(), rollback() | transactions |
context(), close() | the DbContext, release |
Entities are plain DotNetObjects: oBook.getProp("Title"), oBook.setProp("Author", oAuthor); a collection navigation has getProp("Count") and toList().
Query specification#
A query is a list of [item, value] pairs:
| Item | Value |
|---|
where | a condition |
orderby | [[column, "asc"/"desc"], ...] |
skip, take | numbers |
select | [column, ...] — keeps only those columns (RingDatabase) |
include | [navigation, ...] (RingEntityDb) |
ignorefilters | 1 to ignore the table's global filter |
A condition is [column, op, value] or a combination:
| Form | Meaning |
|---|
["Age", ">", 30] | comparison: = != < <= > >= |
["Name", "contains", "an"] | contains, startswith, endswith (text) |
["City", "in", ["Cairo","Giza"]] | membership |
["Boss", "isnull"], ["Boss", "notnull"] | null checks |
["and", c1, c2, ...], ["or", c1, c2, ...], ["not", c] | combinations |
Providers and capabilities#
| Capability | SQLite | In-memory | SQL Server / PostgreSQL / MySQL |
|---|
| tables, rows, relations, queries (expression trees), aggregates | ✔ | ✔ | untested |
where / execute / scalar / sqlRows with SQL | ✔ | — | untested |
migrate / apply | ✔ | — | — |
| transactions | ✔ | ✔ | untested |
typed entities (RingEntityDb) | ✔ | ✔ | untested |
What is not supported#
- Classes written in C# source — entity classes are generated from the schema.
- Lambdas written by hand — queries are data.
- Migrations that alter or drop columns (use
execute or apply). - Hooks, converters and
migrate() in RingEntityDb. - The SQL Server, PostgreSQL and MySQL providers have not been tested.
Where to go next#