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.
- checksums.yaml +4 -4
- data/lib/active_sql_bindings.rb +74 -40
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1906c6795e1f1f02c1ad0ea0085624656dbb293c12e725ca1ee7d0211bfda96
|
4
|
+
data.tar.gz: d5a32f32e11fde42526e9dbbea112942c2b11adb0759af42e4829f6369ca9134
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3d75216869fd7a6e7961ff6dd006bd7b81a9d18f19966ec440df53cad6702fe718fbbcb8ccb200fda2ce7d4ec6d1cdf0def77fc45506a1a077720578104b354a
|
7
|
+
data.tar.gz: 0f6799b0ac7329146cb49b32a351587ade0332c5c4033d0aba74a16d58a38a0ef1a8766c8de53b72a6c6198d04b89de7b48955580f9dafe3b78bbfd128312883
|
data/lib/active_sql_bindings.rb
CHANGED
@@ -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
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
bind.
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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
|
-
#
|
34
|
-
|
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
|
-
|
37
|
-
|
62
|
+
value
|
63
|
+
end
|
38
64
|
|
39
|
-
# Convert JSON
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
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
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
+
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:
|
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
|
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.
|
81
|
+
rubygems_version: 3.1.2
|
82
82
|
signing_key:
|
83
83
|
specification_version: 4
|
84
84
|
summary: Active SQL bindings gem
|