Module: RailsSqlViews::ConnectionAdapters::MysqlAdapter

Defined in:
lib/rails_sql_views/connection_adapters/mysql_adapter.rb

Constant Summary collapse

REQUIRED_METHODS =
[:supports_views?]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/rails_sql_views/connection_adapters/mysql_adapter.rb', line 6

def self.included(base)
  base.class_eval do
    def self.method_added(method)
      public(method) if REQUIRED_METHODS.include?(method) && !self.public_method_defined?(method)
    end
  end
end

Instance Method Details

#base_tables(name = nil) ⇒ Object Also known as: nonview_tables

:nodoc:



19
20
21
22
23
# File 'lib/rails_sql_views/connection_adapters/mysql_adapter.rb', line 19

def base_tables(name = nil) #:nodoc:
  tables = []
  execute("SHOW FULL TABLES WHERE TABLE_TYPE='BASE TABLE'").each{|row| tables << row[0]}
  tables
end

#structure_dumpObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rails_sql_views/connection_adapters/mysql_adapter.rb', line 36

def structure_dump
  structure = ""
  base_tables.each do |table|
    structure += select_one("SHOW CREATE TABLE #{quote_table_name(table)}")["Create Table"] + ";\n\n"
  end

  views.each do |view|
    structure += select_one("SHOW CREATE VIEW #{quote_table_name(view)}")["Create View"] + ";\n\n"
  end

  return structure
end

#supports_views?Boolean

Returns true as this adapter supports views.

Returns:

  • (Boolean)


15
16
17
# File 'lib/rails_sql_views/connection_adapters/mysql_adapter.rb', line 15

def supports_views?
  true
end

#tables_with_views_included(name = nil) ⇒ Object



32
33
34
# File 'lib/rails_sql_views/connection_adapters/mysql_adapter.rb', line 32

def tables_with_views_included(name = nil)
  nonview_tables(name) + views(name)
end

#view_select_statement(view, name = nil) ⇒ Object

Get the view select statement for the specified table.



50
51
52
53
54
55
56
57
58
# File 'lib/rails_sql_views/connection_adapters/mysql_adapter.rb', line 50

def view_select_statement(view, name=nil)
  begin
    row = execute("SHOW CREATE VIEW #{view}", name).each do |row|
      return convert_statement(row[1]) if row[0] == view
    end
  rescue ActiveRecord::StatementInvalid => e
    raise "No view called #{view} found"
  end
end

#views(name = nil) ⇒ Object

:nodoc:



26
27
28
29
30
# File 'lib/rails_sql_views/connection_adapters/mysql_adapter.rb', line 26

def views(name = nil) #:nodoc:
  views = []
  execute("SHOW FULL TABLES WHERE TABLE_TYPE='VIEW'").each{|row| views << row[0]}
  views
end