db2_query 0.2.0 → 0.3.1

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.
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module DB2Query
4
- module ConnectionHandling
5
- RAILS_ENV = -> { (Rails.env if defined?(Rails.env)) || ENV["RAILS_ENV"].presence || ENV["RACK_ENV"].presence }
6
- DEFAULT_ENV = -> { RAILS_ENV.call || "default_env" }
7
-
8
- def resolve_config_for_connection(config_or_env) # :nodoc:
9
- raise "Anonymous class is not allowed." unless name
10
-
11
- config_or_env ||= DEFAULT_ENV.call.to_sym
12
- pool_name = primary_class? ? "primary" : name
13
- self.connection_specification_name = pool_name
14
- resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(Base.configurations)
15
-
16
- config_hash = resolver.resolve(config_or_env, pool_name).symbolize_keys
17
- config_hash[:name] = pool_name
18
-
19
- config_hash
20
- end
21
-
22
- def connection_specification_name
23
- if !defined?(@connection_specification_name) || @connection_specification_name.nil?
24
- return self == Base ? "primary" : superclass.connection_specification_name
25
- end
26
- @connection_specification_name
27
- end
28
-
29
- def primary_class?
30
- self == Base || defined?(ApplicationRecord) && self == ApplicationRecord
31
- end
32
- end
33
- end
@@ -1,88 +0,0 @@
1
- # frozen_String_literal: true
2
-
3
- module DB2Query
4
- module DatabaseStatements
5
- def query(sql)
6
- stmt = @connection.run(sql)
7
- stmt.to_a
8
- ensure
9
- stmt.drop unless stmt.nil?
10
- end
11
-
12
- def query_rows(sql)
13
- query(sql)
14
- end
15
-
16
- def query_value(sql, name = nil)
17
- single_value_from_rows(query(sql))
18
- end
19
-
20
- def query_values(sql, name = nil)
21
- query(sql).map(&:first)
22
- end
23
-
24
- def execute(sql, args = [])
25
- if args.empty?
26
- @connection.do(sql)
27
- else
28
- @connection.do(sql, *args)
29
- end
30
- end
31
-
32
- def exec_query(sql, formatter = {}, args = [], name = "SQL")
33
- binds, args = extract_binds_from_sql(sql, args)
34
- log(sql, name, binds, args) do
35
- begin
36
- if args.empty?
37
- stmt = @connection.run(sql)
38
- else
39
- stmt = @connection.run(sql, *args)
40
- end
41
- columns = stmt.columns.values.map { |col| col.name.downcase }
42
- rows = stmt.to_a
43
- ensure
44
- stmt.drop unless stmt.nil?
45
- end
46
- DB2Query::Result.new(columns, rows, formatter)
47
- end
48
- end
49
-
50
- private
51
- def key_finder_regex(k)
52
- /#{k} =\\? | #{k}=\\? | #{k}= \\? /i
53
- end
54
-
55
- def extract_binds_from_sql(sql, args)
56
- question_mark_positions = sql.enum_for(:scan, /\?/i).map { Regexp.last_match.begin(0) }
57
- args = args.first.is_a?(Hash) ? args.first : args.is_a?(Array) ? args : [args]
58
- given, expected = args.length, question_mark_positions.length
59
-
60
- if given != expected
61
- raise ArgumentError, "wrong number of arguments (given #{given}, expected #{expected})"
62
- end
63
-
64
- if args.is_a?(Hash)
65
- binds = args.map do |key, value|
66
- position = sql.enum_for(:scan, key_finder_regex(key)).map { Regexp.last_match.begin(0) }
67
- if position.empty?
68
- raise ArgumentError, "Column name: `#{key}` not found inside sql statement."
69
- elsif position.length > 1
70
- raise ArgumentError, "Can't handle such this kind of sql. Please refactor your sql."
71
- else
72
- index = position[0]
73
- end
74
-
75
- OpenStruct.new({ name: key.to_s, value: value, index: index })
76
- end
77
- binds = binds.sort_by { |bind| bind.index }
78
- [binds.map { |bind| [bind, bind.value] }, binds.map { |bind| bind.value }]
79
- elsif question_mark_positions.length == 1 && args.length == 1
80
- column = sql[/(.*?) = \?|(.*?) =\?|(.*?)= \?|(.*?)=\?/m, 1].split.last.downcase
81
- bind = OpenStruct.new({ name: column, value: args })
82
- [[[bind, bind.value]], bind.value]
83
- else
84
- [args.map { |arg| [nil, arg] }, args]
85
- end
86
- end
87
- end
88
- end
@@ -1,38 +0,0 @@
1
- # frozen_String_literal: true
2
-
3
- module DB2Query
4
- CONNECTION_TYPES = %i[dsn conn_string].freeze
5
-
6
- class ODBCConnector
7
- attr_reader :connector, :conn_type, :conn_config
8
-
9
- def initialize(type, config)
10
- @conn_type, @conn_config = type, config.transform_keys(&:to_sym)
11
- @connector = DB2Query.const_get("#{conn_type.to_s.camelize}Connector").new
12
- end
13
-
14
- def connect
15
- connector.connect(conn_config)
16
- end
17
- end
18
-
19
- class DsnConnector
20
- def connect(config)
21
- ::ODBC.connect(config[:dsn], config[:uid], config[:pwd])
22
- rescue ::ODBC::Error => e
23
- raise ArgumentError, "Unable to activate ODBC DSN connection #{e}"
24
- end
25
- end
26
-
27
- class ConnStringConnector
28
- def connect(config)
29
- driver = ::ODBC::Driver.new.tap do |d|
30
- d.attrs = config[:conn_string].transform_keys(&:to_s)
31
- d.name = "odbc"
32
- end
33
- ::ODBC::Database.new.drvconnect(driver)
34
- rescue ::ODBC::Error => e
35
- raise ArgumentError, "Unable to activate ODBC Conn String connection #{e}"
36
- end
37
- end
38
- end