Module: Bmg::Sequel

Defined in:
lib/bmg/sequel.rb,
lib/bmg/sequel/relation.rb,
lib/bmg/sequel/translator.rb,
lib/bmg/sequel/type_inference.rb,
lib/bmg/sequel/predicate_translator.rb

Defined Under Namespace

Classes: PredicateTranslator, Relation, Translator, TypeInference

Class Method Summary collapse

Class Method Details

.infer_type(sequel_db, source) ⇒ Object



31
32
33
# File 'lib/bmg/sequel.rb', line 31

def infer_type(sequel_db, source)
  TypeInference.new(sequel_db).call(source) if source.is_a?(Symbol) || source.is_a?(::Sequel::SQL::QualifiedIdentifier)
end

.sequel(*args, &bl) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/bmg/sequel.rb', line 9

def sequel(*args, &bl)
  source, sequel_db, type = sequel_params(*args, &bl)
  if type
    builder = Sql::Builder.new
    sexpr = builder.select_all(type.to_attrlist, source)
    Sequel::Relation.new(type, builder, sexpr, sequel_db).spied(Bmg.main_spy)
  else
    Bmg::Relation.new(source)
  end
end

.sequel_params(source, sequel_db = nil, type = nil) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
# File 'lib/bmg/sequel.rb', line 21

def sequel_params(source, sequel_db = nil, type = nil)
  sequel_db, type = nil, sequel_db if sequel_db.nil? or sequel_db.is_a?(Type)
  sequel_db = source.db if sequel_db.nil? and source.is_a?(::Sequel::Dataset)
  raise ArgumentError, "A Sequel::Database object is required" if sequel_db.nil?
  raise ArgumentError, "Type's attrlist must be known (#{type})" if type && !type.knows_attrlist?
  type = infer_type(sequel_db, source) if type.nil?
  [source, sequel_db, type]
end