Sha256: 2050d39636cd07557c6404cbca28efaf144013d4772ae6a5558bac6612fee86e

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

module Arel
  module Sql
    class Engine
      def initialize(ar = nil)
        @ar = ar
      end

      def connection
        @ar && @ar.connection
      end

      def adapter_name
        @adapter_name ||= case (name = connection.adapter_name)
        # map OracleEnanced adapter to Oracle
        when /Oracle/
          'Oracle'
        else
          name
        end
      end

      def create(relation)
        primary_key_value = if relation.primary_key.blank?
          nil
        elsif relation.record.is_a?(Hash)
          attribute = relation.record.detect { |attr, _| attr.name.to_s == relation.primary_key.to_s }
          attribute && attribute.last.value
        end

        connection.insert(relation.to_sql(false), nil, relation.primary_key, primary_key_value)
      end

      def read(relation)
        rows = connection.select_rows(relation.to_sql)
        Array.new(rows, relation.attributes)
      end

      def update(relation)
        connection.update(relation.to_sql)
      end

      def delete(relation)
        connection.delete(relation.to_sql)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
arel-1.0.1 lib/arel/engines/sql/engine.rb
arel-1.0.0 lib/arel/engines/sql/engine.rb
arel-1.0.0.rc1 lib/arel/engines/sql/engine.rb