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 30 def columns 31 if cols = _columns 32 return cols 33 end 34 if (pcs = probable_columns) && pcs.all? 35 self.columns = pcs 36 else 37 super 38 end 39 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 46 def probable_columns 47 if (cols = opts[:select]) && !cols.empty? 48 cols.map{|c| probable_column_name(c)} 49 elsif !opts[:join] && !opts[:with] && (from = opts[:from]) && from.length == 1 && (from = from.first) 50 if from.is_a?(SQL::AliasedExpression) 51 from = from.expression 52 end 53 54 case from 55 when Dataset 56 from.probable_columns 57 when Symbol, SQL::Identifier, SQL::QualifiedIdentifier 58 schemas = db.instance_variable_get(:@schemas) 59 if schemas && (table = literal(from)) && (sch = Sequel.synchronize{schemas[table]}) 60 sch.map{|c,_| c} 61 end 62 end 63 end 64 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 70 def probable_column_name(c) 71 case c 72 when Symbol 73 _, c, a = split_symbol(c) 74 (a || c).to_sym 75 when SQL::Identifier 76 c.value.to_sym 77 when SQL::QualifiedIdentifier 78 col = c.column 79 col.is_a?(SQL::Identifier) ? col.value.to_sym : col.to_sym 80 when SQL::AliasedExpression 81 a = c.alias 82 a.is_a?(SQL::Identifier) ? a.value.to_sym : a.to_sym 83 end 84 end