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-02-03 22:30:51 +00:00
|
|
|
getter driver
|
2016-01-31 22:40:02 +00:00
|
|
|
|
2016-02-03 22:30:51 +00:00
|
|
|
# Returns the uri with the connection settings to the database
|
2016-02-03 21:29:09 +00:00
|
|
|
getter uri
|
2016-01-29 19:13:01 +00:00
|
|
|
|
2016-01-31 22:40:02 +00:00
|
|
|
# :nodoc:
|
2016-02-03 22:30:51 +00:00
|
|
|
def initialize(@driver, @uri)
|
|
|
|
@in_pool = true
|
|
|
|
@connection = @driver.build_connection(self)
|
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
|
2016-02-03 22:30:51 +00:00
|
|
|
@connection.try &.close
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
|
|
|
|
2016-02-02 00:55:30 +00:00
|
|
|
# :nodoc:
|
2016-02-03 22:30:51 +00:00
|
|
|
def prepare(query)
|
|
|
|
get_from_pool.prepare(query)
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
|
|
|
|
2016-02-03 19:57:54 +00:00
|
|
|
# :nodoc:
|
2016-02-03 22:30:51 +00:00
|
|
|
def get_from_pool
|
|
|
|
raise "DB Pool Exhausted" unless @in_pool
|
|
|
|
@in_pool = false
|
|
|
|
@connection.not_nil!
|
|
|
|
end
|
|
|
|
|
|
|
|
# :nodoc:
|
|
|
|
def return_to_pool(connection)
|
|
|
|
@in_pool = true
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
|
|
|
|
2016-01-31 17:01:52 +00:00
|
|
|
include QueryMethods
|
2016-01-29 19:13:01 +00:00
|
|
|
end
|
|
|
|
end
|