Skip to content

Commit

Permalink
avoid private API substitute_binds and extract_sql
Browse files Browse the repository at this point in the history
besides on derby make sure we never modify the passed sql param
  • Loading branch information
kares committed Jan 30, 2013
1 parent 0231c06 commit 44919f2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 41 deletions.
14 changes: 8 additions & 6 deletions lib/arjdbc/derby/adapter.rb
Expand Up @@ -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.
Expand Down
64 changes: 30 additions & 34 deletions lib/arjdbc/mssql/adapter.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

2 changes: 1 addition & 1 deletion lib/arjdbc/postgresql/adapter.rb
Expand Up @@ -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
Expand Down

0 comments on commit 44919f2

Please sign in to comment.