mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Allow Enumerable
for query args instead of Array
(#207)
This commit is contained in:
parent
3eaac85a5d
commit
532ae075bd
3 changed files with 26 additions and 26 deletions
|
@ -15,7 +15,7 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
# See `QueryMethods#exec`
|
# See `QueryMethods#exec`
|
||||||
def exec(*args_, args : Array? = nil) : ExecResult
|
def exec(*args_, args : Enumerable? = nil) : ExecResult
|
||||||
statement_with_retry &.exec(*args_, args: args)
|
statement_with_retry &.exec(*args_, args: args)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -25,12 +25,12 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
# See `QueryMethods#query`
|
# See `QueryMethods#query`
|
||||||
def query(*args_, args : Array? = nil) : ResultSet
|
def query(*args_, args : Enumerable? = nil) : ResultSet
|
||||||
statement_with_retry &.query(*args_, args: args)
|
statement_with_retry &.query(*args_, args: args)
|
||||||
end
|
end
|
||||||
|
|
||||||
# See `QueryMethods#scalar`
|
# See `QueryMethods#scalar`
|
||||||
def scalar(*args_, args : Array? = nil)
|
def scalar(*args_, args : Enumerable? = nil)
|
||||||
statement_with_retry &.scalar(*args_, args: args)
|
statement_with_retry &.scalar(*args_, args: args)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ module DB
|
||||||
# result = db.query "select name from contacts where id = ?", args: [10]
|
# result = db.query "select name from contacts where id = ?", args: [10]
|
||||||
# ```
|
# ```
|
||||||
#
|
#
|
||||||
def query(query, *args_, args : Array? = nil)
|
def query(query, *args_, args : Enumerable? = nil)
|
||||||
build(query).query(*args_, args: args)
|
build(query).query(*args_, args: args)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -56,7 +56,7 @@ module DB
|
||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
# ```
|
# ```
|
||||||
def query(query, *args_, args : Array? = nil)
|
def query(query, *args_, args : Enumerable? = nil)
|
||||||
# CHECK build(query).query(*args, &block)
|
# CHECK build(query).query(*args, &block)
|
||||||
rs = query(query, *args_, args: args)
|
rs = query(query, *args_, args: args)
|
||||||
yield rs ensure rs.close
|
yield rs ensure rs.close
|
||||||
|
@ -73,7 +73,7 @@ module DB
|
||||||
# ```
|
# ```
|
||||||
# name = db.query_one "select name from contacts where id = ?", 18, &.read(String)
|
# name = db.query_one "select name from contacts where id = ?", 18, &.read(String)
|
||||||
# ```
|
# ```
|
||||||
def query_one(query, *args_, args : Array? = nil, &block : ResultSet -> U) : U forall U
|
def query_one(query, *args_, args : Enumerable? = nil, &block : ResultSet -> U) : U forall U
|
||||||
query(query, *args_, args: args) do |rs|
|
query(query, *args_, args: args) do |rs|
|
||||||
raise DB::NoResultsError.new("no results") unless rs.move_next
|
raise DB::NoResultsError.new("no results") unless rs.move_next
|
||||||
|
|
||||||
|
@ -92,7 +92,7 @@ module DB
|
||||||
# ```
|
# ```
|
||||||
# db.query_one "select name, age from contacts where id = ?", 1, as: {String, Int32}
|
# db.query_one "select name, age from contacts where id = ?", 1, as: {String, Int32}
|
||||||
# ```
|
# ```
|
||||||
def query_one(query, *args_, args : Array? = nil, as types : Tuple)
|
def query_one(query, *args_, args : Enumerable? = nil, as types : Tuple)
|
||||||
query_one(query, *args_, args: args) do |rs|
|
query_one(query, *args_, args: args) do |rs|
|
||||||
rs.read(*types)
|
rs.read(*types)
|
||||||
end
|
end
|
||||||
|
@ -108,7 +108,7 @@ module DB
|
||||||
# ```
|
# ```
|
||||||
# db.query_one "select name, age from contacts where id = ?", 1, as: {name: String, age: Int32}
|
# db.query_one "select name, age from contacts where id = ?", 1, as: {name: String, age: Int32}
|
||||||
# ```
|
# ```
|
||||||
def query_one(query, *args_, args : Array? = nil, as types : NamedTuple)
|
def query_one(query, *args_, args : Enumerable? = nil, as types : NamedTuple)
|
||||||
query_one(query, *args_, args: args) do |rs|
|
query_one(query, *args_, args: args) do |rs|
|
||||||
rs.read(**types)
|
rs.read(**types)
|
||||||
end
|
end
|
||||||
|
@ -123,7 +123,7 @@ module DB
|
||||||
# ```
|
# ```
|
||||||
# db.query_one "select name from contacts where id = ?", 1, as: String
|
# db.query_one "select name from contacts where id = ?", 1, as: String
|
||||||
# ```
|
# ```
|
||||||
def query_one(query, *args_, args : Array? = nil, as type : Class)
|
def query_one(query, *args_, args : Enumerable? = nil, as type : Class)
|
||||||
query_one(query, *args_, args: args) do |rs|
|
query_one(query, *args_, args: args) do |rs|
|
||||||
rs.read(type)
|
rs.read(type)
|
||||||
end
|
end
|
||||||
|
@ -141,7 +141,7 @@ module DB
|
||||||
# name = db.query_one? "select name from contacts where id = ?", 18, &.read(String)
|
# name = db.query_one? "select name from contacts where id = ?", 18, &.read(String)
|
||||||
# typeof(name) # => String | Nil
|
# typeof(name) # => String | Nil
|
||||||
# ```
|
# ```
|
||||||
def query_one?(query, *args_, args : Array? = nil, &block : ResultSet -> U) : U? forall U
|
def query_one?(query, *args_, args : Enumerable? = nil, &block : ResultSet -> U) : U? forall U
|
||||||
query(query, *args_, args: args) do |rs|
|
query(query, *args_, args: args) do |rs|
|
||||||
return nil unless rs.move_next
|
return nil unless rs.move_next
|
||||||
|
|
||||||
|
@ -162,7 +162,7 @@ module DB
|
||||||
# result = db.query_one? "select name, age from contacts where id = ?", 1, as: {String, Int32}
|
# result = db.query_one? "select name, age from contacts where id = ?", 1, as: {String, Int32}
|
||||||
# typeof(result) # => Tuple(String, Int32) | Nil
|
# typeof(result) # => Tuple(String, Int32) | Nil
|
||||||
# ```
|
# ```
|
||||||
def query_one?(query, *args_, args : Array? = nil, as types : Tuple)
|
def query_one?(query, *args_, args : Enumerable? = nil, as types : Tuple)
|
||||||
query_one?(query, *args_, args: args) do |rs|
|
query_one?(query, *args_, args: args) do |rs|
|
||||||
rs.read(*types)
|
rs.read(*types)
|
||||||
end
|
end
|
||||||
|
@ -180,7 +180,7 @@ module DB
|
||||||
# result = db.query_one? "select name, age from contacts where id = ?", 1, as: {age: String, name: Int32}
|
# result = db.query_one? "select name, age from contacts where id = ?", 1, as: {age: String, name: Int32}
|
||||||
# typeof(result) # => NamedTuple(age: String, name: Int32) | Nil
|
# typeof(result) # => NamedTuple(age: String, name: Int32) | Nil
|
||||||
# ```
|
# ```
|
||||||
def query_one?(query, *args_, args : Array? = nil, as types : NamedTuple)
|
def query_one?(query, *args_, args : Enumerable? = nil, as types : NamedTuple)
|
||||||
query_one?(query, *args_, args: args) do |rs|
|
query_one?(query, *args_, args: args) do |rs|
|
||||||
rs.read(**types)
|
rs.read(**types)
|
||||||
end
|
end
|
||||||
|
@ -197,7 +197,7 @@ module DB
|
||||||
# name = db.query_one? "select name from contacts where id = ?", 1, as: String
|
# name = db.query_one? "select name from contacts where id = ?", 1, as: String
|
||||||
# typeof(name) # => String?
|
# typeof(name) # => String?
|
||||||
# ```
|
# ```
|
||||||
def query_one?(query, *args_, args : Array? = nil, as type : Class)
|
def query_one?(query, *args_, args : Enumerable? = nil, as type : Class)
|
||||||
query_one?(query, *args_, args: args) do |rs|
|
query_one?(query, *args_, args: args) do |rs|
|
||||||
rs.read(type)
|
rs.read(type)
|
||||||
end
|
end
|
||||||
|
@ -209,7 +209,7 @@ module DB
|
||||||
# ```
|
# ```
|
||||||
# names = db.query_all "select name from contacts", &.read(String)
|
# names = db.query_all "select name from contacts", &.read(String)
|
||||||
# ```
|
# ```
|
||||||
def query_all(query, *args_, args : Array? = nil, &block : ResultSet -> U) : Array(U) forall U
|
def query_all(query, *args_, args : Enumerable? = nil, &block : ResultSet -> U) : Array(U) forall U
|
||||||
ary = [] of U
|
ary = [] of U
|
||||||
query_each(query, *args_, args: args) do |rs|
|
query_each(query, *args_, args: args) do |rs|
|
||||||
ary.push(yield rs)
|
ary.push(yield rs)
|
||||||
|
@ -223,7 +223,7 @@ module DB
|
||||||
# ```
|
# ```
|
||||||
# contacts = db.query_all "select name, age from contacts", as: {String, Int32}
|
# contacts = db.query_all "select name, age from contacts", as: {String, Int32}
|
||||||
# ```
|
# ```
|
||||||
def query_all(query, *args_, args : Array? = nil, as types : Tuple)
|
def query_all(query, *args_, args : Enumerable? = nil, as types : Tuple)
|
||||||
query_all(query, *args_, args: args) do |rs|
|
query_all(query, *args_, args: args) do |rs|
|
||||||
rs.read(*types)
|
rs.read(*types)
|
||||||
end
|
end
|
||||||
|
@ -236,7 +236,7 @@ module DB
|
||||||
# ```
|
# ```
|
||||||
# contacts = db.query_all "select name, age from contacts", as: {name: String, age: Int32}
|
# contacts = db.query_all "select name, age from contacts", as: {name: String, age: Int32}
|
||||||
# ```
|
# ```
|
||||||
def query_all(query, *args_, args : Array? = nil, as types : NamedTuple)
|
def query_all(query, *args_, args : Enumerable? = nil, as types : NamedTuple)
|
||||||
query_all(query, *args_, args: args) do |rs|
|
query_all(query, *args_, args: args) do |rs|
|
||||||
rs.read(**types)
|
rs.read(**types)
|
||||||
end
|
end
|
||||||
|
@ -248,7 +248,7 @@ module DB
|
||||||
# ```
|
# ```
|
||||||
# names = db.query_all "select name from contacts", as: String
|
# names = db.query_all "select name from contacts", as: String
|
||||||
# ```
|
# ```
|
||||||
def query_all(query, *args_, args : Array? = nil, as type : Class)
|
def query_all(query, *args_, args : Enumerable? = nil, as type : Class)
|
||||||
query_all(query, *args_, args: args) do |rs|
|
query_all(query, *args_, args: args) do |rs|
|
||||||
rs.read(type)
|
rs.read(type)
|
||||||
end
|
end
|
||||||
|
@ -262,7 +262,7 @@ module DB
|
||||||
# puts rs.read(String)
|
# puts rs.read(String)
|
||||||
# end
|
# end
|
||||||
# ```
|
# ```
|
||||||
def query_each(query, *args_, args : Array? = nil)
|
def query_each(query, *args_, args : Enumerable? = nil)
|
||||||
query(query, *args_, args: args) do |rs|
|
query(query, *args_, args: args) do |rs|
|
||||||
rs.each do
|
rs.each do
|
||||||
yield rs
|
yield rs
|
||||||
|
@ -271,7 +271,7 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
# Performs the `query` and returns an `ExecResult`
|
# Performs the `query` and returns an `ExecResult`
|
||||||
def exec(query, *args_, args : Array? = nil)
|
def exec(query, *args_, args : Enumerable? = nil)
|
||||||
build(query).exec(*args_, args: args)
|
build(query).exec(*args_, args: args)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -280,7 +280,7 @@ module DB
|
||||||
# ```
|
# ```
|
||||||
# puts db.scalar("SELECT MAX(name)").as(String) # => (a String)
|
# puts db.scalar("SELECT MAX(name)").as(String) # => (a String)
|
||||||
# ```
|
# ```
|
||||||
def scalar(query, *args_, args : Array? = nil)
|
def scalar(query, *args_, args : Enumerable? = nil)
|
||||||
build(query).scalar(*args_, args: args)
|
build(query).scalar(*args_, args: args)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,7 +3,7 @@ module DB
|
||||||
# and for connection pool statements.
|
# and for connection pool statements.
|
||||||
module StatementMethods
|
module StatementMethods
|
||||||
# See `QueryMethods#scalar`
|
# See `QueryMethods#scalar`
|
||||||
def scalar(*args_, args : Array? = nil)
|
def scalar(*args_, args : Enumerable? = nil)
|
||||||
query(*args_, args: args) do |rs|
|
query(*args_, args: args) do |rs|
|
||||||
rs.each do
|
rs.each do
|
||||||
return rs.read
|
return rs.read
|
||||||
|
@ -14,7 +14,7 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
# See `QueryMethods#query`
|
# See `QueryMethods#query`
|
||||||
def query(*args_, args : Array? = nil)
|
def query(*args_, args : Enumerable? = nil)
|
||||||
rs = query(*args_, args: args)
|
rs = query(*args_, args: args)
|
||||||
yield rs ensure rs.close
|
yield rs ensure rs.close
|
||||||
end
|
end
|
||||||
|
@ -22,12 +22,12 @@ module DB
|
||||||
# See `QueryMethods#exec`
|
# See `QueryMethods#exec`
|
||||||
abstract def exec : ExecResult
|
abstract def exec : ExecResult
|
||||||
# See `QueryMethods#exec`
|
# See `QueryMethods#exec`
|
||||||
abstract def exec(*args_, args : Array? = nil) : ExecResult
|
abstract def exec(*args_, args : Enumerable? = nil) : ExecResult
|
||||||
|
|
||||||
# See `QueryMethods#query`
|
# See `QueryMethods#query`
|
||||||
abstract def query : ResultSet
|
abstract def query : ResultSet
|
||||||
# See `QueryMethods#query`
|
# See `QueryMethods#query`
|
||||||
abstract def query(*args_, args : Array? = nil) : ResultSet
|
abstract def query(*args_, args : Enumerable? = nil) : ResultSet
|
||||||
end
|
end
|
||||||
|
|
||||||
# Represents a query in a `Connection`.
|
# Represents a query in a `Connection`.
|
||||||
|
@ -74,7 +74,7 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
# See `QueryMethods#exec`
|
# See `QueryMethods#exec`
|
||||||
def exec(*args_, args : Array? = nil) : DB::ExecResult
|
def exec(*args_, args : Enumerable? = nil) : DB::ExecResult
|
||||||
perform_exec_and_release(EnumerableConcat.build(args_, args))
|
perform_exec_and_release(EnumerableConcat.build(args_, args))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -84,7 +84,7 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
# See `QueryMethods#query`
|
# See `QueryMethods#query`
|
||||||
def query(*args_, args : Array? = nil) : DB::ResultSet
|
def query(*args_, args : Enumerable? = nil) : DB::ResultSet
|
||||||
perform_query_with_rescue(EnumerableConcat.build(args_, args))
|
perform_query_with_rescue(EnumerableConcat.build(args_, args))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue