2016-01-29 19:13:01 +00:00
|
|
|
module DB
|
|
|
|
# Acts as an entry point for database access.
|
2016-01-30 22:46:43 +00:00
|
|
|
# Currently it creates a single connection to the database.
|
|
|
|
# Eventually a connection pool will be handled.
|
|
|
|
#
|
|
|
|
# It should be created from DB module. See `DB.open`.
|
2016-01-29 19:13:01 +00:00
|
|
|
class Database
|
|
|
|
getter driver_class
|
2016-01-31 20:30:21 +00:00
|
|
|
getter connection_string
|
2016-01-29 19:13:01 +00:00
|
|
|
|
2016-01-31 20:30:21 +00:00
|
|
|
def initialize(@driver_class, @connection_string)
|
|
|
|
@driver = @driver_class.new(@connection_string)
|
2016-01-30 22:46:43 +00:00
|
|
|
@connection = @driver.build_connection
|
2016-01-29 19:13:01 +00:00
|
|
|
end
|
|
|
|
|
2016-01-30 22:46:43 +00:00
|
|
|
# Closes all connection to the database
|
|
|
|
def close
|
|
|
|
@connection.close
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a `Connection` to the database
|
|
|
|
def connection
|
|
|
|
@connection
|
|
|
|
end
|
|
|
|
|
|
|
|
# Prepares a `Statement`. The Statement must be closed explicitly
|
|
|
|
# after is not longer in use.
|
|
|
|
#
|
|
|
|
# Usually `#exec`, `#query` or `#scalar` should be used.
|
2016-01-29 19:13:01 +00:00
|
|
|
def prepare(query)
|
2016-01-30 22:46:43 +00:00
|
|
|
connection.prepare(query)
|
|
|
|
end
|
|
|
|
|
2016-01-31 17:01:52 +00:00
|
|
|
include QueryMethods
|
2016-01-29 19:13:01 +00:00
|
|
|
end
|
|
|
|
end
|