1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
module Arel
class Table
include Relation, Recursion::BaseCase
@@engine = nil
@@tables = nil
class << self # FIXME: Do we really need these?
def engine; @@engine; end
def engine= e; @@engine = e; end
def tables; @@tables; end
def tables= e; @@tables = e; end
end
attr_reader :name, :engine, :table_alias, :options
def initialize(name, options = {})
@name = name.to_s
@table_exists = 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
def as(table_alias)
Table.new(name, options.merge(:as => table_alias))
end
def table_exists?
if @table_exists
true
else
@table_exists = @@tables.include?(name) || engine.connection.table_exists?(name)
end
end
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
def eql?(other)
self == other
end
def hash
@hash ||= :name.hash
end
def column_for(attribute)
has_attribute?(attribute) and columns.detect { |c| c.name == attribute.name.to_s }
end
def columns
@columns ||= engine.connection.columns(name, "#{name} Columns")
end
def reset
@columns = nil
@attributes = Header.new([])
end
def ==(other)
super ||
Table === other &&
name == other.name &&
table_alias == other.table_alias
end
end
end
def Table(name, engine = Arel::Table.engine)
Arel::Table.new(name, engine)
end
|