Skip to content

Commit

Permalink
Use DB::NoResultsError on QueryMethods (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian J. Cardiff authored Apr 6, 2020
1 parent 7543908 commit 511fe20
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion spec/dummy_driver_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe DummyDriver do

it "raises if no rows" do
with_dummy do |db|
expect_raises(DB::Error, "no rows") do
expect_raises(DB::NoResultsError) do
db.query_one("") { }
end
end
Expand Down
14 changes: 9 additions & 5 deletions src/db/query_methods.cr
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ module DB
#
# 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.
# Raises `DB::NoResultsError` 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 = ?", 18, &.read(String)
# ```
def query_one(query, *args_, args : Array? = nil, &block : ResultSet -> U) : U forall U
query(query, *args_, args: args) do |rs|
raise DB::Error.new("no rows") unless rs.move_next
raise DB::NoResultsError.new("no results") unless rs.move_next

value = yield rs
raise DB::Error.new("more than one row") if rs.move_next
Expand All @@ -85,7 +86,8 @@ module DB
# 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.
# Raises `DB::NoResultsError` if there were no rows.
# Raises `DB::Error` if there were more than one row.
#
# ```
# db.query_one "select name, age from contacts where id = ?", 1, as: {String, Int32}
Expand All @@ -100,7 +102,8 @@ module DB
# as a named tuple of the given *types* (the keys of the named tuple
# are not necessarily the column names).
#
# Raises `DB::Error` if there were no rows, or if there were more than one row.
# Raises `DB::NoResultsError` if there were no rows.
# Raises `DB::Error` if there were more than one row.
#
# ```
# db.query_one "select name, age from contacts where id = ?", 1, as: {name: String, age: Int32}
Expand All @@ -114,7 +117,8 @@ module DB
# 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.
# Raises `DB::NoResultsError` if there were no rows.
# Raises `DB::Error` if there were more than one row.
#
# ```
# db.query_one "select name from contacts where id = ?", 1, as: String
Expand Down

0 comments on commit 511fe20

Please sign in to comment.