crystal-mimer
Mimer SQL database driver for Crystal, built on the crystal-db interface.
Prerequisites
- Mimer SQL installed with
libmimerapiavailable on your library path - Crystal >= 1.0.0
Installation
Add to your shard.yml:
dependencies:
mimer:
github: majorproblem/crystal-mimer
version: ~> 0.1.0
Then run:
shards install
Usage
require "mimer"
DB.open("mimer://USER:PASSWORD@/DATABASE") do |db|
# DDL
db.exec "CREATE TABLE users (id INTEGER, name NVARCHAR(100), active BOOLEAN)"
# Parameterized insert
db.exec "INSERT INTO users VALUES (?, ?, ?)", 1, "Alice", true
# Query
db.query "SELECT id, name, active FROM users" do |rs|
rs.each do
id = rs.read(Int32)
name = rs.read(String)
active = rs.read(Bool)
puts "#{id}: #{name} (active=#{active})"
end
end
# Scalar query
count = db.query_one "SELECT COUNT(*) FROM users", as: Int32
# Nullable column
name = db.query_one "SELECT name FROM users WHERE id = ?", 99, as: String?
end
Connection URI
mimer://USER:PASSWORD@/DATABASE # local database
mimer://USER:PASSWORD@SERVER/DATABASE # network connection via server alias
Transactions
Transactions use the Mimer C API (MimerBeginTransaction / MimerEndTransaction) rather than SQL statements:
DB.open("mimer://USER:PASSWORD@/DATABASE") do |db|
db.transaction do |tx|
tx.connection.exec "INSERT INTO users VALUES (?, ?, ?)", 2, "Bob", false
# raises → automatic rollback
end
end
Supported types
| Crystal type | Mimer SQL type |
|---|---|
| Int32 | INTEGER |
| Int64 | BIGINT |
| Float64 | DOUBLE PRECISION, FLOAT |
| String | NVARCHAR, VARCHAR, CHAR |
| Bool | BOOLEAN |
| Nil / nilable | NULL |
Running the tests
Tests require a live Mimer SQL instance and a writable databank:
MIMER_DATABASE=testdb MIMER_USER=SYSADM MIMER_PASSWORD=SYSADM crystal spec
License
MIT — see LICENSE.