Ralph::Database::Backend
Instance methods
Clear the prepared statement cache
This invalidates all cached statements, forcing them to be reparsed on next use. Call this after schema changes or when you want to release memory.
Example
Ralph.database.clear_statement_cache
Returns the dialect identifier for this backend Used by migrations and schema generation
Enable or disable prepared statement caching at runtime
Example
# Disable caching temporarily
Ralph.database.enable_statement_cache = false
# Run some one-off queries...
# Re-enable caching
Ralph.database.enable_statement_cache = true
Execute a query and return the raw result
Execute with DBValue args (converts UUIDs to strings)
Execute a query and return the last inserted ID
Implementation note: Different backends handle this differently:
- SQLite: Uses
SELECT last_insert_rowid()after INSERT - PostgreSQL: Uses
INSERT ... RETURNING id
Insert with DBValue args (converts UUIDs to strings)
Get column information for a specific table
Returns all columns with their types, nullability, defaults, etc.
Get foreign key constraints FROM a table (outgoing FKs)
These are FKs defined on this table that reference other tables.
Used for inferring belongs_to associations.
Get foreign key constraints TO a table (incoming FKs)
These are FKs from OTHER tables that reference this table.
Used for inferring has_many and has_one associations.
Get index information for a specific table
Returns all indexes including primary key index.
Introspect the entire database schema
Returns a DatabaseSchema containing all tables with their columns, indexes, and foreign keys.
Introspect a single table completely
Returns a DatabaseTable with all columns, indexes, and foreign keys.
Introspect specific tables only
More efficient than full schema introspection when you only need a subset of tables.
Check if the connection pool is healthy
Performs a simple query to verify database connectivity. Returns true if the database is reachable and responsive.
Example
if backend.pool_healthy?
puts "Database connection OK"
else
puts "Database connection FAILED"
end
Get current connection pool statistics
Returns a PoolStats record with information about the pool state. Useful for monitoring and debugging connection issues.
Example
stats = backend.pool_stats
puts "Connections: #{stats.open_connections}/#{stats.max_connections}"
Query multiple rows
Query all with DBValue args (converts UUIDs to strings)
Query a single row and map it to a result
Query one with DBValue args (converts UUIDs to strings)
Execute a query and return a single scalar value (first column of first row)
Scalar with DBValue args (converts UUIDs to strings)
Get statistics about the prepared statement cache
Returns a NamedTuple with cache size, max size, and enabled status.
Example
stats = Ralph.database.statement_cache_stats
puts "Cached: #{stats[:size]}/#{stats[:max_size]}"
Get list of all user tables (excluding system tables)
Should exclude:
- SQLite: sqlite_* tables, schema_migrations
- PostgreSQL: pg_* tables, information_schema tables, schema_migrations
Returns table names in alphabetical order.