Class: Symbol

Inherits:
Object show all
Defined in:
lib/sequel/core_sql.rb

Constant Summary collapse

COLUMN_REF_RE1 =
/^(\w+)__(\w+)___(\w+)/.freeze
COLUMN_REF_RE2 =
/^(\w+)___(\w+)$/.freeze
COLUMN_REF_RE3 =
/^(\w+)__(\w+)$/.freeze

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym) ⇒ Object

Converts missing method calls into functions on columns, if the method name is made of all upper case letters.



184
185
186
187
188
189
190
# File 'lib/sequel/core_sql.rb', line 184

def method_missing(sym)
  if ((s = sym.to_s) =~ /^([A-Z]+)$/)
    Sequel::SQL::Function.new(s.downcase, self)
  else
    super
  end
end

Instance Method Details

#[](*args) ⇒ Object



147
# File 'lib/sequel/core_sql.rb', line 147

def [](*args); Sequel::SQL::Function.new(self, *args); end

#to_column_ref(ds) ⇒ Object

Converts a symbol into a column name. This method supports underscore notation in order to express qualified (two underscores) and aliased (three underscores) columns:

ds = DB[:items]
:abc.to_column_ref(ds) #=> "abc"
:abc___a.to_column_ref(ds) #=> "abc AS a"
:items__abc.to_column_ref(ds) #=> "items.abc"
:items__abc___a.to_column_ref(ds) #=> "items.abc AS a"


169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/sequel/core_sql.rb', line 169

def to_column_ref(ds)
  case s = to_s
  when COLUMN_REF_RE1
    "#{$1}.#{ds.quote_column_ref($2)} AS #{ds.quote_column_ref($3)}"
  when COLUMN_REF_RE2
    "#{ds.quote_column_ref($1)} AS #{ds.quote_column_ref($2)}"
  when COLUMN_REF_RE3
    "#{$1}.#{ds.quote_column_ref($2)}"
  else
    ds.quote_column_ref(s)
  end
end

#|(sub) ⇒ Object



148
149
150
151
152
153
# File 'lib/sequel/core_sql.rb', line 148

def |(sub)
  unless Array === sub
    sub = [sub]
  end
  Sequel::SQL::Subscript.new(self, sub)
end