Class: Upsert

Inherits:
Object
  • Object
show all
Defined in:
lib/upsert.rb,
lib/upsert/row.rb,
lib/upsert/binary.rb,
lib/upsert/buffer.rb,
lib/upsert/quoter.rb,
lib/upsert/version.rb,
lib/upsert/buffer/mysql2_client.rb,
lib/upsert/buffer/pg_connection.rb,
lib/upsert/buffer/sqlite3_database.rb,
lib/upsert/buffer/pg_connection/column_definition.rb

Defined Under Namespace

Classes: Binary, TooBig

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, table_name) ⇒ Upsert

Returns a new instance of Upsert.

Parameters:

  • connection (Mysql2::Client, Sqlite3::Database, PG::Connection, #raw_connection)

    A supported database connection.

  • table_name (String, Symbol)

    The name of the table into which you will be upserting.



51
52
53
# File 'lib/upsert.rb', line 51

def initialize(connection, table_name)
  @buffer = Buffer.for connection, table_name
end

Class Method Details

.binary(v) ⇒ Upsert::Binary

Parameters:

  • v (String)

    A string containing binary data that should be inserted/escaped as such.

Returns:



17
18
19
# File 'lib/upsert.rb', line 17

def binary(v)
  Binary.new v
end

.stream(connection, table_name) {|Upsert| ... } ⇒ nil

Note:

Buffered in memory until it’s efficient to send to the server a packet.

Examples:

Many at once

Upsert.stream(Pet.connection, Pet.table_name) do |upsert|
  upsert.row({:name => 'Jerry'}, :breed => 'beagle')
  upsert.row({:name => 'Pierre'}, :breed => 'tabby')
end

Yields:

  • (Upsert)

    An Upsert object in streaming mode. You can call #row on it multiple times and it will try to optimize on speed.

Returns:

  • (nil)

Raises:

  • (Upsert::TooBig)

    If any row is too big to fit inside a single packet.



34
35
36
37
38
39
# File 'lib/upsert.rb', line 34

def stream(connection, table_name)
  upsert = new connection, table_name
  upsert.buffer.async!
  yield upsert
  upsert.buffer.sync!
end

Instance Method Details

#row(selector, document) ⇒ nil

Upsert a row given a selector and a document.

Examples:

One at a time

upsert = Upsert.new Pet.connection, Pet.table_name
upsert.row({:name => 'Jerry'}, :breed => 'beagle')
upsert.row({:name => 'Pierre'}, :breed => 'tabby')

Parameters:

  • selector (Hash)

    Key-value pairs that will be used to find or create a row.

  • document (Hash)

    Key-value pairs that will be set on the row, whether it previously existed or not.

Returns:

  • (nil)

Raises:

  • (Upsert::TooBig)

    If any row is too big to fit inside a single packet.

See Also:



70
71
72
73
# File 'lib/upsert.rb', line 70

def row(selector, document)
  buffer.add selector, document
  nil
end