active_sql_bindings 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/active_sql_bindings.rb +74 -40
  3. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f8cabb420ed7c20634aea656e5fc73860c4873821448e2880417ee5a1ae19637
4
- data.tar.gz: c84ed030038c3b1be486a9e74e2303242b68d8344e7a4135378b25e61863c61b
3
+ metadata.gz: b1906c6795e1f1f02c1ad0ea0085624656dbb293c12e725ca1ee7d0211bfda96
4
+ data.tar.gz: d5a32f32e11fde42526e9dbbea112942c2b11adb0759af42e4829f6369ca9134
5
5
  SHA512:
6
- metadata.gz: 2db7501bd65cc82635210bab845081d5e5867012b58520b0e35dfd875b6f95f1032d3ef32b60b1fff3bf9be958861c49888c92ea0ff58d1867c5c495ae2c0980
7
- data.tar.gz: 06c6448a3bf4c9a99fcc7535ef107529f51430a4d77e1dd5e71e8ab76abb8067efed5756a1de2680c5f6dadfd271330067cd2005fb4328cfe03e41088a9ed0b3
6
+ metadata.gz: 3d75216869fd7a6e7961ff6dd006bd7b81a9d18f19966ec440df53cad6702fe718fbbcb8ccb200fda2ce7d4ec6d1cdf0def77fc45506a1a077720578104b354a
7
+ data.tar.gz: 0f6799b0ac7329146cb49b32a351587ade0332c5c4033d0aba74a16d58a38a0ef1a8766c8de53b72a6c6198d04b89de7b48955580f9dafe3b78bbfd128312883
@@ -6,52 +6,86 @@ require 'active_record'
6
6
  # You can use native SQL with bindings as hash.
7
7
  # Auto converting JSON fields to hash.
8
8
  class ActiveSqlBindings
9
- # Create sql query with hash named bindings
10
- #
11
- # Example: ActiveSqlBindings.execute('SELECT name FROM test WHERE id = :id', id: id)
12
- #
13
- # @param [String] sql SQL query
14
- # @param [Hash] bind bindings data for query
15
- #
16
- # @return [Array] executed SQL request data and return array with hashes
17
- def self.execute(sql, bind = {})
18
- bindings = []
19
- bind_index = 1
20
-
21
- # Get all bindings if exist
22
- unless bind.empty?
23
- bind.each do |key, value|
24
- # Change name bind to $ bind
25
- sql.gsub!(/(?<!:):#{key}(?=\b)/, "$#{bind_index}")
26
- bind_index += 1
27
-
28
- # Add new bind data
29
- 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
32
+ end
33
+
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
39
+
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
42
+
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
30
51
  end
31
52
  end
32
53
 
33
- # Execute query, convert to hash with symbol keys
34
- sql_result = ActiveRecord::Base.connection.exec_query(sql, 'SQL', bindings)
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)
35
61
 
36
- # Find fields JSON/JSONb type
37
- json_fields = sql_result.column_types.select { |_k, v| v.type == :json || v.type == :jsonb }.keys
62
+ value
63
+ end
38
64
 
39
- # Convert JSON data to hash
40
- sql_result.map do |v|
41
- v.map do |key, value|
42
- [
43
- key.to_sym,
44
- json_fields.include?(key) ? json_to_hash(value) : value
45
- ]
46
- end.to_h
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
47
71
  end
48
- end
49
72
 
50
- # Convert JSON to hash if correct data
51
- #
52
- # @param [String] json string
53
- # @return [Hash] return hash if json is correct or input data
54
- def self.json_to_hash(json)
55
- JSON.parse(json, symbolize_names: true) rescue json
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
56
90
  end
57
91
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_sql_bindings
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
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: 2020-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: 6.0.2
55
- description: You can use native SQL query with named bindings
55
+ description: You can use native PostgreSQL query with named bindings
56
56
  email: k.danilevsky@gmail.com
57
57
  executables: []
58
58
  extensions: []
@@ -78,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
78
  - !ruby/object:Gem::Version
79
79
  version: '0'
80
80
  requirements: []
81
- rubygems_version: 3.0.3
81
+ rubygems_version: 3.1.2
82
82
  signing_key:
83
83
  specification_version: 4
84
84
  summary: Active SQL bindings gem