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:

TypeC# MappingDescription
NullnullAbsence of value
Integerlong64-bit signed integer
Realdouble64-bit IEEE floating point
TextstringUTF-8 encoded text
Blobbyte[]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:

PropertyTypeDescription
NamestringColumn name
TypeDbTypeData type
IsPrimaryKeyboolWhether this is the primary key column
NullableboolWhether NULL values are allowed