module Sequel::ColumnsIntrospection

Public Instance Methods

columns() click to toggle source

Attempt to guess the columns that will be returned if there are columns selected, in order to skip a database query to retrieve the columns. This should work with Symbols, SQL::Identifiers, SQL::QualifiedIdentifiers, and SQL::AliasedExpressions.

Calls superclass method
# File lib/sequel/extensions/columns_introspection.rb, line 26
def columns
  return @columns if @columns
  if (pcs = probable_columns) && pcs.all?
    @columns = pcs
  else
    super
  end
end

Protected Instance Methods

probable_columns() click to toggle source

Return an array of probable column names for the dataset, or nil if it is not possible to determine that through introspection.

# File lib/sequel/extensions/columns_introspection.rb, line 40
def probable_columns
  if (cols = opts[:select]) && !cols.empty?
    cols.map{|c| probable_column_name(c)}
  elsif !opts[:join] && !opts[:with] && (from = opts[:from]) && from.length == 1 && (from = from.first)
    if from.is_a?(SQL::AliasedExpression)
      from = from.expression
    end
    
    case from
    when Dataset
      from.probable_columns
    when Symbol, SQL::Identifier, SQL::QualifiedIdentifier
      schemas = db.instance_variable_get(:@schemas)
      if schemas && (table = literal(from)) && (sch = Sequel.synchronize{schemas[table]})
        sch.map{|c,_| c}
      end
    end
  end
end

Private Instance Methods

probable_column_name(c) click to toggle source

Return the probable name of the column, or nil if one cannot be determined.

# File lib/sequel/extensions/columns_introspection.rb, line 64
def probable_column_name(c)
  case c
  when Symbol
    _, c, a = split_symbol(c)
    (a || c).to_sym
  when SQL::Identifier
    c.value.to_sym
  when SQL::QualifiedIdentifier
    col = c.column
    col.is_a?(SQL::Identifier) ? col.value.to_sym : col.to_sym
  when SQL::AliasedExpression
    a = c.alias
    a.is_a?(SQL::Identifier) ? a.value.to_sym : a.to_sym
  end
end