active_sql_bindings 0.0.1 → 0.0.6
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 -37
- metadata +35 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 197f5277c975f55258df8db121d1ce8dafde38d4c8fc74fbfec476d46f288bb9
|
4
|
+
data.tar.gz: 2e4eaccabaebb7392767cc9b4bab1797be694c87faffaf885c02e10e75149f51
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1318c8797e86802c3de00c4adf4a0b7e760f060955ef6bde5053e4038a5f75c887cf96e39a85d4c0ae9cf8582394edbeb186d9b00fda9020ac10ef9d20b40a36
|
7
|
+
data.tar.gz: 72086da0c63e25f3ba959fcfc10b98ede6351e4bd3e6a81abde0d0da1e199e48acd99a0037c925d3ec798f2396bc1286290f9d95a41f0c83dd23a6c9469875c4
|
data/lib/active_sql_bindings.rb
CHANGED
@@ -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
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
bind.
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
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
|
-
|
35
|
-
|
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
|
-
|
38
|
-
|
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
|
-
|
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
|
43
51
|
end
|
44
52
|
end
|
45
|
-
end
|
46
53
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
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.
|
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:
|
11
|
+
date: 2021-05-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: minitest
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
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:
|
27
|
-
|
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.
|
81
|
+
rubygems_version: 3.1.2
|
54
82
|
signing_key:
|
55
83
|
specification_version: 4
|
56
84
|
summary: Active SQL bindings gem
|