Class: Superstore::Adapters::JsonbAdapter

Inherits:
AbstractAdapter show all
Defined in:
lib/superstore/adapters/jsonb_adapter.rb

Defined Under Namespace

Classes: QueryBuilder

Constant Summary collapse

PRIMARY_KEY_COLUMN =
'id'.freeze
OJ_OPTIONS =
{mode: :compat}

Instance Attribute Summary

Attributes inherited from AbstractAdapter

#config

Instance Method Summary collapse

Methods inherited from AbstractAdapter

#batch, #batching?, #execute_batchable, #initialize

Constructor Details

This class inherits a constructor from Superstore::Adapters::AbstractAdapter

Instance Method Details

#active_record_klassObject



100
101
102
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 100

def active_record_klass
  @active_record_klass ||= ActiveRecord::Base
end

#active_record_klass=(klass) ⇒ Object



96
97
98
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 96

def active_record_klass=(klass)
  @active_record_klass = klass
end

#connectionObject



92
93
94
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 92

def connection
  active_record_klass.connection
end

#create_ids_where_clause(ids) ⇒ Object



176
177
178
179
180
181
182
183
184
185
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 176

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



164
165
166
167
168
169
170
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 164

def create_table(table_name, options = {})
  ActiveRecord::Migration.create_table table_name, id: false do |t|
    t.string :id, null: false
    t.jsonb :document, null: false
  end
  connection.execute "ALTER TABLE \"#{table_name}\" ADD CONSTRAINT #{table_name}_pkey PRIMARY KEY (id)"
end

#delete(table, ids) ⇒ Object



152
153
154
155
156
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 152

def delete(table, ids)
  statement = "DELETE FROM #{table} WHERE #{create_ids_where_clause(ids)}"

  execute_batchable statement
end

#drop_table(table_name) ⇒ Object



172
173
174
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 172

def drop_table(table_name)
  ActiveRecord::Migration.drop_table table_name
end

#execute(statement) ⇒ Object



104
105
106
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 104

def execute(statement)
  connection.execute statement
end

#execute_batch(statements) ⇒ Object



158
159
160
161
162
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 158

def execute_batch(statements)
  connection.transaction do
    execute(statements * ";\n")
  end
end

#fields_to_postgres_array(fields) ⇒ Object



191
192
193
194
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 191

def fields_to_postgres_array(fields)
  quoted_fields = fields.map { |field| quote(field) }.join(',')
  "ARRAY[#{quoted_fields}]"
end

#insert(table, id, attributes) ⇒ Object



137
138
139
140
141
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 137

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_batchable statement
end

#primary_key_columnObject



88
89
90
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 88

def primary_key_column
  PRIMARY_KEY_COLUMN
end

#quote(value) ⇒ Object



187
188
189
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 187

def quote(value)
  connection.quote(value)
end

#scroll(scope, batch_size) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 121

def scroll(scope, batch_size)
  statement   = QueryBuilder.new(self, scope).to_query
  cursor_name = "cursor_#{SecureRandom.hex(6)}"
  fetch_sql   = "FETCH FORWARD #{batch_size} FROM #{cursor_name}"

  connection.transaction do
    connection.execute "DECLARE #{cursor_name} NO SCROLL CURSOR FOR (#{statement})"

    while (batch = connection.execute(fetch_sql)).any?
      batch.each do |result|
        yield result[primary_key_column], Oj.compat_load(result['document'])
      end
    end
  end
end

#select(scope) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 113

def select(scope)
  statement = QueryBuilder.new(self, scope).to_query

  connection.execute(statement).each do |result|
    yield result[primary_key_column], Oj.compat_load(result['document'])
  end
end

#to_ids(scope) ⇒ Object



108
109
110
111
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 108

def to_ids(scope)
  statement = QueryBuilder.new(self, scope.select(primary_key_column)).to_query
  connection.select_values(statement)
end

#to_quoted_jsonb(data) ⇒ Object



197
198
199
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 197

def to_quoted_jsonb(data)
  "#{quote(Oj.dump(data, OJ_OPTIONS))}::JSONB"
end

#update(table, id, attributes) ⇒ Object



143
144
145
146
147
148
149
150
# File 'lib/superstore/adapters/jsonb_adapter.rb', line 143

def update(table, id, attributes)
  return if attributes.empty?

  value_update = "jsonb_strip_nulls(document || #{to_quoted_jsonb(attributes)})"
  statement = "UPDATE #{table} SET document = #{value_update} WHERE #{primary_key_column} = #{quote(id)}"

  execute_batchable statement
end