Class: Arel::Table

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

Constant Summary collapse

@@engine =
nil
@@tables =
nil

Instance Attribute Summary collapse

Attributes included from Relation

#count

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Relation

#[], #alias, #bind, #call, #compiler, #delete, #each, #exclusion_predicate_sql, #externalizable?, #externalize, #find_attribute_matching_name, #from, #from_clauses, #group_clauses, #groupings, #having_clauses, #havings, #inclusion_predicate_sql, #insert, #inserts, #join, #join?, #joins, #lock, #locked, #order_clauses, #orders, #outer_join, #position_of, #primary_key, #project, #projections, #select_clauses, #session, #skip, #skipped, #sources, #take, #taken, #to_sql, #update, #where, #where_clauses, #wheres

Methods included from Recursion::BaseCase

#table, #table_sql

Constructor Details

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

Returns a new instance of Table.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/arel/engines/sql/relations/table.rb', line 17

def initialize(name, options = {})
  @name = name.to_s
  @table_exists = nil
  @table_alias = nil
  @christener = Sql::Christener.new
  @attributes = nil
  @matching_attributes = nil

  if options.is_a?(Hash)
    @options = options
    @engine = options[:engine] || Table.engine

    if options[:as]
      as = options[:as].to_s
      @table_alias = as unless as == @name
    end
  else
    @engine = options # Table.new('foo', engine)
  end

  if @engine.connection
    begin
      require "arel/engines/sql/compilers/#{@engine.adapter_name.downcase}_compiler"
    rescue LoadError
      begin
        # try to load an externally defined compiler, in case this adapter has defined the compiler on its own.
        require "#{@engine.adapter_name.downcase}/arel_compiler"
      rescue LoadError
        raise "#{@engine.adapter_name} is not supported by Arel."
      end
    end

    @@tables ||= engine.connection.tables
  end
end

Instance Attribute Details

#christenerObject (readonly)

Returns the value of attribute christener.



15
16
17
# File 'lib/arel/engines/sql/relations/table.rb', line 15

def christener
  @christener
end

#engineObject (readonly)

Returns the value of attribute engine.



15
16
17
# File 'lib/arel/engines/sql/relations/table.rb', line 15

def engine
  @engine
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/arel/engines/sql/relations/table.rb', line 15

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/arel/engines/sql/relations/table.rb', line 15

def options
  @options
end

#table_aliasObject (readonly)

Returns the value of attribute table_alias.



15
16
17
# File 'lib/arel/engines/sql/relations/table.rb', line 15

def table_alias
  @table_alias
end

Class Method Details

.engineObject



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

def engine; @@engine; end

.engine=(e) ⇒ Object



9
# File 'lib/arel/engines/sql/relations/table.rb', line 9

def engine= e; @@engine = e; end

.tablesObject



11
# File 'lib/arel/engines/sql/relations/table.rb', line 11

def tables; @@tables; end

.tables=(e) ⇒ Object



12
# File 'lib/arel/engines/sql/relations/table.rb', line 12

def tables= e; @@tables = e; end

Instance Method Details

#as(table_alias) ⇒ Object



53
54
55
# File 'lib/arel/engines/sql/relations/table.rb', line 53

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

#attributesObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/arel/engines/sql/relations/table.rb', line 61

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

#column_for(attribute) ⇒ Object



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

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

#columnsObject



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

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

#resetObject



81
82
83
84
# File 'lib/arel/engines/sql/relations/table.rb', line 81

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

#table_exists?Boolean

Returns:

  • (Boolean)


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

def table_exists?
  @table_exists ||= @@tables.include?(name) || engine.connection.table_exists?(name)
end