Sha256: 935ece4adcf9080767466b69c949b721076b616325469805d6cc080b9584d42b

Contents?: true

Size: 1015 Bytes

Versions: 5

Compression:

Stored size: 1015 Bytes

Contents

require "arel/visitors/mysql"

module Arel
  module Visitors
    class MySQL
      def visit_PluckMap_BuildJsonObject(o, collector)
        collector << "json_object("
        visit o.args, collector
        collector << ")"
      end

      def visit_PluckMap_BuildJsonArray(o, collector)
        collector << "json_array("
        visit o.args, collector
        collector << ")"
      end

      def visit_PluckMap_JsonArrayAggregate(o, collector)
        collector << "json_arrayagg("
        visit o.arg, collector
        collector << ")"
      end

      def visit_PluckMap_JsonSubqueryAggregate(o, collector)
        interior = compile(o.select)
        if o.scope.order_values.present?
          interior = "#{interior} ORDER BY #{compile(o.scope.order_values)}"
        end
        interior = "CAST(CONCAT('[',GROUP_CONCAT(#{interior}),']') AS JSON)"
        sql = o.scope.reorder(nil).select(Arel.sql(interior)).to_sql

        collector << "COALESCE((#{sql}), json_array())"
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pluck_map-2.0.0 lib/pluck_map/visitors/mysql.rb
pluck_map-2.0.0.rc1 lib/pluck_map/visitors/mysql.rb
pluck_map-1.0.0 lib/pluck_map/visitors/mysql.rb
pluck_map-1.0.0.rc2 lib/pluck_map/visitors/mysql.rb
pluck_map-1.0.0.rc1 lib/pluck_map/visitors/mysql.rb