Class: Upsert

Inherits:
Object
  • Object
show all
Defined in:
lib/upsert.rb,
lib/upsert/row.rb,
lib/upsert/binary.rb,
lib/upsert/version.rb,
lib/upsert/connection.rb,
lib/upsert/merge_function.rb,
lib/upsert/connection/jdbc.rb,
lib/upsert/column_definition.rb,
lib/upsert/connection/sqlite3.rb,
lib/upsert/active_record_upsert.rb,
lib/upsert/merge_function/mysql.rb,
lib/upsert/connection/postgresql.rb,
lib/upsert/merge_function/sqlite3.rb,
lib/upsert/column_definition/mysql.rb,
lib/upsert/connection/Mysql2_Client.rb,
lib/upsert/connection/PG_Connection.rb,
lib/upsert/merge_function/postgresql.rb,
lib/upsert/connection/SQLite3_Database.rb,
lib/upsert/column_definition/postgresql.rb,
lib/upsert/merge_function/Mysql2_Client.rb,
lib/upsert/merge_function/PG_Connection.rb,
lib/upsert/connection/Java_OrgSqliteConn.rb,
lib/upsert/merge_function/SQLite3_Database.rb,
lib/upsert/merge_function/Java_OrgSqliteConn.rb,
lib/upsert/connection/Java_ComMysqlJdbc_JDBC4Connection.rb,
lib/upsert/merge_function/Java_ComMysqlJdbc_JDBC4Connection.rb,
lib/upsert/connection/Java_OrgPostgresqlJdbc4_Jdbc4Connection.rb,
lib/upsert/merge_function/Java_OrgPostgresqlJdbc4_Jdbc4Connection.rb

Defined Under Namespace

Modules: ActiveRecordUpsert

Constant Summary collapse

SINGLE_QUOTE =
%{'}
DOUBLE_QUOTE =
%{"}
BACKTICK =
%{`}
X_AND_SINGLE_QUOTE =
%{x'}
USEC_SPRINTF =
'%06d'
SEC_FRACTION =
8.64e10
NANO_FRACTION =
8.64e13
ISO8601_DATETIME =
'%Y-%m-%d %H:%M:%S'
ISO8601_DATE =
'%F'
UTC_TZ =
'+00:00'
NULL_WORD =
'NULL'
METAL_CLASS_ALIAS =
{
  'PGConn'           => 'PG::Connection',
  'org.sqlite.Conn'  => 'Java::OrgSqliteConn' # for some reason, org.sqlite.Conn doesn't have a ruby class name
}
VERSION =
"1.1.3"

Class Attribute Summary collapse

Instance Attribute Summary collapse

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, #metal)

    A supported database connection.

  • table_name (String, Symbol)

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



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/upsert.rb', line 177

def initialize(connection, table_name)
  @table_name = table_name.to_s
  metal = Upsert.metal connection
  @flavor = Upsert.flavor metal
  @adapter = Upsert.adapter metal
  # todo memoize
  Dir[File.expand_path("../upsert/**/{#{flavor.downcase},#{adapter}}.rb", __FILE__)].each do |path|
    require path
  end
  @connection = Connection.const_get(adapter).new self, metal
  @merge_function_class = MergeFunction.const_get adapter
end

Class Attribute Details

.logger#info, ...

The current logger

Returns:

  • (#info, #warn, #debug)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/upsert.rb', line 20

def logger
  @logger || Thread.exclusive do
    @logger ||= if defined?(::Rails) and (rails_logger = ::Rails.logger)
      rails_logger
    elsif defined?(::ActiveRecord) and ::ActiveRecord.const_defined?(:Base) and (ar_logger = ::ActiveRecord::Base.logger)
      ar_logger
    else
      my_logger = Logger.new $stderr
      case ENV['UPSERT_DEBUG']
      when 'true'
        my_logger.level = Logger::DEBUG
      when 'false'
        my_logger.level = Logger::INFO
      end
      my_logger
    end
  end
end

Instance Attribute Details

#connectionUpsert::Connection (readonly)

Returns:

  • (Upsert::Connection)


161
162
163
# File 'lib/upsert.rb', line 161

def connection
  @connection
end

#table_nameString (readonly)

Returns:

  • (String)


164
165
166
# File 'lib/upsert.rb', line 164

def table_name
  @table_name
end

Class Method Details

.batch(connection, table_name) {|Upsert| ... } ⇒ nil Also known as: stream

More efficient way of upserting multiple rows at once.

Examples:

Many at once

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

Parameters:

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

    A supported database connection.

  • table_name (String, Symbol)

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

Yields:

  • (Upsert)

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

Returns:

  • (nil)


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

def batch(connection, table_name)
  upsert = new connection, table_name
  yield upsert
end

.binary(v) ⇒ Upsert::Binary

Parameters:

  • v (String)

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

Returns:

  • (Upsert::Binary)


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

def binary(v)
  Binary.new v
end

.clear_database_functions(connection) ⇒ Object

Clear any database functions that may have been created.

Currently only applies to PostgreSQL.

Parameters:

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

    A supported database connection.



44
45
46
47
# File 'lib/upsert.rb', line 44

def clear_database_functions(connection)
  dummy = new(connection, :dummy)
  dummy.clear_database_functions
end

Instance Method Details

#row(selector, setter = {}) ⇒ nil

Upsert a row given a selector and a setter.

The selector values are used as setters if it’s a new row. So if your selector is ‘name=Jerry` and your setter is `age=4`, and there is no Jerry yet, then a new row will be created with name Jerry and age 4.

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.

  • setter (Hash) (defaults to: {})

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

Returns:

  • (nil)

See Also:



205
206
207
208
# File 'lib/upsert.rb', line 205

def row(selector, setter = {})
  merge_function_class.execute self, Row.new(selector, setter)
  nil
end