mass_insert 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/.travis.yml +10 -0
- data/Gemfile +0 -2
- data/README.md +15 -23
- data/Rakefile +22 -33
- data/lib/mass_insert.rb +11 -7
- data/lib/mass_insert/base.rb +22 -58
- data/lib/mass_insert/builder/adapters.rb +16 -0
- data/lib/mass_insert/builder/adapters/adapter.rb +65 -0
- data/lib/mass_insert/builder/adapters/helpers/abstract_query.rb +52 -0
- data/lib/mass_insert/builder/adapters/helpers/column_value.rb +90 -0
- data/lib/mass_insert/builder/adapters/mysql2_adapter.rb +15 -0
- data/lib/mass_insert/builder/adapters/postgresql_adapter.rb +7 -0
- data/lib/mass_insert/builder/adapters/sqlite3_adapter.rb +27 -0
- data/lib/mass_insert/builder/adapters/sqlserver_adapter.rb +26 -0
- data/lib/mass_insert/builder/base.rb +28 -0
- data/lib/mass_insert/builder/utilities.rb +13 -0
- data/lib/mass_insert/executer.rb +13 -0
- data/lib/mass_insert/process.rb +24 -0
- data/lib/mass_insert/result.rb +33 -0
- data/lib/mass_insert/version.rb +1 -1
- data/mass_insert.gemspec +2 -2
- data/spec/adapters/column_types/binary_spec.rb +64 -0
- data/spec/adapters/column_types/boolean_spec.rb +48 -0
- data/spec/adapters/column_types/decimal_spec.rb +59 -0
- data/spec/adapters/column_types/integer_spec.rb +59 -0
- data/spec/adapters/column_types/string_spec.rb +46 -0
- data/spec/{active_record_models → adapters}/model_spec.rb +1 -21
- data/spec/{active_record_dummy → dummy}/.gitignore +0 -0
- data/spec/{active_record_dummy → dummy}/Gemfile +1 -1
- data/spec/{active_record_dummy → dummy}/README.rdoc +0 -0
- data/spec/{active_record_dummy → dummy}/Rakefile +0 -0
- data/spec/{active_record_dummy → dummy}/app/assets/images/rails.png +0 -0
- data/spec/{active_record_dummy → dummy}/app/assets/javascripts/application.js +0 -0
- data/spec/{active_record_dummy → dummy}/app/assets/stylesheets/application.css +0 -0
- data/spec/{active_record_dummy → dummy}/app/controllers/application_controller.rb +0 -0
- data/spec/{active_record_dummy → dummy}/app/helpers/application_helper.rb +0 -0
- data/spec/{active_record_dummy → dummy}/app/mailers/.gitkeep +0 -0
- data/spec/{active_record_dummy → dummy}/app/models/.gitkeep +0 -0
- data/spec/{active_record_dummy → dummy}/app/models/user.rb +0 -0
- data/spec/{active_record_dummy → dummy}/app/views/layouts/application.html.erb +0 -0
- data/spec/{active_record_dummy → dummy}/config.ru +0 -0
- data/spec/{active_record_dummy → dummy}/config/application.rb +0 -0
- data/spec/{active_record_dummy → dummy}/config/boot.rb +0 -0
- data/spec/{active_record_dummy → dummy}/config/database.yml +3 -8
- data/spec/{active_record_dummy → dummy}/config/environment.rb +0 -0
- data/spec/{active_record_dummy → dummy}/config/environments/development.rb +0 -0
- data/spec/{active_record_dummy → dummy}/config/environments/mysql2.rb +0 -0
- data/spec/{active_record_dummy → dummy}/config/environments/postgresql.rb +0 -0
- data/spec/{active_record_dummy → dummy}/config/environments/production.rb +0 -0
- data/spec/{active_record_dummy → dummy}/config/environments/sqlite3.rb +0 -0
- data/spec/{active_record_dummy → dummy}/config/environments/test.rb +0 -0
- data/spec/{active_record_dummy → dummy}/config/initializers/backtrace_silencers.rb +0 -0
- data/spec/{active_record_dummy → dummy}/config/initializers/inflections.rb +0 -0
- data/spec/{active_record_dummy → dummy}/config/initializers/mime_types.rb +0 -0
- data/spec/{active_record_dummy → dummy}/config/initializers/secret_token.rb +0 -0
- data/spec/{active_record_dummy → dummy}/config/initializers/session_store.rb +0 -0
- data/spec/{active_record_dummy → dummy}/config/initializers/wrap_parameters.rb +0 -0
- data/spec/{active_record_dummy → dummy}/config/locales/en.yml +0 -0
- data/spec/{active_record_dummy → dummy}/config/routes.rb +0 -0
- data/spec/{active_record_dummy → dummy}/db/migrate/20130412154541_create_users.rb +0 -0
- data/spec/{active_record_dummy → dummy}/db/schema.rb +0 -0
- data/spec/{active_record_dummy → dummy}/db/seeds.rb +0 -0
- data/spec/{active_record_dummy → dummy}/lib/assets/.gitkeep +0 -0
- data/spec/{active_record_dummy → dummy}/lib/tasks/.gitkeep +0 -0
- data/spec/{active_record_dummy → dummy}/log/.gitkeep +0 -0
- data/spec/{active_record_dummy → dummy}/public/404.html +0 -0
- data/spec/{active_record_dummy → dummy}/public/422.html +0 -0
- data/spec/{active_record_dummy → dummy}/public/500.html +0 -0
- data/spec/{active_record_dummy → dummy}/public/favicon.ico +0 -0
- data/spec/{active_record_dummy → dummy}/public/index.html +0 -0
- data/spec/{active_record_dummy → dummy}/public/robots.txt +0 -0
- data/spec/{active_record_dummy → dummy}/script/rails +0 -0
- data/spec/{active_record_dummy → dummy}/vendor/assets/javascripts/.gitkeep +0 -0
- data/spec/{active_record_dummy → dummy}/vendor/assets/stylesheets/.gitkeep +0 -0
- data/spec/{active_record_dummy → dummy}/vendor/plugins/.gitkeep +0 -0
- data/spec/lib/mass_insert/base_spec.rb +40 -0
- data/spec/lib/mass_insert/builder/adapters/adapter_spec.rb +129 -0
- data/spec/lib/mass_insert/builder/adapters/helpers/abstract_query_spec.rb +130 -0
- data/spec/{mass_insert/adapters/adapter_helpers → lib/mass_insert/builder/adapters/helpers}/column_value_spec.rb +42 -94
- data/spec/lib/mass_insert/builder/adapters/mysql_adapter_spec.rb +15 -0
- data/spec/lib/mass_insert/builder/adapters/postgresql_adapter_spec.rb +9 -0
- data/spec/lib/mass_insert/builder/adapters/sqlite3_adapter_spec.rb +52 -0
- data/spec/lib/mass_insert/builder/adapters/sqlserver_adapter_spec.rb +38 -0
- data/spec/lib/mass_insert/builder/adapters_spec.rb +31 -0
- data/spec/lib/mass_insert/builder/base_spec.rb +28 -0
- data/spec/lib/mass_insert/builder/utilities_spec.rb +11 -0
- data/spec/lib/mass_insert/executer_spec.rb +33 -0
- data/spec/lib/mass_insert/process_spec.rb +44 -0
- data/spec/lib/mass_insert/result_spec.rb +45 -0
- data/spec/lib/mass_insert_spec.rb +35 -0
- data/spec/spec_helper.rb +7 -2
- data/spec/support/mass_insert_support.rb +12 -0
- metadata +160 -163
- data/lib/mass_insert/adapters.rb +0 -10
- data/lib/mass_insert/adapters/adapter.rb +0 -26
- data/lib/mass_insert/adapters/adapter_helpers.rb +0 -11
- data/lib/mass_insert/adapters/adapter_helpers/abstract_query.rb +0 -56
- data/lib/mass_insert/adapters/adapter_helpers/column_value.rb +0 -110
- data/lib/mass_insert/adapters/adapter_helpers/sanitizer.rb +0 -21
- data/lib/mass_insert/adapters/adapter_helpers/timestamp.rb +0 -33
- data/lib/mass_insert/adapters/mysql2_adapter.rb +0 -13
- data/lib/mass_insert/adapters/postgresql_adapter.rb +0 -5
- data/lib/mass_insert/adapters/sqlite3_adapter.rb +0 -37
- data/lib/mass_insert/adapters/sqlserver_adapter.rb +0 -29
- data/lib/mass_insert/process_control.rb +0 -46
- data/lib/mass_insert/query_builder.rb +0 -39
- data/lib/mass_insert/query_execution.rb +0 -29
- data/spec/active_record_models/column_types/binary_spec.rb +0 -60
- data/spec/active_record_models/column_types/boolean_spec.rb +0 -52
- data/spec/active_record_models/column_types/decimal_spec.rb +0 -49
- data/spec/active_record_models/column_types/integer_spec.rb +0 -49
- data/spec/active_record_models/column_types/string_spec.rb +0 -50
- data/spec/dummy_models/test.rb +0 -5
- data/spec/mass_insert/adapters/adapter_helpers/abstract_query_spec.rb +0 -119
- data/spec/mass_insert/adapters/adapter_helpers/sanitizer_spec.rb +0 -46
- data/spec/mass_insert/adapters/adapter_helpers/timestamp_spec.rb +0 -75
- data/spec/mass_insert/adapters/adapter_helpers_spec.rb +0 -24
- data/spec/mass_insert/adapters/adapter_spec.rb +0 -79
- data/spec/mass_insert/adapters/mysql_adapter_spec.rb +0 -22
- data/spec/mass_insert/adapters/postgresql_adapter_spec.rb +0 -11
- data/spec/mass_insert/adapters/sqlite3_adapter_spec.rb +0 -84
- data/spec/mass_insert/adapters/sqlserver_adapter_spec.rb +0 -61
- data/spec/mass_insert/adapters_spec.rb +0 -32
- data/spec/mass_insert/base_spec.rb +0 -114
- data/spec/mass_insert/process_control_spec.rb +0 -125
- data/spec/mass_insert/query_builder_spec.rb +0 -84
- data/spec/mass_insert/query_execution_spec.rb +0 -50
- data/spec/mass_insert_spec.rb +0 -28
data/lib/mass_insert/adapters.rb
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
module MassInsert
|
2
|
-
module Adapters
|
3
|
-
autoload :Adapter, 'mass_insert/adapters/adapter.rb'
|
4
|
-
autoload :AdapterHelpers, 'mass_insert/adapters/adapter_helpers.rb'
|
5
|
-
autoload :Mysql2Adapter, 'mass_insert/adapters/mysql2_adapter.rb'
|
6
|
-
autoload :PostgreSQLAdapter, 'mass_insert/adapters/postgresql_adapter.rb'
|
7
|
-
autoload :SQLite3Adapter, 'mass_insert/adapters/sqlite3_adapter.rb'
|
8
|
-
autoload :SQLServerAdapter, 'mass_insert/adapters/sqlserver_adapter.rb'
|
9
|
-
end
|
10
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
module MassInsert
|
2
|
-
module Adapters
|
3
|
-
class Adapter
|
4
|
-
include AdapterHelpers
|
5
|
-
|
6
|
-
attr_accessor :values, :options, :columns
|
7
|
-
|
8
|
-
def initialize values, options
|
9
|
-
@values = values
|
10
|
-
@options = options
|
11
|
-
end
|
12
|
-
|
13
|
-
# Returns the options according to the method that wasn't found.
|
14
|
-
def method_missing method, *args
|
15
|
-
@options.has_key?(method) ? @options[method] : super
|
16
|
-
end
|
17
|
-
|
18
|
-
# Returns the array with the column names valid to be included into
|
19
|
-
# the query string according to the options.
|
20
|
-
def columns
|
21
|
-
@columns ||= sanitized_columns
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
@@ -1,56 +0,0 @@
|
|
1
|
-
module MassInsert
|
2
|
-
module Adapters
|
3
|
-
module AdapterHelpers
|
4
|
-
module AbstractQuery
|
5
|
-
|
6
|
-
# Returns a begin string to a basic mysql query insertion. Include
|
7
|
-
# the class table_name and it's included in the string.
|
8
|
-
def begin_string
|
9
|
-
"INSERT INTO #{table_name} "
|
10
|
-
end
|
11
|
-
|
12
|
-
# Returns a string with the column names to the class table name
|
13
|
-
# and divided by commas.
|
14
|
-
def string_columns
|
15
|
-
"(#{columns.join(", ")}) "
|
16
|
-
end
|
17
|
-
|
18
|
-
# Returns the string with all the row values that will be included
|
19
|
-
# in the sql string.
|
20
|
-
def string_values
|
21
|
-
"VALUES (#{string_rows_values});"
|
22
|
-
end
|
23
|
-
|
24
|
-
# Gives the correct format to the values string to all rows. This
|
25
|
-
# functions calls a function that will generate a single string row
|
26
|
-
# and at the end all the strings are concatenated.
|
27
|
-
def string_rows_values
|
28
|
-
values.map{ |row| string_single_row_values(row) }.join("), (")
|
29
|
-
end
|
30
|
-
|
31
|
-
# Returns the row column values string to be added in query string
|
32
|
-
# according to the type column and values.
|
33
|
-
# Before that row is prepared with the correct values.
|
34
|
-
def string_single_row_values row
|
35
|
-
row.merge!(timestamp_hash) if timestamp?
|
36
|
-
columns.map{ |col| string_single_value(row, col) }.join(", ")
|
37
|
-
end
|
38
|
-
|
39
|
-
# Returns a single column string value with the correct format and
|
40
|
-
# according to the database configuration, column type and presence.
|
41
|
-
def string_single_value row, column
|
42
|
-
ColumnValue.new(row, column, class_name).build
|
43
|
-
end
|
44
|
-
|
45
|
-
# This functions calls the necessary functions to create a complete
|
46
|
-
# mysql query to multiple insertion. The methods are in the Abstract
|
47
|
-
# Sql String module. If some method is too specific to this database
|
48
|
-
# adapter you can overwrite it.
|
49
|
-
def execute
|
50
|
-
"#{begin_string}#{string_columns}#{string_values}"
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
@@ -1,110 +0,0 @@
|
|
1
|
-
module MassInsert
|
2
|
-
module Adapters
|
3
|
-
module AdapterHelpers
|
4
|
-
class ColumnValue
|
5
|
-
|
6
|
-
attr_accessor :row, :column, :class_name
|
7
|
-
|
8
|
-
def initialize row, column, class_name
|
9
|
-
@row = row
|
10
|
-
@column = column
|
11
|
-
@class_name = class_name
|
12
|
-
end
|
13
|
-
|
14
|
-
# Returns a symbol with the column type in the database. The column or
|
15
|
-
# attribute should belongs to the class that invokes the mass insert.
|
16
|
-
def column_type
|
17
|
-
class_name.columns_hash[@column.to_s].type
|
18
|
-
end
|
19
|
-
|
20
|
-
# Returns the value to this column in the row hash. The value is
|
21
|
-
# finding by symbol or string key to be most flexible. This method
|
22
|
-
# tries to get a value by symbol key with the column name first and
|
23
|
-
# if the symbol key doesn't exist it will try to find it by string
|
24
|
-
# key. Otherwise it will return nil.
|
25
|
-
def column_value
|
26
|
-
row.fetch(column){row[@column.to_s]}
|
27
|
-
end
|
28
|
-
|
29
|
-
# Returns the string with the database adapter name usually in the
|
30
|
-
# database.yml file in your Rails project.
|
31
|
-
def adapter
|
32
|
-
ActiveRecord::Base.connection.instance_values["config"][:adapter]
|
33
|
-
end
|
34
|
-
|
35
|
-
# Returns the default value string to be included in query string.
|
36
|
-
# This default value is added to the query if the row hash does not
|
37
|
-
# contains the database column value.
|
38
|
-
def default_value
|
39
|
-
default_db_value ? default_db_value.to_s : "null"
|
40
|
-
end
|
41
|
-
|
42
|
-
# Return the database default value using methods that ActiveRecord
|
43
|
-
# provides to see database columns settings.
|
44
|
-
def default_db_value
|
45
|
-
class_name.columns_hash[@column.to_s].default
|
46
|
-
end
|
47
|
-
|
48
|
-
# Returns a single column string value with the correct format and
|
49
|
-
# according to the database configuration, column type and presence.
|
50
|
-
# If the row hash does not include the value to this column return the
|
51
|
-
# default value according to database configuration.
|
52
|
-
def build
|
53
|
-
column_value.nil? ? default_value : send(:"column_value_#{column_type}")
|
54
|
-
end
|
55
|
-
|
56
|
-
# Returns the correct value when the column value is string, text,
|
57
|
-
# date, time, datetime, timestamp. There are alias methods to the
|
58
|
-
# other column types that need a similar query value.
|
59
|
-
def column_value_string
|
60
|
-
"'#{column_value}'"
|
61
|
-
end
|
62
|
-
alias :column_value_text :column_value_string
|
63
|
-
alias :column_value_date :column_value_string
|
64
|
-
alias :column_value_time :column_value_string
|
65
|
-
alias :column_value_datetime :column_value_string
|
66
|
-
alias :column_value_timestamp :column_value_string
|
67
|
-
alias :column_value_binary :column_value_string
|
68
|
-
|
69
|
-
# Returns the correct value to integer column. The column values is
|
70
|
-
# converted to integer to be sure that the value is correct to be
|
71
|
-
# persisted into the database and after it, it's converted to string.
|
72
|
-
def column_value_integer
|
73
|
-
column_value.to_i.to_s
|
74
|
-
end
|
75
|
-
|
76
|
-
# Returns the correct value to decimal column. There is an alias method
|
77
|
-
# to float type. The column values is converted to decimal to be sure
|
78
|
-
# that the value is correct to be persisted into the database and after
|
79
|
-
# it, it's converted to string.
|
80
|
-
def column_value_decimal
|
81
|
-
column_value.to_f.to_s
|
82
|
-
end
|
83
|
-
alias :column_value_float :column_value_decimal
|
84
|
-
|
85
|
-
# Returns the correct value to boolean column. This column calls the
|
86
|
-
# correct method according to the database adapter to return the correct
|
87
|
-
# value to that database engine.
|
88
|
-
def column_value_boolean
|
89
|
-
self.send(:"#{adapter}_column_value_boolean")
|
90
|
-
end
|
91
|
-
|
92
|
-
# Returns the column value to boolean column like a string. If the
|
93
|
-
# column value exists returns a true string else false string. There
|
94
|
-
# are alias methods to the database engines that works similarity.
|
95
|
-
def mysql2_column_value_boolean
|
96
|
-
column_value ? "true" : "false"
|
97
|
-
end
|
98
|
-
alias :postgresql_column_value_boolean :mysql2_column_value_boolean
|
99
|
-
alias :sqlserver_column_value_boolean :mysql2_column_value_boolean
|
100
|
-
|
101
|
-
# Returns the column value to boolean column like a string. If the
|
102
|
-
# column value exists returns a "1" else "0".
|
103
|
-
def sqlite3_column_value_boolean
|
104
|
-
column_value ? "1" : "0"
|
105
|
-
end
|
106
|
-
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module MassInsert
|
2
|
-
module Adapters
|
3
|
-
module AdapterHelpers
|
4
|
-
module Sanitizer
|
5
|
-
|
6
|
-
# Returns an array with the columns in the table like symbols.
|
7
|
-
def table_columns
|
8
|
-
class_name.column_names.map(&:to_sym)
|
9
|
-
end
|
10
|
-
|
11
|
-
# Prepare array with the column names according to the options.
|
12
|
-
def sanitized_columns
|
13
|
-
columns = table_columns
|
14
|
-
columns.delete(primary_key) if primary_key_mode == :auto
|
15
|
-
columns
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
module MassInsert
|
2
|
-
module Adapters
|
3
|
-
module AdapterHelpers
|
4
|
-
module Timestamp
|
5
|
-
|
6
|
-
# Returns true o false if the database table has timestamp columns.
|
7
|
-
def timestamp?
|
8
|
-
columns.include?(:created_at) && columns.include?(:updated_at)
|
9
|
-
end
|
10
|
-
|
11
|
-
# Returns timestamp format according to the database adapter. This
|
12
|
-
# function can be overwrite in database adapters classes.
|
13
|
-
def timestamp_format
|
14
|
-
"%Y-%m-%d %H:%M:%S.%6N"
|
15
|
-
end
|
16
|
-
|
17
|
-
# Returns the timestamp value according to the correct timestamp
|
18
|
-
# format to that database engine.
|
19
|
-
def timestamp
|
20
|
-
Time.now.strftime(timestamp_format)
|
21
|
-
end
|
22
|
-
|
23
|
-
# Returns the timestamp hash to be merge into row values that will
|
24
|
-
# be saved in the database.
|
25
|
-
def timestamp_hash
|
26
|
-
timestamp_value = timestamp
|
27
|
-
{:created_at => timestamp_value, :updated_at => timestamp_value}
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
module MassInsert
|
2
|
-
module Adapters
|
3
|
-
class Mysql2Adapter < Adapter
|
4
|
-
|
5
|
-
# This method is overwrite because the timestamp format to this
|
6
|
-
# database engine does not need precision in nanoseconds.
|
7
|
-
def timestamp_format
|
8
|
-
"%Y-%m-%d %H:%M:%S"
|
9
|
-
end
|
10
|
-
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
module MassInsert
|
2
|
-
module Adapters
|
3
|
-
class SQLite3Adapter < Adapter
|
4
|
-
|
5
|
-
MAX_VALUES_PER_INSERTION = 500
|
6
|
-
|
7
|
-
# This method is overwrite because the query string to the Sqlite3
|
8
|
-
# adapter is different. Then the method in the AbstractQuery module
|
9
|
-
# is ignored.
|
10
|
-
def string_values
|
11
|
-
"SELECT #{string_rows_values};"
|
12
|
-
end
|
13
|
-
|
14
|
-
# This method is overwrite because the query string to complete the
|
15
|
-
# string rows values is different. The separator to sqlite adapter is
|
16
|
-
# 'UNION SELECT' instead of '), (' in other sql adapters.
|
17
|
-
def string_rows_values
|
18
|
-
values.map{ |row| string_single_row_values(row) }.join(" UNION SELECT ")
|
19
|
-
end
|
20
|
-
|
21
|
-
# This functions calls the necessary functions to create a complete
|
22
|
-
# sqlite3 query to multiple insertion. The methods are in the Abstract
|
23
|
-
# Query module. If some method is too specific to this database adapter
|
24
|
-
# you can overwrite it. The values that the user gave will be treated
|
25
|
-
# in batches of 500 items because sqlite database allows by default
|
26
|
-
# batches of 500.and each batch will generate a query. This method will
|
27
|
-
# generate an array with batch queries.
|
28
|
-
def execute
|
29
|
-
@values.each_slice(MAX_VALUES_PER_INSERTION).map do |slice|
|
30
|
-
@values = slice
|
31
|
-
super
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
module MassInsert
|
2
|
-
module Adapters
|
3
|
-
class SQLServerAdapter < Adapter
|
4
|
-
|
5
|
-
MAX_VALUES_PER_INSERTION = 1000
|
6
|
-
|
7
|
-
# This method is overwrite because the timestamp format to this
|
8
|
-
# database engine needs precision in three nanoseconds.
|
9
|
-
def timestamp_format
|
10
|
-
"%Y-%m-%d %H:%M:%S.%3N"
|
11
|
-
end
|
12
|
-
|
13
|
-
# This functions calls the necessary functions to create a complete
|
14
|
-
# sqlserver query to multiple insertion. The methods are in the Abstract
|
15
|
-
# Query module. If some method is too specific to this database adapter
|
16
|
-
# you can overwrite it. The values that the user gave will be treated
|
17
|
-
# in batches of 500 items because sqlite database allows by default
|
18
|
-
# batches of 500.and each batch will generate a query. This method will
|
19
|
-
# generate an array with batch queries.
|
20
|
-
def execute
|
21
|
-
@values.each_slice(MAX_VALUES_PER_INSERTION).map do |slice|
|
22
|
-
@values = slice
|
23
|
-
super
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
@@ -1,46 +0,0 @@
|
|
1
|
-
require 'benchmark'
|
2
|
-
require 'ostruct'
|
3
|
-
|
4
|
-
module MassInsert
|
5
|
-
class ProcessControl
|
6
|
-
|
7
|
-
attr_accessor :values, :options
|
8
|
-
|
9
|
-
def initialize values, options
|
10
|
-
@values = values
|
11
|
-
@options = options
|
12
|
-
end
|
13
|
-
|
14
|
-
# This method is responsible to call all the necessary process to
|
15
|
-
# complete the mass insertion process and save the time each method
|
16
|
-
# takes being executed.
|
17
|
-
def start
|
18
|
-
@build_time = Benchmark.measure{ build_query }
|
19
|
-
@execute_time = Benchmark.measure{ execute_query }
|
20
|
-
end
|
21
|
-
|
22
|
-
# Returns the correct query string according to database adapter
|
23
|
-
# previosly configured usually in database.yml in Rails project.
|
24
|
-
def build_query
|
25
|
-
@query = QueryBuilder.new(values, options).build
|
26
|
-
end
|
27
|
-
|
28
|
-
# This method does a QueryExecution instance where the query will be
|
29
|
-
# execute. The query string is the instance variable @query.
|
30
|
-
def execute_query
|
31
|
-
QueryExecution.new(@query).execute if @query
|
32
|
-
end
|
33
|
-
|
34
|
-
# Provides an OpenStruc instance to see the process results. This
|
35
|
-
# method is usually called from mass_insert_results in Base module.
|
36
|
-
def results
|
37
|
-
result = OpenStruct.new
|
38
|
-
result.time = @build_time.total + @execute_time.total
|
39
|
-
result.records = values.count
|
40
|
-
result.build_time = @build_time.total
|
41
|
-
result.execute_time = @execute_time.total
|
42
|
-
result
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
46
|
-
end
|
@@ -1,39 +0,0 @@
|
|
1
|
-
module MassInsert
|
2
|
-
class QueryBuilder
|
3
|
-
|
4
|
-
attr_accessor :values, :options
|
5
|
-
|
6
|
-
def initialize values, options
|
7
|
-
@values = values
|
8
|
-
@options = options
|
9
|
-
end
|
10
|
-
|
11
|
-
# This function gets the correct adapter class and returns the
|
12
|
-
# sql string ready to be executed.
|
13
|
-
def build
|
14
|
-
adapter_class.new(values, options).execute
|
15
|
-
end
|
16
|
-
|
17
|
-
# Returns a string that contains the adapter type previosly
|
18
|
-
# configured in Rails project usually in the database.yml file.
|
19
|
-
def adapter
|
20
|
-
ActiveRecord::Base.connection.instance_values["config"][:adapter]
|
21
|
-
end
|
22
|
-
|
23
|
-
# Returns the class of the correct database adapter according to the
|
24
|
-
# database engine that is used in Rails project.
|
25
|
-
def adapter_class
|
26
|
-
case adapter
|
27
|
-
when "mysql2"
|
28
|
-
Adapters::Mysql2Adapter
|
29
|
-
when "postgresql"
|
30
|
-
Adapters::PostgreSQLAdapter
|
31
|
-
when "sqlite3"
|
32
|
-
Adapters::SQLite3Adapter
|
33
|
-
when "sqlserver"
|
34
|
-
Adapters::SQLServerAdapter
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
39
|
-
end
|