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
906 def initialize(expr, path, opts=OPTS)
907   @expr = expr
908   @path = path
909   @passing = opts[:passing]
910   @on_error = opts[:on_error]
911   freeze
912 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
923 def sequel_ast_transform(transformer)
924   opts = {}
925   transform_opts(transformer, opts)
926   self.class.new(transformer.call(@expr), @path, opts)
927 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
915 def to_s_append(ds, sql)
916   to_s_append_function_name(ds, sql)
917   to_s_append_args_passing(ds, sql)
918   to_s_append_on_error(ds, sql)
919   sql << ')'
920 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
949 def to_s_append_args_passing(ds, sql)
950   ds.literal_append(sql, @expr)
951   sql << ', '
952   ds.literal_append(sql, @path)
953 
954   if (passing = @passing) && !passing.empty?
955     sql << ' PASSING '
956     comma = false
957     passing.each do |k, v|
958       if comma
959         sql << ', '
960       else
961         comma = true
962       end
963       ds.literal_append(sql, v)
964       sql << " AS " << k.to_s
965     end
966   end
967 end
to_s_append_function_name(ds, sql) click to toggle source
    # File lib/sequel/extensions/pg_json_ops.rb
944 def to_s_append_function_name(ds, sql)
945   sql << 'json_exists('
946 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
970 def to_s_append_on_error(ds, sql)
971   unless @on_error.nil?
972     sql << " "
973     to_s_append_on_value(ds, sql, @on_error)
974     sql << " ON ERROR"
975   end
976 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
979 def to_s_append_on_value(ds, sql, value)
980   sql << ON_ERROR_SQL.fetch(value)
981 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
933 def transform_opts(transformer, opts)
934   if @passing
935     passing = opts[:passing] = {}
936     @passing.each do |k, v|
937       passing[k] = transformer.call(v)
938     end
939   end
940 
941   opts[:on_error] = @on_error
942 end