Sha256: c76a07621a67458f65ed565fa1685a755643a2de0fde272fbe23ea1fa04957dd

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

module ROM
  module SQL

    class Adapter < ROM::Adapter
      attr_reader :connection

      def self.schemes
        [:ado, :amalgalite, :cubrid, :db2, :dbi, :do, :fdbsql, :firebird, :ibmdb,
         :informix, :jdbc, :mysql, :mysql2, :odbc, :openbase, :oracle, :postgres,
         :sqlanywhere, :sqlite, :swift, :tinytds]
      end

      def initialize(*args)
        super
        @connection = ::Sequel.connect(uri.to_s)
      end

      def [](name)
        connection[name]
      end

      def schema
        tables.map { |table| [table, dataset(table), columns(table)] }
      end

      def extend_relation_class(klass)
        klass.send(:include, RelationInclusion)
      end

      def extend_relation_instance(relation)
        relation.extend(RelationExtension)
      end

      private

      def tables
        connection.tables
      end

      def columns(table)
        dataset(table).columns
      end

      def dataset(table)
        connection[table]
      end

      def attributes(table)
        map_attribute_types connection.schema(table)
      end

      def map_attribute_types(attrs)
        attrs.map do |column, opts|
          [column, { type: map_schema_type(opts[:type]) }]
        end.to_h
      end

      def map_schema_type(type)
        connection.class::SCHEMA_TYPE_CLASSES.fetch(type)
      end

      ROM::Adapter.register(self)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-sql-0.1.1 lib/rom/sql/adapter.rb