From 552b6e12b47705f720cfd03d7d36b55f3b74ef9a Mon Sep 17 00:00:00 2001 From: Santiago Palladino Date: Mon, 4 Jul 2016 12:13:39 -0300 Subject: [PATCH] Rebase to latest DB version and upgrade to Crystal 0.18 --- spec/dummy_driver.cr | 8 ++++++++ spec/dummy_driver_spec.cr | 13 +++++++++++++ spec/mapping_spec.cr | 21 ++++++++++++++++++++- spec/result_set_spec.cr | 4 ++-- src/db/mapping.cr | 9 ++++----- src/db/result_set.cr | 2 +- 6 files changed, 48 insertions(+), 9 deletions(-) diff --git a/spec/dummy_driver.cr b/spec/dummy_driver.cr index f62034d..aa05f83 100644 --- a/spec/dummy_driver.cr +++ b/spec/dummy_driver.cr @@ -131,10 +131,18 @@ class DummyDriver < DB::Driver read(String).to_i32 end + def read(t : Int32?.class) + read(String?).try &.to_i32 + end + def read(t : Int64.class) read(String).to_i64 end + def read(t : Int64?.class) + read(String?).try &.to_i64 + end + def read(t : Float32.class) read(String).to_f32 end diff --git a/spec/dummy_driver_spec.cr b/spec/dummy_driver_spec.cr index 328b7c8..97a47da 100644 --- a/spec/dummy_driver_spec.cr +++ b/spec/dummy_driver_spec.cr @@ -96,6 +96,19 @@ describe DummyDriver do end end + it "should enumerate nillable int64 fields" do + with_dummy do |db| + db.query "3,4 1,NULL" do |rs| + rs.move_next + rs.read(Int64 | Nil).should eq(3i64) + rs.read(Int64 | Nil).should eq(4i64) + rs.move_next + rs.read(Int64 | Nil).should eq(1i64) + rs.read(Int64 | Nil).should be_nil + end + end + end + describe "query one" do it "queries" do with_dummy do |db| diff --git a/spec/mapping_spec.cr b/spec/mapping_spec.cr index b662275..8c622af 100644 --- a/spec/mapping_spec.cr +++ b/spec/mapping_spec.cr @@ -8,6 +8,13 @@ class SimpleMapping }) end +class NonStrictMapping + DB.mapping({ + c1: Int32, + c2: String + }, strict: false) +end + class MappingWithDefaults DB.mapping({ c0: { type: Int32, default: 10 }, @@ -17,7 +24,7 @@ end class MappingWithNilables DB.mapping({ - c0: { type: Int32, nilable: true }, + c0: { type: Int32, nilable: true, default: 10 }, c1: { type: String, nilable: true }, }) end @@ -79,6 +86,10 @@ describe "DB.mapping" do expect_raises { from_dummy("1,a,b", SimpleMapping) } end + it "should initialize a non-strict mapping if there is an unexpected column" do + expect_mapping("1,2,a,b", NonStrictMapping, {c1: 2, c2: "a"}) + end + it "should initialize a mapping with default values" do expect_mapping("1,a", MappingWithDefaults, {c0: 1, c1: "a"}) end @@ -87,10 +98,18 @@ describe "DB.mapping" do expect_mapping("1", MappingWithDefaults, {c0: 1, c1: "c"}) end + it "should initialize a mapping using default values if values are nil and types are non nilable" do + expect_mapping("1,NULL", MappingWithDefaults, {c0: 1, c1: "c"}) + end + it "should initialize a mapping with nils if columns are missing" do expect_mapping("1", MappingWithNilables, {c0: 1, c1: nil}) end + it "should initialize a mapping with nils ignoring default value is type is nilable" do + expect_mapping("NULL,a", MappingWithNilables, {c0: nil, c1: "a"}) + end + it "should initialize a mapping with different keys" do expect_mapping("1,a", MappingWithKeys, {foo: 1, bar: "a"}) end diff --git a/spec/result_set_spec.cr b/spec/result_set_spec.cr index 56ad396..21d8424 100644 --- a/spec/result_set_spec.cr +++ b/spec/result_set_spec.cr @@ -41,15 +41,15 @@ describe DB::ResultSet do end the_rs.closed?.should be_true end + end it "should enumerate columns" do cols = [] of String with_dummy do |db| db.query "3,4 1,2" do |rs| - rs.each_column do |col, col_type| + rs.each_column do |col| cols << col - col_type.should eq(Slice(UInt8)) end end end diff --git a/src/db/mapping.cr b/src/db/mapping.cr index 9c0cac1..04949ce 100644 --- a/src/db/mapping.cr +++ b/src/db/mapping.cr @@ -57,7 +57,7 @@ module DB # This macro also declares instance variables of the types given in the mapping. macro mapping(properties, strict = true) {% for key, value in properties %} - {% properties[key] = {type: value} unless value.is_a?(HashLiteral) %} + {% properties[key] = {type: value} unless value.is_a?(HashLiteral) || value.is_a?(NamedTupleLiteral) %} {% end %} {% for key, value in properties %} @@ -86,7 +86,7 @@ module DB %found{key.id} = false {% end %} - %rs.each_column do |col_name, col_type| + %rs.each_column do |col_name| case col_name {% for key, value in properties %} when {{value[:key] || key.id.stringify}} @@ -95,7 +95,7 @@ module DB {% if value[:converter] %} {{value[:converter]}}.from_rs(%rs) {% elsif value[:nilable] || value[:default] != nil %} - %rs.read?({{value[:type]}}) + %rs.read(Union({{value[:type]}} | Nil)) {% else %} %rs.read({{value[:type]}}) {% end %} @@ -104,8 +104,7 @@ module DB {% if strict %} raise DB::MappingException.new("unknown result set attribute: #{col_name}") {% else %} - # TODO: col_type can be Nil, and read?(Nil) is undefined; how to skip a column? - #%rs.read?(col_type) + %rs.read(Nil) {% end %} end end diff --git a/src/db/result_set.cr b/src/db/result_set.cr index a739eac..c6a5f09 100644 --- a/src/db/result_set.cr +++ b/src/db/result_set.cr @@ -44,7 +44,7 @@ module DB # Iterates over all the columns def each_column column_count.times do |x| - yield column_name(x), column_type(x) + yield column_name(x) end end