Class: Upsert
- Inherits:
-
Object
- Object
- Upsert
- 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
-
.logger ⇒ #info, ...
The current logger.
Instance Attribute Summary collapse
- #connection ⇒ Upsert::Connection readonly
- #table_name ⇒ String readonly
Class Method Summary collapse
-
.batch(connection, table_name) {|Upsert| ... } ⇒ nil
(also: stream)
More efficient way of upserting multiple rows at once.
- .binary(v) ⇒ Upsert::Binary
-
.clear_database_functions(connection) ⇒ Object
Clear any database functions that may have been created.
Instance Method Summary collapse
-
#initialize(connection, table_name) ⇒ Upsert
constructor
A new instance of Upsert.
-
#row(selector, setter = {}) ⇒ nil
Upsert a row given a selector and a setter.
Constructor Details
#initialize(connection, table_name) ⇒ Upsert
Returns a new instance of Upsert.
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 = Upsert. connection @flavor = Upsert.flavor @adapter = Upsert.adapter # todo memoize Dir[File.("../upsert/**/{#{flavor.downcase},#{adapter}}.rb", __FILE__)].each do |path| require path end @connection = Connection.const_get(adapter).new self, @merge_function_class = MergeFunction.const_get adapter end |
Class Attribute Details
.logger ⇒ #info, ...
The current logger
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
#connection ⇒ Upsert::Connection (readonly)
161 162 163 |
# File 'lib/upsert.rb', line 161 def connection @connection end |
#table_name ⇒ String (readonly)
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.
70 71 72 73 |
# File 'lib/upsert.rb', line 70 def batch(connection, table_name) upsert = new connection, table_name yield upsert end |
.clear_database_functions(connection) ⇒ Object
Clear any database functions that may have been created.
Currently only applies to PostgreSQL.
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.
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 |