mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Rebase to latest DB version and upgrade to Crystal 0.18
This commit is contained in:
parent
7fcedc6711
commit
552b6e12b4
6 changed files with 48 additions and 9 deletions
|
@ -131,10 +131,18 @@ class DummyDriver < DB::Driver
|
||||||
read(String).to_i32
|
read(String).to_i32
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def read(t : Int32?.class)
|
||||||
|
read(String?).try &.to_i32
|
||||||
|
end
|
||||||
|
|
||||||
def read(t : Int64.class)
|
def read(t : Int64.class)
|
||||||
read(String).to_i64
|
read(String).to_i64
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def read(t : Int64?.class)
|
||||||
|
read(String?).try &.to_i64
|
||||||
|
end
|
||||||
|
|
||||||
def read(t : Float32.class)
|
def read(t : Float32.class)
|
||||||
read(String).to_f32
|
read(String).to_f32
|
||||||
end
|
end
|
||||||
|
|
|
@ -96,6 +96,19 @@ describe DummyDriver do
|
||||||
end
|
end
|
||||||
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
|
describe "query one" do
|
||||||
it "queries" do
|
it "queries" do
|
||||||
with_dummy do |db|
|
with_dummy do |db|
|
||||||
|
|
|
@ -8,6 +8,13 @@ class SimpleMapping
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class NonStrictMapping
|
||||||
|
DB.mapping({
|
||||||
|
c1: Int32,
|
||||||
|
c2: String
|
||||||
|
}, strict: false)
|
||||||
|
end
|
||||||
|
|
||||||
class MappingWithDefaults
|
class MappingWithDefaults
|
||||||
DB.mapping({
|
DB.mapping({
|
||||||
c0: { type: Int32, default: 10 },
|
c0: { type: Int32, default: 10 },
|
||||||
|
@ -17,7 +24,7 @@ end
|
||||||
|
|
||||||
class MappingWithNilables
|
class MappingWithNilables
|
||||||
DB.mapping({
|
DB.mapping({
|
||||||
c0: { type: Int32, nilable: true },
|
c0: { type: Int32, nilable: true, default: 10 },
|
||||||
c1: { type: String, nilable: true },
|
c1: { type: String, nilable: true },
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
@ -79,6 +86,10 @@ describe "DB.mapping" do
|
||||||
expect_raises { from_dummy("1,a,b", SimpleMapping) }
|
expect_raises { from_dummy("1,a,b", SimpleMapping) }
|
||||||
end
|
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
|
it "should initialize a mapping with default values" do
|
||||||
expect_mapping("1,a", MappingWithDefaults, {c0: 1, c1: "a"})
|
expect_mapping("1,a", MappingWithDefaults, {c0: 1, c1: "a"})
|
||||||
end
|
end
|
||||||
|
@ -87,10 +98,18 @@ describe "DB.mapping" do
|
||||||
expect_mapping("1", MappingWithDefaults, {c0: 1, c1: "c"})
|
expect_mapping("1", MappingWithDefaults, {c0: 1, c1: "c"})
|
||||||
end
|
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
|
it "should initialize a mapping with nils if columns are missing" do
|
||||||
expect_mapping("1", MappingWithNilables, {c0: 1, c1: nil})
|
expect_mapping("1", MappingWithNilables, {c0: 1, c1: nil})
|
||||||
end
|
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
|
it "should initialize a mapping with different keys" do
|
||||||
expect_mapping("1,a", MappingWithKeys, {foo: 1, bar: "a"})
|
expect_mapping("1,a", MappingWithKeys, {foo: 1, bar: "a"})
|
||||||
end
|
end
|
||||||
|
|
|
@ -41,15 +41,15 @@ describe DB::ResultSet do
|
||||||
end
|
end
|
||||||
the_rs.closed?.should be_true
|
the_rs.closed?.should be_true
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it "should enumerate columns" do
|
it "should enumerate columns" do
|
||||||
cols = [] of String
|
cols = [] of String
|
||||||
|
|
||||||
with_dummy do |db|
|
with_dummy do |db|
|
||||||
db.query "3,4 1,2" do |rs|
|
db.query "3,4 1,2" do |rs|
|
||||||
rs.each_column do |col, col_type|
|
rs.each_column do |col|
|
||||||
cols << col
|
cols << col
|
||||||
col_type.should eq(Slice(UInt8))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -57,7 +57,7 @@ module DB
|
||||||
# This macro also declares instance variables of the types given in the mapping.
|
# This macro also declares instance variables of the types given in the mapping.
|
||||||
macro mapping(properties, strict = true)
|
macro mapping(properties, strict = true)
|
||||||
{% for key, value in properties %}
|
{% 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 %}
|
{% end %}
|
||||||
|
|
||||||
{% for key, value in properties %}
|
{% for key, value in properties %}
|
||||||
|
@ -86,7 +86,7 @@ module DB
|
||||||
%found{key.id} = false
|
%found{key.id} = false
|
||||||
{% end %}
|
{% end %}
|
||||||
|
|
||||||
%rs.each_column do |col_name, col_type|
|
%rs.each_column do |col_name|
|
||||||
case col_name
|
case col_name
|
||||||
{% for key, value in properties %}
|
{% for key, value in properties %}
|
||||||
when {{value[:key] || key.id.stringify}}
|
when {{value[:key] || key.id.stringify}}
|
||||||
|
@ -95,7 +95,7 @@ module DB
|
||||||
{% if value[:converter] %}
|
{% if value[:converter] %}
|
||||||
{{value[:converter]}}.from_rs(%rs)
|
{{value[:converter]}}.from_rs(%rs)
|
||||||
{% elsif value[:nilable] || value[:default] != nil %}
|
{% elsif value[:nilable] || value[:default] != nil %}
|
||||||
%rs.read?({{value[:type]}})
|
%rs.read(Union({{value[:type]}} | Nil))
|
||||||
{% else %}
|
{% else %}
|
||||||
%rs.read({{value[:type]}})
|
%rs.read({{value[:type]}})
|
||||||
{% end %}
|
{% end %}
|
||||||
|
@ -104,8 +104,7 @@ module DB
|
||||||
{% if strict %}
|
{% if strict %}
|
||||||
raise DB::MappingException.new("unknown result set attribute: #{col_name}")
|
raise DB::MappingException.new("unknown result set attribute: #{col_name}")
|
||||||
{% else %}
|
{% else %}
|
||||||
# TODO: col_type can be Nil, and read?(Nil) is undefined; how to skip a column?
|
%rs.read(Nil)
|
||||||
#%rs.read?(col_type)
|
|
||||||
{% end %}
|
{% end %}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -44,7 +44,7 @@ module DB
|
||||||
# Iterates over all the columns
|
# Iterates over all the columns
|
||||||
def each_column
|
def each_column
|
||||||
column_count.times do |x|
|
column_count.times do |x|
|
||||||
yield column_name(x), column_type(x)
|
yield column_name(x)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue