diff --git a/src/clear/model/modules/has_saving.cr b/src/clear/model/modules/has_saving.cr index 47b319abf..0f7457e46 100644 --- a/src/clear/model/modules/has_saving.cr +++ b/src/clear/model/modules/has_saving.cr @@ -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 @@ -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 diff --git a/src/clear/sql/delete_query.cr b/src/clear/sql/delete_query.cr index 57e0c1fcc..f770c5bd4 100644 --- a/src/clear/sql/delete_query.cr +++ b/src/clear/sql/delete_query.cr @@ -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) diff --git a/src/clear/sql/insert_query.cr b/src/clear/sql/insert_query.cr index fd473028e..3fff1e61a 100644 --- a/src/clear/sql/insert_query.cr +++ b/src/clear/sql/insert_query.cr @@ -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 @@ -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 diff --git a/src/clear/sql/query/execute.cr b/src/clear/sql/query/execute.cr index 1da7e06ce..31b6ddce6 100644 --- a/src/clear/sql/query/execute.cr +++ b/src/clear/sql/query/execute.cr @@ -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 diff --git a/src/clear/sql/sql.cr b/src/clear/sql/sql.cr index 51dd9a34c..a4a551cb1 100644 --- a/src/clear/sql/sql.cr +++ b/src/clear/sql/sql.cr @@ -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: @@ -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 diff --git a/src/clear/sql/update_query.cr b/src/clear/sql/update_query.cr index 5c00807ef..40ddb661f 100644 --- a/src/clear/sql/update_query.cr +++ b/src/clear/sql/update_query.cr @@ -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 @@ -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)