mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
use connection_string as database configuration instead of a hash
This commit is contained in:
parent
8c0313a306
commit
a96776e336
6 changed files with 17 additions and 18 deletions
|
@ -7,16 +7,15 @@ describe DB do
|
||||||
DB.driver_class("dummy").should eq(DummyDriver)
|
DB.driver_class("dummy").should eq(DummyDriver)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should instantiate driver with options" do
|
it "should instantiate driver with connection_string" do
|
||||||
db = DB.open "dummy", {"host": "localhost", "port": "1027"}
|
db = DB.open "dummy", "localhost:1027"
|
||||||
db.driver_class.should eq(DummyDriver)
|
db.driver_class.should eq(DummyDriver)
|
||||||
db.options["host"].should eq("localhost")
|
db.connection_string.should eq("localhost:1027")
|
||||||
db.options["port"].should eq("1027")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should create a connection and close it" do
|
it "should create a connection and close it" do
|
||||||
cnn = nil
|
cnn = nil
|
||||||
DB.open "dummy", {"host": "localhost"} do |db|
|
DB.open "dummy", "localhost" do |db|
|
||||||
cnn = db.connection
|
cnn = db.connection
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ require "spec"
|
||||||
|
|
||||||
class DummyDriver < DB::Driver
|
class DummyDriver < DB::Driver
|
||||||
def build_connection
|
def build_connection
|
||||||
DummyConnection.new(options)
|
DummyConnection.new(connection_string)
|
||||||
end
|
end
|
||||||
|
|
||||||
class DummyConnection < DB::Connection
|
class DummyConnection < DB::Connection
|
||||||
|
@ -152,7 +152,7 @@ def with_witness(count = 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
def with_dummy
|
def with_dummy
|
||||||
DB.open "dummy", {} of String => String do |db|
|
DB.open "dummy", "" do |db|
|
||||||
yield db
|
yield db
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
module DB
|
module DB
|
||||||
abstract class Connection
|
abstract class Connection
|
||||||
getter options
|
getter connection_string
|
||||||
|
|
||||||
def initialize(@options)
|
def initialize(@connection_string)
|
||||||
@closed = false
|
@closed = false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -6,10 +6,10 @@ module DB
|
||||||
# It should be created from DB module. See `DB.open`.
|
# It should be created from DB module. See `DB.open`.
|
||||||
class Database
|
class Database
|
||||||
getter driver_class
|
getter driver_class
|
||||||
getter options
|
getter connection_string
|
||||||
|
|
||||||
def initialize(@driver_class, @options)
|
def initialize(@driver_class, @connection_string)
|
||||||
@driver = @driver_class.new(@options)
|
@driver = @driver_class.new(@connection_string)
|
||||||
@connection = @driver.build_connection
|
@connection = @driver.build_connection
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -12,12 +12,12 @@ module DB
|
||||||
@@drivers.not_nil![name] = klass
|
@@drivers.not_nil![name] = klass
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.open(name, options)
|
def self.open(name, connection_string)
|
||||||
Database.new(driver_class(name), options)
|
Database.new(driver_class(name), connection_string)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.open(name, options, &block)
|
def self.open(name, connection_string, &block)
|
||||||
open(name, options).tap do |db|
|
open(name, connection_string).tap do |db|
|
||||||
yield db
|
yield db
|
||||||
db.close
|
db.close
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
module DB
|
module DB
|
||||||
abstract class Driver
|
abstract class Driver
|
||||||
getter options
|
getter connection_string
|
||||||
|
|
||||||
def initialize(@options)
|
def initialize(@connection_string : String)
|
||||||
end
|
end
|
||||||
|
|
||||||
abstract def build_connection : Connection
|
abstract def build_connection : Connection
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue