multi_insert 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cef8e229718dc7e2ab4056de9c5acf7727753cf5
4
+ data.tar.gz: b21f08bf1fed8b841ea928efda408d5a39ee44e3
5
+ SHA512:
6
+ metadata.gz: 4718827cdc18dfc58bcae9d4f6e3ed670867ed2e30dbdf056c7c6be1fcfdcbb45baa51d6898b7c822a51eb1bb445c2b1cb469c028af463a2de66c370fc2ef721
7
+ data.tar.gz: 7728c0c7f4cff1061c49ce95c433a5743cbf9ec5520e715551bb3d97945e00e3a3ec822e7a0b9a9eeefd8c8362996c8fd724faa37829b45488172bd6c8519ca6
@@ -0,0 +1,3 @@
1
+ require 'multi_insert/version'
2
+ require 'multi_insert/query'
3
+ require 'multi_insert/hooks/active_record'
@@ -0,0 +1,8 @@
1
+ require 'active_record'
2
+ require 'multi_insert/query'
3
+
4
+ class ActiveRecord::Base
5
+ def self.multi_insert(columns, values)
6
+ ::MultiInsert::Query.new(table_name, columns, values)
7
+ end
8
+ end
@@ -0,0 +1,44 @@
1
+ require 'active_record'
2
+ require 'multi_insert/query_builder'
3
+
4
+ module MultiInsert
5
+ class Query
6
+ def initialize(table, columns, values)
7
+ @table = table.to_sym
8
+ @sql_insert = ::MultiInsert::QueryBuilder.insert(table, columns, values)
9
+ end
10
+
11
+ def returning(columns)
12
+ @sql_returning = ::MultiInsert::QueryBuilder.returning(columns)
13
+ @returning_flat = false
14
+ end
15
+
16
+ def returning_id
17
+ @sql_returning = ::MultiInsert::QueryBuilder.returning([:id])
18
+ @returning_flat = true
19
+ end
20
+
21
+ def to_sql
22
+ sql = @sql_insert
23
+ sql = "#{sql} #{@sql_returning}" unless @sql_returning.nil?
24
+ sql
25
+ end
26
+
27
+ def to_s
28
+ to_sql
29
+ end
30
+
31
+ def execute
32
+ result = ActiveRecord::Base.connection.execute(to_sql)
33
+ if @sql_returning.nil?
34
+ nil
35
+ else
36
+ if @returning_flat
37
+ result.rows.map{|r| r.first}
38
+ else
39
+ result
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,31 @@
1
+ require 'active_record'
2
+
3
+ module MultiInsert
4
+ module QueryBuilder
5
+ def self.insert(table, columns, values)
6
+ ar = ActiveRecord::Base.connection
7
+
8
+ now = Time.now.to_s(:db)
9
+ table = ar.quote_table_name(table.to_s)
10
+
11
+ # Format columns
12
+ columns = columns + [:created_at, :updated_at]
13
+ columns = columns.map!{|c| ar.quote_column_name(c.to_s)}
14
+ columns = join_params(columns)
15
+
16
+ # Format values
17
+ values = values.map{|v| join_params((v + [now, now]).map{|vv| ar.quote(vv.to_s)})}.join(',')
18
+
19
+ "INSERT INTO #{table} #{columns} VALUES #{values}"
20
+ end
21
+ end
22
+
23
+ def self.returning(columns)
24
+ columns = columns.map{|c| ActiveRecord::Base.connection.quote_column_name(c.to_s)}.join(',')
25
+ "RETURNING #{columns}"
26
+ end
27
+
28
+ def self.join_params(params)
29
+ "(" + params.join(',') + ")"
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module MultiInsert
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multi_insert
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nax
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '4.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
69
+ description:
70
+ email:
71
+ - max@bacoux.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - lib/multi_insert.rb
77
+ - lib/multi_insert/hooks/active_record.rb
78
+ - lib/multi_insert/query.rb
79
+ - lib/multi_insert/query_builder.rb
80
+ - lib/multi_insert/version.rb
81
+ homepage: https://github.com/SparkUpFR/gem_multi_insert
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: 2.0.0
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.6.10
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Simple additions to write efficient insert queries.
105
+ test_files: []