diff --git a/lib/arjdbc/derby/adapter.rb b/lib/arjdbc/derby/adapter.rb index fa33199e6..5ef91d4a7 100644 --- a/lib/arjdbc/derby/adapter.rb +++ b/lib/arjdbc/derby/adapter.rb @@ -181,16 +181,18 @@ def add_column(table_name, column_name, type, options = {}) end def execute(sql, name = nil, binds = []) - sql = extract_sql(sql) + sql = to_sql(sql) if sql =~ /\A\s*(UPDATE|INSERT)/i - i = sql =~ /\swhere\s/im - if i - sql[i..-1] = sql[i..-1].gsub(/!=\s*NULL/, 'IS NOT NULL').gsub(/=\sNULL/i, 'IS NULL') + if ( i = sql =~ /\sWHERE\s/im ) + where_part = sql[i..-1]; sql = sql.dup + where_part.gsub!(/!=\s*NULL/, 'IS NOT NULL') + where_part.gsub!(/=\sNULL/i, 'IS NULL') + sql[i..-1] = where_part end else - sql.gsub!(/= NULL/i, 'IS NULL') + sql = sql.gsub(/=\sNULL/i, 'IS NULL') end - super + super(sql, name, binds) end # SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause. diff --git a/lib/arjdbc/mssql/adapter.rb b/lib/arjdbc/mssql/adapter.rb index 97e39c44e..d5a66322e 100644 --- a/lib/arjdbc/mssql/adapter.rb +++ b/lib/arjdbc/mssql/adapter.rb @@ -4,7 +4,7 @@ require 'arjdbc/mssql/lock_helpers' require 'arjdbc/jdbc/serialized_attributes_helper' -module ::ArJdbc +module ArJdbc module MsSQL include TSqlMethods include LimitHelpers @@ -215,9 +215,9 @@ def quote(value, column = nil) else super end - when TrueClass then '1' - when FalseClass then '0' - else super + when TrueClass then '1' + when FalseClass then '0' + else super end end @@ -367,36 +367,6 @@ def columns(table_name, name = nil) @table_columns[table_name] end - def _execute(sql, name = nil) - # Match the start of the sql to determine appropriate behaviour. Be aware of - # multi-line sql which might begin with 'create stored_proc' and contain 'insert into ...' lines. - # Possible improvements include ignoring comment blocks prior to the first statement. - if sql.lstrip =~ /\Ainsert/i - if query_requires_identity_insert?(sql) - table_name = get_table_name(sql) - with_identity_insert_enabled(table_name) do - id = @connection.execute_insert(sql) - end - else - @connection.execute_insert(sql) - end - elsif sql.lstrip =~ /\A(create|exec)/i - @connection.execute_update(sql) - elsif sql.lstrip =~ /\A\(?\s*(select|show)/i - repair_special_columns(sql) - @connection.execute_query(sql) - else - @connection.execute_update(sql) - end - end - - def select(sql, name = nil, binds = []) - sql = substitute_binds(sql, binds) - log(sql, name) do - @connection.execute_query(sql) - end - end - # Turns IDENTITY_INSERT ON for table during execution of the block # N.B. This sets the state of IDENTITY_INSERT to OFF after the # block has been executed without regard to its previous state @@ -476,6 +446,32 @@ def clear_cached_table(name) def reset_column_information @table_columns = nil end + + private + + def _execute(sql, name = nil) + # Match the start of the SQL to determine appropriate behavior. + # Be aware of multi-line SQL which might begin with 'create stored_proc' + # and contain 'insert into ...' lines. + # TODO test and refactor using `self.class.insert?(sql)` etc + # NOTE: ignoring comment blocks prior to the first statement ?! + if sql.lstrip =~ /\Ainsert/i # self.class.insert?(sql) + if query_requires_identity_insert?(sql) + table_name = get_table_name(sql) + with_identity_insert_enabled(table_name) do + @connection.execute_insert(sql) + end + else + @connection.execute_insert(sql) + end + elsif sql.lstrip =~ /\A\(?\s*(select|show)/i # self.class.select?(sql) + repair_special_columns(sql) + @connection.execute_query(sql) + else # sql.lstrip =~ /\A(create|exec)/i + @connection.execute_update(sql) + end + end + end end diff --git a/lib/arjdbc/postgresql/adapter.rb b/lib/arjdbc/postgresql/adapter.rb index e6bcf0403..4589d29d6 100644 --- a/lib/arjdbc/postgresql/adapter.rb +++ b/lib/arjdbc/postgresql/adapter.rb @@ -377,7 +377,7 @@ def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil, b if supports_insert_with_returning? && id_value.nil? pk, sequence_name = *pk_and_sequence_for(table) unless pk if pk - sql = substitute_binds(sql, binds) + sql = to_sql(sql, binds) id_value = select_value("#{sql} RETURNING #{quote_column_name(pk)}") clear_query_cache #FIXME: Why now? return id_value