method
translate_sql
v1.1.6 -
Show latest stable
- Class:
ActiveRecord::ConnectionAdapters::OpenBaseAdapter
translate_sql(sql)private
No documentation available.
# File activerecord/lib/active_record/connection_adapters/openbase_adapter.rb, line 278
def translate_sql(sql)
# Change table.* to list of columns in table
while (sql =~ /SELECT.*\s(\w+)\.\*/)
table = $1
cols = columns(table)
if ( cols.size == 0 ) then
# Maybe this is a table alias
sql =~ /FROM(.+?)(?:LEFT|OUTER|JOIN|WHERE|GROUP|HAVING|ORDER|RETURN|$)/
$1 =~ /[\s|,](\w+)\s+#{table}[\s|,]/ # get the tablename for this alias
cols = columns($1)
end
select_columns = []
cols.each do |col|
select_columns << table + '.' + col.name
end
sql.gsub!(table + '.*',select_columns.join(", ")) if select_columns
end
# Change JOIN clause to table list and WHERE condition
while (sql =~ /JOIN/)
sql =~ /((LEFT )?(OUTER )?JOIN (\w+) ON )(.+?)(?:LEFT|OUTER|JOIN|WHERE|GROUP|HAVING|ORDER|RETURN|$)/
join_clause = $1 + $5
is_outer_join = $3
join_table = $4
join_condition = $5
join_condition.gsub!(/=/,"*") if is_outer_join
if (sql =~ /WHERE/)
sql.gsub!(/WHERE/,"WHERE (#{join_condition}) AND")
else
sql.gsub!(join_clause,"#{join_clause} WHERE #{join_condition}")
end
sql =~ /(FROM .+?)(?:LEFT|OUTER|JOIN|WHERE|$)/
from_clause = $1
sql.gsub!(from_clause,"#{from_clause}, #{join_table} ")
sql.gsub!(join_clause,"")
end
# ORDER BY _rowid if no explicit ORDER BY
# This will ensure that find(:first) returns the first inserted row
if (sql !~ /(ORDER BY)|(GROUP BY)/)
if (sql =~ /RETURN RESULTS/)
sql.sub!(/RETURN RESULTS/,"ORDER BY _rowid RETURN RESULTS")
else
sql << " ORDER BY _rowid"
end
end
sql
end