Sha256: 277f5820c120380d6336dd64bd2b742db02957a14e525277d92604b2ea64bcc7

Contents?: true

Size: 1.14 KB

Versions: 59

Compression:

Stored size: 1.14 KB

Contents

module Bmg
  module Sequel
    class TypeInference

      def initialize(sequel_db)
        @sequel_db = sequel_db
      end
      attr_reader :sequel_db

      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

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

      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

    end # class TypeInference
  end # module Sequel
end # module Bmg

Version data entries

59 entries across 59 versions & 1 rubygems

Version Path
bmg-0.23.3 lib/bmg/sequel/type_inference.rb
bmg-0.23.2 lib/bmg/sequel/type_inference.rb
bmg-0.23.1 lib/bmg/sequel/type_inference.rb
bmg-0.23.0 lib/bmg/sequel/type_inference.rb
bmg-0.21.5 lib/bmg/sequel/type_inference.rb
bmg-0.21.4 lib/bmg/sequel/type_inference.rb
bmg-0.20.5 lib/bmg/sequel/type_inference.rb
bmg-0.19.3 lib/bmg/sequel/type_inference.rb
bmg-0.21.3 lib/bmg/sequel/type_inference.rb
bmg-0.21.2 lib/bmg/sequel/type_inference.rb
bmg-0.21.0 lib/bmg/sequel/type_inference.rb
bmg-0.20.4 lib/bmg/sequel/type_inference.rb
bmg-0.20.2 lib/bmg/sequel/type_inference.rb
bmg-0.20.1 lib/bmg/sequel/type_inference.rb
bmg-0.20.0 lib/bmg/sequel/type_inference.rb
bmg-0.19.2 lib/bmg/sequel/type_inference.rb
bmg-0.19.1 lib/bmg/sequel/type_inference.rb
bmg-0.19.0 lib/bmg/sequel/type_inference.rb
bmg-0.18.15 lib/bmg/sequel/type_inference.rb
bmg-0.18.14 lib/bmg/sequel/type_inference.rb