Class: Arel::Table

Inherits:
Object
  • Object
show all
Includes:
Crud
Defined in:
lib/arel/table.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Crud

#delete, #insert, #update

Constructor Details

#initialize(name, engine = Table.engine) ⇒ Table

Returns a new instance of Table.



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

def initialize name, engine = Table.engine
  @name    = name.to_s
  @engine  = engine
  @columns = nil
  @aliases = []
  @table_alias = nil
  @primary_key = nil

  if Hash === engine
    @engine  = engine[:engine] || Table.engine
    @columns = attributes_for engine[:columns]

    # Sometime AR sends an :as parameter to table, to let the table know
    # that it is an Alias.  We may want to override new, and return a
    # TableAlias node?
    @table_alias = engine[:as] unless engine[:as].to_s == @name
  end
end

Class Attribute Details

.engineObject

Returns the value of attribute engine.



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

def engine
  @engine
end

Instance Attribute Details

#aliasesObject

Returns the value of attribute aliases.



8
9
10
# File 'lib/arel/table.rb', line 8

def aliases
  @aliases
end

#engineObject

Returns the value of attribute engine.



8
9
10
# File 'lib/arel/table.rb', line 8

def engine
  @engine
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/arel/table.rb', line 8

def name
  @name
end

#table_aliasObject

Returns the value of attribute table_alias.



8
9
10
# File 'lib/arel/table.rb', line 8

def table_alias
  @table_alias
end

Instance Method Details

#[](name) ⇒ Object



100
101
102
103
104
105
# File 'lib/arel/table.rb', line 100

def [] name
  return nil unless table_exists?

  name = name.to_sym
  columns.find { |column| column.name == name }
end

#alias(name = "#{self.name}_2") ⇒ Object



37
38
39
40
41
# File 'lib/arel/table.rb', line 37

def alias name = "#{self.name}_2"
  Nodes::TableAlias.new(name, self).tap do |node|
    @aliases << node
  end
end

#columnsObject



95
96
97
98
# File 'lib/arel/table.rb', line 95

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

#from(table) ⇒ Object



43
44
45
# File 'lib/arel/table.rb', line 43

def from table
  SelectManager.new(@engine, table)
end

#group(*columns) ⇒ Object



67
68
69
# File 'lib/arel/table.rb', line 67

def group *columns
  from(self).group(*columns)
end

#having(expr) ⇒ Object



91
92
93
# File 'lib/arel/table.rb', line 91

def having expr
  from(self).having expr
end

#join(relation, klass = Nodes::InnerJoin) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/arel/table.rb', line 55

def join relation, klass = Nodes::InnerJoin
  return from(self) unless relation

  case relation
  when String, Nodes::SqlLiteral
    raise if relation.blank?
    from Nodes::StringJoin.new(self, relation)
  else
    from klass.new(self, relation, nil)
  end
end

#joins(manager) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/arel/table.rb', line 47

def joins manager
  if $VERBOSE
    warn "joins is deprecated and will be removed in 2.2"
    warn "please remove your call to joins from #{caller.first}"
  end
  nil
end

#order(*expr) ⇒ Object



71
72
73
# File 'lib/arel/table.rb', line 71

def order *expr
  from(self).order(*expr)
end

#primary_keyObject



29
30
31
32
33
34
35
# File 'lib/arel/table.rb', line 29

def primary_key
  @primary_key ||= begin
    primary_key_name = @engine.connection.primary_key(name)
    # some tables might be without primary key
    primary_key_name && self[primary_key_name]
  end
end

#project(*things) ⇒ Object



79
80
81
# File 'lib/arel/table.rb', line 79

def project *things
  from(self).project(*things)
end

#select_managerObject



107
108
109
# File 'lib/arel/table.rb', line 107

def select_manager
  SelectManager.new(@engine)
end

#skip(amount) ⇒ Object



87
88
89
# File 'lib/arel/table.rb', line 87

def skip amount
  from(self).skip amount
end

#take(amount) ⇒ Object



83
84
85
# File 'lib/arel/table.rb', line 83

def take amount
  from(self).take amount
end

#where(condition) ⇒ Object



75
76
77
# File 'lib/arel/table.rb', line 75

def where condition
  from(self).where condition
end