module Sequel::ColumnsIntrospection

  1. lib/sequel/extensions/columns_introspection.rb
Parent: Sequel

Methods

Public Instance

  1. columns

Protected Instance

  1. probable_columns

Public Instance methods

columns ()

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.

[show source]
# File lib/sequel/extensions/columns_introspection.rb, line 25
def columns
  return @columns if @columns
  if (pcs = probable_columns) && pcs.all?
    @columns = pcs
  else
    super
  end
end

Protected Instance methods

probable_columns ()

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

[show source]
# File lib/sequel/extensions/columns_introspection.rb, line 39
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 && (sch = Sequel.synchronize{schemas[literal(from)]})
        sch.map{|c,_| c}
      end
    end
  end
end