temp_table 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +19 -0
- data/README.md +59 -0
- data/Rakefile +4 -0
- data/lib/temp_table/copy.rb +104 -0
- data/lib/temp_table/create.rb +31 -0
- data/lib/temp_table/fetch.rb +25 -0
- data/lib/temp_table/insert.rb +30 -0
- data/lib/temp_table/insert_row_from_original.rb +31 -0
- data/lib/temp_table/insert_row_from_reference.rb +25 -0
- data/lib/temp_table/version.rb +5 -0
- data/lib/temp_table.rb +30 -0
- data/sig/temp_table.rbs +4 -0
- metadata +61 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 44da43c646cffbe6a251c79f16469da742f609adb204503bb8b0dc73605d6a9f
|
4
|
+
data.tar.gz: 39c4439c7934b9780522396cf5ad5763668fa0f4dcd5d23d4e93400edc3ad362
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e8041d90092e7fd691affe89927f36bd8b194328a10d84455004d497c0ac20a5320ca38624bf83dcd5a92023107dcdb82cadc2feddea5d4285beaf1befb3ec30
|
7
|
+
data.tar.gz: 702c147394ba3572b8fc2eea6bf671825752a6f9fe3f3b459b36da6d925ba95b90c47a1e0dd17e56824a2733f7e819309bd906daf5d979f51ee51e07cd206475
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# TempTable
|
2
|
+
|
3
|
+
You can create the temporary table using this gem so that you can make a temp table and store its data temproraly until the user's session is active.
|
4
|
+
|
5
|
+
To create temporary table.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'temp_table'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install temp_table
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
# To create copy of existing table Example:
|
25
|
+
require "temp_table"
|
26
|
+
TempTable.copy("original_table_name", "temp_table_name")
|
27
|
+
|
28
|
+
# To create copy of existing table with extra columns Example:
|
29
|
+
TempTable.copy("original_table_name", "temp_table_name", [{ name: "temp_table_id", data_type: "integer" }])
|
30
|
+
|
31
|
+
# To insert row from original table to temp table Example:
|
32
|
+
TempTable.insert_row_from_original("original_table_name", original_table_id, "temp_table_name")
|
33
|
+
|
34
|
+
# To insert data to reference table Example:
|
35
|
+
TempTable.insert_row_from_reference("temp_reference_id", reference_id, data, "original_table_name", "temp_table_name")
|
36
|
+
|
37
|
+
# To create table Example:
|
38
|
+
columns = { id: "integer", name: "varchar", phone: "bigint", email: "string" }
|
39
|
+
TempTable.create("temp_table_name", columns)
|
40
|
+
|
41
|
+
# To fetch data from temporary table
|
42
|
+
# with condition
|
43
|
+
conditions = {
|
44
|
+
age: 25,
|
45
|
+
status: 'active'
|
46
|
+
}
|
47
|
+
TempTable.fetch("temp_table_name", conditions)
|
48
|
+
# without condition
|
49
|
+
TempTable.fetch("temp_table_name") # to fetch all data of the table
|
50
|
+
|
51
|
+
## Development
|
52
|
+
|
53
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
54
|
+
|
55
|
+
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).
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Shesh-08/temp_table.
|
data/Rakefile
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module TempTable
|
4
|
+
class Copy
|
5
|
+
attr_accessor :table_name, :original_table_name, :extra_columns
|
6
|
+
|
7
|
+
def initialize(original_table_name, table_name, extra_columns = nil)
|
8
|
+
super()
|
9
|
+
@table_name = table_name
|
10
|
+
@original_table_name = original_table_name
|
11
|
+
@extra_columns = extra_columns
|
12
|
+
end
|
13
|
+
|
14
|
+
def perform
|
15
|
+
ActiveRecord::Base.connection.execute <<-SQL.squish
|
16
|
+
CREATE TEMPORARY TABLE IF NOT EXISTS #{table_name} (
|
17
|
+
id SERIAL PRIMARY KEY,
|
18
|
+
original_id INTEGER
|
19
|
+
);
|
20
|
+
SQL
|
21
|
+
add_columns_from_original
|
22
|
+
add_extra_columns unless extra_columns.nil?
|
23
|
+
add_foreign_key
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def add_columns_from_original
|
29
|
+
original_columns = ActiveRecord::Base.connection.columns(original_table_name).reject do |c|
|
30
|
+
c.name == "id"
|
31
|
+
end
|
32
|
+
|
33
|
+
return if original_columns.blank?
|
34
|
+
|
35
|
+
column_definitions = fetch_column_definitions(original_columns)
|
36
|
+
|
37
|
+
return if column_definitions.blank?
|
38
|
+
|
39
|
+
column_definitions_string = column_definitions.join(", ")
|
40
|
+
|
41
|
+
sql = <<-SQL.squish
|
42
|
+
ALTER TABLE #{table_name}
|
43
|
+
#{column_definitions_string};
|
44
|
+
SQL
|
45
|
+
|
46
|
+
Rails.logger.info("Executing SQL: #{sql}")
|
47
|
+
|
48
|
+
ActiveRecord::Base.connection.execute(sql)
|
49
|
+
rescue ActiveRecord::StatementInvalid => e
|
50
|
+
Rails.logger.info("SQL Error: #{e.message}")
|
51
|
+
end
|
52
|
+
|
53
|
+
def add_foreign_key
|
54
|
+
return unless temporary_table?(table_name)
|
55
|
+
|
56
|
+
if temporary_table?(original_table_name)
|
57
|
+
ActiveRecord::Base.connection.execute <<-SQL.squish
|
58
|
+
ALTER TABLE #{table_name}
|
59
|
+
ADD CONSTRAINT fk_original
|
60
|
+
FOREIGN KEY (original_id) REFERENCES #{original_table_name}(id);
|
61
|
+
SQL
|
62
|
+
else
|
63
|
+
Rails.logger.info("Cannot add foreign key: #{original_table_name} is not a temporary table.")
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def temporary_table?(table_name)
|
68
|
+
ActiveRecord::Base.connection.tables.include?(table_name) &&
|
69
|
+
ActiveRecord::Base.connection.execute("SELECT pg_is_temp('#{table_name}')").first["pg_is_temp"]
|
70
|
+
end
|
71
|
+
|
72
|
+
def fetch_column_definitions(original_columns)
|
73
|
+
existing_columns = ActiveRecord::Base.connection.columns(table_name).map(&:name)
|
74
|
+
|
75
|
+
column_definitions_from_original = original_columns.reject do |column|
|
76
|
+
existing_columns.include?(column.name)
|
77
|
+
end
|
78
|
+
column_definitions_from_original.map do |column|
|
79
|
+
"ADD COLUMN #{column.name} #{column.sql_type}"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def add_extra_columns
|
84
|
+
existing_columns = ActiveRecord::Base.connection.columns(table_name).map(&:name)
|
85
|
+
|
86
|
+
column_definitions_from_original = extra_columns.reject do |column|
|
87
|
+
existing_columns.include?(column[:name])
|
88
|
+
end
|
89
|
+
|
90
|
+
return if column_definitions_from_original.blank?
|
91
|
+
|
92
|
+
column_definitions = extra_columns.map do |column|
|
93
|
+
"ADD COLUMN #{column[:name]} #{column[:data_type]}"
|
94
|
+
end
|
95
|
+
|
96
|
+
extra_difinitions_columns = column_definitions.join(", ")
|
97
|
+
|
98
|
+
ActiveRecord::Base.connection.execute <<-SQL.squish
|
99
|
+
ALTER TABLE #{table_name}
|
100
|
+
#{extra_difinitions_columns};
|
101
|
+
SQL
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module TempTable
|
4
|
+
class Create
|
5
|
+
attr_accessor :table_name, :columns
|
6
|
+
|
7
|
+
def initialize(table_name, columns)
|
8
|
+
super()
|
9
|
+
@table_name = table_name
|
10
|
+
@columns = columns
|
11
|
+
end
|
12
|
+
|
13
|
+
def perform
|
14
|
+
column_definitions = columns.map { |name, type| "#{name} #{type}" }.join(", ")
|
15
|
+
if column_definitions.empty?
|
16
|
+
ActiveRecord::Base.connection.execute <<-SQL.squish
|
17
|
+
CREATE TEMPORARY IF NOT EXISTS #{@table_name} (
|
18
|
+
id SERIAL PRIMARY KEY
|
19
|
+
);
|
20
|
+
SQL
|
21
|
+
else
|
22
|
+
ActiveRecord::Base.connection.execute <<-SQL.squish
|
23
|
+
CREATE TEMPORARY TABLE #{@table_name} (
|
24
|
+
id SERIAL PRIMARY KEY,
|
25
|
+
#{column_definitions}
|
26
|
+
);
|
27
|
+
SQL
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module TempTable
|
4
|
+
class Fetch
|
5
|
+
attr_accessor :table_name, :conditions
|
6
|
+
|
7
|
+
def initialize(table_name, conditions = nil)
|
8
|
+
@table_name = table_name
|
9
|
+
@conditions = conditions
|
10
|
+
end
|
11
|
+
|
12
|
+
def perform
|
13
|
+
query = "SELECT * FROM #{table_name}"
|
14
|
+
|
15
|
+
if conditions.present?
|
16
|
+
condition_strings = conditions.map do |column, value|
|
17
|
+
"#{column} = #{ActiveRecord::Base.connection.quote(value)}"
|
18
|
+
end
|
19
|
+
query += " WHERE #{condition_strings.join(' AND ')}"
|
20
|
+
end
|
21
|
+
|
22
|
+
ActiveRecord::Base.connection.execute(query).to_a
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module TempTable
|
4
|
+
class Insert
|
5
|
+
attr_accessor :table_name, :data
|
6
|
+
|
7
|
+
def initialize(table_name, data)
|
8
|
+
super()
|
9
|
+
@table_name = table_name
|
10
|
+
@data = data
|
11
|
+
end
|
12
|
+
|
13
|
+
def perform
|
14
|
+
data.each do |row|
|
15
|
+
column_names = row.keys.join(", ")
|
16
|
+
values = row.values.map do |value|
|
17
|
+
if value.is_a?(Array)
|
18
|
+
ActiveRecord::Base.connection.quote(value.to_json)
|
19
|
+
else
|
20
|
+
ActiveRecord::Base.connection.quote(value)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
ActiveRecord::Base.connection.execute <<-SQL.squish
|
24
|
+
INSERT INTO #{@table_name} (#{column_names})
|
25
|
+
VALUES (#{values.join(', ')});
|
26
|
+
SQL
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module TempTable
|
4
|
+
class InsertRowFromOriginal
|
5
|
+
attr_accessor :table_name, :original_table_name, :original_id
|
6
|
+
|
7
|
+
def initialize(original_table_name, original_id, table_name)
|
8
|
+
super()
|
9
|
+
@original_table_name = original_table_name
|
10
|
+
@table_name = table_name
|
11
|
+
@original_id = original_id
|
12
|
+
end
|
13
|
+
|
14
|
+
def perform
|
15
|
+
result = ActiveRecord::Base.connection.execute <<-SQL.squish
|
16
|
+
INSERT INTO #{@table_name} (original_id, #{column_names})
|
17
|
+
SELECT id, #{column_names} FROM #{@original_table_name}
|
18
|
+
WHERE id = #{ActiveRecord::Base.connection.quote(original_id)}
|
19
|
+
RETURNING id;
|
20
|
+
SQL
|
21
|
+
|
22
|
+
result.getvalue(0, 0)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def column_names
|
28
|
+
ActiveRecord::Base.connection.columns(@original_table_name).reject { |c| c.name == "id" }.map(&:name).join(", ")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module TempTable
|
4
|
+
class InsertRowFromReference
|
5
|
+
attr_accessor :table_name, :original_table_name, :reference_column, :reference_id, :data, :original_id
|
6
|
+
|
7
|
+
def initialize(reference_column, reference_id, data, original_table_name, table_name)
|
8
|
+
super()
|
9
|
+
@original_table_name = original_table_name
|
10
|
+
@table_name = table_name
|
11
|
+
@original_id = data.id
|
12
|
+
@reference_column = reference_column
|
13
|
+
@reference_id = reference_id
|
14
|
+
@data = data
|
15
|
+
end
|
16
|
+
|
17
|
+
def perform
|
18
|
+
formated_data = []
|
19
|
+
formated_data << data.attributes
|
20
|
+
formated_data[0]["original_id"] = original_id
|
21
|
+
formated_data[0][reference_column] = reference_id
|
22
|
+
InsertService.new(table_name, formated_data).perform
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/temp_table.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "temp_table/version"
|
4
|
+
|
5
|
+
module TempTable
|
6
|
+
class Error < StandardError; end
|
7
|
+
def self.fetch(table_name, conditions = nil)
|
8
|
+
TempTable::Fetch.new(table_name, conditions).perform
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.insert(table_name, data)
|
12
|
+
TempTable::Insert.new(table_name, data).perform
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.insert_row_from_original(original_table_name, original_id, table_name)
|
16
|
+
TempTable::InsertRowFromOriginal.new(original_table_name, original_id, table_name).perform
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.insert_row_from_reference(reference_column, reference_id, data, original_table_name, table_name)
|
20
|
+
TempTable::InsertRowFromReferenceService.new(reference_column, reference_id, data, original_table_name, table_name).perform
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.create(table_name, columns)
|
24
|
+
TempTable::Create.new(table_name, columns).perform
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.copy(original_table_name, table_name, extra_columns = nil)
|
28
|
+
TempTable::Copy.new(original_table_name, table_name, extra_columns = nil).perform
|
29
|
+
end
|
30
|
+
end
|
data/sig/temp_table.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: temp_table
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Shesh Nath Goswami
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-10-27 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: You can create temporary tables using this gem to store data temporarily
|
14
|
+
until the user's session is active.
|
15
|
+
email:
|
16
|
+
- sheshwork08@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- Gemfile
|
22
|
+
- Gemfile.lock
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- lib/temp_table.rb
|
26
|
+
- lib/temp_table/copy.rb
|
27
|
+
- lib/temp_table/create.rb
|
28
|
+
- lib/temp_table/fetch.rb
|
29
|
+
- lib/temp_table/insert.rb
|
30
|
+
- lib/temp_table/insert_row_from_original.rb
|
31
|
+
- lib/temp_table/insert_row_from_reference.rb
|
32
|
+
- lib/temp_table/version.rb
|
33
|
+
- sig/temp_table.rbs
|
34
|
+
homepage: https://github.com/Shesh-08/temp-table
|
35
|
+
licenses:
|
36
|
+
- MIT
|
37
|
+
metadata:
|
38
|
+
allowed_push_host: https://rubygems.org
|
39
|
+
homepage_uri: https://github.com/Shesh-08/temp-table
|
40
|
+
source_code_uri: https://github.com/Shesh-08/temp-table
|
41
|
+
changelog_uri: https://github.com/Shesh-08/temp-table/blob/master/CHANGELOG.md
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 2.6.0
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubygems_version: 3.5.22
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: To create temporary tables.
|
61
|
+
test_files: []
|