SqliteBackend¶
class
Defined in src/ralph/backends/sqlite.cr:75
SQLite database backend implementation
Provides SQLite-specific database operations for Ralph ORM. Uses the crystal-sqlite3 shard for database connectivity.
Example¶
# File-based database
backend = Ralph::Database::SqliteBackend.new("sqlite3://./db/development.sqlite3")
# In-memory database (useful for testing)
backend = Ralph::Database::SqliteBackend.new("sqlite3::memory:")
# Enable WAL mode for better concurrency in production
backend = Ralph::Database::SqliteBackend.new("sqlite3://./db.sqlite3", wal_mode: true)
Connection String Format¶
SQLite connection strings follow the format: sqlite3://path/to/database.db
Special values:
- sqlite3::memory: - Creates an in-memory database
Connection Pooling¶
Connection pooling is configured automatically from Ralph.settings:
Ralph.configure do |config|
config.initial_pool_size = 5
config.max_pool_size = 25
config.max_idle_pool_size = 10
config.checkout_timeout = 5.0
config.retry_attempts = 3
config.retry_delay = 0.2
end
Prepared Statement Caching¶
This backend supports prepared statement caching for improved query performance. Enable and configure via Ralph.settings:
Ralph.configure do |config|
config.enable_prepared_statements = true
config.prepared_statement_cache_size = 100
end
Concurrency¶
SQLite only supports one writer at a time. This backend provides two modes:
-
Default mode (wal_mode: false): Uses a mutex to serialize all write operations from this application. This prevents "database is locked" errors but limits write throughput to one operation at a time.
-
WAL mode (wal_mode: true): Enables SQLite's Write-Ahead Logging, which allows concurrent reads during writes. Writes are still serialized by SQLite but don't block readers. Recommended for production use with concurrent requests.
Note: WAL mode creates additional files (.sqlite3-wal, .sqlite3-shm) and is not supported for in-memory databases.
Constructors¶
.new(connection_string : String, wal_mode : Bool = false, busy_timeout : Int32 = 5000, apply_pool_settings : Bool = true)¶
Creates a new SQLite backend with the given connection string
Parameters¶
connection_string: SQLite connection URIwal_mode: Enable WAL mode for better concurrency (default: false)busy_timeout: Milliseconds to wait for locks (default: 5000)apply_pool_settings: Whether to apply pool settings from Ralph.settings (default: true)
Example¶
# Basic usage
backend = Ralph::Database::SqliteBackend.new("sqlite3://./db.sqlite3")
# Production usage with WAL mode
backend = Ralph::Database::SqliteBackend.new("sqlite3://./db.sqlite3", wal_mode: true)
# Skip pool settings (useful for CLI tools)
backend = Ralph::Database::SqliteBackend.new("sqlite3://./db.sqlite3", apply_pool_settings: false)
Instance Methods¶
#begin_transaction_sql¶
SQL to begin a transaction
#clear_statement_cache¶
Clear all cached prepared statements
#close¶
Close the database connection
#closed?¶
Check if the connection is open
#commit_sql¶
SQL to commit a transaction
#connection_string¶
Get the original connection string (without pool params)
#dialect¶
Returns the dialect identifier for this backend Used by migrations and schema generation
#execute(query : String, args : Array(DB::Any) = [] of DB::Any)¶
Execute a write query (INSERT, UPDATE, DELETE, DDL) Serialized through mutex when not in WAL mode Uses prepared statement cache when enabled
#insert(query : String, args : Array(DB::Any) = [] of DB::Any) : Int64¶
Insert a record and return the last inserted row ID Uses the same connection for both operations to ensure correctness
#introspect_columns(table : String) : Array(Schema::DatabaseColumn)¶
Get column information for a specific table
#introspect_foreign_keys(table : String) : Array(Schema::DatabaseForeignKey)¶
Get foreign key constraints FROM a table (outgoing FKs)
#introspect_foreign_keys_referencing(table : String) : Array(Schema::DatabaseForeignKey)¶
Get foreign key constraints TO a table (incoming FKs)
This requires scanning all tables since SQLite doesn't have a reverse lookup for foreign keys.
#introspect_indexes(table : String) : Array(Schema::DatabaseIndex)¶
Get index information for a specific table
#query_all(query : String, args : Array(DB::Any) = [] of DB::Any) : DB::ResultSet¶
Query for multiple rows Uses prepared statement cache when enabled
#query_one(query : String, args : Array(DB::Any) = [] of DB::Any) : DB::ResultSet | Nil¶
Query for a single row, returns nil if no results Uses prepared statement cache when enabled
#raw_connection¶
Get the underlying DB::Database connection for advanced operations
#release_savepoint_sql(name : String) : String¶
SQL to release a savepoint
#rollback_sql¶
SQL to rollback a transaction
#rollback_to_savepoint_sql(name : String) : String¶
SQL to rollback to a savepoint
#savepoint_sql(name : String) : String¶
SQL to create a savepoint
#scalar(query : String, args : Array(DB::Any) = [] of DB::Any) : DB::Any | Nil¶
Execute a scalar query and return a single value Uses prepared statement cache when enabled
#statement_cache_enabled?¶
Check if statement caching is enabled
#statement_cache_stats¶
Get statement cache statistics
#table_names¶
Get all user table names (excluding system tables)
#transaction¶
Execute a block within a database transaction The entire transaction is protected by the write lock
#wal_mode?¶
Whether WAL mode is enabled