class Onyx::SQL::Repository

Overview

A gateway between Serializable and DB. Its main features are logging, expanding Onyx::SQL::Query instances and serializing from resulting DB::ResultSet.

db = DB.open(ENV["DATABASE_URL"])
repo = Onyx::SQL::Repository.new(db)

repo.scalar("SELECT 1").as(Int32)
# [sql] SELECT 1
# 593μs

repo.scalar("SELECT ?", 1).as(Int32)
# ditto

repo.query("SELECT * FROM users")       # Returns raw `DB::ResultSet`
repo.query(User, "SELECT * FROM users") # Returns `Array(User)`
repo.query(User.all)                    # Returns `Array(User)` as well
# [sql] SELECT users.* FROM users
# 442μs

Defined in:

onyx-sql/repository/exec.cr
onyx-sql/repository/logger.cr
onyx-sql/repository/query.cr
onyx-sql/repository/scalar.cr
onyx-sql/repository.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(db : DB::Database | DB::Connection, logger : Logger = Logger::Standard.new) #

Initialize the repository.


[View source]

Instance Method Detail

def db : DB::Connection | DB::Database #

A ::DB::Database | ::DB::Connection instance for this repository.


[View source]
def db=(db) #

A ::DB::Database | ::DB::Connection instance for this repository.


[View source]
def driver #

Return #db driver name, e.g. "postgresql" for PG::Driver.


[View source]
def exec(sql : String, params : Enumerable(DB::Any)? = nil) : DB::ExecResult #

[View source]
def exec(sql : String, *params : DB::Any) : DB::ExecResult #

[View source]
def exec(query : BulkQuery) : DB::ExecResult #

Call db.exec(*query.build). It removes any Query#returning clauses to avoid DB hanging.


[View source]
def exec(query : Query) : DB::ExecResult #

Call db.exec(*query.build). It removes any Query#returning clauses to avoid DB hanging.


[View source]

A Repository::Logger instance for this repository.


[View source]
def logger=(logger) #

A Repository::Logger instance for this repository.


[View source]
def prepare_query(sql_query : String) #

Prepare query for initialization.

If the #db driver is PG::Driver, replace all ? with $1, $2 etc. Otherwise return sql_query untouched.


[View source]
def query(query : Onyx::SQL::Query(T)) : Array(T) forall T #

Call db.query(*query.build) and map the result to Array(T).


[View source]
def query(query : Onyx::SQL::BulkQuery(T)) : Array(T) forall T #

Call db.query(*query.build) and map the result to Array(T).


[View source]
def query(sql : String, params : Enumerable(DB::Any)? = nil) #

[View source]
def query(klass : T.class, sql : String, params : Enumerable(DB::Any)? = nil) : Array(T) forall T #

Call db.query(sql, params) and map the result to Array(T).


[View source]
def query(sql : String, *params : DB::Any) #

[View source]
def query(klass : T.class, sql : String, *params : DB::Any) : Array(T) forall T #

Call db.query(sql, *params) and map the result to Array(T).


[View source]
def scalar(query : Query) #

[View source]
def scalar(query : BulkQuery) #

[View source]
def scalar(sql : String, *params : DB::Any) #

[View source]
def scalar(sql : String, params : Enumerable(DB::Any)? = nil) #

[View source]