Class: ActiveRecord::ConnectionAdapters::ViewDefinition

Inherits:
Object
  • Object
show all
Includes:
SchemaProcs
Defined in:
lib/connection_adapters/view_definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SchemaProcs

#behavior, #cascade_or_restrict, #definer_or_invoker, #strict_or_null

Constructor Details

#initialize(id, name, columns = [], &block) ⇒ ViewDefinition

Returns a new instance of ViewDefinition.



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

def initialize(id, name, columns=[], &block)
  @id            = id
  self.name      = name
  self.columns   = columns
  self.view_body = block
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



5
6
7
# File 'lib/connection_adapters/view_definition.rb', line 5

def columns
  @columns
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/connection_adapters/view_definition.rb', line 5

def id
  @id
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/connection_adapters/view_definition.rb', line 5

def name
  @name
end

#view_bodyObject

Returns the value of attribute view_body.



5
6
7
# File 'lib/connection_adapters/view_definition.rb', line 5

def view_body
  @view_body
end

Instance Method Details

#to_rdlObject



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

def to_rdl
     "  create_view(#{ActiveSupport::Inflector.symbolize(name)}) { $#{name}_body$\n    #{view_body.call}\n  $#{name}_body$ }"
end

#to_sql(action = "create", options = {}) ⇒ Object

CREATE [ OR REPLACE ] [ TEMP | TEMPORARY ] VIEW NAME [ ( column_name [, …] ) ]

AS query
[ WITH [ CASCADED | LOCAL ] CHECK OPTION ]

DROP VIEW [ IF EXISTS ] NAME [, …] [ CASCADE | RESTRICT ]



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/connection_adapters/view_definition.rb', line 21

def to_sql(action="create", options={})
  case action
    when "create", :create
      ret = "CREATE OR REPLACE#{' TEMPORARY' if options[:temp] } VIEW #{name.to_sql_name}
      AS #{view_body.call}"
        # TODO - [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
    when "drop", :drop
      ret = "DROP VIEW #{name.to_sql_name} #{cascade_or_restrict(options[:cascade])}"
  end
  ret
end