Skip to content

Commit

Permalink
Supporting multiple connections.
Browse files Browse the repository at this point in the history
This is the first I've gotten this to work. Some refactoring needs to be
done as well as specs.
  • Loading branch information
russ authored and anykeyh committed Jun 24, 2018
1 parent 64817f4 commit 9c45705
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/clear/model/model.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Clear::Model
include Clear::Model::HasValidation
include Clear::Model::HasRelations
include Clear::Model::HasScope
include Clear::Model::Connection
include Clear::Model::ClassMethods
include Clear::Model::IsPolymorphic

Expand Down
2 changes: 1 addition & 1 deletion src/clear/model/modules/class_methods.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Clear::Model::ClassMethods
class Collection < Clear::Model::CollectionBase(\{{@type}}); end

def self.query
Collection.new.from(table)
Collection.new.use_connection(connection).from(table)
end

def self.find(x)
Expand Down
7 changes: 7 additions & 0 deletions src/clear/model/modules/connection.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Clear::Model::Connection
macro included # When included into Model
macro included # When included into final Model
class_property connection : Clear::SQL::Symbolic = "default"
end
end
end
3 changes: 2 additions & 1 deletion src/clear/sql/insert_query.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require "./query/*"
#
class Clear::SQL::InsertQuery
include Query::Change
include Query::Connection

alias Inserable = ::Clear::SQL::Any | BigInt | BigFloat | Time
getter keys : Array(Symbolic) = [] of Symbolic
Expand All @@ -32,7 +33,7 @@ class Clear::SQL::InsertQuery
Clear::SQL.log_query to_sql do
h = {} of String => ::Clear::SQL::Any

Clear::SQL.connection.query(to_sql) do |rs|
Clear::SQL.connection(self.connection_name).query(to_sql) do |rs|
fetch_result_set(h, rs) { |x| yield(x) }
end
end
Expand Down
9 changes: 9 additions & 0 deletions src/clear/sql/query/connection.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Clear::SQL
module Query::Connection
getter connection_name : Symbolic = "default"

def use_connection(@connection_name : Symbolic)
self
end
end
end
9 changes: 5 additions & 4 deletions src/clear/sql/query/fetch.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module Clear::SQL::Query::Fetch
trigger_before_query

Clear::SQL.transaction do
cnx = Clear::SQL.connection
cnx = Clear::SQL.connection(self.connection_name)
cursor_name = "__cursor_#{Time.now.epoch ^ (rand * 0xfffffff).to_i}__"

cursor_declaration = "DECLARE #{cursor_name} CURSOR FOR #{to_sql}"
Expand Down Expand Up @@ -55,7 +55,7 @@ module Clear::SQL::Query::Fetch
trigger_before_query

Clear::SQL.log_query to_sql do
Clear::SQL.connection.scalar(to_sql).as(T)
Clear::SQL.connection(self.connection_name).scalar(to_sql).as(T)
end
end

Expand All @@ -71,7 +71,7 @@ module Clear::SQL::Query::Fetch
to_sql = self.to_sql

rs = uninitialized PG::ResultSet
Clear::SQL.log_query(to_sql) { rs = Clear::SQL.connection.query(to_sql) }
Clear::SQL.log_query(to_sql) { rs = Clear::SQL.connection(self.connection_name).query(to_sql) }

o = [] of Hash(String, ::Clear::SQL::Any)
fetch_result_set(h, rs) { |x| o << x.dup }
Expand All @@ -92,7 +92,8 @@ module Clear::SQL::Query::Fetch
to_sql = self.to_sql

rs = uninitialized PG::ResultSet
Clear::SQL.log_query(to_sql) { rs = Clear::SQL.connection.query(to_sql) }

Clear::SQL.log_query(to_sql) { rs = Clear::SQL.connection(connection_name).query(to_sql) }

if fetch_all
o = [] of Hash(String, ::Clear::SQL::Any)
Expand Down
1 change: 1 addition & 0 deletions src/clear/sql/select_builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Clear::SQL::SelectBuilder
end

include Query::Change
include Query::Connection
include Query::Select
include Query::From
include Query::Join
Expand Down
26 changes: 19 additions & 7 deletions src/clear/sql/sql.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ module Clear
include Clear::SQL::Logger
extend self

@@connection : DB::Database?
@@connections : Hash(Symbolic, DB::Database?)?

def self.connection : DB::Database
if @@connection
@@connection.not_nil!
def self.connection(connection) : DB::Database
if @@connections.nil?
raise "No database connections have been defined"
else
raise "The database connection is not initialized"
if connection = @@connections.not_nil![connection]
connection.not_nil!
else
raise "The database connection is not initialized"
end
end
end

Expand All @@ -68,7 +72,15 @@ module Clear
end

def init(url : String)
@@connection = DB.open(url)
@@connections ||= {} of Symbolic => DB::Database?
@@connections.not_nil!["default"] = DB.open(url)
end

def init(connections : Hash(Symbolic, String))
@@connections ||= {} of Symbolic => DB::Database?
connections.each do |key, connection_string|
@@connections.not_nil![key] = DB.open(connection_string)
end
end

@@in_transaction : Bool = false
Expand Down Expand Up @@ -146,7 +158,7 @@ module Clear
# Clear::SQL.execute("SELECT 1 FROM users")
#
def execute(sql)
log_query(sql) { Clear::SQL.connection.exec(sql) }
log_query(sql) { Clear::SQL.connection("default").exec(sql) }
end

# :nodoc:
Expand Down

0 comments on commit 9c45705

Please sign in to comment.