use URI as connection string

This commit is contained in:
Brian J. Cardiff 2016-02-03 18:29:09 -03:00
parent b92d08bf74
commit 562d5076bf
5 changed files with 37 additions and 24 deletions

View File

@ -11,15 +11,17 @@ describe DB do
DB.driver_class("dummy").should eq(DummyDriver)
end
it "should instantiate driver with connection_string" do
db = DB.open "dummy", "localhost:1027"
it "should instantiate driver with connection uri" do
db = DB.open "dummy://localhost:1027"
db.driver_class.should eq(DummyDriver)
db.connection_string.should eq("localhost:1027")
db.uri.scheme.should eq("dummy")
db.uri.host.should eq("localhost")
db.uri.port.should eq(1027)
end
it "should create a connection and close it" do
cnn = nil
DB.open "dummy", "localhost" do |db|
DB.open "dummy://localhost" do |db|
cnn = db.connection
end

View File

@ -2,13 +2,13 @@ require "spec"
class DummyDriver < DB::Driver
def build_connection
DummyConnection.new(connection_string)
DummyConnection.new(uri)
end
class DummyConnection < DB::Connection
getter connection_string
getter uri
def initialize(@connection_string)
def initialize(@uri)
@@connections ||= [] of DummyConnection
@@connections.not_nil! << self
end
@ -173,7 +173,7 @@ end
def with_dummy
DummyDriver::DummyConnection.clear_connections
DB.open "dummy", "" do |db|
DB.open "dummy://host" do |db|
yield db
end
end

View File

@ -11,11 +11,11 @@ module DB
getter driver_class
# Connection configuration to the database.
getter connection_string
getter uri
# :nodoc:
def initialize(@driver_class, @connection_string)
@driver = @driver_class.new(@connection_string)
def initialize(@driver_class, @uri)
@driver = @driver_class.new(@uri)
@connection = @driver.build_connection
end

View File

@ -1,3 +1,5 @@
require "uri"
# The DB module is a unified interface to database access.
# Database dialects is supported by custom database driver shards.
# Check [manastech/crystal-sqlite3](https://github.com/manastech/crystal-sqlite3) for example.
@ -12,14 +14,14 @@
# Assuming `crystal-sqlite3` is included a sqlite3 database can be opened with `#open`.
#
# ```
# db = DB.open "sqlite3", ":memory:" # or the sqlite3 file path
# db = DB.open "sqlite3://%3Amemory%3A" # or sqlite3:///path/to/db/file.db
# db.close
# ```
#
# If a block is given to `#open` the database is closed automatically
#
# ```
# DB.open "sqlite3", ":memory:" do |db|
# DB.open "sqlite3://%3Amemory%3A" do |db|
# # work with db
# end # db is closed
# ```
@ -37,7 +39,7 @@
# require "db"
# require "sqlite3"
#
# DB.open "sqlite3", ":memory:" do |db|
# DB.open "sqlite3://%3Amemory%3A" do |db|
# db.exec "create table contacts (name string, age integer)"
# db.exec "insert into contacts values (?, ?)", "John Doe", 30
# db.exec "insert into contacts values (:name, :age)", {name: "Sarah", age: 33}
@ -75,27 +77,36 @@ module DB
@@drivers.not_nil![driver_name]
end
# Registers a driver class for a given `driver_name`.
# Registers a driver class for a given *driver_name*.
# Should be called by drivers implementors only.
def self.register_driver(driver_name, driver_class : Driver.class)
@@drivers ||= {} of String => Driver.class
@@drivers.not_nil![driver_name] = driver_class
end
# Opens a database using the `driver_name` registered driver.
# Uses `connection_string` for connection configuration.
# Opens a database using the specified *uri*.
# The scheme of the *uri* determines the driver to use.
# Returned database must be closed by `Database#close`.
def self.open(driver_name, connection_string)
Database.new(driver_class(driver_name), connection_string)
# If a block is used the database is yielded and closed automatically.
def self.open(uri : URI | String)
build_database(uri)
end
# Same as `#open` but the database is yielded and closed automatically.
def self.open(driver_name, connection_string, &block)
open(driver_name, connection_string).tap do |db|
def self.open(uri : URI | String, &block)
build_database(uri).tap do |db|
yield db
db.close
end
end
private def self.build_database(connection_string : String)
build_database(URI.parse(connection_string))
end
private def self.build_database(uri : URI)
Database.new(driver_class(uri.scheme), uri)
end
end
require "./query_methods"

View File

@ -8,7 +8,7 @@ module DB
#
# class FakeDriver < Driver
# def build_connection
# FakeConnection.new connection_string
# FakeConnection.new uri
# end
# end
#
@ -26,9 +26,9 @@ module DB
# Refer to `Connection`, `Statement` and `ResultSet` for further
# driver implementation instructions.
abstract class Driver
getter connection_string
getter uri
def initialize(@connection_string : String)
def initialize(@uri : URI)
end
abstract def build_connection : Connection