Class: Superstore::Adapters::JsonbAdapter
Constant Summary
collapse
- PRIMARY_KEY_COLUMN =
'id'.freeze
Instance Method Summary
collapse
#initialize, #select
Instance Method Details
#active_record_klass ⇒ Object
20
21
22
|
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 20
def active_record_klass
@active_record_klass ||= ActiveRecord::Base
end
|
#active_record_klass=(klass) ⇒ Object
16
17
18
|
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 16
def active_record_klass=(klass)
@active_record_klass = klass
end
|
#connection ⇒ Object
12
13
14
|
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 12
def connection
active_record_klass.connection
end
|
#create_ids_where_clause(ids) ⇒ Object
60
61
62
63
64
65
66
67
68
69
|
# 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) ⇒ Object
54
55
56
57
58
|
# 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) ⇒ Object
24
25
26
|
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 24
def execute(statement)
connection.execute statement
end
|
#fields_to_postgres_array(fields) ⇒ Object
75
76
77
78
|
# 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) ⇒ Object
28
29
30
31
32
|
# 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 ⇒ Object
8
9
10
|
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 8
def primary_key_column
PRIMARY_KEY_COLUMN
end
|
#quote(value) ⇒ Object
71
72
73
|
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 71
def quote(value)
connection.quote(value)
end
|
#to_quoted_jsonb(data) ⇒ Object
80
81
82
|
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 80
def to_quoted_jsonb(data)
"#{quote(JSON.generate(data))}::JSONB"
end
|
#update(table, id, attributes) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# 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
|