Compare commits

...

4 Commits

Author SHA1 Message Date
Lawrence Wakefield 532ae075bd
Allow `Enumerable` for query args instead of `Array` (#207) 2024-07-03 15:46:40 +02:00
Brian J. Cardiff 3eaac85a5d
Release 0.13.1 (#203) 2023-12-21 09:53:14 -03:00
Brian J. Cardiff 1d0105ffeb
Gracefully allow spec helper to fail on older crystal (#202) 2023-12-17 20:03:45 -03:00
Brian J. Cardiff 26599a740f
Update CHANGELOG.md 2023-12-11 19:31:47 -03:00
7 changed files with 48 additions and 38 deletions

View File

@ -1,4 +1,8 @@
## v0.13.0 (2023-12-??) ## v0.13.1 (2023-12-21)
* Gracefully allow spec helper to fail on older crystal. ([#202](https://github.com/crystal-lang/crystal-db/pull/202), thanks @bcardiff)
## v0.13.0 (2023-12-11)
* **(breaking-change)** Deprecate `DB.mapping`. ([#196](https://github.com/crystal-lang/crystal-db/pull/196), thanks @straight-shoota) * **(breaking-change)** Deprecate `DB.mapping`. ([#196](https://github.com/crystal-lang/crystal-db/pull/196), thanks @straight-shoota)
* **(breaking-change)** Drop `Pool#checkout_some`, make `PoolStatement` a struct. ([#200](https://github.com/crystal-lang/crystal-db/pull/200), thanks @bcardiff) * **(breaking-change)** Drop `Pool#checkout_some`, make `PoolStatement` a struct. ([#200](https://github.com/crystal-lang/crystal-db/pull/200), thanks @bcardiff)

View File

@ -1,5 +1,5 @@
name: db name: db
version: 0.13.0 version: 0.13.1
authors: authors:
- Brian J. Cardiff <bcardiff@gmail.com> - Brian J. Cardiff <bcardiff@gmail.com>

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -1,3 +1,3 @@
module DB module DB
VERSION = "0.13.0" VERSION = "0.13.1"
end end

View File

@ -398,9 +398,13 @@ module DB
@before.call @before.call
if options if options
uri = URI.parse connection_string {% if compare_versions(Crystal::VERSION, "1.9.0") >= 0 %}
uri.query_params.merge! URI::Params.parse(options) uri = URI.parse connection_string
connection_string_with_options = uri.to_s uri.query_params.merge! URI::Params.parse(options)
connection_string_with_options = uri.to_s
{% else %}
raise "Crystal 1.9.0 or greater is required to run with_db with options"
{% end %}
else else
connection_string_with_options = connection_string connection_string_with_options = connection_string
end end
@ -561,14 +565,16 @@ module DB
end end
end end
else else
values.each do |prepared_statements| {% if compare_versions(Crystal::VERSION, "1.9.0") >= 0 %}
it("#{db_it.description} (prepared_statements=#{prepared_statements})", db_it.file, db_it.line, db_it.end_line) do values.each do |prepared_statements|
ctx.with_db "prepared_statements=#{prepared_statements}" do |db| it("#{db_it.description} (prepared_statements=#{prepared_statements})", db_it.file, db_it.line, db_it.end_line) do
db_it.block.call db ctx.with_db "prepared_statements=#{prepared_statements}" do |db|
nil db_it.block.call db
nil
end
end end
end end
end {% end %}
end end
else else
raise "Invalid prepared value. Allowed values are :both and :default" raise "Invalid prepared value. Allowed values are :both and :default"