Rebase to latest DB version and upgrade to Crystal 0.18

This commit is contained in:
Santiago Palladino 2016-07-04 12:13:39 -03:00
parent 7fcedc6711
commit 552b6e12b4
6 changed files with 48 additions and 9 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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