Skip to content

Commit 91dcada

Browse files
authored
Update to crystal-db 0.12.0 (refactor connection factory) (#107)
* Refactor connection builder * Update specs * Update for ConnectionBuilder * Update to crystal-db ~> 0.12.0
1 parent 7689c58 commit 91dcada

File tree

4 files changed

+50
-25
lines changed

4 files changed

+50
-25
lines changed

shard.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ version: 0.14.0
44
dependencies:
55
db:
66
github: crystal-lang/crystal-db
7-
version: ~> 0.11.0
7+
version: ~> 0.12.0
88

99
authors:
1010
- Juan Wajnerman <[email protected]>
11-
- Brian J. Cardiff <bcardiff@manas.tech>
11+
- Brian J. Cardiff <bcardiff@gmail.com>
1212

1313
crystal: ">= 1.0.0, < 2.0.0"
1414

spec/db_spec.cr

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require "semantic_version"
55
private class NotSupportedType
66
end
77

8-
DB::DriverSpecs(MySql::Any).run do
8+
DB::DriverSpecs(MySql::Any).run do |ctx|
99
before do
1010
DB.open db_url do |db|
1111
db.exec "DROP DATABASE IF EXISTS crystal_mysql_test"
@@ -149,7 +149,7 @@ DB::DriverSpecs(MySql::Any).run do
149149
db.exec %(insert into a (i, str) values (23, "bai bai");)
150150

151151
2.times do |i|
152-
DB.open db.uri do |db|
152+
DB.open ctx.connection_string do |db|
153153
begin
154154
db.query("SELECT i, str FROM a WHERE i = ?", 23) do |rs|
155155
rs.move_next
@@ -170,7 +170,7 @@ DB::DriverSpecs(MySql::Any).run do
170170

171171
it "does not close a connection before cleaning up the result set" do |db|
172172
begin
173-
DB.open db.uri do |db|
173+
DB.open ctx.connection_string do |db|
174174
db.query("select 'foo'") do |rs|
175175
rs.each do
176176
rs.read(String)
@@ -189,7 +189,7 @@ DB::DriverSpecs(MySql::Any).run do
189189

190190
it "does not close a connection before cleaning up the text result set" do |db|
191191
begin
192-
DB.open db.uri do |db|
192+
DB.open ctx.connection_string do |db|
193193
db.unprepared.query("select 'foo'") do |rs|
194194
rs.each do
195195
rs.read(String)
@@ -211,7 +211,7 @@ DB::DriverSpecs(MySql::Any).run do
211211
db.exec %(insert into a (i, str) values (23, "bai bai");)
212212

213213
2.times do |i|
214-
DB.open db.uri do |db|
214+
DB.open ctx.connection_string do |db|
215215
begin
216216
db.unprepared.query("SELECT i, str FROM a WHERE i = 23") do |rs|
217217
rs.each do

src/mysql/connection.cr

+31-16
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,46 @@
11
require "socket"
22

33
class MySql::Connection < DB::Connection
4-
def initialize(context : DB::ConnectionContext)
5-
super(context)
6-
@socket = uninitialized TCPSocket
7-
8-
begin
9-
host = context.uri.hostname || raise "no host provided"
10-
port = context.uri.port || 3306
11-
username = context.uri.user
12-
password = context.uri.password
13-
14-
charset = context.uri.query_params.fetch "encoding", Collations.default_collation
15-
charset_id = Collations.id_for_collation(charset).to_u8
16-
17-
path = context.uri.path
4+
record Options,
5+
host : String,
6+
port : Int32,
7+
username : String?,
8+
password : String?,
9+
initial_catalog : String?,
10+
charset : String do
11+
def self.from_uri(uri : URI) : Options
12+
host = uri.hostname || raise "no host provided"
13+
port = uri.port || 3306
14+
username = uri.user
15+
password = uri.password
16+
17+
charset = uri.query_params.fetch "encoding", Collations.default_collation
18+
19+
path = uri.path
1820
if path && path.size > 1
1921
initial_catalog = path[1..-1]
2022
else
2123
initial_catalog = nil
2224
end
2325

24-
@socket = TCPSocket.new(host, port)
26+
Options.new(
27+
host: host, port: port, username: username, password: password,
28+
initial_catalog: initial_catalog, charset: charset)
29+
end
30+
end
31+
32+
def initialize(options : ::DB::Connection::Options, mysql_options : ::MySql::Connection::Options)
33+
super(options)
34+
@socket = uninitialized TCPSocket
35+
36+
begin
37+
charset_id = Collations.id_for_collation(mysql_options.charset).to_u8
38+
39+
@socket = TCPSocket.new(mysql_options.host, mysql_options.port)
2540
handshake = read_packet(Protocol::HandshakeV10)
2641

2742
write_packet(1) do |packet|
28-
Protocol::HandshakeResponse41.new(username, password, initial_catalog, handshake.auth_plugin_data, charset_id).write(packet)
43+
Protocol::HandshakeResponse41.new(mysql_options.username, mysql_options.password, mysql_options.initial_catalog, handshake.auth_plugin_data, charset_id).write(packet)
2944
end
3045

3146
read_ok_or_err do |packet, status|

src/mysql/driver.cr

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
class MySql::Driver < DB::Driver
2-
def build_connection(context : DB::ConnectionContext) : MySql::Connection
3-
MySql::Connection.new(context)
2+
class ConnectionBuilder < ::DB::ConnectionBuilder
3+
def initialize(@options : ::DB::Connection::Options, @mysql_options : MySql::Connection::Options)
4+
end
5+
6+
def build : ::DB::Connection
7+
MySql::Connection.new(@options, @mysql_options)
8+
end
9+
end
10+
11+
def connection_builder(uri : URI) : ::DB::ConnectionBuilder
12+
params = HTTP::Params.parse(uri.query || "")
13+
ConnectionBuilder.new(connection_options(params), MySql::Connection::Options.from_uri(uri))
414
end
515
end
616

0 commit comments

Comments
 (0)