Class: Bmg::Sequel::TypeInference

Inherits:
Object
  • Object
show all
Defined in:
lib/bmg/sequel/type_inference.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sequel_db) ⇒ TypeInference

Returns a new instance of TypeInference.



5
6
7
# File 'lib/bmg/sequel/type_inference.rb', line 5

def initialize(sequel_db)
  @sequel_db = sequel_db
end

Instance Attribute Details

#sequel_dbObject (readonly)

Returns the value of attribute sequel_db.



8
9
10
# File 'lib/bmg/sequel/type_inference.rb', line 8

def sequel_db
  @sequel_db
end

Instance Method Details

#attrlist(name) ⇒ Object



24
25
26
# File 'lib/bmg/sequel/type_inference.rb', line 24

def attrlist(name)
  sequel_db.schema(name).map{|(k,v)| k }
end

#call(name) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bmg/sequel/type_inference.rb', line 10

def call(name)
  if type = sequel_db.bmg_schema_cache[name]
    type
  else
    type = Type.new
      .with_attrlist(attrlist(name))
      .with_keys(keys(name))
    ::Sequel.synchronize do
      sequel_db.bmg_schema_cache[name] = type
    end if sequel_db.cache_schema
    type
  end
end

#keys(name) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bmg/sequel/type_inference.rb', line 28

def keys(name)
  # take the indexes
  indexes = sequel_db
    .indexes(name)
    .values
    .select{|i| i[:unique] == true }
    .map{|i| i[:columns] }
    .sort{|a1, a2| a1.size <=> a2.size }

  # take single keys as well
  key = sequel_db
    .schema(name)
    .select{|(colname, colinfo)| colinfo[:primary_key] }
    .map(&:first)

  indexes.unshift(key) unless key.empty?

  indexes
end