Sha256: 8f07898c45245354172f5c535ecade79b017b07f6f0893cc22f07fae1b30d3b4

Contents?: true

Size: 1.33 KB

Versions: 4

Compression:

Stored size: 1.33 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 :ONCONFLICT_NOTHING
          collector << 'DO NOTHING'
        when :ONCONFLICT_UPDATE
          collector << 'DO UPDATE SET '
        else
          raise "Unknown conflict clause `#{o.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

4 entries across 4 versions & 1 rubygems

Version Path
arel_toolkit-0.4.9 lib/arel/extensions/conflict.rb
arel_toolkit-0.4.8 lib/arel/extensions/conflict.rb
arel_toolkit-0.4.7 lib/arel/extensions/conflict.rb
arel_toolkit-0.4.6 lib/arel/extensions/conflict.rb