Class: CassandraObject::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
56
57
58
59
60
|
# File 'lib/cassandra_object/adapters/hstore_adapter.rb', line 56
def connection
ActiveRecord::Base.connection
end
|
#create_ids_where_clause(ids) ⇒ Object
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/cassandra_object/adapters/hstore_adapter.rb', line 125
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
116
117
118
119
120
121
122
123
|
# File 'lib/cassandra_object/adapters/hstore_adapter.rb', line 116
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
100
101
102
103
104
|
# File 'lib/cassandra_object/adapters/hstore_adapter.rb', line 100
def delete(table, ids)
statement = "DELETE FROM #{table} WHERE #{create_ids_where_clause(ids)}"
execute_batchable statement
end
|
#execute(statement) ⇒ Object
62
63
64
65
66
|
# File 'lib/cassandra_object/adapters/hstore_adapter.rb', line 62
def execute(statement)
ActiveSupport::Notifications.instrument("cql.cassandra_object", cql: statement) do
connection.exec_query statement
end
end
|
#execute_batch(statements) ⇒ Object
106
107
108
109
110
111
112
113
114
|
# File 'lib/cassandra_object/adapters/hstore_adapter.rb', line 106
def execute_batch(statements)
stmt = [
"BEGIN",
statements * ";\n",
'COMMIT'
] * ";\n"
execute stmt
end
|
#fields_to_postgres_array(fields) ⇒ Object
140
141
142
143
|
# File 'lib/cassandra_object/adapters/hstore_adapter.rb', line 140
def fields_to_postgres_array(fields)
quoted_fields = fields.map { |field| "'#{field}'" }.join(',')
"ARRAY[#{quoted_fields}]"
end
|
#insert(table, id, attributes) ⇒ Object
76
77
78
79
80
|
# File 'lib/cassandra_object/adapters/hstore_adapter.rb', line 76
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
52
53
54
|
# File 'lib/cassandra_object/adapters/hstore_adapter.rb', line 52
def primary_key_column
'id'
end
|
#quote(value) ⇒ Object
136
137
138
|
# File 'lib/cassandra_object/adapters/hstore_adapter.rb', line 136
def quote(value)
connection.quote(value)
end
|
#select(scope) ⇒ Object
68
69
70
71
72
73
74
|
# File 'lib/cassandra_object/adapters/hstore_adapter.rb', line 68
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/cassandra_object/adapters/hstore_adapter.rb', line 82
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
|