active_sql_bindings 0.0.1 → 0.0.6

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/active_sql_bindings.rb +74 -37
  3. metadata +35 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2aaf144f23edc98cb60202a23fdb601b4f714193f44d51088481983b24d98667
4
- data.tar.gz: 33a2e654b7f2bbae4fd3bdc650631871b9bd1a93408982dd3904a32bbfac0735
3
+ metadata.gz: 197f5277c975f55258df8db121d1ce8dafde38d4c8fc74fbfec476d46f288bb9
4
+ data.tar.gz: 2e4eaccabaebb7392767cc9b4bab1797be694c87faffaf885c02e10e75149f51
5
5
  SHA512:
6
- metadata.gz: 49fd556a8aaf3eb9ee32b7c2fa9a3486c01c03908cae2621eb33900908e8f45bc352b6bca9094b5735fe71746551e6e6c4c1cdac738f38cc4c006a816099b7ce
7
- data.tar.gz: 6dbf72960efe999175f6e697cae610480504a796df1ae1afe6d229019c35e4531b3c126bd42a79e5d37158ca6ae1271b756a8bac50af57fea4ed910e515657a5
6
+ metadata.gz: 1318c8797e86802c3de00c4adf4a0b7e760f060955ef6bde5053e4038a5f75c887cf96e39a85d4c0ae9cf8582394edbeb186d9b00fda9020ac10ef9d20b40a36
7
+ data.tar.gz: 72086da0c63e25f3ba959fcfc10b98ede6351e4bd3e6a81abde0d0da1e199e48acd99a0037c925d3ec798f2396bc1286290f9d95a41f0c83dd23a6c9469875c4
@@ -1,54 +1,91 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rubygems'
4
3
  require 'active_record'
5
4
 
6
5
  # Class for work with SQL query.
7
6
  # You can use native SQL with bindings as hash.
8
7
  # Auto converting JSON fields to hash.
9
8
  class ActiveSqlBindings
10
- # Create sql query with hash named bindings
11
- #
12
- # Example: ActiveSqlBindings.execute('SELECT name FROM test WHERE id = :id', id: id)
13
- #
14
- # @param [String] sql SQL query
15
- # @param [Hash] bind bindings data for query
16
- #
17
- # @return [Array] executed SQL request data and return array with hashes
18
- def self.execute(sql, bind = {})
19
- bindings = []
20
- bind_index = 1
21
-
22
- # Get all bindings if exist
23
- unless bind.empty?
24
- bind.each do |key, value|
25
- # Change name bind to $ bind
26
- sql.gsub!(/(?<!:):#{key}(?=\b)/, "$#{bind_index}")
27
- bind_index += 1
28
-
29
- # Add new bind data
30
- bindings << [nil, value]
9
+ class << self
10
+ # Create sql query with hash named bindings
11
+ #
12
+ # Example: ActiveSqlBindings.execute('SELECT name FROM test WHERE id = :id', id: id)
13
+ #
14
+ # @param [String] sql SQL query
15
+ # @param [Hash] bind bindings data for query
16
+ #
17
+ # @return [Array] executed SQL request data and return array with hashes
18
+ def execute(sql, bind = {})
19
+ bindings = []
20
+ bind_index = 1
21
+
22
+ # Get all bindings if exist
23
+ unless bind.empty?
24
+ bind.each do |key, value|
25
+ # Change name bind to $ bind
26
+ sql.gsub!(/(?<!:):#{key}(?=\b)/, "$#{bind_index}")
27
+ bind_index += 1
28
+
29
+ # Add new bind data
30
+ bindings << [nil, value]
31
+ end
31
32
  end
32
- end
33
33
 
34
- # Execute query, convert to hash with symbol keys
35
- result = ActiveRecord::Base.connection.exec_query(sql, 'SQL', bindings).map(&:symbolize_keys)
34
+ # Execute query, convert to hash with symbol keys
35
+ sql_result = ActiveRecord::Base.connection.exec_query(sql, 'SQL', bindings)
36
+
37
+ # Find fields JSON/JSONb type
38
+ json_fields = sql_result.column_types.select { |_k, v| v.class.name.split('::').last.downcase.to_sym == :json || v.class.name.split('::').last.downcase.to_sym == :jsonb }.keys
36
39
 
37
- # Convert JSON data to hash
38
- result.map do |v|
39
- next if v.nil?
40
+ # Find fields ARRAY type
41
+ array_fields = sql_result.column_types.select { |_k, v| v.class.name.split('::').last.downcase.to_sym == :array }.keys
40
42
 
41
- v.each do |key, val|
42
- v[key] = json_to_hash(val)
43
+ # Convert JSON data to hash
44
+ sql_result.map do |v|
45
+ v.map do |key, value|
46
+ [
47
+ key.to_sym,
48
+ check_fields(key: key, value: value, fields: { json: json_fields, array: array_fields })
49
+ ]
50
+ end.to_h
43
51
  end
44
52
  end
45
- end
46
53
 
47
- # Convert JSON to hash if correct data
48
- #
49
- # @param [String] json string
50
- # @return [Hash] return hash if json is correct or input data
51
- def self.json_to_hash(json)
52
- JSON.parse(json, symbolize_names: true) rescue json
54
+ # Check fields' type and replace it
55
+ #
56
+ # @param [String] key for check type
57
+ # @param [String] value for converting
58
+ def check_fields(key:, value:, fields:)
59
+ return json_to_hash(value) if fields[:json].include?(key)
60
+ return postgres_to_array(value) if fields[:array].include?(key)
61
+
62
+ value
63
+ end
64
+
65
+ # Convert JSON to hash if correct data
66
+ #
67
+ # @param [String] json string
68
+ # @return [Hash] return hash if json is correct or input data
69
+ def json_to_hash(json)
70
+ JSON.parse(json, symbolize_names: true) rescue json
71
+ end
72
+
73
+ # Convert ruby array to Postgres array
74
+ #
75
+ # @param [Array] array data for converting
76
+ def array_to_postgres(array)
77
+ array = [array] if array.is_a?(String)
78
+
79
+ '{' + array.join(',') + '}'
80
+ end
81
+
82
+ # Convert Postgres array to ruby array
83
+ #
84
+ # @param [String] array string data for converting
85
+ def postgres_to_array(array)
86
+ return array.gsub(/[{}]/, '').split(',') if array.is_a?(String)
87
+
88
+ []
89
+ end
53
90
  end
54
91
  end
metadata CHANGED
@@ -1,30 +1,58 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_sql_bindings
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danilevsky Kirill
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-05 00:00:00.000000000 Z
11
+ date: 2021-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rspec
14
+ name: minitest
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '3.8'
19
+ version: 5.13.0
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '3.8'
27
- description: You can use native SQL query with named bindings
26
+ version: 5.13.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.4.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.4.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 6.1.3.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 6.1.3.2
55
+ description: You can use native PostgreSQL query with named bindings
28
56
  email: k.danilevsky@gmail.com
29
57
  executables: []
30
58
  extensions: []
@@ -50,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
78
  - !ruby/object:Gem::Version
51
79
  version: '0'
52
80
  requirements: []
53
- rubygems_version: 3.0.3
81
+ rubygems_version: 3.1.2
54
82
  signing_key:
55
83
  specification_version: 4
56
84
  summary: Active SQL bindings gem