mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
use URI as connection string
This commit is contained in:
parent
b92d08bf74
commit
562d5076bf
5 changed files with 37 additions and 24 deletions
|
@ -11,15 +11,17 @@ describe DB do
|
||||||
DB.driver_class("dummy").should eq(DummyDriver)
|
DB.driver_class("dummy").should eq(DummyDriver)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should instantiate driver with connection_string" do
|
it "should instantiate driver with connection uri" do
|
||||||
db = DB.open "dummy", "localhost:1027"
|
db = DB.open "dummy://localhost:1027"
|
||||||
db.driver_class.should eq(DummyDriver)
|
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
|
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", "localhost" do |db|
|
DB.open "dummy://localhost" do |db|
|
||||||
cnn = db.connection
|
cnn = db.connection
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -2,13 +2,13 @@ require "spec"
|
||||||
|
|
||||||
class DummyDriver < DB::Driver
|
class DummyDriver < DB::Driver
|
||||||
def build_connection
|
def build_connection
|
||||||
DummyConnection.new(connection_string)
|
DummyConnection.new(uri)
|
||||||
end
|
end
|
||||||
|
|
||||||
class DummyConnection < DB::Connection
|
class DummyConnection < DB::Connection
|
||||||
getter connection_string
|
getter uri
|
||||||
|
|
||||||
def initialize(@connection_string)
|
def initialize(@uri)
|
||||||
@@connections ||= [] of DummyConnection
|
@@connections ||= [] of DummyConnection
|
||||||
@@connections.not_nil! << self
|
@@connections.not_nil! << self
|
||||||
end
|
end
|
||||||
|
@ -173,7 +173,7 @@ end
|
||||||
def with_dummy
|
def with_dummy
|
||||||
DummyDriver::DummyConnection.clear_connections
|
DummyDriver::DummyConnection.clear_connections
|
||||||
|
|
||||||
DB.open "dummy", "" do |db|
|
DB.open "dummy://host" do |db|
|
||||||
yield db
|
yield db
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -11,11 +11,11 @@ module DB
|
||||||
getter driver_class
|
getter driver_class
|
||||||
|
|
||||||
# Connection configuration to the database.
|
# Connection configuration to the database.
|
||||||
getter connection_string
|
getter uri
|
||||||
|
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
def initialize(@driver_class, @connection_string)
|
def initialize(@driver_class, @uri)
|
||||||
@driver = @driver_class.new(@connection_string)
|
@driver = @driver_class.new(@uri)
|
||||||
@connection = @driver.build_connection
|
@connection = @driver.build_connection
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
31
src/db/db.cr
31
src/db/db.cr
|
@ -1,3 +1,5 @@
|
||||||
|
require "uri"
|
||||||
|
|
||||||
# The DB module is a unified interface to database access.
|
# The DB module is a unified interface to database access.
|
||||||
# Database dialects is supported by custom database driver shards.
|
# Database dialects is supported by custom database driver shards.
|
||||||
# Check [manastech/crystal-sqlite3](https://github.com/manastech/crystal-sqlite3) for example.
|
# 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`.
|
# 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
|
# db.close
|
||||||
# ```
|
# ```
|
||||||
#
|
#
|
||||||
# If a block is given to `#open` the database is closed automatically
|
# 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
|
# # work with db
|
||||||
# end # db is closed
|
# end # db is closed
|
||||||
# ```
|
# ```
|
||||||
|
@ -37,7 +39,7 @@
|
||||||
# require "db"
|
# require "db"
|
||||||
# require "sqlite3"
|
# 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 "create table contacts (name string, age integer)"
|
||||||
# db.exec "insert into contacts values (?, ?)", "John Doe", 30
|
# db.exec "insert into contacts values (?, ?)", "John Doe", 30
|
||||||
# db.exec "insert into contacts values (:name, :age)", {name: "Sarah", age: 33}
|
# db.exec "insert into contacts values (:name, :age)", {name: "Sarah", age: 33}
|
||||||
|
@ -75,27 +77,36 @@ module DB
|
||||||
@@drivers.not_nil![driver_name]
|
@@drivers.not_nil![driver_name]
|
||||||
end
|
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.
|
# Should be called by drivers implementors only.
|
||||||
def self.register_driver(driver_name, driver_class : Driver.class)
|
def self.register_driver(driver_name, driver_class : Driver.class)
|
||||||
@@drivers ||= {} of String => Driver.class
|
@@drivers ||= {} of String => Driver.class
|
||||||
@@drivers.not_nil![driver_name] = driver_class
|
@@drivers.not_nil![driver_name] = driver_class
|
||||||
end
|
end
|
||||||
|
|
||||||
# Opens a database using the `driver_name` registered driver.
|
# Opens a database using the specified *uri*.
|
||||||
# Uses `connection_string` for connection configuration.
|
# The scheme of the *uri* determines the driver to use.
|
||||||
# Returned database must be closed by `Database#close`.
|
# Returned database must be closed by `Database#close`.
|
||||||
def self.open(driver_name, connection_string)
|
# If a block is used the database is yielded and closed automatically.
|
||||||
Database.new(driver_class(driver_name), connection_string)
|
def self.open(uri : URI | String)
|
||||||
|
build_database(uri)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Same as `#open` but the database is yielded and closed automatically.
|
# Same as `#open` but the database is yielded and closed automatically.
|
||||||
def self.open(driver_name, connection_string, &block)
|
def self.open(uri : URI | String, &block)
|
||||||
open(driver_name, connection_string).tap do |db|
|
build_database(uri).tap do |db|
|
||||||
yield db
|
yield db
|
||||||
db.close
|
db.close
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
require "./query_methods"
|
require "./query_methods"
|
||||||
|
|
|
@ -8,7 +8,7 @@ module DB
|
||||||
#
|
#
|
||||||
# class FakeDriver < Driver
|
# class FakeDriver < Driver
|
||||||
# def build_connection
|
# def build_connection
|
||||||
# FakeConnection.new connection_string
|
# FakeConnection.new uri
|
||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
|
@ -26,9 +26,9 @@ module DB
|
||||||
# Refer to `Connection`, `Statement` and `ResultSet` for further
|
# Refer to `Connection`, `Statement` and `ResultSet` for further
|
||||||
# driver implementation instructions.
|
# driver implementation instructions.
|
||||||
abstract class Driver
|
abstract class Driver
|
||||||
getter connection_string
|
getter uri
|
||||||
|
|
||||||
def initialize(@connection_string : String)
|
def initialize(@uri : URI)
|
||||||
end
|
end
|
||||||
|
|
||||||
abstract def build_connection : Connection
|
abstract def build_connection : Connection
|
||||||
|
|
Loading…
Reference in a new issue