Class: Superstore::Adapters::HstoreAdapter
Defined Under Namespace
Classes: QueryBuilder
Instance Attribute Summary
#config
Instance Method Summary
collapse
#batch, #batching?, #execute_batchable, #initialize
Instance Method Details
#connection ⇒ Object
62
63
64
65
66
|
# File 'lib/superstore/adapters/hstore_adapter.rb', line 62
def connection
ActiveRecord::Base.connection
end
|
#create_ids_where_clause(ids) ⇒ Object
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/superstore/adapters/hstore_adapter.rb', line 135
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
|
#create_table(table_name, options = {}) ⇒ Object
122
123
124
125
126
127
128
129
|
# File 'lib/superstore/adapters/hstore_adapter.rb', line 122
def create_table(table_name, options = {})
connection.execute 'CREATE EXTENSION IF NOT EXISTS hstore'
ActiveRecord::Migration.create_table table_name, id: false do |t|
t.string :id, null: false
t.hstore :attribute_store, null: false
end
connection.execute "ALTER TABLE \"#{table_name}\" ADD CONSTRAINT #{table_name}_pkey PRIMARY KEY (id)"
end
|
#delete(table, ids) ⇒ Object
106
107
108
109
110
|
# File 'lib/superstore/adapters/hstore_adapter.rb', line 106
def delete(table, ids)
statement = "DELETE FROM #{table} WHERE #{create_ids_where_clause(ids)}"
execute_batchable statement
end
|
#drop_table(table_name) ⇒ Object
131
132
133
|
# File 'lib/superstore/adapters/hstore_adapter.rb', line 131
def drop_table(table_name)
ActiveRecord::Migration.drop_table table_name
end
|
#execute(statement) ⇒ Object
68
69
70
71
72
|
# File 'lib/superstore/adapters/hstore_adapter.rb', line 68
def execute(statement)
ActiveSupport::Notifications.instrument("cql.cassandra_object", cql: statement) do
connection.execute statement
end
end
|
#execute_batch(statements) ⇒ Object
112
113
114
115
116
117
118
119
120
|
# File 'lib/superstore/adapters/hstore_adapter.rb', line 112
def execute_batch(statements)
stmt = [
"BEGIN",
statements * ";\n",
'COMMIT'
] * ";\n"
execute stmt
end
|
#fields_to_postgres_array(fields) ⇒ Object
150
151
152
153
|
# File 'lib/superstore/adapters/hstore_adapter.rb', line 150
def fields_to_postgres_array(fields)
quoted_fields = fields.map { |field| "'#{field}'" }.join(',')
"ARRAY[#{quoted_fields}]"
end
|
#insert(table, id, attributes) ⇒ Object
82
83
84
85
86
|
# File 'lib/superstore/adapters/hstore_adapter.rb', line 82
def insert(table, id, attributes)
not_nil_attributes = attributes.reject { |key, value| value.nil? }
statement = "INSERT INTO #{table} (#{primary_key_column}, attribute_store) VALUES (#{quote(id)}, #{attributes_to_hstore(not_nil_attributes)})"
execute_batchable statement
end
|
#primary_key_column ⇒ Object
58
59
60
|
# File 'lib/superstore/adapters/hstore_adapter.rb', line 58
def primary_key_column
'id'
end
|
#quote(value) ⇒ Object
146
147
148
|
# File 'lib/superstore/adapters/hstore_adapter.rb', line 146
def quote(value)
connection.quote(value)
end
|
#select(scope) ⇒ Object
74
75
76
77
78
79
80
|
# File 'lib/superstore/adapters/hstore_adapter.rb', line 74
def select(scope)
statement = QueryBuilder.new(self, scope).to_query
connection.execute(statement).each do |attributes|
yield attributes[primary_key_column], hstore_to_attributes(attributes['attribute_store'])
end
end
|
#update(table, id, attributes) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/superstore/adapters/hstore_adapter.rb', line 88
def update(table, id, attributes)
return if attributes.empty?
not_nil_attributes = attributes.reject { |key, value| value.nil? }
nil_attributes = attributes.select { |key, value| value.nil? }
if not_nil_attributes.any? && nil_attributes.any?
value_update = "(attribute_store - #{fields_to_postgres_array(nil_attributes.keys)}) || #{attributes_to_hstore(not_nil_attributes)}"
elsif not_nil_attributes.any?
value_update = "attribute_store || #{attributes_to_hstore(not_nil_attributes)}"
elsif nil_attributes.any?
value_update = "attribute_store - #{fields_to_postgres_array(nil_attributes.keys)}"
end
statement = "UPDATE #{table} SET attribute_store = #{value_update} WHERE #{primary_key_column} = #{quote(id)}"
execute_batchable statement
end
|