Skip to content

Commit

Permalink
Update to crystal-db 0.12.0 (refactor connection factory) (#107)
Browse files Browse the repository at this point in the history
* Refactor connection builder

* Update specs

* Update for ConnectionBuilder

* Update to crystal-db ~> 0.12.0
  • Loading branch information
bcardiff authored Jun 23, 2023
1 parent 7689c58 commit 91dcada
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 25 deletions.
4 changes: 2 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ version: 0.14.0
dependencies:
db:
github: crystal-lang/crystal-db
version: ~> 0.11.0
version: ~> 0.12.0

authors:
- Juan Wajnerman <jwajnerman@manas.tech>
- Brian J. Cardiff <bcardiff@manas.tech>
- Brian J. Cardiff <bcardiff@gmail.com>

crystal: ">= 1.0.0, < 2.0.0"

Expand Down
10 changes: 5 additions & 5 deletions spec/db_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require "semantic_version"
private class NotSupportedType
end

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

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

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

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

2.times do |i|
DB.open db.uri do |db|
DB.open ctx.connection_string do |db|
begin
db.unprepared.query("SELECT i, str FROM a WHERE i = 23") do |rs|
rs.each do
Expand Down
47 changes: 31 additions & 16 deletions src/mysql/connection.cr
Original file line number Diff line number Diff line change
@@ -1,31 +1,46 @@
require "socket"

class MySql::Connection < DB::Connection
def initialize(context : DB::ConnectionContext)
super(context)
@socket = uninitialized TCPSocket

begin
host = context.uri.hostname || raise "no host provided"
port = context.uri.port || 3306
username = context.uri.user
password = context.uri.password

charset = context.uri.query_params.fetch "encoding", Collations.default_collation
charset_id = Collations.id_for_collation(charset).to_u8

path = context.uri.path
record Options,
host : String,
port : Int32,
username : String?,
password : String?,
initial_catalog : String?,
charset : String do
def self.from_uri(uri : URI) : Options
host = uri.hostname || raise "no host provided"
port = uri.port || 3306
username = uri.user
password = uri.password

charset = uri.query_params.fetch "encoding", Collations.default_collation

path = uri.path
if path && path.size > 1
initial_catalog = path[1..-1]
else
initial_catalog = nil
end

@socket = TCPSocket.new(host, port)
Options.new(
host: host, port: port, username: username, password: password,
initial_catalog: initial_catalog, charset: charset)
end
end

def initialize(options : ::DB::Connection::Options, mysql_options : ::MySql::Connection::Options)
super(options)
@socket = uninitialized TCPSocket

begin
charset_id = Collations.id_for_collation(mysql_options.charset).to_u8

@socket = TCPSocket.new(mysql_options.host, mysql_options.port)
handshake = read_packet(Protocol::HandshakeV10)

write_packet(1) do |packet|
Protocol::HandshakeResponse41.new(username, password, initial_catalog, handshake.auth_plugin_data, charset_id).write(packet)
Protocol::HandshakeResponse41.new(mysql_options.username, mysql_options.password, mysql_options.initial_catalog, handshake.auth_plugin_data, charset_id).write(packet)
end

read_ok_or_err do |packet, status|
Expand Down
14 changes: 12 additions & 2 deletions src/mysql/driver.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
class MySql::Driver < DB::Driver
def build_connection(context : DB::ConnectionContext) : MySql::Connection
MySql::Connection.new(context)
class ConnectionBuilder < ::DB::ConnectionBuilder
def initialize(@options : ::DB::Connection::Options, @mysql_options : MySql::Connection::Options)
end

def build : ::DB::Connection
MySql::Connection.new(@options, @mysql_options)
end
end

def connection_builder(uri : URI) : ::DB::ConnectionBuilder
params = HTTP::Params.parse(uri.query || "")
ConnectionBuilder.new(connection_options(params), MySql::Connection::Options.from_uri(uri))
end
end

Expand Down

0 comments on commit 91dcada

Please sign in to comment.