Core Concepts
The foundational types that power CSharpDB's type system and schema definitions.
DbType
CSharpDB supports five fundamental data types, inspired by SQLite's type affinity system:
| Type | C# Mapping | Description |
|---|---|---|
Null | null | Absence of value |
Integer | long | 64-bit signed integer |
Real | double | 64-bit IEEE floating point |
Text | string | UTF-8 encoded text |
Blob | byte[] | Raw binary data |
DbValue
DbValue is a lightweight struct that represents a dynamically-typed database value. It wraps the five core types and provides type-safe access, comparison, and truthiness evaluation.
var id = DbValue.FromInteger(42);
var name = DbValue.FromText("Alice");
var score = DbValue.FromReal(98.5);
var empty = DbValue.Null;
// Type-safe access
long idVal = id.AsInteger; // 42
string nameVal = name.AsText; // "Alice"
bool isNull = empty.IsNull; // true
// Comparison
int cmp = DbValue.Compare(id, DbValue.FromInteger(43)); // -1
TableSchema
Defines the structure of a table: its name, columns, types, nullability, and primary key.
var schema = new TableSchema("Orders", [
new("OrderId", DbType.Integer, isPrimaryKey: true),
new("CustomerId",DbType.Integer),
new("Total", DbType.Real),
new("Notes", DbType.Text, nullable: true),
]);
// Access column info
int idx = schema.GetColumnIndex("Total"); // 2
int pk = schema.PrimaryKeyColumnIndex; // 0
IndexSchema
IndexSchema defines secondary indexes on table columns. Indexes support composite keys, unique constraints, and efficient range scans via BTreeIndexCursor.
var index = new IndexSchema(
name: "idx_orders_customer",
tableName: "Orders",
columns: ["CustomerId"],
isUnique: false
);
TriggerSchema
TriggerSchema stores trigger metadata — timing (BEFORE/AFTER), event (INSERT/UPDATE/DELETE), and the SQL body for execution by the query engine.
ColumnDefinition
Each column in a TableSchema is described by a ColumnDefinition that captures:
| Property | Type | Description |
|---|---|---|
Name | string | Column name |
Type | DbType | Data type |
IsPrimaryKey | bool | Whether this is the primary key column |
Nullable | bool | Whether NULL values are allowed |