crystal-mimer

Version, currently 0.1.02 versions

github.com/majorproblem/crystal-mimer

Mimer SQL database driver for Crystal

1 stars
0 dependents
License: MIT

Nothing has been indexed for 0.1.0 yet. The tag is recorded, its shard.yml has not been read, so the manifest and dependency list below are empty because they are unknown rather than because they are absent.

Installation

# Add this to your shard.yml
dependencies:
  crystal-mimer:
    github: majorproblem/crystal-mimer
    version: ~> 0.1.0

Then run:

shards install

shard.yml

No shard.yml has been indexed for 0.1.0. You can read it on the repository.

Dependencies

Unknown: the shard.yml for this version has not been read yet.

README

This README is the one indexed from the repository at its latest ref, not from the tag for this version.

crystal-mimer

Mimer SQL database driver for Crystal, built on the crystal-db interface.

Prerequisites

  • Mimer SQL installed with libmimerapi available 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 typeMimer SQL type
Int32INTEGER
Int64BIGINT
Float64DOUBLE PRECISION, FLOAT
StringNVARCHAR, VARCHAR, CHAR
BoolBOOLEAN
Nil / nilableNULL

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.