Module: Arel::Sql::Engine::CRUD

Included in:
Arel::Sql::Engine
Defined in:
lib/arel/engines/sql/engine.rb

Instance Method Summary collapse

Instance Method Details

#create(relation) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/arel/engines/sql/engine.rb', line 28

def create(relation)
  primary_key_value = if relation.primary_key.blank?
    nil
  elsif relation.record.is_a?(Hash)
    attribute = relation.record.detect { |attr, _| attr.name.to_s == relation.primary_key.to_s }
    attribute && attribute.last.value
  end

  connection.insert(relation.to_sql(false), nil, relation.primary_key, primary_key_value)
end

#delete(relation) ⇒ Object



48
49
50
# File 'lib/arel/engines/sql/engine.rb', line 48

def delete(relation)
  connection.delete(relation.to_sql)
end

#read(relation) ⇒ Object



39
40
41
42
# File 'lib/arel/engines/sql/engine.rb', line 39

def read(relation)
  rows = connection.select_rows(relation.to_sql)
  Array.new(rows, relation.attributes)
end

#update(relation) ⇒ Object



44
45
46
# File 'lib/arel/engines/sql/engine.rb', line 44

def update(relation)
  connection.update(relation.to_sql)
end