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.
|
|
|
|
#
|
2016-01-31 22:40:02 +00:00
|
|
|
# It should be created from DB module. See `DB#open`.
|
|
|
|
#
|
|
|
|
# Refer to `QueryMethods` for documentation about querying the database.
|
2016-01-29 19:13:01 +00:00
|
|
|
class Database
|
2016-01-31 22:40:02 +00:00
|
|
|
# :nodoc:
|
2016-01-29 19:13:01 +00:00
|
|
|
getter driver_class
|
2016-01-31 22:40:02 +00:00
|
|
|
|
|
|
|
# Connection configuration to the database.
|
2016-01-31 20:30:21 +00:00
|
|
|
getter connection_string
|
2016-01-29 19:13:01 +00:00
|
|
|
|
2016-01-31 22:40:02 +00:00
|
|
|
# :nodoc:
|
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-31 22:40:02 +00:00
|
|
|
# Closes all connection to the database.
|
2016-01-30 22:46:43 +00:00
|
|
|
def close
|
|
|
|
@connection.close
|
|
|
|
end
|
|
|
|
|
2016-02-02 00:55:30 +00:00
|
|
|
# :nodoc:
|
2016-01-30 22:46:43 +00:00
|
|
|
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
|