Skip to content

Commit

Permalink
Moving the connection selection logic down into the execute methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
russ authored and anykeyh committed Jun 24, 2018
1 parent e21b562 commit 3caf1d6
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 48 deletions.
6 changes: 3 additions & 3 deletions src/clear/model/modules/has_saving.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ module Clear::Model::HasSaving

if h.any?
with_triggers(:update) do
Clear::SQL.update(@@connection, self.class.table).set(update_h).where { var("#{self.class.pkey}") == pkey }.execute
Clear::SQL.update(self.class.table).set(update_h).where { var("#{self.class.pkey}") == pkey }.execute(@@connection)
end
end
else
with_triggers(:create) do
@persisted = true
hash = Clear::SQL.insert_into(@@connection, self.class.table, to_h).returning("*").execute
hash = Clear::SQL.insert_into(self.class.table, to_h).returning("*").execute(@@connection)
self.set(hash)
end
end
Expand Down Expand Up @@ -54,7 +54,7 @@ module Clear::Model::HasSaving
return false unless persisted?

with_triggers(:delete) do
Clear::SQL::DeleteQuery.new(@@connection).from(self.class.table).where{ var("#{self.class.pkey}") == pkey }.execute
Clear::SQL::DeleteQuery.new.from(self.class.table).where{ var("#{self.class.pkey}") == pkey }.execute(@@connection)

@persisted = false
clear_change_flags
Expand Down
8 changes: 0 additions & 8 deletions src/clear/sql/delete_query.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ class Clear::SQL::DeleteQuery

def initialize(@from = nil,
@wheres = [] of Clear::Expression::Node)
@connection = "default"
@connection_name = "default"
end

def initialize(@connection : String,
@from = nil,
@wheres = [] of Clear::Expression::Node)
@connection_name = @connection
end

def from(x)
Expand Down
14 changes: 5 additions & 9 deletions src/clear/sql/insert_query.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,13 @@ class Clear::SQL::InsertQuery
getter returning : String?

def initialize(@table : Selectable)
@connection = "default"
end

def initialize(@connection : String, @table : Selectable)
end

def fetch(&block : Hash(String, ::Clear::SQL::Any) -> Void)
def fetch(connection_name : String = "default", &block : Hash(String, ::Clear::SQL::Any) -> Void)
Clear::SQL.log_query to_sql do
h = {} of String => ::Clear::SQL::Any

Clear::SQL.connection(@connection).query(to_sql) do |rs|
Clear::SQL.connection(connection_name).query(to_sql) do |rs|
fetch_result_set(h, rs) { |x| yield(x) }
end
end
Expand All @@ -61,14 +57,14 @@ class Clear::SQL::InsertQuery
rs.close
end

def execute : Hash(String, ::Clear::SQL::Any)
def execute(connection_name : String = "default") : Hash(String, ::Clear::SQL::Any)
o = {} of String => ::Clear::SQL::Any

if @returning.nil?
Clear::SQL.execute(self.connection_name, to_sql)
Clear::SQL.execute(connection_name, to_sql)
else
# return {} of String => ::Clear::SQL::Any
fetch { |x| o = x; break }
fetch(connection_name) { |x| o = x; break }
end

o
Expand Down
4 changes: 2 additions & 2 deletions src/clear/sql/query/execute.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "db"

module Clear::SQL::Query::Execute
def execute
Clear::SQL.execute(self.connection_name, to_sql)
def execute(connection_name : String = "default")
Clear::SQL.execute(connection_name, to_sql)
end
end
24 changes: 6 additions & 18 deletions src/clear/sql/sql.cr
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ module Clear
#
# Usage:
# Clear::SQL.execute("seconddatabase", "SELECT 1 FROM users")
def execute(connection : Symbolic, sql)
log_query(sql) { Clear::SQL.connection(connection).exec(sql) }
def execute(connection_name : String, sql)
log_query(sql) { Clear::SQL.connection(connection_name).exec(sql) }
end

# :nodoc:
Expand All @@ -183,33 +183,21 @@ module Clear

# Start an INSERT INTO table query
def insert_into(table, *args)
Clear::SQL::InsertQuery.new("default", table).insert(*args)
end

def insert_into(connection : Symbolic, table, *args)
Clear::SQL::InsertQuery.new(connection, table).insert(*args)
Clear::SQL::InsertQuery.new(table).insert(*args)
end

# Alias of `insert_into`, for hurry developers
def insert(table, *args)
insert_into("default", table, *args)
insert_into(table, *args)
end

def insert(table, args : NamedTuple)
insert_into("default", table, args)
end

def insert(connection : Symbolic, table, *args)
insert_into(connection, table, *args)
insert_into(table, args)
end

# Start a UPDATE table query
def update(table)
Clear::SQL::UpdateQuery.new("default", table)
end

def update(connection : Symbolic, table)
Clear::SQL::UpdateQuery.new(connection, table)
Clear::SQL::UpdateQuery.new(table)
end

# Start a SELECT FROM table query
Expand Down
8 changes: 0 additions & 8 deletions src/clear/sql/update_query.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class Clear::SQL::UpdateQuery
alias UpdateInstruction = Hash(String, Updatable) | String

@values : Array(UpdateInstruction) = [] of UpdateInstruction
@connection : String
@table : String

include Query::Connection
Expand All @@ -18,13 +17,6 @@ class Clear::SQL::UpdateQuery

def initialize(table, @wheres = [] of Clear::Expression::Node)
@table = table.to_s
@connection = "default"
@connection_name = "default"
end

def initialize(@connection : String, table, @wheres = [] of Clear::Expression::Node)
@table = table.to_s
@connection_name = @connection
end

def set(row : NamedTuple)
Expand Down

0 comments on commit 3caf1d6

Please sign in to comment.