Skip to content

Instantly share code, notes, and snippets.

@plukevdh
Created May 13, 2010 13:26
Show Gist options
  • Save plukevdh/399826 to your computer and use it in GitHub Desktop.
Save plukevdh/399826 to your computer and use it in GitHub Desktop.
oracle + rails3 stuff
# monkey patches for Rails 3 beta that are needed for oracle_enhanced adapter
require 'rails3_oracle_patches'
# it's recommended to set time zone to ensure that Oracle session time zone
# will be the same as Ruby Time.local time zone
ENV['TZ'] = 'Eastern Time (US & Canada)'
ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.class_eval do
# some sample settings that I use in my projects
self.emulate_integers_by_column_name = true
self.emulate_dates_by_column_name = true
self.emulate_booleans_from_strings = true
# set string to date format if using e.g. calendar helpers
# self.string_to_date_format = "%d.%m.%Y"
# self.string_to_time_format = "%d.%m.%Y %H:%M:%S"
# to ensure that sequences will start from 1 and without gaps
self.default_sequence_start_value = "1 NOCACHE INCREMENT BY 1"
end
# PL/SQL connection
plsql.activerecord_class = ActiveRecord::Base
# Cache column descriptions between requests.
# Highly recommended as currently Arel is doing a lot of additional queries
# to get table columns and primary key.
# If this is used then you need to restart server in development environment
# after running migrations which change table columns.
if ['development', 'test', 'production'].include? Rails.env
ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.cache_columns = true
end
# patches for Rails 3.0 beta1 that are required by oracle_enhanced adapter
# PATCH: downcase table names in aliased_table_name_for and references_eager_loaded_tables? methods
# (as Oracle quoted table names are in uppercase)
ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.class_eval do
protected
def aliased_table_name_for(name, suffix = nil)
# always downcase quoted table name as Oracle quoted table names are in uppercase
if !parent.table_joins.blank? && parent.table_joins.to_s.downcase =~ %r{join(\s+\w+)?\s+#{active_record.connection.quote_table_name(name).downcase}\son}
@join_dependency.table_aliases[name] += 1
end
unless @join_dependency.table_aliases[name].zero?
# if the table name has been used, then use an alias
name = active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}#{suffix}"
table_index = @join_dependency.table_aliases[name]
@join_dependency.table_aliases[name] += 1
name = name[0..active_record.connection.table_alias_length-3] + "_#{table_index+1}" if table_index > 0
else
@join_dependency.table_aliases[name] += 1
end
name
end
end
# PATCH: fix conditions when DateTime#to_date and DateTime#xmlschema methods are defined
ActiveRecord::Relation.class_eval do
private
def references_eager_loaded_tables?
# always convert table names to downcase as in Oracle quoted table names are in uppercase
joined_tables = (tables_in_string(arel.joins(arel)) + [table.name, table.table_alias]).compact.map(&:downcase).uniq
(tables_in_string(to_sql) - joined_tables).any?
end
def tables_in_string(string)
return [] if string.blank?
# always convert table names to downcase as in Oracle quoted table names are in uppercase
string.scan(/([a-zA-Z_][\.\w]+).?\./).flatten.map(&:downcase).uniq
end
end
# PATCH: fix conditions when DateTime#to_date and DateTime#xmlschema methods are defined
class DateTime
# Converts self to a Ruby Date object; time portion is discarded
def to_date
::Date.new(year, month, day)
end unless instance_methods(false).include?(:to_date)
# Converts datetime to an appropriate format for use in XML
def xmlschema
strftime("%Y-%m-%dT%H:%M:%S%Z")
end unless instance_methods(false).include?(:xmlschema)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment