From d0d89c1e13c0b18a5d46558c0a029ebd2025df2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Thu, 5 Sep 2019 17:37:11 +0200 Subject: [PATCH] Statement#exec and #query require named argument for array values This change allows to use an array as single argument for #exec and #query methods. Before it was shadowed by the *args splat overload. --- spec/dummy_driver.cr | 6 +++++- spec/statement_spec.cr | 20 ++++++++++++++++++-- src/db/statement.cr | 8 ++++---- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/spec/dummy_driver.cr b/spec/dummy_driver.cr index 830d371..0158770 100644 --- a/spec/dummy_driver.cr +++ b/spec/dummy_driver.cr @@ -97,7 +97,7 @@ class DummyDriver < DB::Driver property params def initialize(connection, @query : String, @prepared : Bool) - @params = Hash(Int32 | String, DB::Any).new + @params = Hash(Int32 | String, DB::Any | Array(DB::Any)).new super(connection) raise DB::Error.new(query) if query == "syntax error" end @@ -126,6 +126,10 @@ class DummyDriver < DB::Driver @params[index] = value end + private def set_param(index, value : Array) + @params[index] = value.map(&.as(DB::Any)) + end + private def set_param(index, value) raise "not implemented for #{value.class}" end diff --git a/spec/statement_spec.cr b/spec/statement_spec.cr index fcdd05a..a080b35 100644 --- a/spec/statement_spec.cr +++ b/spec/statement_spec.cr @@ -43,10 +43,18 @@ describe DB::Statement do end end - it "should initialize positional params in query with array" do + it "accepts array as single argument" do with_dummy_connection do |cnn| stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement) stmt.query ["a", 1, nil] + stmt.params[0].should eq(["a", 1, nil]) + end + end + + it "should initialize positional params in query with array" do + with_dummy_connection do |cnn| + stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement) + stmt.query args: ["a", 1, nil] stmt.params[0].should eq("a") stmt.params[1].should eq(1) stmt.params[2].should eq(nil) @@ -63,10 +71,18 @@ describe DB::Statement do end end - it "should initialize positional params in exec with array" do + it "accepts array as single argument" do with_dummy_connection do |cnn| stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement) stmt.exec ["a", 1, nil] + stmt.params[0].should eq(["a", 1, nil]) + end + end + + it "should initialize positional params in exec with array" do + with_dummy_connection do |cnn| + stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement) + stmt.exec args: ["a", 1, nil] stmt.params[0].should eq("a") stmt.params[1].should eq(1) stmt.params[2].should eq(nil) diff --git a/src/db/statement.cr b/src/db/statement.cr index 0ab4be3..f43bd8b 100644 --- a/src/db/statement.cr +++ b/src/db/statement.cr @@ -29,14 +29,14 @@ module DB # See `QueryMethods#exec` abstract def exec(*args) : ExecResult # See `QueryMethods#exec` - abstract def exec(args : Array) : ExecResult + abstract def exec(*, args : Array) : ExecResult # See `QueryMethods#query` abstract def query : ResultSet # See `QueryMethods#query` abstract def query(*args) : ResultSet # See `QueryMethods#query` - abstract def query(args : Array) : ResultSet + abstract def query(*, args : Array) : ResultSet end # Represents a query in a `Connection`. @@ -68,7 +68,7 @@ module DB end # See `QueryMethods#exec` - def exec(args : Array) : DB::ExecResult + def exec(*, args : Array) : DB::ExecResult perform_exec_and_release(args) end @@ -84,7 +84,7 @@ module DB end # See `QueryMethods#query` - def query(args : Array) : DB::ResultSet + def query(*, args : Array) : DB::ResultSet perform_query_with_rescue args end