Class: ConnectionAdapters::MysqlWithViewsAdapter

Inherits:
ActiveRecord::ConnectionAdapters::MysqlAdapter
  • Object
show all
Defined in:
lib/active_record/connection_adapters/mysql_with_views_adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ MysqlWithViewsAdapter

Returns a new instance of MysqlWithViewsAdapter.



14
15
16
17
# File 'lib/active_record/connection_adapters/mysql_with_views_adapter.rb', line 14

def initialize(config)
  super ActiveRecord::Base.mysql_connection(config)
  @view_options = (config.symbolize_keys[:view_options] || {}).symbolize_keys.freeze
end

Instance Attribute Details

#view_optionsObject (readonly)

Returns the value of attribute view_options.



13
14
15
# File 'lib/active_record/connection_adapters/mysql_with_views_adapter.rb', line 13

def view_options
  @view_options
end

Instance Method Details

#create_view(name, column_names = nil, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
# File 'lib/active_record/connection_adapters/mysql_with_views_adapter.rb', line 19

def create_view(name, column_names = nil, options = {})
  raise ArgumentError, "You must provide the SQL statement that defines the view." unless block_given?
  select_statement = yield
  command = ["CREATE"]
  command << "OR REPLACE" if options[:force]
  command << "VIEW #{quote_table_name(name)}"
  command << "(" + [column_names].flatten.join(", ") + ")" if column_names
  command << "AS #{select_statement}"
  execute command.join(" ")
end

#drop_view(name) ⇒ Object



34
35
36
# File 'lib/active_record/connection_adapters/mysql_with_views_adapter.rb', line 34

def drop_view(name)
  execute "DROP VIEW #{quote_table_name(name)}"
end

#pk_and_sequence_for(table) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/active_record/connection_adapters/mysql_with_views_adapter.rb', line 46

def pk_and_sequence_for(table)
  if table_key = super(table)
    table_key
  elsif views.include?( table.to_s ) and columns(table).any?{|col| col.name == 'id'}
    view_key = ['id', nil]
  end
end

#update_view(name, column_names = nil, options = {}, &block) ⇒ Object



30
31
32
# File 'lib/active_record/connection_adapters/mysql_with_views_adapter.rb', line 30

def update_view(name, column_names = nil, options = {}, &block)
  create_view name, column_names, options.merge(:force => true), &block
end

#views(name = nil) ⇒ Object

:nodoc:



38
39
40
41
42
43
44
# File 'lib/active_record/connection_adapters/mysql_with_views_adapter.rb', line 38

def views(name = nil) #:nodoc:
  tables = []
  result = execute("SHOW FULL TABLES", name)
  result.each { |field| tables << field[0] if field[1] == 'VIEW' }
  result.free
  tables
end