-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrepository.cr
More file actions
86 lines (77 loc) · 2.47 KB
/
repository.cr
File metadata and controls
86 lines (77 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
require "./repository/*"
module Onyx::SQL
# 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
# ```
class Repository
# A `::DB::Database | ::DB::Connection` instance for this repository.
property db
# A `Repository::Logger` instance for this repository.
property logger
# Initialize the repository.
def initialize(@db : ::DB::Database | ::DB::Connection, @logger : Logger = Logger::Standard.new)
end
def db=(transaction : ::DB::Transaction)
self.db = transaction.connection
end
protected def postgresql?
{% if Object.all_subclasses.any? { |sc| sc.stringify == "PG::Driver" } %}
return db.is_a?(PG::Driver)
{% end %}
return false
end
# Prepare query for initialization.
#
# If the `#db` driver is `PG::Driver`, replace all `?` with `$1`, `$2` etc. Otherwise return *sql_query* untouched.
def prepare_query(sql_query : String)
{% begin %}
case db_driver
{% if Object.all_subclasses.any? { |sc| sc.stringify == "PG::Driver" } %}
when PG::Driver
counter = 0
sql_query.gsub("?") { '$' + (counter += 1).to_s }
{% end %}
else sql_query
end
{% end %}
end
# Return `#db` driver name, e.g. `"postgresql"` for `PG::Driver`.
def driver
{% begin %}
case db_driver
{% if Object.all_subclasses.any? { |sc| sc.stringify == "PG::Driver" } %}
when PG::Driver then "postgresql"
{% end %}
{% if Object.all_subclasses.any? { |sc| sc.stringify == "SQLite3::Driver" } %}
when SQLite3::Driver then "sqlite3"
{% end %}
else "sql"
end
{% end %}
end
protected def db_driver
if db.is_a?(::DB::Database)
db.as(::DB::Database).driver
else
db.as(::DB::Connection).context.as(::DB::Database).driver
end
end
end
end