active_sql_bindings 0.0.1

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 +7 -0
  2. data/lib/active_sql_bindings.rb +54 -0
  3. metadata +57 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2aaf144f23edc98cb60202a23fdb601b4f714193f44d51088481983b24d98667
4
+ data.tar.gz: 33a2e654b7f2bbae4fd3bdc650631871b9bd1a93408982dd3904a32bbfac0735
5
+ SHA512:
6
+ metadata.gz: 49fd556a8aaf3eb9ee32b7c2fa9a3486c01c03908cae2621eb33900908e8f45bc352b6bca9094b5735fe71746551e6e6c4c1cdac738f38cc4c006a816099b7ce
7
+ data.tar.gz: 6dbf72960efe999175f6e697cae610480504a796df1ae1afe6d229019c35e4531b3c126bd42a79e5d37158ca6ae1271b756a8bac50af57fea4ed910e515657a5
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubygems'
4
+ require 'active_record'
5
+
6
+ # Class for work with SQL query.
7
+ # You can use native SQL with bindings as hash.
8
+ # Auto converting JSON fields to hash.
9
+ 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]
31
+ end
32
+ end
33
+
34
+ # Execute query, convert to hash with symbol keys
35
+ result = ActiveRecord::Base.connection.exec_query(sql, 'SQL', bindings).map(&:symbolize_keys)
36
+
37
+ # Convert JSON data to hash
38
+ result.map do |v|
39
+ next if v.nil?
40
+
41
+ v.each do |key, val|
42
+ v[key] = json_to_hash(val)
43
+ end
44
+ end
45
+ end
46
+
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
53
+ end
54
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_sql_bindings
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Danilevsky Kirill
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-11-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.8'
27
+ description: You can use native SQL query with named bindings
28
+ email: k.danilevsky@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/active_sql_bindings.rb
34
+ homepage: https://github.com/kirill-dan/active_sql_bindings
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.5.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.0.3
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Active SQL bindings gem
57
+ test_files: []