Created
November 22, 2011 13:45
-
-
Save ciaranarcher/1385695 to your computer and use it in GitHub Desktop.
Base Database Module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "singleton" | |
module SIA | |
# Singleton class to enable database access for those occasions where a model will just not do. | |
# @author Ciaran Archer | |
class Database | |
include Singleton | |
@databases = nil | |
# Setup database connections with configuration file. | |
# @author Ciaran Archer | |
# @param [String] config @todo this may have to be adjusted to cater for multiple database hosts | |
# @return self | |
def setup(config) | |
raise ArgumentError, "database_config is required" unless config.is_a? Hash | |
@databases = { | |
:sia => Sequel.connect("jdbc:jtds:sqlserver://#{config[:host]}/SIA;user=#{config[:username]};password=#{config[:password]}"), | |
:br => Sequel.connect("jdbc:jtds:sqlserver://#{config[:host]}/BR;user=#{config[:username]};password=#{config[:password]}") | |
} | |
self | |
end | |
# Return a set of database connections as a hash indexed by database name. | |
# @author Ciaran Archer | |
# @return Hash | |
# @raise [LoadError] if the database has not been configured. | |
def databases | |
raise LoadError, "database has not been configured" unless @databases.is_a? Hash | |
@databases | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SIA | |
module SIAModels | |
@sia_db = SIA::Database.instance.databases[:sia] | |
# Model to represent the Clients table. | |
# @author Ciaran Archer | |
class Client < Sequel::Model(@sia_db[:clients]) | |
set_primary_key [:clientid] | |
def self.get_all | |
dataset.filter("clientid > 0") | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get "/all" do | |
total_time = Benchmark.realtime do | |
results = SIA::SIAModels::Client.get_all | |
results.each do |c| | |
puts c[:firstname] | |
end | |
end | |
puts "done in #{total_time}" | |
"done in #{total_time}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment