Skip to content

Commit

Permalink
Merge pull request #83 from crystal-lang/crystal-db/0.7.0
Browse files Browse the repository at this point in the history
Update to crystal-db 0.7.0
  • Loading branch information
Brian J. Cardiff authored Sep 20, 2019
2 parents b6f3d9f + 9f2f785 commit 73640df
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 9 deletions.
4 changes: 2 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ version: 0.8.0
dependencies:
db:
github: crystal-lang/crystal-db
version: ~> 0.6.0
version: ~> 0.7.0

authors:
- Juan Wajnerman <[email protected]>
- Brian J. Cardiff <[email protected]>

crystal: 0.25.0
crystal: 0.30.0

license: MIT
5 changes: 5 additions & 0 deletions spec/db_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ DB::DriverSpecs(MySql::Any).run do
sample_value 123, "mediumint(2)", "123", type_safe_value: false
sample_value 1, "int", "1", type_safe_value: false
sample_value 1_i64, "bigint", "1"
sample_value -5_i8, "tinyint(1)", "-5", type_safe_value: false
sample_value -54_i16, "smallint(2)", "-54", type_safe_value: false
sample_value -123, "mediumint(2)", "-123", type_safe_value: false
sample_value -1, "int", "-1", type_safe_value: false
sample_value -1_i64, "bigint", "-1"
sample_value "hello", "varchar(25)", "'hello'"
sample_value 1.5_f32, "float", "1.5", type_safe_value: false
sample_value 1.5, "double", "1.5"
Expand Down
59 changes: 59 additions & 0 deletions spec/pool_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require "./spec_helper"

describe DB::Pool do
it "should write from multiple connections" do
channel = Channel(Nil).new
fibers = 20
max_pool_size = 5
max_n = 50

with_db "crystal_mysql_test", "max_pool_size=#{max_pool_size}" do |db|
db.exec "create table numbers (n int, fiber int)"

fibers.times do |f|
spawn do
(1..max_n).each do |n|
db.exec "insert into numbers (n, fiber) values (?, ?)", n, f
sleep 0.01
end
channel.send nil
end
end

fibers.times { channel.receive }

# all numbers were inserted
s = fibers * max_n * (max_n + 1) // 2
db.scalar("select sum(n) from numbers").should eq(s)

# numbers were not inserted one fiber at a time
rows = db.query_all "select n, fiber from numbers", as: {Int32, Int32}
rows.map(&.[1]).should_not eq(rows.map(&.[1]).sort)
end
end

it "starting multiple connections does not exceed max pool size" do
channel = Channel(Nil).new
fibers = 100
max_pool_size = 5

with_db "crystal_mysql_test", "max_pool_size=#{max_pool_size}" do |db|
db.exec "create table numbers (n int, fiber int)"

max_open_connections = Atomic.new(0)

fibers.times do |f|
spawn do
cnn = db.checkout
max_open_connections.max(db.pool.stats.open_connections)
sleep 0.01
cnn.release
channel.send nil
end
end

fibers.times { channel.receive }
max_open_connections.get.should be <= max_pool_size
end
end
end
13 changes: 13 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,16 @@ end
def database_host
ENV.fetch("DATABASE_HOST", "localhost")
end

def with_db(database_name, options = nil, &block : DB::Database ->)
DB.open db_url do |db|
db.exec "DROP DATABASE IF EXISTS crystal_mysql_test"
db.exec "CREATE DATABASE crystal_mysql_test"
end

DB.open "#{db_url(database_name)}?#{options}", &block
ensure
DB.open db_url do |db|
db.exec "DROP DATABASE IF EXISTS crystal_mysql_test"
end
end
12 changes: 6 additions & 6 deletions src/mysql/connection.cr
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ class MySql::Connection < DB::Connection
org_name = packet.read_lenenc_string
next_length = packet.read_lenenc_int # length of fixed-length fields, always 0x0c
raise "Unexpected next_length value: #{next_length}." unless next_length == 0x0c
character_set = packet.read_fixed_int(2).to_u16
column_length = packet.read_fixed_int(4).to_u32
column_type = packet.read_fixed_int(1).to_u8
flags = packet.read_fixed_int(2).to_u16
decimal = packet.read_fixed_int(1).to_u8
filler = packet.read_fixed_int(2).to_u16 # filler [00] [00]
character_set = packet.read_fixed_int(2).to_u16!
column_length = packet.read_fixed_int(4).to_u32!
column_type = packet.read_fixed_int(1).to_u8!
flags = packet.read_fixed_int(2).to_u16!
decimal = packet.read_fixed_int(1).to_u8!
filler = packet.read_fixed_int(2).to_u16! # filler [00] [00]
raise "Unexpected filler value #{filler}" unless filler == 0x0000

target << ColumnSpec.new(catalog, schema, table, org_table, name, org_name, character_set, column_length, column_type, flags, decimal)
Expand Down
2 changes: 1 addition & 1 deletion src/mysql/write_packet.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class MySql::WritePacket < IO

def write_lenenc_int(v)
if v < 251
write_bytes(v.to_i8, IO::ByteFormat::LittleEndian)
write_bytes(v.to_u8, IO::ByteFormat::LittleEndian)
elsif v < 65_536
write_bytes(0xfc_u8, IO::ByteFormat::LittleEndian)
write_bytes(v.to_u16, IO::ByteFormat::LittleEndian)
Expand Down

0 comments on commit 73640df

Please sign in to comment.