mass_insert 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mass_insert/adapters.rb +0 -1
- data/lib/mass_insert/adapters/adapter.rb +0 -3
- data/lib/mass_insert/adapters/helpers.rb +1 -0
- data/lib/mass_insert/adapters/helpers/abstract_query.rb +8 -0
- data/lib/mass_insert/adapters/helpers/column_value.rb +116 -0
- data/lib/mass_insert/adapters/mysql2_adapter.rb +0 -8
- data/lib/mass_insert/adapters/postgresql_adapter.rb +0 -8
- data/lib/mass_insert/adapters/sqlite3_adapter.rb +1 -1
- data/lib/mass_insert/adapters/sqlserver_adapter.rb +7 -1
- data/lib/mass_insert/version.rb +1 -1
- data/spec/active_record_models/column_types/binary_spec.rb +6 -6
- data/spec/active_record_models/column_types/boolean_spec.rb +5 -5
- data/spec/active_record_models/column_types/decimal_spec.rb +5 -5
- data/spec/active_record_models/column_types/integer_spec.rb +5 -5
- data/spec/active_record_models/column_types/string_spec.rb +5 -5
- data/spec/active_record_models/model_spec.rb +60 -7
- data/spec/mass_insert/adapters/helpers/abstract_query_spec.rb +14 -1
- data/spec/mass_insert/adapters/{column_value_spec.rb → helpers/column_value_spec.rb} +2 -2
- data/spec/mass_insert/adapters/helpers_spec.rb +5 -1
- data/spec/mass_insert/adapters/mysql_adapter_spec.rb +0 -13
- data/spec/mass_insert/adapters/postgresql_adapter_spec.rb +0 -14
- data/spec/mass_insert/adapters/sqlserver_adapter_spec.rb +10 -0
- data/spec/mass_insert/adapters_spec.rb +0 -4
- metadata +7 -7
- data/lib/mass_insert/adapters/column_value.rb +0 -111
data/lib/mass_insert/adapters.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
module MassInsert
|
2
2
|
module Adapters
|
3
3
|
autoload :Adapter, 'mass_insert/adapters/adapter.rb'
|
4
|
-
autoload :ColumnValue, 'mass_insert/adapters/column_value.rb'
|
5
4
|
autoload :Helpers, 'mass_insert/adapters/helpers.rb'
|
6
5
|
autoload :Mysql2Adapter, 'mass_insert/adapters/mysql2_adapter.rb'
|
7
6
|
autoload :PostgreSQLAdapter, 'mass_insert/adapters/postgresql_adapter.rb'
|
@@ -1,8 +1,5 @@
|
|
1
1
|
module MassInsert
|
2
2
|
module Adapters
|
3
|
-
# This class provides some helper methods to build the sql string that
|
4
|
-
# be executed. The methods here provides a functionality that be required
|
5
|
-
# in all the adapters.
|
6
3
|
class Adapter
|
7
4
|
include Helpers::AbstractQuery
|
8
5
|
include Helpers::Timestamp
|
@@ -2,6 +2,7 @@ module MassInsert
|
|
2
2
|
module Adapters
|
3
3
|
module Helpers
|
4
4
|
autoload :AbstractQuery, 'mass_insert/adapters/helpers/abstract_query.rb'
|
5
|
+
autoload :ColumnValue, 'mass_insert/adapters/helpers/column_value.rb'
|
5
6
|
autoload :Timestamp, 'mass_insert/adapters/helpers/timestamp.rb'
|
6
7
|
autoload :Sanitizer, 'mass_insert/adapters/helpers/sanitizer.rb'
|
7
8
|
end
|
@@ -42,6 +42,14 @@ module MassInsert
|
|
42
42
|
ColumnValue.new(row, column, options).build
|
43
43
|
end
|
44
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
|
+
|
45
53
|
end
|
46
54
|
end
|
47
55
|
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
module MassInsert
|
2
|
+
module Adapters
|
3
|
+
module Helpers
|
4
|
+
class ColumnValue
|
5
|
+
|
6
|
+
attr_accessor :row, :column, :options
|
7
|
+
|
8
|
+
def initialize row, column, options
|
9
|
+
@row = row
|
10
|
+
@column = column
|
11
|
+
@options = options
|
12
|
+
end
|
13
|
+
|
14
|
+
# Returns the class that invokes the mass insert process. The class
|
15
|
+
# is in the options hash.
|
16
|
+
def class_name
|
17
|
+
options[:class_name]
|
18
|
+
end
|
19
|
+
|
20
|
+
# Returns a symbol with the column type in the database. The column or
|
21
|
+
# attribute should belongs to the class that invokes the mass insert.
|
22
|
+
def column_type
|
23
|
+
class_name.columns_hash[@column.to_s].type
|
24
|
+
end
|
25
|
+
|
26
|
+
# Returns the value to this column in the row hash. The value is
|
27
|
+
# finding by symbol or string key to be most flexible. This method
|
28
|
+
# tries to get a value by symbol key with the column name first and
|
29
|
+
# if the symbol key doesn't exist it will try to find it by string
|
30
|
+
# key. Otherwise it will return nil.
|
31
|
+
def column_value
|
32
|
+
row.fetch(column.to_sym){row[column.to_s]}
|
33
|
+
end
|
34
|
+
|
35
|
+
# Returns the string with the database adapter name usually in the
|
36
|
+
# database.yml file in your Rails project.
|
37
|
+
def adapter
|
38
|
+
ActiveRecord::Base.connection.instance_values["config"][:adapter]
|
39
|
+
end
|
40
|
+
|
41
|
+
# Returns the default value string to be included in query string.
|
42
|
+
# This default value is added to the query if the row hash does not
|
43
|
+
# contains the database column value.
|
44
|
+
def default_value
|
45
|
+
default_db_value ? default_db_value.to_s : "null"
|
46
|
+
end
|
47
|
+
|
48
|
+
# Return the database default value using methods that ActiveRecord
|
49
|
+
# provides to see database columns settings.
|
50
|
+
def default_db_value
|
51
|
+
class_name.columns_hash[@column.to_s].default
|
52
|
+
end
|
53
|
+
|
54
|
+
# Returns a single column string value with the correct format and
|
55
|
+
# according to the database configuration, column type and presence.
|
56
|
+
# If the row hash does not include the value to this column return the
|
57
|
+
# default value according to database configuration.
|
58
|
+
def build
|
59
|
+
column_value.nil? ? default_value : send(:"column_value_#{column_type}")
|
60
|
+
end
|
61
|
+
|
62
|
+
# Returns the correct value when the column value is string, text,
|
63
|
+
# date, time, datetime, timestamp. There are alias methods to the
|
64
|
+
# other column types that need a similar query value.
|
65
|
+
def column_value_string
|
66
|
+
"'#{column_value}'"
|
67
|
+
end
|
68
|
+
alias :column_value_text :column_value_string
|
69
|
+
alias :column_value_date :column_value_string
|
70
|
+
alias :column_value_time :column_value_string
|
71
|
+
alias :column_value_datetime :column_value_string
|
72
|
+
alias :column_value_timestamp :column_value_string
|
73
|
+
alias :column_value_binary :column_value_string
|
74
|
+
|
75
|
+
# Returns the correct value to integer column. The column values is
|
76
|
+
# converted to integer to be sure that the value is correct to be
|
77
|
+
# persisted into the database and after it, it's converted to string.
|
78
|
+
def column_value_integer
|
79
|
+
column_value.to_i.to_s
|
80
|
+
end
|
81
|
+
|
82
|
+
# Returns the correct value to decimal column. There is an alias method
|
83
|
+
# to float type. The column values is converted to decimal to be sure
|
84
|
+
# that the value is correct to be persisted into the database and after
|
85
|
+
# it, it's converted to string.
|
86
|
+
def column_value_decimal
|
87
|
+
column_value.to_f.to_s
|
88
|
+
end
|
89
|
+
alias :column_value_float :column_value_decimal
|
90
|
+
|
91
|
+
# Returns the correct value to boolean column. This column calls the
|
92
|
+
# correct method according to the database adapter to return the correct
|
93
|
+
# value to that database engine.
|
94
|
+
def column_value_boolean
|
95
|
+
self.send(:"#{adapter}_column_value_boolean")
|
96
|
+
end
|
97
|
+
|
98
|
+
# Returns the column value to boolean column like a string. If the
|
99
|
+
# column value exists returns a true string else false string. There
|
100
|
+
# are alias methods to the database engines that works similarity.
|
101
|
+
def mysql2_column_value_boolean
|
102
|
+
column_value ? "true" : "false"
|
103
|
+
end
|
104
|
+
alias :postgresql_column_value_boolean :mysql2_column_value_boolean
|
105
|
+
alias :sqlserver_column_value_boolean :mysql2_column_value_boolean
|
106
|
+
|
107
|
+
# Returns the column value to boolean column like a string. If the
|
108
|
+
# column value exists returns a "1" else "0".
|
109
|
+
def sqlite3_column_value_boolean
|
110
|
+
column_value ? "1" : "0"
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -8,14 +8,6 @@ module MassInsert
|
|
8
8
|
"%Y-%m-%d %H:%M:%S"
|
9
9
|
end
|
10
10
|
|
11
|
-
# This functions calls the necessary functions to create a complete
|
12
|
-
# mysql query to multiple insertion. The methods are in the Abstract
|
13
|
-
# Sql String module. If some method is too specific to this database
|
14
|
-
# adapter you can overwrite it.
|
15
|
-
def execute
|
16
|
-
"#{begin_string}#{string_columns}#{string_values}"
|
17
|
-
end
|
18
|
-
|
19
11
|
end
|
20
12
|
end
|
21
13
|
end
|
@@ -2,14 +2,6 @@ module MassInsert
|
|
2
2
|
module Adapters
|
3
3
|
class PostgreSQLAdapter < Adapter
|
4
4
|
|
5
|
-
# This functions calls the necessary functions to create a complete
|
6
|
-
# postgresql query to multiple insertion. The methods are in the
|
7
|
-
# AbstractQuery module. If some method is too specific to this database
|
8
|
-
# adapter you can overwrite it.
|
9
|
-
def execute
|
10
|
-
"#{begin_string}#{string_columns}#{string_values}"
|
11
|
-
end
|
12
|
-
|
13
5
|
end
|
14
6
|
end
|
15
7
|
end
|
@@ -4,6 +4,12 @@ module MassInsert
|
|
4
4
|
|
5
5
|
MAX_VALUES_PER_INSERTION = 1000
|
6
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
|
+
|
7
13
|
# This functions calls the necessary functions to create a complete
|
8
14
|
# sqlserver query to multiple insertion. The methods are in the Abstract
|
9
15
|
# Query module. If some method is too specific to this database adapter
|
@@ -14,7 +20,7 @@ module MassInsert
|
|
14
20
|
def execute
|
15
21
|
@values.each_slice(MAX_VALUES_PER_INSERTION).map do |slice|
|
16
22
|
@values = slice
|
17
|
-
|
23
|
+
super
|
18
24
|
end
|
19
25
|
end
|
20
26
|
|
data/lib/mass_insert/version.rb
CHANGED
@@ -12,7 +12,7 @@ describe "Binary" do
|
|
12
12
|
context "when is 1" do
|
13
13
|
it "should be saved correctly" do
|
14
14
|
User.mass_insert(@values, @options)
|
15
|
-
User.last.checked.
|
15
|
+
expect(User.last.checked).to eq("1")
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
@@ -20,7 +20,7 @@ describe "Binary" do
|
|
20
20
|
it "should be saved correctly" do
|
21
21
|
@values.first.merge!(:checked => 0)
|
22
22
|
User.mass_insert(@values, @options)
|
23
|
-
User.last.checked.
|
23
|
+
expect(User.last.checked).to eq("0")
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
@@ -29,7 +29,7 @@ describe "Binary" do
|
|
29
29
|
it "should convert string value to binary" do
|
30
30
|
@values.first.merge!(:checked => "string")
|
31
31
|
User.mass_insert(@values, @options)
|
32
|
-
User.last.checked.
|
32
|
+
expect(User.last.checked).to eq("string")
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
@@ -37,7 +37,7 @@ describe "Binary" do
|
|
37
37
|
it "should convert decimal value to binary" do
|
38
38
|
@values.first.merge!(:checked => 25.34)
|
39
39
|
User.mass_insert(@values, @options)
|
40
|
-
User.last.checked.
|
40
|
+
expect(User.last.checked).to eq("25.34")
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
@@ -45,7 +45,7 @@ describe "Binary" do
|
|
45
45
|
it "should convert boolean value to binary" do
|
46
46
|
@values.first.merge!(:checked => true)
|
47
47
|
User.mass_insert(@values, @options)
|
48
|
-
User.last.checked.
|
48
|
+
expect(User.last.checked).to eq("true")
|
49
49
|
end
|
50
50
|
end
|
51
51
|
end
|
@@ -54,7 +54,7 @@ describe "Binary" do
|
|
54
54
|
it "should save the default value" do
|
55
55
|
@values.first.delete(:checked)
|
56
56
|
User.mass_insert(@values, @options)
|
57
|
-
User.last.checked.
|
57
|
+
expect(User.last.checked).to eq(nil)
|
58
58
|
end
|
59
59
|
end
|
60
60
|
end
|
@@ -12,7 +12,7 @@ describe "Boolean" do
|
|
12
12
|
context "when is true" do
|
13
13
|
it "should be saved correctly" do
|
14
14
|
User.mass_insert(@values, @options)
|
15
|
-
User.last.active.
|
15
|
+
expect(User.last.active).to eq(true)
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
@@ -20,7 +20,7 @@ describe "Boolean" do
|
|
20
20
|
it "should be saved correctly" do
|
21
21
|
@values.first.merge!(:active => false)
|
22
22
|
User.mass_insert(@values, @options)
|
23
|
-
User.last.active.
|
23
|
+
expect(User.last.active).to eq(false)
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
@@ -29,7 +29,7 @@ describe "Boolean" do
|
|
29
29
|
it "should convert string value to boolean" do
|
30
30
|
@values.first.merge!(:active => "string")
|
31
31
|
User.mass_insert(@values, @options)
|
32
|
-
User.last.active.
|
32
|
+
expect(User.last.active).to eq(true)
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
@@ -37,7 +37,7 @@ describe "Boolean" do
|
|
37
37
|
it "should convert decimal value to boolean" do
|
38
38
|
@values.first.merge!(:active => 25.34)
|
39
39
|
User.mass_insert(@values, @options)
|
40
|
-
User.last.active.
|
40
|
+
expect(User.last.active).to eq(true)
|
41
41
|
end
|
42
42
|
end
|
43
43
|
end
|
@@ -46,7 +46,7 @@ describe "Boolean" do
|
|
46
46
|
it "should save the default value" do
|
47
47
|
@values.first.delete(:active)
|
48
48
|
User.mass_insert(@values, @options)
|
49
|
-
User.last.active.
|
49
|
+
expect(User.last.active).to eq(nil)
|
50
50
|
end
|
51
51
|
end
|
52
52
|
end
|
@@ -12,7 +12,7 @@ describe "Decimal" do
|
|
12
12
|
it "should convert integer value to decimal" do
|
13
13
|
@values.first.merge!(:money => 10)
|
14
14
|
User.mass_insert(@values, @options)
|
15
|
-
User.last.money.
|
15
|
+
expect(User.last.money).to eq(10.0)
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
@@ -20,21 +20,21 @@ describe "Decimal" do
|
|
20
20
|
it "should convert string value to decimal" do
|
21
21
|
@values.first.merge!(:money => "string")
|
22
22
|
User.mass_insert(@values, @options)
|
23
|
-
User.last.money.
|
23
|
+
expect(User.last.money).to eq(0.0)
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
27
|
context "when contains a decimal" do
|
28
28
|
it "should save the correct value" do
|
29
29
|
User.mass_insert(@values, @options)
|
30
|
-
User.last.money.
|
30
|
+
expect(User.last.money).to eq(20.50)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
34
|
context "when contains a boolean" do
|
35
35
|
it "should raise an exception" do
|
36
36
|
@values.first.merge!(:money => true)
|
37
|
-
lambda{ User.mass_insert(@values, @options) }.
|
37
|
+
expect(lambda{ User.mass_insert(@values, @options) }).to raise_exception
|
38
38
|
end
|
39
39
|
end
|
40
40
|
end
|
@@ -43,7 +43,7 @@ describe "Decimal" do
|
|
43
43
|
it "should save the default value" do
|
44
44
|
@values.first.delete(:money)
|
45
45
|
User.mass_insert(@values, @options)
|
46
|
-
User.last.money.
|
46
|
+
expect(User.last.money).to eq(nil)
|
47
47
|
end
|
48
48
|
end
|
49
49
|
end
|
@@ -11,7 +11,7 @@ describe "Integer" do
|
|
11
11
|
context "when contains an integer" do
|
12
12
|
it "should be saved correctly" do
|
13
13
|
User.mass_insert(@values, @options)
|
14
|
-
User.last.age.
|
14
|
+
expect(User.last.age).to eq(20)
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
@@ -19,7 +19,7 @@ describe "Integer" do
|
|
19
19
|
it "should convert string value to integer" do
|
20
20
|
@values.first.merge!(:age => "string")
|
21
21
|
User.mass_insert(@values, @options)
|
22
|
-
User.last.age.
|
22
|
+
expect(User.last.age).to eq(0)
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
@@ -27,14 +27,14 @@ describe "Integer" do
|
|
27
27
|
it "should convert decimal value to integer" do
|
28
28
|
@values.first.merge!(:age => 25.69)
|
29
29
|
User.mass_insert(@values, @options)
|
30
|
-
User.last.age.
|
30
|
+
expect(User.last.age).to eq(25)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
34
|
context "when contains a boolean" do
|
35
35
|
it "should raise an exception" do
|
36
36
|
@values.first.merge!(:age => true)
|
37
|
-
lambda{ User.mass_insert(@values, @options) }.
|
37
|
+
expect(lambda{ User.mass_insert(@values, @options) }).to raise_exception
|
38
38
|
end
|
39
39
|
end
|
40
40
|
end
|
@@ -43,7 +43,7 @@ describe "Integer" do
|
|
43
43
|
it "should save the default value" do
|
44
44
|
@values.first.delete(:age)
|
45
45
|
User.mass_insert(@values, @options)
|
46
|
-
User.last.age.
|
46
|
+
expect(User.last.age).to eq(nil)
|
47
47
|
end
|
48
48
|
end
|
49
49
|
end
|
@@ -11,7 +11,7 @@ describe "String" do
|
|
11
11
|
context "when contains a string" do
|
12
12
|
it "should be saved correctly" do
|
13
13
|
User.mass_insert(@values, @options)
|
14
|
-
User.last.name.
|
14
|
+
expect(User.last.name).to eq("some name")
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
@@ -19,7 +19,7 @@ describe "String" do
|
|
19
19
|
it "should convert integer value to string" do
|
20
20
|
@values.first.merge!(:name => 10)
|
21
21
|
User.mass_insert(@values, @options)
|
22
|
-
User.last.name.
|
22
|
+
expect(User.last.name).to eq("10")
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
@@ -27,7 +27,7 @@ describe "String" do
|
|
27
27
|
it "should convert decimal value to string" do
|
28
28
|
@values.first.merge!(:name => 25.69)
|
29
29
|
User.mass_insert(@values, @options)
|
30
|
-
User.last.name.
|
30
|
+
expect(User.last.name).to eq("25.69")
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
@@ -35,7 +35,7 @@ describe "String" do
|
|
35
35
|
it "should convert boolean value to string" do
|
36
36
|
@values.first.merge!(:name => true)
|
37
37
|
User.mass_insert(@values, @options)
|
38
|
-
User.last.name.
|
38
|
+
expect(User.last.name).to eq("true")
|
39
39
|
end
|
40
40
|
end
|
41
41
|
end
|
@@ -44,7 +44,7 @@ describe "String" do
|
|
44
44
|
it "should save the default value" do
|
45
45
|
@values.first.delete(:name)
|
46
46
|
User.mass_insert(@values, @options)
|
47
|
-
User.last.name.
|
47
|
+
expect(User.last.name).to eq(nil)
|
48
48
|
end
|
49
49
|
end
|
50
50
|
end
|
@@ -17,16 +17,69 @@ describe "Model" do
|
|
17
17
|
end
|
18
18
|
|
19
19
|
context "when is used without options" do
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
context "when value hashes have string keys" do
|
21
|
+
before :each do
|
22
|
+
value_hash = {
|
23
|
+
"name" => "some_name",
|
24
|
+
"email" => "some_email",
|
25
|
+
"age" => 20,
|
26
|
+
"active" => true,
|
27
|
+
"checked" => true
|
28
|
+
}
|
29
|
+
5.times{ @values << value_hash }
|
30
|
+
User.mass_insert(@values, @options)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should save correct values" do
|
34
|
+
expect(User.first.name).to eq("some_name")
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should have saved 5 records" do
|
38
|
+
expect(User.count).to eq(5)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "when value hashes have symbol keys" do
|
43
|
+
before :each do
|
44
|
+
5.times{ @values << @value_hash }
|
45
|
+
User.mass_insert(@values, @options)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should save correct values" do
|
49
|
+
expect(User.first.age).to eq(20)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should have saved 5 records" do
|
53
|
+
expect(User.count).to eq(5)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "when value hashes have symbol and string keys" do
|
58
|
+
before :each do
|
59
|
+
value_hash = {
|
60
|
+
"name" => "some_name",
|
61
|
+
:email => "some_email",
|
62
|
+
"age" => 20,
|
63
|
+
:active => true,
|
64
|
+
"checked" => true
|
65
|
+
}
|
66
|
+
5.times{ @values << value_hash }
|
67
|
+
User.mass_insert(@values, @options)
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should save correct values" do
|
71
|
+
expect(User.first.email).to eq("some_email")
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should have saved 5 records" do
|
75
|
+
expect(User.count).to eq(5)
|
76
|
+
end
|
24
77
|
end
|
25
78
|
|
26
79
|
it "should save if values cointains 1200 records" do
|
27
80
|
1200.times{ @values << @value_hash }
|
28
81
|
User.mass_insert(@values, @options)
|
29
|
-
User.count.
|
82
|
+
expect(User.count).to eq(1200)
|
30
83
|
end
|
31
84
|
end
|
32
85
|
|
@@ -35,7 +88,7 @@ describe "Model" do
|
|
35
88
|
it "should not save any record" do
|
36
89
|
5.times{ @values << @value_hash }
|
37
90
|
@options.merge!(:table_name => "countries")
|
38
|
-
lambda{ User.mass_insert(@values, @options) }.
|
91
|
+
expect(lambda{ User.mass_insert(@values, @options) }).to raise_exception
|
39
92
|
end
|
40
93
|
end
|
41
94
|
|
@@ -43,7 +96,7 @@ describe "Model" do
|
|
43
96
|
it "should not save any record" do
|
44
97
|
5.times{ @values << @value_hash }
|
45
98
|
@options.merge!(:class_name => Test)
|
46
|
-
lambda{ User.mass_insert(@values, @options) }.
|
99
|
+
expect(lambda{ User.mass_insert(@values, @options) }).to raise_exception
|
47
100
|
end
|
48
101
|
end
|
49
102
|
end
|
@@ -97,9 +97,22 @@ describe MassInsert::Adapters::Helpers::AbstractQuery do
|
|
97
97
|
end
|
98
98
|
|
99
99
|
it "should call build method in ColumnValue class" do
|
100
|
-
column_value = MassInsert::Adapters::ColumnValue.any_instance
|
100
|
+
column_value = MassInsert::Adapters::Helpers::ColumnValue.any_instance
|
101
101
|
column_value.stub(:build).and_return("value")
|
102
102
|
expect(subject.string_single_value({}, :name)).to eq("value")
|
103
103
|
end
|
104
104
|
end
|
105
|
+
|
106
|
+
describe "#execute" do
|
107
|
+
it "should respond to execute method" do
|
108
|
+
expect(subject).to respond_to(:execute)
|
109
|
+
end
|
110
|
+
|
111
|
+
it "call methods and returns their values concatenated" do
|
112
|
+
subject.stub(:begin_string).and_return("a")
|
113
|
+
subject.stub(:string_columns).and_return("b")
|
114
|
+
subject.stub(:string_values).and_return("c")
|
115
|
+
expect(subject.execute).to eq("abc")
|
116
|
+
end
|
117
|
+
end
|
105
118
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
require './spec/spec_helper'
|
2
2
|
require "./lib/mass_insert"
|
3
3
|
|
4
|
-
describe MassInsert::Adapters::ColumnValue do
|
4
|
+
describe MassInsert::Adapters::Helpers::ColumnValue do
|
5
5
|
let(:options) {{ :class_name => User }}
|
6
6
|
let(:row) {{ :name => "name", :age => 10 }}
|
7
7
|
let(:column){ :name }
|
8
|
-
let!(:subject){ MassInsert::Adapters::ColumnValue.new(row, column, options) }
|
8
|
+
let!(:subject){ MassInsert::Adapters::Helpers::ColumnValue.new(row, column, options) }
|
9
9
|
|
10
10
|
describe "#initialize" do
|
11
11
|
it "should assign options param to option attribute" do
|
@@ -6,10 +6,14 @@ describe MassInsert::Adapters::Helpers do
|
|
6
6
|
expect(MassInsert::Adapters::Helpers).to be
|
7
7
|
end
|
8
8
|
|
9
|
-
it 'should define AbstractQuery
|
9
|
+
it 'should define AbstractQuery module' do
|
10
10
|
expect(MassInsert::Adapters::Helpers::AbstractQuery).to be
|
11
11
|
end
|
12
12
|
|
13
|
+
it 'should define ColumnValue class' do
|
14
|
+
expect(MassInsert::Adapters::Helpers::ColumnValue).to be
|
15
|
+
end
|
16
|
+
|
13
17
|
it 'should define Timestamp module' do
|
14
18
|
expect(MassInsert::Adapters::Helpers::Timestamp).to be
|
15
19
|
end
|
@@ -18,18 +18,5 @@ describe MassInsert::Adapters::Mysql2Adapter do
|
|
18
18
|
expect(subject.timestamp_format).to eq("%Y-%m-%d %H:%M:%S")
|
19
19
|
end
|
20
20
|
end
|
21
|
-
|
22
|
-
describe "#execute" do
|
23
|
-
it "should respond to execute method" do
|
24
|
-
expect(subject).to respond_to(:execute)
|
25
|
-
end
|
26
|
-
|
27
|
-
it "call methods and returns their values concatenated" do
|
28
|
-
subject.stub(:begin_string).and_return("a")
|
29
|
-
subject.stub(:string_columns).and_return("b")
|
30
|
-
subject.stub(:string_values).and_return("c")
|
31
|
-
expect(subject.execute).to eq("abc")
|
32
|
-
end
|
33
|
-
end
|
34
21
|
end
|
35
22
|
end
|
@@ -8,18 +8,4 @@ describe MassInsert::Adapters::PostgreSQLAdapter do
|
|
8
8
|
expect(subject).to be_a(MassInsert::Adapters::Adapter)
|
9
9
|
end
|
10
10
|
|
11
|
-
describe "instance methods" do
|
12
|
-
describe "#execute" do
|
13
|
-
it "should respond to execute method" do
|
14
|
-
expect(subject).to respond_to(:execute)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "call methods and returns their values concatenated" do
|
18
|
-
subject.stub(:begin_string).and_return("a")
|
19
|
-
subject.stub(:string_columns).and_return("b")
|
20
|
-
subject.stub(:string_values).and_return("c")
|
21
|
-
expect(subject.execute).to eq("abc")
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
11
|
end
|
@@ -9,6 +9,16 @@ describe MassInsert::Adapters::SQLServerAdapter do
|
|
9
9
|
end
|
10
10
|
|
11
11
|
describe "instance methods" do
|
12
|
+
describe "#timestamp_format" do
|
13
|
+
it "should respond to timestamp_format method" do
|
14
|
+
expect(subject).to respond_to(:timestamp_format)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return the format string" do
|
18
|
+
expect(subject.timestamp_format).to eq("%Y-%m-%d %H:%M:%S.%3N")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
12
22
|
describe "#execute" do
|
13
23
|
before :each do
|
14
24
|
subject.stub(:begin_string).and_return("a")
|
@@ -10,10 +10,6 @@ describe MassInsert::Adapters do
|
|
10
10
|
expect(MassInsert::Adapters::Adapter).to be
|
11
11
|
end
|
12
12
|
|
13
|
-
it 'should define ColumnValue class' do
|
14
|
-
expect(MassInsert::Adapters::ColumnValue).to be
|
15
|
-
end
|
16
|
-
|
17
13
|
it 'should define ColumnValue class' do
|
18
14
|
expect(MassInsert::Adapters::Helpers).to be
|
19
15
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mass_insert
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-06-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -61,9 +61,9 @@ files:
|
|
61
61
|
- lib/mass_insert.rb
|
62
62
|
- lib/mass_insert/adapters.rb
|
63
63
|
- lib/mass_insert/adapters/adapter.rb
|
64
|
-
- lib/mass_insert/adapters/column_value.rb
|
65
64
|
- lib/mass_insert/adapters/helpers.rb
|
66
65
|
- lib/mass_insert/adapters/helpers/abstract_query.rb
|
66
|
+
- lib/mass_insert/adapters/helpers/column_value.rb
|
67
67
|
- lib/mass_insert/adapters/helpers/sanitizer.rb
|
68
68
|
- lib/mass_insert/adapters/helpers/timestamp.rb
|
69
69
|
- lib/mass_insert/adapters/mysql2_adapter.rb
|
@@ -132,8 +132,8 @@ files:
|
|
132
132
|
- spec/active_record_models/model_spec.rb
|
133
133
|
- spec/dummy_models/test.rb
|
134
134
|
- spec/mass_insert/adapters/adapter_spec.rb
|
135
|
-
- spec/mass_insert/adapters/column_value_spec.rb
|
136
135
|
- spec/mass_insert/adapters/helpers/abstract_query_spec.rb
|
136
|
+
- spec/mass_insert/adapters/helpers/column_value_spec.rb
|
137
137
|
- spec/mass_insert/adapters/helpers/sanitizer_spec.rb
|
138
138
|
- spec/mass_insert/adapters/helpers/timestamp_spec.rb
|
139
139
|
- spec/mass_insert/adapters/helpers_spec.rb
|
@@ -162,7 +162,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
162
162
|
version: '0'
|
163
163
|
segments:
|
164
164
|
- 0
|
165
|
-
hash:
|
165
|
+
hash: -4593774517207519689
|
166
166
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
167
167
|
none: false
|
168
168
|
requirements:
|
@@ -171,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
171
171
|
version: '0'
|
172
172
|
segments:
|
173
173
|
- 0
|
174
|
-
hash:
|
174
|
+
hash: -4593774517207519689
|
175
175
|
requirements: []
|
176
176
|
rubyforge_project:
|
177
177
|
rubygems_version: 1.8.25
|
@@ -236,8 +236,8 @@ test_files:
|
|
236
236
|
- spec/active_record_models/model_spec.rb
|
237
237
|
- spec/dummy_models/test.rb
|
238
238
|
- spec/mass_insert/adapters/adapter_spec.rb
|
239
|
-
- spec/mass_insert/adapters/column_value_spec.rb
|
240
239
|
- spec/mass_insert/adapters/helpers/abstract_query_spec.rb
|
240
|
+
- spec/mass_insert/adapters/helpers/column_value_spec.rb
|
241
241
|
- spec/mass_insert/adapters/helpers/sanitizer_spec.rb
|
242
242
|
- spec/mass_insert/adapters/helpers/timestamp_spec.rb
|
243
243
|
- spec/mass_insert/adapters/helpers_spec.rb
|
@@ -1,111 +0,0 @@
|
|
1
|
-
module MassInsert
|
2
|
-
module Adapters
|
3
|
-
class ColumnValue
|
4
|
-
|
5
|
-
attr_accessor :row, :column, :options
|
6
|
-
|
7
|
-
def initialize row, column, options
|
8
|
-
@row = row
|
9
|
-
@column = column
|
10
|
-
@options = options
|
11
|
-
end
|
12
|
-
|
13
|
-
# Returns the class that invokes the mass insert process. The class
|
14
|
-
# is in the options hash.
|
15
|
-
def class_name
|
16
|
-
options[:class_name]
|
17
|
-
end
|
18
|
-
|
19
|
-
# Returns a symbol with the column type in the database. The column or
|
20
|
-
# attribute should belongs to the class that invokes the mass insert.
|
21
|
-
def column_type
|
22
|
-
class_name.columns_hash[@column.to_s].type
|
23
|
-
end
|
24
|
-
|
25
|
-
# Returns the value to this column in the row hash. The value is
|
26
|
-
# finding by symbol or string key to be most flexible.
|
27
|
-
def column_value
|
28
|
-
row[column.to_sym]
|
29
|
-
end
|
30
|
-
|
31
|
-
# Returns the string with the database adapter name usually in the
|
32
|
-
# database.yml file in your Rails project.
|
33
|
-
def adapter
|
34
|
-
ActiveRecord::Base.connection.instance_values["config"][:adapter]
|
35
|
-
end
|
36
|
-
|
37
|
-
# Returns the default value string to be included in query string.
|
38
|
-
# This default value is added to the query if the row hash does not
|
39
|
-
# contains the database column value.
|
40
|
-
def default_value
|
41
|
-
default_db_value ? default_db_value.to_s : "null"
|
42
|
-
end
|
43
|
-
|
44
|
-
# Return the database default value using methods that ActiveRecord
|
45
|
-
# provides to see database columns settings.
|
46
|
-
def default_db_value
|
47
|
-
class_name.columns_hash[@column.to_s].default
|
48
|
-
end
|
49
|
-
|
50
|
-
# Returns a single column string value with the correct format and
|
51
|
-
# according to the database configuration, column type and presence.
|
52
|
-
# If the row hash does not include the value to this column return the
|
53
|
-
# default value according to database configuration.
|
54
|
-
def build
|
55
|
-
column_value.nil? ? default_value : send(:"column_value_#{column_type}")
|
56
|
-
end
|
57
|
-
|
58
|
-
# Returns the correct value when the column value is string, text,
|
59
|
-
# date, time, datetime, timestamp. There are alias methods to the
|
60
|
-
# other column types that need a similar query value.
|
61
|
-
def column_value_string
|
62
|
-
"'#{column_value}'"
|
63
|
-
end
|
64
|
-
alias :column_value_text :column_value_string
|
65
|
-
alias :column_value_date :column_value_string
|
66
|
-
alias :column_value_time :column_value_string
|
67
|
-
alias :column_value_datetime :column_value_string
|
68
|
-
alias :column_value_timestamp :column_value_string
|
69
|
-
alias :column_value_binary :column_value_string
|
70
|
-
|
71
|
-
# Returns the correct value to integer column. The column values is
|
72
|
-
# converted to integer to be sure that the value is correct to be
|
73
|
-
# persisted into the database and after it, it's converted to string.
|
74
|
-
def column_value_integer
|
75
|
-
column_value.to_i.to_s
|
76
|
-
end
|
77
|
-
|
78
|
-
# Returns the correct value to decimal column. There is an alias method
|
79
|
-
# to float type. The column values is converted to decimal to be sure
|
80
|
-
# that the value is correct to be persisted into the database and after
|
81
|
-
# it, it's converted to string.
|
82
|
-
def column_value_decimal
|
83
|
-
column_value.to_f.to_s
|
84
|
-
end
|
85
|
-
alias :column_value_float :column_value_decimal
|
86
|
-
|
87
|
-
# Returns the correct value to boolean column. This column calls the
|
88
|
-
# correct method according to the database adapter to return the correct
|
89
|
-
# value to that database engine.
|
90
|
-
def column_value_boolean
|
91
|
-
self.send(:"#{adapter}_column_value_boolean")
|
92
|
-
end
|
93
|
-
|
94
|
-
# Returns the column value to boolean column like a string. If the
|
95
|
-
# column value exists returns a true string else false string. There
|
96
|
-
# are alias methods to the database engines that works similarity.
|
97
|
-
def mysql2_column_value_boolean
|
98
|
-
column_value ? "true" : "false"
|
99
|
-
end
|
100
|
-
alias :postgresql_column_value_boolean :mysql2_column_value_boolean
|
101
|
-
alias :sqlserver_column_value_boolean :mysql2_column_value_boolean
|
102
|
-
|
103
|
-
# Returns the column value to boolean column like a string. If the
|
104
|
-
# column value exists returns a "1" else "0".
|
105
|
-
def sqlite3_column_value_boolean
|
106
|
-
column_value ? "1" : "0"
|
107
|
-
end
|
108
|
-
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|