cquill/adapter/memory
Types
Configuration for batch operations
pub type BatchConfig {
BatchConfig(max_batch_size: Int, use_transaction: Bool)
}
Constructors
-
BatchConfig(max_batch_size: Int, use_transaction: Bool)Arguments
- max_batch_size
-
Maximum records per batch (for chunking large inserts)
- use_transaction
-
Whether to wrap in transaction (for atomicity)
Foreign key constraint definition
pub type ForeignKeyConstraint {
ForeignKeyConstraint(
column_index: Int,
referenced_table: String,
referenced_column: String,
)
}
Constructors
-
ForeignKeyConstraint( column_index: Int, referenced_table: String, referenced_column: String, )
Row type for memory adapter (list of dynamic values)
pub type MemoryRow =
List(dynamic.Dynamic)
The in-memory database connection/store
pub type MemoryStore {
MemoryStore(
tables: dict.Dict(String, MemoryTable),
in_transaction: Bool,
snapshots: List(Snapshot),
snapshot: option.Option(Snapshot),
)
}
Constructors
-
MemoryStore( tables: dict.Dict(String, MemoryTable), in_transaction: Bool, snapshots: List(Snapshot), snapshot: option.Option(Snapshot), )Arguments
- tables
-
Tables indexed by name
- in_transaction
-
Whether we’re in a transaction
- snapshots
-
Stack of snapshots for nested transactions (most recent first)
- snapshot
-
Legacy single snapshot field for backwards compatibility
A table in the memory store with full schema metadata for constraint checking
pub type MemoryTable {
MemoryTable(
name: String,
primary_key: List(String),
columns: List(String),
rows: dict.Dict(String, List(dynamic.Dynamic)),
next_id: Int,
unique_constraints: dict.Dict(String, List(Int)),
not_null_columns: List(Int),
foreign_keys: List(ForeignKeyConstraint),
)
}
Constructors
-
MemoryTable( name: String, primary_key: List(String), columns: List(String), rows: dict.Dict(String, List(dynamic.Dynamic)), next_id: Int, unique_constraints: dict.Dict(String, List(Int)), not_null_columns: List(Int), foreign_keys: List(ForeignKeyConstraint), )Arguments
- name
-
Table name
- primary_key
-
Primary key column name(s)
- columns
-
Column names in order (for mapping row indices to field names)
- rows
-
Rows indexed by primary key (as string)
- next_id
-
Auto-increment counter for serial IDs
- unique_constraints
-
Unique constraints: maps constraint name to column indices
- not_null_columns
-
Not-null column indices
- foreign_keys
-
Foreign key constraints: (column_index, referenced_table, referenced_column)
Snapshot of store state for transaction rollback or savepoint
pub type Snapshot {
Snapshot(
name: option.Option(String),
tables: dict.Dict(String, MemoryTable),
id_counters: dict.Dict(String, Int),
)
}
Constructors
-
Snapshot( name: option.Option(String), tables: dict.Dict(String, MemoryTable), id_counters: dict.Dict(String, Int), )Arguments
- name
-
Optional name for savepoints (None for transaction snapshots)
- tables
-
Tables at snapshot time
- id_counters
-
Auto-increment counters at snapshot time
Values
pub fn add_unique_constraint(
store: MemoryStore,
table_name: String,
constraint_name: String,
column_names: List(String),
) -> Result(MemoryStore, error.AdapterError)
Add a unique constraint to a table
pub fn commit_and_continue(
store: MemoryStore,
) -> Result(MemoryStore, error.AdapterError)
Get the store state after committing a transaction This is needed for the memory adapter since commit_transaction returns Nil
pub fn create_savepoint(
store: MemoryStore,
name: String,
) -> Result(MemoryStore, error.SavepointError(Nil))
Create a savepoint with the given name. Requires an active transaction.
pub fn create_table(
store: MemoryStore,
name: String,
primary_key: String,
) -> MemoryStore
Create a table in the store (simple version with just primary key)
pub fn create_table_from_schema(
store: MemoryStore,
schema: schema.Schema,
) -> MemoryStore
Create a table from a schema with full constraint support
pub fn delete_all(
store: MemoryStore,
table_name: String,
predicate: fn(String, List(dynamic.Dynamic)) -> Bool,
) -> Result(#(MemoryStore, Int), error.AdapterError)
Delete all rows matching a predicate. Returns the number of deleted rows.
pub fn delete_all_rows(
store: MemoryStore,
table_name: String,
) -> Result(#(MemoryStore, Int), error.AdapterError)
Delete all rows in a table (truncate). Returns the number of deleted rows.
pub fn delete_row(
store: MemoryStore,
table_name: String,
key: String,
) -> Result(MemoryStore, error.AdapterError)
Delete a row by primary key
pub fn execute_savepoint(
store: MemoryStore,
name: String,
operation: fn(MemoryStore) -> Result(
#(MemoryStore, a),
error.AdapterError,
),
) -> Result(#(MemoryStore, a), error.SavepointError(Nil))
Execute a function within a savepoint. If the function returns an error, rolls back to the savepoint. If successful, releases the savepoint.
pub fn execute_transaction(
store: MemoryStore,
operation: fn(MemoryStore) -> Result(
#(MemoryStore, a),
error.AdapterError,
),
) -> Result(#(MemoryStore, a), error.TransactionError(Nil))
Execute a function within a transaction on the memory store. Automatically commits on success and rolls back on error. Returns the updated store along with the result.
pub fn get_all_rows(
store: MemoryStore,
table_name: String,
) -> Result(List(List(dynamic.Dynamic)), error.AdapterError)
Get all rows from a table
pub fn get_row(
store: MemoryStore,
table_name: String,
key: String,
) -> Result(List(dynamic.Dynamic), error.AdapterError)
Get a row by primary key
pub fn has_savepoint(store: MemoryStore, name: String) -> Bool
Check if a savepoint with the given name exists
pub fn in_transaction(store: MemoryStore) -> Bool
Check if the store is currently in a transaction
pub fn insert_all(
store: MemoryStore,
table_name: String,
rows: List(#(String, List(dynamic.Dynamic))),
) -> Result(#(MemoryStore, Int), error.AdapterError)
Insert multiple rows atomically. All rows are inserted or none are (atomic behavior). Returns the number of inserted rows on success.
pub fn insert_all_with_auto_keys(
store: MemoryStore,
table_name: String,
rows: List(List(dynamic.Dynamic)),
) -> Result(#(MemoryStore, List(String), Int), error.AdapterError)
Insert multiple rows using auto-generated keys. Returns the inserted keys and updated store.
pub fn insert_all_with_config(
store: MemoryStore,
table_name: String,
rows: List(#(String, List(dynamic.Dynamic))),
config: BatchConfig,
) -> Result(#(MemoryStore, Int), error.AdapterError)
Insert multiple rows with configuration options. When use_transaction is True, all rows are inserted atomically. Returns the number of inserted rows on success.
pub fn insert_row(
store: MemoryStore,
table_name: String,
key: String,
row: List(dynamic.Dynamic),
) -> Result(MemoryStore, error.AdapterError)
Insert a row into a table with full constraint checking
pub fn memory_adapter() -> adapter.Adapter(
MemoryStore,
List(dynamic.Dynamic),
)
Create the memory adapter.
Note: In a real implementation, the connection type would be a reference to mutable state (e.g., an Actor). This simplified version passes the store directly for demonstration purposes.
pub fn memory_capabilities() -> adapter.AdapterCapabilities
Memory adapter capabilities
pub fn next_id(
store: MemoryStore,
table_name: String,
) -> Result(Int, error.AdapterError)
Get the next auto-increment ID for a table
pub fn release_savepoint(
store: MemoryStore,
name: String,
) -> Result(MemoryStore, error.SavepointError(Nil))
Release a savepoint without rolling back. This removes the savepoint, making its changes permanent (within the transaction).
pub fn reset(store: MemoryStore) -> MemoryStore
Reset the store, clearing all data but preserving table structure
pub fn rollback_and_restore(
store: MemoryStore,
) -> Result(MemoryStore, error.AdapterError)
Get the restored store after a rollback This is a helper for tests since rollback_transaction returns Nil
pub fn rollback_to_savepoint(
store: MemoryStore,
name: String,
) -> Result(MemoryStore, error.SavepointError(Nil))
Rollback to a named savepoint, discarding all changes since it was created. Also removes the savepoint and any savepoints created after it.
pub fn row_count(
store: MemoryStore,
table_name: String,
) -> Result(Int, error.AdapterError)
Get the row count for a table
pub fn savepoint_names(store: MemoryStore) -> List(String)
Get the names of all active savepoints
pub fn transaction_depth(store: MemoryStore) -> Int
Get the current transaction depth (0 = not in transaction)
pub fn update_all(
store: MemoryStore,
table_name: String,
predicate: fn(String, List(dynamic.Dynamic)) -> Bool,
updater: fn(List(dynamic.Dynamic)) -> List(dynamic.Dynamic),
) -> Result(#(MemoryStore, Int), error.AdapterError)
Update all rows matching a predicate. Returns the number of updated rows.
pub fn update_all_rows(
store: MemoryStore,
table_name: String,
updater: fn(List(dynamic.Dynamic)) -> List(dynamic.Dynamic),
) -> Result(#(MemoryStore, Int), error.AdapterError)
Update all rows with new values (simple version without predicate). Useful for “UPDATE table SET column = value” style updates.
pub fn update_row(
store: MemoryStore,
table_name: String,
key: String,
row: List(dynamic.Dynamic),
) -> Result(MemoryStore, error.AdapterError)
Update a row in a table with constraint checking