This repository has been archived by the owner on May 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
rails_sequel.rb
46 lines (40 loc) · 1.71 KB
/
rails_sequel.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module Rails
module SequelConnection
# Load configuration for current environment
CONFIG = YAML::load(ERB.new(IO.read(Rails.root + "/config/database.yml")).result)[Rails.env].with_indifferent_access unless defined?(CONFIG)
# Connects to database using constructed Database Connection URI
def self.connect
options = self.prepare_options
connection = Sequel.connect(options)
if options[:adapter] == 'mysql'
connection.execute("SET SQL_AUTO_IS_NULL=0")
end
end
# Returns loaded database.yml configuration for current environment
def self.config
CONFIG
end
# Constructs Database Connection URI
def self.prepare_options
options = {}
# Use SQLite by default
options[:adapter] = (config[:adapter] || "sqlite")
# Use localhost as default host
options[:host] = (config[:host] || "localhost")
# Default user is an empty string. Both username and user keys are supported.
options[:user] = (config[:username] || config[:user] || "")
options[:password] = config[:password] || ""
# Both encoding and charset options are supported, default is utf8
options[:encoding] = (config[:encoding] || config[:charset] || "utf8")
# Default database is hey_dude_configure_your_database
options[:database] = config[:database] || "hey_dude_configure_your_database"
# MSSQL support
options[:db_type] = config[:db_type] if config[:db_type]
options[:socket] = config[:socket] if config[:socket]
options[:charset] = config[:charset] if config[:charset]
options[:encoding] = config[:encoding] if config[:encoding]
options[:loggers] = [Rails.logger]
options
end
end
end