class Sequel::Postgres::JSONExistsOp

Object representing json_exists calls

Constants

ON_ERROR_SQL

Attributes

expr[R]

Expression (context_item in PostgreSQL terms), usually JSONBaseOp instance

on_error[R]

How to handle errors when evaluating the JSON path expression

passing[R]

Variables to set in the JSON path expression

path[R]

JSON path expression to apply against the expression

Public Class Methods

new(expr, path, opts=OPTS) click to toggle source

See JSONBaseOp#exists for documentation on the options.

    # File lib/sequel/extensions/pg_json_ops.rb
922 def initialize(expr, path, opts=OPTS)
923   @expr = expr
924   @path = path
925   @passing = opts[:passing]
926   @on_error = opts[:on_error]
927   freeze
928 end

Public Instance Methods

sequel_ast_transform(transformer) click to toggle source

Support transforming of function call expression

    # File lib/sequel/extensions/pg_json_ops.rb
939 def sequel_ast_transform(transformer)
940   opts = {}
941   transform_opts(transformer, opts)
942   self.class.new(transformer.call(@expr), @path, opts)
943 end
to_s_append(ds, sql) click to toggle source

Append the SQL function call expression to the SQL

    # File lib/sequel/extensions/pg_json_ops.rb
931 def to_s_append(ds, sql)
932   to_s_append_function_name(ds, sql)
933   to_s_append_args_passing(ds, sql)
934   to_s_append_on_error(ds, sql)
935   sql << ')'
936 end

Private Instance Methods

to_s_append_args_passing(ds, sql) click to toggle source

Append the expression, path, and optional PASSING fragments

    # File lib/sequel/extensions/pg_json_ops.rb
965 def to_s_append_args_passing(ds, sql)
966   ds.literal_append(sql, @expr)
967   sql << ', '
968   ds.literal_append(sql, @path)
969 
970   if (passing = @passing) && !passing.empty?
971     sql << ' PASSING '
972     comma = false
973     passing.each do |k, v|
974       if comma
975         sql << ', '
976       else
977         comma = true
978       end
979       ds.literal_append(sql, v)
980       sql << " AS " << k.to_s
981     end
982   end
983 end
to_s_append_function_name(ds, sql) click to toggle source
    # File lib/sequel/extensions/pg_json_ops.rb
960 def to_s_append_function_name(ds, sql)
961   sql << 'json_exists('
962 end
to_s_append_on_error(ds, sql) click to toggle source

Append the optional ON ERROR fragments

    # File lib/sequel/extensions/pg_json_ops.rb
986 def to_s_append_on_error(ds, sql)
987   unless @on_error.nil?
988     sql << " "
989     to_s_append_on_value(ds, sql, @on_error)
990     sql << " ON ERROR"
991   end
992 end
to_s_append_on_value(ds, sql, value) click to toggle source

Append the value to use for ON ERROR

    # File lib/sequel/extensions/pg_json_ops.rb
995 def to_s_append_on_value(ds, sql, value)
996   sql << ON_ERROR_SQL.fetch(value)
997 end
transform_opts(transformer, opts) click to toggle source

Set the :passing and :on_error options when doing an AST transform.

    # File lib/sequel/extensions/pg_json_ops.rb
949 def transform_opts(transformer, opts)
950   if @passing
951     passing = opts[:passing] = {}
952     @passing.each do |k, v|
953       passing[k] = transformer.call(v)
954     end
955   end
956 
957   opts[:on_error] = @on_error
958 end