Sha256: 35b14382d9c200a1096ad8d89e1f55b2dd51187c9178ae71c58ac0f2e37199d4

Contents?: true

Size: 1.3 KB

Versions: 6

Compression:

Stored size: 1.3 KB

Contents

# rubocop:disable Naming/MethodName
# rubocop:disable Naming/UncommunicativeMethodParamName

module Arel
  module Nodes
    # https://www.postgresql.org/docs/9.5/sql-insert.html
    class Conflict < Arel::Nodes::Node
      attr_accessor :action
      attr_accessor :infer
      attr_accessor :values
      attr_accessor :wheres
    end
  end

  module Visitors
    class ToSql
      # rubocop:disable Metrics/AbcSize
      def visit_Arel_Nodes_Conflict(o, collector)
        collector << ' ON CONFLICT '

        visit(o.infer, collector) if o.infer

        case o.action
        when 1
          collector << 'DO NOTHING'
        when 2
          collector << 'DO UPDATE SET '
        else
          raise "Unknown conflict clause `#{action}`"
        end

        o.values.any? && (inject_join o.values, collector, ', ')

        if o.wheres.any?
          collector << ' WHERE '
          collector = inject_join o.wheres, collector, ' AND '
        end

        collector
      end
      # rubocop:enable Metrics/AbcSize
    end

    class Dot
      def visit_Arel_Nodes_Conflict(o)
        visit_edge o, 'action'
        visit_edge o, 'infer'
        visit_edge o, 'values'
        visit_edge o, 'wheres'
      end
    end
  end
end

# rubocop:enable Naming/MethodName
# rubocop:enable Naming/UncommunicativeMethodParamName

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
arel_toolkit-0.4.5 lib/arel/extensions/conflict.rb
arel_toolkit-0.4.4 lib/arel/extensions/conflict.rb
arel_toolkit-0.4.3 lib/arel/extensions/conflict.rb
arel_toolkit-0.4.2 lib/arel/extensions/conflict.rb
arel_toolkit-0.4.1 lib/arel/extensions/conflict.rb
arel_toolkit-0.4.0 lib/arel/extensions/conflict.rb