mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Introduce DB::ConnectionContext (#44)
* make Database a ConnectionContext. * introduce SingleConnectionContext for independant connections. * add `DB#connect` to create non pooled connections.
This commit is contained in:
parent
c63ea48748
commit
385cf70a8a
8 changed files with 115 additions and 23 deletions
|
@ -43,8 +43,8 @@ class FooDriver < DB::Driver
|
|||
@@row
|
||||
end
|
||||
|
||||
def build_connection(db : DB::Database) : DB::Connection
|
||||
FooConnection.new(db)
|
||||
def build_connection(context : DB::ConnectionContext) : DB::Connection
|
||||
FooConnection.new(context)
|
||||
end
|
||||
|
||||
class FooConnection < DB::Connection
|
||||
|
@ -106,8 +106,8 @@ class BarDriver < DB::Driver
|
|||
@@row
|
||||
end
|
||||
|
||||
def build_connection(db : DB::Database) : DB::Connection
|
||||
BarConnection.new(db)
|
||||
def build_connection(context : DB::ConnectionContext) : DB::Connection
|
||||
BarConnection.new(context)
|
||||
end
|
||||
|
||||
class BarConnection < DB::Connection
|
||||
|
|
|
@ -25,6 +25,25 @@ describe DB do
|
|||
connections.first.closed?.should be_true
|
||||
end
|
||||
|
||||
it "should create a connection and close it" do
|
||||
DummyDriver::DummyConnection.clear_connections
|
||||
DB.connect "dummy://localhost" do |cnn|
|
||||
cnn.should be_a(DummyDriver::DummyConnection)
|
||||
end
|
||||
connections.size.should eq(1)
|
||||
connections.first.closed?.should be_true
|
||||
end
|
||||
|
||||
it "should create a connection and wait for explicit closing" do
|
||||
DummyDriver::DummyConnection.clear_connections
|
||||
cnn = DB.connect "dummy://localhost"
|
||||
cnn.should be_a(DummyDriver::DummyConnection)
|
||||
connections.size.should eq(1)
|
||||
connections.first.closed?.should be_false
|
||||
cnn.close
|
||||
connections.first.closed?.should be_true
|
||||
end
|
||||
|
||||
it "query should close result_set" do
|
||||
with_witness do |w|
|
||||
with_dummy do |db|
|
||||
|
|
|
@ -2,13 +2,13 @@ require "spec"
|
|||
require "../src/db"
|
||||
|
||||
class DummyDriver < DB::Driver
|
||||
def build_connection(db : DB::Database) : DB::Connection
|
||||
DummyConnection.new(db)
|
||||
def build_connection(context : DB::ConnectionContext) : DB::Connection
|
||||
DummyConnection.new(context)
|
||||
end
|
||||
|
||||
class DummyConnection < DB::Connection
|
||||
def initialize(db)
|
||||
super(db)
|
||||
def initialize(context)
|
||||
super(context)
|
||||
@connected = true
|
||||
@@connections ||= [] of DummyConnection
|
||||
@@connections.not_nil! << self
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue