-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
query_methods.cr
212 lines (197 loc) · 6.39 KB
/
query_methods.cr
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
module DB
# Methods to allow querying a database.
# All methods accepts a `query : String` and a set arguments.
#
# Three kind of statements can be performed:
# 1. `#exec` waits no record response from the database. An `ExecResult` is returned.
# 2. `#scalar` reads a single value of the response. A union of possible values is returned.
# 3. `#query` returns a `ResultSet` that allows iteration over the rows in the response and column information.
#
# Arguments can be passed by position
#
# ```
# db.query("SELECT name FROM ... WHERE age > ?", age)
# ```
#
# Convention of mapping how arguments are mapped to the query depends on each driver.
#
# Including `QueryMethods` requires a `build(query) : Statement` method that is not expected
# to be called directly.
module QueryMethods
# :nodoc:
abstract def build(query) : Statement
# Executes a *query* and returns a `ResultSet` with the results.
# The `ResultSet` must be closed manually.
#
# ```
# result = db.query "select name from contacts where id = ?", 10
# begin
# if result.move_next
# id = result.read(Int32)
# end
# ensure
# result.close
# end
# ```
def query(query, *args)
build(query).query(*args)
end
# Executes a *query* and yields a `ResultSet` with the results.
# The `ResultSet` is closed automatically.
#
# ```
# db.query("select name from contacts where age > ?", 18) do |rs|
# rs.each do
# name = rs.read(String)
# end
# end
# ```
def query(query, *args)
# CHECK build(query).query(*args, &block)
rs = query(query, *args)
yield rs ensure rs.close
end
# Executes a *query* that expects a single row and yields a `ResultSet`
# positioned at that first row.
#
# The given block must not invoke `move_next` on the yielded result set.
#
# Raises `DB::Error` if there were no rows, or if there were more than one row.
#
# ```
# name = db.query_one "select name from contacts where id = ?", 18, &.read(String)
# ```
def query_one(query, *args, &block : ResultSet -> U) : U forall U
query(query, *args) do |rs|
raise DB::Error.new("no rows") unless rs.move_next
value = yield rs
raise DB::Error.new("more than one row") if rs.move_next
return value
end
end
# Executes a *query* that expects a single row and returns it
# as a tuple of the given *types*.
#
# Raises `DB::Error` if there were no rows, or if there were more than one row.
#
# ```
# db.query_one "select name, age from contacts where id = ?", 1, as: {String, Int32}
# ```
def query_one(query, *args, as types : Tuple)
query_one(query, *args) do |rs|
rs.read(*types)
end
end
# Executes a *query* that expects a single row
# and returns the first column's value as the given *type*.
#
# Raises `DB::Error` if there were no rows, or if there were more than one row.
#
# ```
# db.query_one "select name from contacts where id = ?", 1, as: String
# ```
def query_one(query, *args, as type : Class)
query_one(query, *args) do |rs|
rs.read(type)
end
end
# Executes a *query* that expects at most a single row and yields a `ResultSet`
# positioned at that first row.
#
# Returns `nil`, not invoking the block, if there were no rows.
#
# Raises `DB::Error` if there were more than one row
# (this ends up invoking the block once).
#
# ```
# name = db.query_one? "select name from contacts where id = ?", 18, &.read(String)
# typeof(name) # => String | Nil
# ```
def query_one?(query, *args, &block : ResultSet -> U) : U? forall U
query(query, *args) do |rs|
return nil unless rs.move_next
value = yield rs
raise DB::Error.new("more than one row") if rs.move_next
return value
end
end
# Executes a *query* that expects a single row and returns it
# as a tuple of the given *types*.
#
# Returns `nil` if there were no rows.
#
# Raises `DB::Error` if there were more than one row.
#
# ```
# result = db.query_one? "select name, age from contacts where id = ?", 1, as: {String, Int32}
# typeof(result) # => Tuple(String, Int32) | Nil
# ```
def query_one?(query, *args, as types : Tuple)
query_one?(query, *args) do |rs|
rs.read(*types)
end
end
# Executes a *query* that expects a single row
# and returns the first column's value as the given *type*.
#
# Returns `nil` if there were no rows.
#
# Raises `DB::Error` if there were more than one row.
#
# ```
# name = db.query_one? "select name from contacts where id = ?", 1, as: String
# typeof(name) # => String?
# ```
def query_one?(query, *args, as type : Class)
query_one?(query, *args) do |rs|
rs.read(type)
end
end
# Executes a *query* and yield a `ResultSet` positioned at the beginning
# of each row, returning an array of the values of the blocks.
#
# ```
# names = db.query_all "select name from contacts", &.read(String)
# ```
def query_all(query, *args, &block : ResultSet -> U) : Array(U) forall U
ary = [] of U
query(query, *args) do |rs|
rs.each do
ary.push(yield rs)
end
end
ary
end
# Executes a *query* and returns an array where each row is
# read as a tuple of the given *types*.
#
# ```
# contacts = db.query_all "select name, age from contactas", as: {String, Int32}
# ```
def query_all(query, *args, as types : Tuple)
query_all(query, *args) do |rs|
rs.read(*types)
end
end
# Executes a *query* and returns an array where there first
# column's value of each row is read as the given *type*.
#
# ```
# names = db.query_all "select name from contactas", as: String
# ```
def query_all(query, *args, as type : Class)
query_all(query, *args) do |rs|
rs.read(type)
end
end
# Performs the `query` and returns an `ExecResult`
def exec(query, *args)
build(query).exec(*args)
end
# Performs the `query` and returns a single scalar value
# puts db.scalar("SELECT MAX(name)").as(String) # => (a String)
def scalar(query, *args)
build(query).scalar(*args)
end
end
end