Class: Arel::Table

Inherits:
Object
  • Object
show all
Includes:
Recursion::BaseCase, Relation
Defined in:
lib/arel/engines/sql/relations/table.rb

Instance Attribute Summary collapse

Attributes included from Relation

#count

Instance Method Summary collapse

Methods included from Relation

#bind, #call, #christener, #compiler, #exclusion_predicate_sql, #externalizable?, #externalize, #inclusion_predicate_sql, #join?, #primary_key, #session, #to_sql

Methods included from Relation::DefaultOperations

#groupings, #havings, #inserts, #joins, #locked, #orders, #projections, #skipped, #sources, #taken, #wheres

Methods included from Relation::AttributeAccessable

#[], #find_attribute_matching_attribute, #find_attribute_matching_name, #position_of

Methods included from Relation::Operable

#alias, #join, #lock, #outer_join

Methods included from Relation::Operable::Writable

#delete, #insert, #update

Methods included from Relation::Enumerable

#each, #first

Methods included from Recursion::BaseCase

#table, #table_sql

Constructor Details

#initialize(name, options = {}) ⇒ Table

Returns a new instance of Table.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/arel/engines/sql/relations/table.rb', line 8

def initialize(name, options = {})
  @name = name.to_s
  @table_exists = nil

  if options.is_a?(Hash)
    @options = options
    @engine = options[:engine] || Table.engine
    @table_alias = options[:as].to_s if options[:as].present? && options[:as].to_s != @name
  else
    @engine = options # Table.new('foo', engine)
  end

  if @engine.connection
    begin
      require "arel/engines/sql/compilers/#{@engine.adapter_name.downcase}_compiler"
      @@tables ||= engine.tables
    rescue LoadError
      raise "#{@engine.adapter_name} is not supported by Arel."
    end
  end
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



6
7
8
# File 'lib/arel/engines/sql/relations/table.rb', line 6

def engine
  @engine
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/arel/engines/sql/relations/table.rb', line 6

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/arel/engines/sql/relations/table.rb', line 6

def options
  @options
end

#table_aliasObject (readonly)

Returns the value of attribute table_alias.



6
7
8
# File 'lib/arel/engines/sql/relations/table.rb', line 6

def table_alias
  @table_alias
end

Instance Method Details

#==(other) ⇒ Object



77
78
79
80
81
# File 'lib/arel/engines/sql/relations/table.rb', line 77

def ==(other)
  Table       === other and
  name        ==  other.name and
  table_alias ==  other.table_alias
end

#as(table_alias) ⇒ Object



30
31
32
# File 'lib/arel/engines/sql/relations/table.rb', line 30

def as(table_alias)
  Table.new(name, options.merge(:as => table_alias))
end

#attributesObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/arel/engines/sql/relations/table.rb', line 42

def attributes
  return @attributes if defined?(@attributes)
  if table_exists?
    @attributes ||= begin
      attrs = columns.collect do |column|
        Sql::Attributes.for(column).new(column, self, column.name.to_sym)
      end
      Header.new(attrs)
    end
  else
    Header.new
  end
end

#column_for(attribute) ⇒ Object



64
65
66
# File 'lib/arel/engines/sql/relations/table.rb', line 64

def column_for(attribute)
  has_attribute?(attribute) and columns.detect { |c| c.name == attribute.name.to_s }
end

#columnsObject



68
69
70
# File 'lib/arel/engines/sql/relations/table.rb', line 68

def columns
  @columns ||= engine.columns(name, "#{name} Columns")
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/arel/engines/sql/relations/table.rb', line 56

def eql?(other)
  self == other
end

#hashObject



60
61
62
# File 'lib/arel/engines/sql/relations/table.rb', line 60

def hash
  @hash ||= :name.hash
end

#resetObject



72
73
74
75
# File 'lib/arel/engines/sql/relations/table.rb', line 72

def reset
  @columns = nil
  @attributes = Header.new([])
end

#table_exists?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
# File 'lib/arel/engines/sql/relations/table.rb', line 34

def table_exists?
  if @table_exists
    true
  else
    @table_exists = @@tables.include?(name) || engine.table_exists?(name)
  end
end