Class: ActiveRecord::Refined::AST::JsonKeys
- Includes:
- ComputedJson, JsonComparable, Predications
- Defined in:
- lib/active_record/refined/ast.rb
Overview
The keys of a JSON document, as Hash#keys gives them: a JSON array. Only the MySQL family has a function for it; the other two reach the same array through a subquery over their key-listing functions. The type guard is what makes all four answer alike: the keys of what is not an object are NULL rather than SQLite's array indices or PostgreSQL's error, and the keys of {} are [] rather than PostgreSQL's NULL, jsonb_agg over no rows.
Instance Attribute Summary collapse
-
#operand ⇒ Object
readonly
Returns the value of attribute operand.
Instance Method Summary collapse
-
#initialize(operand) ⇒ JsonKeys
constructor
A new instance of JsonKeys.
- #to_arel(table, model) ⇒ Object
Methods included from ComputedJson
Methods included from JsonComparable
#between?, #in?, #not_between?, #not_in?, #~
Methods included from Predications
#!=, #!~, #<, #<=, #==, #=~, #>, #>=, #between?, #bury, #casecmp?, #contains?, #dig, #dig_text, #distinct_from?, #end_with?, #except, #false?, #ilike?, #in?, #include?, #intersect?, #key?, #keys, #like?, #member?, #not_between?, #not_distinct_from?, #not_false?, #not_ilike?, #not_in?, #not_like?, #not_null?, #not_true?, #null?, #start_with?, #subset?, #superset?, #true?, #when
Methods inherited from Node
Constructor Details
#initialize(operand) ⇒ JsonKeys
Returns a new instance of JsonKeys.
1073 1074 1075 |
# File 'lib/active_record/refined/ast.rb', line 1073 def initialize(operand) @operand = operand end |
Instance Attribute Details
#operand ⇒ Object (readonly)
Returns the value of attribute operand.
1071 1072 1073 |
# File 'lib/active_record/refined/ast.rb', line 1071 def operand @operand end |
Instance Method Details
#to_arel(table, model) ⇒ Object
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 |
# File 'lib/active_record/refined/ast.rb', line 1077 def to_arel(table, model) document = to_arel_operand(operand, table, model) case AST.adapter_family(model) when :sqlite sql = compile(document, model) Arel.sql("CASE WHEN json_type(#{sql}) = 'object' " \ "THEN (SELECT json_group_array(key) FROM json_each(#{sql})) END") when :postgresql sql = compile(document, model) Arel.sql("CASE WHEN jsonb_typeof(#{sql}) = 'object' " \ "THEN COALESCE((SELECT jsonb_agg(k) FROM jsonb_object_keys(#{sql}) k), " \ "CAST('[]' AS jsonb)) END") else Arel::Nodes::NamedFunction.new("JSON_KEYS", [document]) end end |