Module: ROM::SQL::Relation::Writing

Included in:
ROM::SQL::Relation
Defined in:
lib/rom/sql/relation/writing.rb

Instance Method Summary collapse

Instance Method Details

#deleteInteger

Delete tuples from the relation

Examples:

users.delete # deletes all
users.where(name: 'Jane').delete # delete tuples
                                   from restricted relation

Returns:

  • (Integer)

    Number of deleted tuples



81
82
83
# File 'lib/rom/sql/relation/writing.rb', line 81

def delete(...)
  dataset.delete(...)
end

#import(other_sql_relation, options) ⇒ Integer #import(other, options) ⇒ Integer

Insert tuples from other relation

NOTE: The method implicitly uses a transaction

Examples:

users.import(new_users)

Overloads:

  • #import(other_sql_relation, options) ⇒ Integer

    If both relations uses the same gateway the INSERT ... SELECT statement will be used for importing the data

    Parameters:

  • #import(other, options) ⇒ Integer

    Import data from another relation. The source relation will be materialized before loading

    Parameters:

Returns:

  • (Integer)

    Number of imported tuples



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rom/sql/relation/writing.rb', line 115

def import(other, options = EMPTY_HASH)
  columns = other.schema.map { |a| a.alias || a.name }

  if other.gateway.eql?(gateway)
    dataset.import(columns, other.dataset, options)
  else
    keys = columns.map(&:to_sym)

    dataset.import(columns, other.to_a.map { |record| record.to_h.values_at(*keys) }, options)
  end
end

#insertHash

Insert tuple into relation

Examples:

users.insert(name: 'Jane')

Parameters:

  • args (Hash)

Returns:

  • (Hash)

    Inserted tuple



40
41
42
# File 'lib/rom/sql/relation/writing.rb', line 40

def insert(...)
  dataset.insert(...)
end

#multi_insertArray<String>

Multi insert tuples into relation

Examples:

users.multi_insert([{name: 'Jane'}, {name: 'Jack'}])

Parameters:

  • args (Array<Hash>)

Returns:

  • (Array<String>)

    A list of executed SQL statements



54
55
56
# File 'lib/rom/sql/relation/writing.rb', line 54

def multi_insert(...)
  dataset.multi_insert(...)
end

#updateInteger

Update tuples in the relation

Examples:

users.update(name: 'Jane')
users.where(name: 'Jane').update(name: 'Jane Doe')

Returns:

  • (Integer)

    Number of updated rows



67
68
69
# File 'lib/rom/sql/relation/writing.rb', line 67

def update(...)
  dataset.update(...)
end

#upsert(*args, &block) ⇒ Integer

Add upsert option (only PostgreSQL >= 9.5) Uses internal Sequel implementation Default - ON CONFLICT DO NOTHING more options: http://sequel.jeremyevans.net/rdoc-adapters/classes/Sequel/Postgres/DatasetMethods.html#method-i-insert_conflict

Examples:

users.upsert({ name: 'Jane', email: '[email protected]' },
             { target: :email, update: { name: :excluded__name } })

Returns:

  • (Integer)

    Number of affected rows



19
20
21
22
23
24
25
26
27
28
# File 'lib/rom/sql/relation/writing.rb', line 19

def upsert(*args, &block)
  if args.size > 1 && args[-1].is_a?(Hash)
    *values, opts = args
  else
    values = args
    opts = EMPTY_HASH
  end

  dataset.insert_conflict(opts).insert(*values, &block)
end