# File lib/superstore/adapters/jsonb_adapter.rb, line 8 def primary_key_column PRIMARY_KEY_COLUMN end
class Superstore::Adapters::JsonbAdapter
Constants
- PRIMARY_KEY_COLUMN
Public Instance Methods
active_record_klass()
click to toggle source
# File lib/superstore/adapters/jsonb_adapter.rb, line 20 def active_record_klass @active_record_klass ||= ActiveRecord::Base end
active_record_klass=(klass)
click to toggle source
# File lib/superstore/adapters/jsonb_adapter.rb, line 16 def active_record_klass=(klass) @active_record_klass = klass end
connection()
click to toggle source
# File lib/superstore/adapters/jsonb_adapter.rb, line 12 def connection active_record_klass.connection end
create_ids_where_clause(ids)
click to toggle source
# File lib/superstore/adapters/jsonb_adapter.rb, line 60 def create_ids_where_clause(ids) ids = ids.first if ids.is_a?(Array) && ids.one? if ids.is_a?(Array) id_list = ids.map { |id| quote(id) }.join(',') "#{primary_key_column} IN (#{id_list})" else "#{primary_key_column} = #{quote(ids)}" end end
delete(table, ids)
click to toggle source
# File lib/superstore/adapters/jsonb_adapter.rb, line 54 def delete(table, ids) statement = "DELETE FROM #{table} WHERE #{create_ids_where_clause(ids)}" execute statement end
execute(statement)
click to toggle source
# File lib/superstore/adapters/jsonb_adapter.rb, line 24 def execute(statement) connection.execute statement end
fields_to_postgres_array(fields)
click to toggle source
# File lib/superstore/adapters/jsonb_adapter.rb, line 75 def fields_to_postgres_array(fields) quoted_fields = fields.map { |field| quote(field) }.join(',') "ARRAY[#{quoted_fields}]" end
insert(table, id, attributes)
click to toggle source
# File lib/superstore/adapters/jsonb_adapter.rb, line 28 def insert(table, id, attributes) not_nil_attributes = attributes.reject { |key, value| value.nil? } statement = "INSERT INTO #{table} (#{primary_key_column}, document) VALUES (#{quote(id)}, #{to_quoted_jsonb(not_nil_attributes)})" execute statement end
primary_key_column()
click to toggle source
quote(value)
click to toggle source
# File lib/superstore/adapters/jsonb_adapter.rb, line 71 def quote(value) connection.quote(value) end
to_quoted_jsonb(data)
click to toggle source
# File lib/superstore/adapters/jsonb_adapter.rb, line 80 def to_quoted_jsonb(data) "#{quote(JSON.generate(data))}::JSONB" end
update(table, id, attributes)
click to toggle source
# File lib/superstore/adapters/jsonb_adapter.rb, line 34 def update(table, id, attributes) return if attributes.empty? nil_properties = attributes.each_key.select { |k| attributes[k].nil? } not_nil_attributes = attributes.reject { |key, value| value.nil? } value_update = "document" nil_properties.each do |property| value_update = "(#{value_update} - '#{property}')" end if not_nil_attributes.any? value_update = "(#{value_update} || #{to_quoted_jsonb(not_nil_attributes)})" end statement = "UPDATE #{table} SET document = #{value_update} WHERE #{primary_key_column} = #{quote(id)}" execute statement end