insert_select 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +0 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +56 -0
- data/LICENSE.txt +21 -0
- data/README.md +100 -0
- data/Rakefile +8 -0
- data/lib/insert_select/active_record.rb +94 -0
- data/lib/insert_select/adapters/base_adapter.rb +29 -0
- data/lib/insert_select/adapters/mysql_adapter.rb +14 -0
- data/lib/insert_select/adapters/postgresql_adapter.rb +17 -0
- data/lib/insert_select/adapters/sqlite_adapter.rb +14 -0
- data/lib/insert_select/builder.rb +105 -0
- data/lib/insert_select/errors.rb +15 -0
- data/lib/insert_select/version.rb +5 -0
- data/lib/insert_select.rb +11 -0
- data/sig/insert_select.rbs +4 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3ed398397217cbd7d7a636948b89a3cfb32bbfa6c9288619e249e014f895febc
|
4
|
+
data.tar.gz: 015b72e911c1183215858260e3aa96dc6ec47000f2221ec7c7147929deb94167
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9b5ec0d676bb246917b348e656bcb20b89ea4fdb509ae834dc1f32704f2c8faa97a8ce394b6629deb26001f06cdc59550c01c78f49febbb94e936cc1ec88d9a1
|
7
|
+
data.tar.gz: 5179a2af5b2b62670f42f1b1fec76f9c38cfc2c9d025e82de60b4d5f6e3a16df27096992896533abf25759b8423dc68afce0939e09b1486392022e74168b50fe
|
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.0.0
|
data/CHANGELOG.md
ADDED
File without changes
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at sh07e1916@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
insert_select (0.1.0)
|
5
|
+
activerecord
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (7.0.5)
|
11
|
+
activesupport (= 7.0.5)
|
12
|
+
activerecord (7.0.5)
|
13
|
+
activemodel (= 7.0.5)
|
14
|
+
activesupport (= 7.0.5)
|
15
|
+
activesupport (7.0.5)
|
16
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
|
+
i18n (>= 1.6, < 2)
|
18
|
+
minitest (>= 5.1)
|
19
|
+
tzinfo (~> 2.0)
|
20
|
+
concurrent-ruby (1.2.2)
|
21
|
+
diff-lcs (1.5.0)
|
22
|
+
i18n (1.14.0)
|
23
|
+
concurrent-ruby (~> 1.0)
|
24
|
+
minitest (5.18.0)
|
25
|
+
rake (13.0.6)
|
26
|
+
rspec (3.12.0)
|
27
|
+
rspec-core (~> 3.12.0)
|
28
|
+
rspec-expectations (~> 3.12.0)
|
29
|
+
rspec-mocks (~> 3.12.0)
|
30
|
+
rspec-core (3.12.2)
|
31
|
+
rspec-support (~> 3.12.0)
|
32
|
+
rspec-expectations (3.12.3)
|
33
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
34
|
+
rspec-support (~> 3.12.0)
|
35
|
+
rspec-mocks (3.12.5)
|
36
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
37
|
+
rspec-support (~> 3.12.0)
|
38
|
+
rspec-support (3.12.0)
|
39
|
+
sqlite3 (1.6.3-x86_64-darwin)
|
40
|
+
sqlite3 (1.6.3-x86_64-linux)
|
41
|
+
tzinfo (2.0.6)
|
42
|
+
concurrent-ruby (~> 1.0)
|
43
|
+
|
44
|
+
PLATFORMS
|
45
|
+
x86_64-darwin-19
|
46
|
+
x86_64-darwin-22
|
47
|
+
x86_64-linux
|
48
|
+
|
49
|
+
DEPENDENCIES
|
50
|
+
insert_select!
|
51
|
+
rake (~> 13.0)
|
52
|
+
rspec (~> 3.0)
|
53
|
+
sqlite3
|
54
|
+
|
55
|
+
BUNDLED WITH
|
56
|
+
2.4.12
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 a5-stable
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
# InsertSelect
|
2
|
+
[![Ruby](https://github.com/a5-stable/insert_select/actions/workflows/ruby.yml/badge.svg)](https://github.com/a5-stable/insert_select/actions/workflows/ruby.yml)
|
3
|
+
![Code Climate](https://codeclimate.com/github/a5-stable/insert_select.png)
|
4
|
+
|
5
|
+
This is a custom gem that extends ActiveRecord to enable the expression of SQL `INSERT INTO ... SELECT ...` queries in a more convenient way. It allows you to copy data from one table to another based on specified conditions using a simple and expressive syntax.
|
6
|
+
|
7
|
+
SQL example of `INSERT INTO ... SELECT ...`:
|
8
|
+
```
|
9
|
+
INSERT INTO films SELECT * FROM tmp_films WHERE date_prod < '2004-05-07';
|
10
|
+
```
|
11
|
+
|
12
|
+
documentation:
|
13
|
+
- MySQL: https://dev.mysql.com/doc/refman/8.0/en/insert-select.html
|
14
|
+
- PostgreSQL: https://www.postgresql.org/docs/current/sql-insert.html
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Add this line to your Gemfile:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
gem 'insert_select'
|
22
|
+
```
|
23
|
+
|
24
|
+
And then execute:
|
25
|
+
```ruby
|
26
|
+
bundle install
|
27
|
+
```
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
```ruby
|
31
|
+
gem install insert_select
|
32
|
+
```
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
### Copy all data from OldUser to NewUser
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
NewUser.insert_select_from(OldUser)
|
40
|
+
|
41
|
+
#=> INSERT INTO "new_users" SELECT "old_users".* FROM "old_users"
|
42
|
+
```
|
43
|
+
|
44
|
+
### Filter the columns to be copied
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
NewUser.insert_select_from(OldUser.select(:name))
|
48
|
+
|
49
|
+
#=> INSERT INTO "new_users" ("name") SELECT "old_users"."name" FROM "old_users"
|
50
|
+
```
|
51
|
+
|
52
|
+
### Copy data between different column names
|
53
|
+
|
54
|
+
You can specify column name mappings between tables.
|
55
|
+
```ruby
|
56
|
+
AnotherUser.insert_select_from(OldUser, mapping: { old_name: :another_name })
|
57
|
+
|
58
|
+
#=> INSERT INTO "another_users" ("another_name") SELECT "old_users"."name" FROM "old_users"
|
59
|
+
```
|
60
|
+
|
61
|
+
### Set a constant value
|
62
|
+
```ruby
|
63
|
+
AnotherUser.create_with(another_constant_column: "20").insert_select_from(OldUser, mapping: { old_name: :another_name })
|
64
|
+
|
65
|
+
#=> INSERT INTO "another_users" ("another_name", "another_constant_column") SELECT "old_users"."name", "20" FROM "old_users"
|
66
|
+
```
|
67
|
+
|
68
|
+
### Use a WHERE clause to filter the data
|
69
|
+
```ruby
|
70
|
+
NewUser.insert_select_from(OldUser.where("age > ?", 20))
|
71
|
+
|
72
|
+
#=> INSERT INTO "new_users" SELECT "old_users".*, FROM "old_users" WHERE ("age" > 20)
|
73
|
+
```
|
74
|
+
|
75
|
+
### Use the RETURNING clause (only for PostgreSQL connection)
|
76
|
+
```ruby
|
77
|
+
NewUser.insert_select_from(OldUser, returning: [:id])
|
78
|
+
|
79
|
+
#=> INSERT INTO "new_users" SELECT "old_users".* FROM "old_users" RETURNING "id"
|
80
|
+
```
|
81
|
+
|
82
|
+
Other options, which are enabled in [`insert_all`](https://www.rubydoc.info/github/rails/rails/ActiveRecord%2FPersistence%2FClassMethods:insert_all) should be also supported, but are not yet implemented.
|
83
|
+
|
84
|
+
## Development
|
85
|
+
|
86
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
87
|
+
|
88
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
89
|
+
|
90
|
+
## Contributing
|
91
|
+
|
92
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/a5-stable/insert_select. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/a5-stable/insert_select/blob/main/CODE_OF_CONDUCT.md).
|
93
|
+
|
94
|
+
## License
|
95
|
+
|
96
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
97
|
+
|
98
|
+
## Code of Conduct
|
99
|
+
|
100
|
+
Everyone interacting in the InsertSelect project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/a5-stable/insert_select/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require_relative "builder"
|
3
|
+
|
4
|
+
module InsertSelect
|
5
|
+
module ActiveRecord
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
require_relative "adapters/base_adapter"
|
8
|
+
require_relative "adapters/postgresql_adapter"
|
9
|
+
require_relative "adapters/mysql_adapter"
|
10
|
+
require_relative "adapters/sqlite_adapter"
|
11
|
+
|
12
|
+
included do
|
13
|
+
class << self
|
14
|
+
#
|
15
|
+
# Copy data from the specified data source to the table of the model easily.
|
16
|
+
#
|
17
|
+
# @example Filter the columns to be copied
|
18
|
+
#
|
19
|
+
# NewUser.insert_select_from(OldUser.select(:name))
|
20
|
+
# #=> INSERT INTO "new_users" ("name") SELECT "old_users"."name" FROM "old_users"
|
21
|
+
#
|
22
|
+
# To see more examples, please refer to the [README](https://github.com/a5-stable/insert_select#readme)
|
23
|
+
#
|
24
|
+
# @param [ActiveRecord::Relation] relation
|
25
|
+
# The data source to be copied.
|
26
|
+
#
|
27
|
+
# @param [Hash] mapping
|
28
|
+
# The column mapping hash. Specify the mapping when the column name is different between the source table and the destination table.
|
29
|
+
# Usage: { source_column_name: :destination_column_name }
|
30
|
+
#
|
31
|
+
# @param returning
|
32
|
+
# The returning clause option (only for PostgreSQL connection).
|
33
|
+
#
|
34
|
+
# @return [ActiveRecord::Result] The result of the insert select operation.
|
35
|
+
#
|
36
|
+
def insert_select_from(relation, mapping: {}, returning: nil)
|
37
|
+
InsertSelect::ActiveRecord::InsertSelectFrom.new(self, relation, mapping: mapping, returning: returning).execute
|
38
|
+
end
|
39
|
+
|
40
|
+
def except(*columns)
|
41
|
+
select( column_names - columns.map(&:to_s) )
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class InsertSelectFrom
|
47
|
+
attr_reader :model, :connection, :relation, :adapter, :mapping, :returning
|
48
|
+
|
49
|
+
def initialize(model, relation, mapping:, returning: nil)
|
50
|
+
@model = model
|
51
|
+
@connection = model.connection
|
52
|
+
@relation = relation
|
53
|
+
@adapter = find_adapter(connection)
|
54
|
+
@mapping = mapping
|
55
|
+
@returning = returning
|
56
|
+
end
|
57
|
+
|
58
|
+
def execute
|
59
|
+
sql = model.sanitize_sql_array([to_sql, *builder.constant_values])
|
60
|
+
connection.exec_insert_all(sql, "")
|
61
|
+
end
|
62
|
+
|
63
|
+
def to_sql
|
64
|
+
@to_sql ||= adapter.build_sql(builder)
|
65
|
+
end
|
66
|
+
|
67
|
+
def builder
|
68
|
+
@builder ||= Builder.new(self)
|
69
|
+
end
|
70
|
+
|
71
|
+
def ensure_valid_options_for_connection!
|
72
|
+
if returning && !connection.supports_insert_returning?
|
73
|
+
raise ArgumentError, "#{connection.class} does not support :returning"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def find_adapter(connection)
|
80
|
+
table_name = model.table_name
|
81
|
+
case connection.adapter_name.to_s.downcase
|
82
|
+
when /mysql/
|
83
|
+
InsertSelect::Adapters::MysqlAdapter.new(table_name, connection)
|
84
|
+
when /postgresql/
|
85
|
+
InsertSelect::Adapters::PostgresqlAdapter.new(table_name, connection)
|
86
|
+
when /sqlite/
|
87
|
+
InsertSelect::Adapters::SqliteAdapter.new(table_name, connection)
|
88
|
+
else
|
89
|
+
raise "Unsupported adapter: #{connection.adapter_name}"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module InsertSelect
|
2
|
+
module Adapters
|
3
|
+
require "insert_select/errors"
|
4
|
+
|
5
|
+
class BaseAdapter
|
6
|
+
attr_reader :table_name, :connection
|
7
|
+
def initialize(table_name, connection)
|
8
|
+
@table_name = table_name
|
9
|
+
@connection = connection
|
10
|
+
end
|
11
|
+
|
12
|
+
def build_sql(builder)
|
13
|
+
into = builder.into
|
14
|
+
|
15
|
+
if into.present?
|
16
|
+
builder.reselect_relation!
|
17
|
+
|
18
|
+
stmt = "INSERT #{into}"
|
19
|
+
stmt += " #{builder.relation_sql}"
|
20
|
+
else
|
21
|
+
quoted_table_name = @connection.quote_table_name(table_name)
|
22
|
+
stmt = "INSERT INTO #{quoted_table_name} #{builder.relation_sql}"
|
23
|
+
end
|
24
|
+
|
25
|
+
stmt
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module InsertSelect
|
2
|
+
module Adapters
|
3
|
+
class PostgresqlAdapter < BaseAdapter
|
4
|
+
def initialize(table_name, connection)
|
5
|
+
@table_name = table_name
|
6
|
+
@connection = connection
|
7
|
+
end
|
8
|
+
|
9
|
+
def build_sql(builder)
|
10
|
+
sql = super
|
11
|
+
sql += " RETURNING #{builder.returning}" if builder.returning
|
12
|
+
|
13
|
+
sql
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
module InsertSelect
|
2
|
+
module ActiveRecord
|
3
|
+
class Builder
|
4
|
+
attr_reader :relation, :mapping, :model, :returning, :insert_select_from, :connection
|
5
|
+
|
6
|
+
def initialize(insert_select_from)
|
7
|
+
@insert_select_from = insert_select_from
|
8
|
+
@connection = insert_select_from.connection
|
9
|
+
@relation = insert_select_from.relation.all
|
10
|
+
@mapping = insert_select_from.mapping || {}
|
11
|
+
@model = insert_select_from.model
|
12
|
+
@returning = insert_select_from.returning
|
13
|
+
end
|
14
|
+
|
15
|
+
def mapper
|
16
|
+
@mapper ||= begin
|
17
|
+
result = {}
|
18
|
+
insert_mapping = {}
|
19
|
+
|
20
|
+
if selected_column_names.present?
|
21
|
+
selected_column_names.each do |column_name|
|
22
|
+
insert_mapping[column_name.to_s] = mapping[column_name.to_sym] || column_name
|
23
|
+
end
|
24
|
+
elsif mapping.present? || model.scope_attributes.present?
|
25
|
+
@connection.columns(relation.table_name).each do |column|
|
26
|
+
insert_mapping[column.name.to_s] = mapping[column.name.to_sym] || column.name
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
model.scope_attributes.keys.each {|column_name| insert_mapping.delete(column_name.to_s) }
|
31
|
+
|
32
|
+
result[:insert_mapping] = insert_mapping
|
33
|
+
result[:constant_mapping] = model.scope_attributes || {}
|
34
|
+
|
35
|
+
result
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def insert_mapping
|
40
|
+
@insert_mapping ||= mapper[:insert_mapping]
|
41
|
+
end
|
42
|
+
|
43
|
+
def constant_mapping
|
44
|
+
@constant_mapping ||= mapper[:constant_mapping]
|
45
|
+
end
|
46
|
+
|
47
|
+
def into
|
48
|
+
return nil if insert_mapping.blank? && constant_mapping.blank?
|
49
|
+
"INTO #{model.quoted_table_name} (#{inserting_column_names.join(", ")})"
|
50
|
+
end
|
51
|
+
|
52
|
+
def inserting_column_names
|
53
|
+
(insert_mapping.values + constant_mapping.keys).map {|k| @connection.quote_column_name(k) }
|
54
|
+
end
|
55
|
+
|
56
|
+
def relation_sql
|
57
|
+
relation.to_sql
|
58
|
+
end
|
59
|
+
|
60
|
+
def constant_values
|
61
|
+
constant_mapping.values
|
62
|
+
end
|
63
|
+
|
64
|
+
def reselect_relation!
|
65
|
+
current_selected_column_names = relation.select_values.map(&:to_s)
|
66
|
+
|
67
|
+
insert_mapping.keys.each do |k, v|
|
68
|
+
relation._select!(k) if current_selected_column_names.exclude?(k.to_s)
|
69
|
+
end
|
70
|
+
|
71
|
+
constant_mapping.keys.each do |k, v|
|
72
|
+
relation._select!("?")
|
73
|
+
remove_select_values!(k)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def returning
|
78
|
+
return unless @returning
|
79
|
+
|
80
|
+
if @returning.is_a?(String)
|
81
|
+
@returning
|
82
|
+
else
|
83
|
+
Array( @returning ).map do |attribute|
|
84
|
+
if model.attribute_alias?(attribute)
|
85
|
+
"#{@connection.quote_column_name(model.attribute_alias(attribute))} AS #{@connection.quote_column_name(attribute)}"
|
86
|
+
else
|
87
|
+
@connection.quote_column_name(attribute)
|
88
|
+
end
|
89
|
+
end.join(",")
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
private
|
94
|
+
|
95
|
+
def remove_select_values!(column_name)
|
96
|
+
relation.select_values = relation.select_values - [column_name.to_sym]
|
97
|
+
end
|
98
|
+
|
99
|
+
def selected_column_names
|
100
|
+
relation.select_values
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module InsertSelect
|
2
|
+
class Error < StandardError; end
|
3
|
+
|
4
|
+
class ColumnCountMisMatchError < Error
|
5
|
+
def initialize(message)
|
6
|
+
super(message)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class ColumnNameMisMatchError < Error
|
11
|
+
def initialize(message)
|
12
|
+
super(message)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_record"
|
4
|
+
|
5
|
+
require_relative "insert_select/active_record"
|
6
|
+
require_relative "insert_select/errors"
|
7
|
+
require_relative "insert_select/version"
|
8
|
+
|
9
|
+
ActiveSupport.on_load(:active_record) do
|
10
|
+
ActiveRecord::Base.include(InsertSelect::ActiveRecord)
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: insert_select
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- a5-stable
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-06-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: This is a custom gem that extends ActiveRecord to enable the expression
|
28
|
+
of SQL `INSERT INTO ... SELECT ...` queries in a more convenient way. It allows
|
29
|
+
you to copy data from one table to another based on specified conditions using a
|
30
|
+
simple and expressive syntax.
|
31
|
+
email:
|
32
|
+
- sh07e1916@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- ".rspec"
|
38
|
+
- ".ruby-version"
|
39
|
+
- CHANGELOG.md
|
40
|
+
- CODE_OF_CONDUCT.md
|
41
|
+
- Gemfile
|
42
|
+
- Gemfile.lock
|
43
|
+
- LICENSE.txt
|
44
|
+
- README.md
|
45
|
+
- Rakefile
|
46
|
+
- lib/insert_select.rb
|
47
|
+
- lib/insert_select/active_record.rb
|
48
|
+
- lib/insert_select/adapters/base_adapter.rb
|
49
|
+
- lib/insert_select/adapters/mysql_adapter.rb
|
50
|
+
- lib/insert_select/adapters/postgresql_adapter.rb
|
51
|
+
- lib/insert_select/adapters/sqlite_adapter.rb
|
52
|
+
- lib/insert_select/builder.rb
|
53
|
+
- lib/insert_select/errors.rb
|
54
|
+
- lib/insert_select/version.rb
|
55
|
+
- sig/insert_select.rbs
|
56
|
+
homepage:
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
metadata:
|
60
|
+
homepage_uri: https://github.com/a5-stable/insert_select
|
61
|
+
source_code_uri: https://github.com/a5-stable/insert_select
|
62
|
+
changelog_uri: https://github.com/a5-stable/insert_select/blob/main/CHANGELOG.md
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: 2.6.0
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubygems_version: 3.2.3
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Gem for performing simple data copying using SQL `INSERT INTO ... SELECT
|
82
|
+
...` queries
|
83
|
+
test_files: []
|