2016-01-31 22:40:02 +00:00
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# Drivers implementors check `Driver` class.
|
|
|
|
#
|
|
|
|
# Currently a *single connection* to the database is stablished.
|
|
|
|
# In the future a connection pool and transaction support will be available.
|
|
|
|
#
|
|
|
|
# ### Usage
|
|
|
|
#
|
|
|
|
# Assuming `crystal-sqlite3` is included a sqlite3 database can be opened with `#open`.
|
|
|
|
#
|
|
|
|
# ```
|
|
|
|
# db = DB.open "sqlite3", ":memory:" # or the sqlite3 file path
|
|
|
|
# db.close
|
|
|
|
# ```
|
|
|
|
#
|
|
|
|
# If a block is given to `#open` the database is closed automatically
|
|
|
|
#
|
|
|
|
# ```
|
|
|
|
# DB.open "sqlite3", ":memory:" do |db|
|
|
|
|
# # work with db
|
|
|
|
# end # db is closed
|
|
|
|
# ```
|
|
|
|
#
|
|
|
|
# Three kind of statements can be performed:
|
|
|
|
# 1. `Database#exec` waits no response from the database.
|
|
|
|
# 2. `Database#scalar` reads a single value of the response.
|
|
|
|
# 3. `Database#query` returns a ResultSet that allows iteration over the rows in the response and column information.
|
|
|
|
#
|
|
|
|
# All of the above methods allows parametrised query. Either positional or named arguments.
|
|
|
|
#
|
|
|
|
# Check a full working version:
|
|
|
|
#
|
|
|
|
# ```
|
|
|
|
# require "db"
|
|
|
|
# require "sqlite3"
|
|
|
|
#
|
|
|
|
# DB.open "sqlite3", ":memory:" 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}
|
|
|
|
#
|
|
|
|
# puts "max age:"
|
|
|
|
# puts db.scalar "select max(age) from contacts" # => 33
|
|
|
|
#
|
|
|
|
# puts "contacts:"
|
|
|
|
# db.query "select name, age from contacts order by age desc" do |rs|
|
|
|
|
# puts "#{rs.column_name(0)} (#{rs.column_name(1)})"
|
|
|
|
# # => name (age)
|
|
|
|
# rs.each do
|
|
|
|
# puts "#{rs.read(String)} (#{rs.read(Int32)})"
|
|
|
|
# # => Sarah (33)
|
|
|
|
# # => John Doe (30)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# ```
|
|
|
|
#
|
2016-01-28 22:41:12 +00:00
|
|
|
module DB
|
2016-01-31 22:40:02 +00:00
|
|
|
# Types supported to interface with database driver.
|
|
|
|
# These can be used in any `ResultSet#read` or any `Database#query` related
|
|
|
|
# method to be used as query parameters
|
2016-01-29 22:21:48 +00:00
|
|
|
TYPES = [String, Int32, Int64, Float32, Float64, Slice(UInt8)]
|
2016-01-31 22:40:02 +00:00
|
|
|
|
|
|
|
# See `DB::TYPES` in `DB`
|
2016-01-29 22:21:48 +00:00
|
|
|
alias Any = String | Int32 | Int64 | Float32 | Float64 | Slice(UInt8)
|
2016-01-29 19:13:01 +00:00
|
|
|
|
2016-01-30 22:46:43 +00:00
|
|
|
# :nodoc:
|
2016-01-31 22:40:02 +00:00
|
|
|
def self.driver_class(driver_name) # : Driver.class
|
|
|
|
@@drivers.not_nil![driver_name]
|
2016-01-28 22:41:12 +00:00
|
|
|
end
|
|
|
|
|
2016-01-31 22:40:02 +00:00
|
|
|
# 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)
|
2016-01-28 22:41:12 +00:00
|
|
|
@@drivers ||= {} of String => Driver.class
|
2016-01-31 22:40:02 +00:00
|
|
|
@@drivers.not_nil![driver_name] = driver_class
|
2016-01-28 22:41:12 +00:00
|
|
|
end
|
|
|
|
|
2016-01-31 22:40:02 +00:00
|
|
|
# Opens a database using the `driver_name` registered driver.
|
|
|
|
# Uses `connection_string` for connection configuration.
|
|
|
|
# Returned database must be closed by `Database#close`.
|
|
|
|
def self.open(driver_name, connection_string)
|
|
|
|
Database.new(driver_class(driver_name), connection_string)
|
2016-01-28 22:41:12 +00:00
|
|
|
end
|
2016-01-30 22:46:43 +00:00
|
|
|
|
2016-01-31 22:40:02 +00:00
|
|
|
# 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|
|
2016-01-30 22:46:43 +00:00
|
|
|
yield db
|
|
|
|
db.close
|
|
|
|
end
|
|
|
|
end
|
2016-01-28 22:41:12 +00:00
|
|
|
end
|
|
|
|
|
2016-01-31 17:01:52 +00:00
|
|
|
require "./query_methods"
|
2016-01-29 19:13:01 +00:00
|
|
|
require "./database"
|
2016-01-28 22:41:12 +00:00
|
|
|
require "./driver"
|
2016-01-30 22:46:43 +00:00
|
|
|
require "./connection"
|
2016-01-28 23:31:35 +00:00
|
|
|
require "./statement"
|
|
|
|
require "./result_set"
|