db2_query 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +19 -9
- data/lib/db2_query.rb +1 -1
- data/lib/db2_query/connection.rb +2 -5
- data/lib/db2_query/core.rb +29 -9
- data/lib/db2_query/logger.rb +3 -3
- data/lib/db2_query/tasks/database.rake +3 -0
- data/lib/db2_query/tasks/initializer.rake +8 -8
- data/lib/db2_query/version.rb +1 -1
- metadata +9 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a62d60225cd34e421f6d6fd8becf1d45bb6e0ffb35db266bac881734a4b15340
|
4
|
+
data.tar.gz: 8b02f4869439de1dcdb14cfca0f731ee99ce143f504b6a56c7e04db74684da65
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a684c13ff35469b6f53c13b0bcd2c21c997b9b51abce517bc66ca920a4cc764ae885a1746b1e256d50945cc038d240739c8de72045b899af6e63421b8a520c24
|
7
|
+
data.tar.gz: a2698708b312200a55f93334c5f27b5345f9b0515a6ca8aee82926562388178588766bb708959178f26488774a3994a5e88f2181691e8604f9d7ac6f62a2dcfb
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/db2_query.svg)](https://badge.fury.io/rb/db2_query)
|
4
4
|
|
5
|
-
A Rails 5 & Rails 6 plugin for handling Db2 SQL database `SIUD` statement (`SELECT`, `
|
5
|
+
A Rails 5 & Rails 6 plugin for handling Db2 SQL database `SIUD` statement (`SELECT`, `INSERT`, `UPDATE`, `DELETE`) by using ODBC connection.
|
6
6
|
|
7
7
|
Note: Tested at Rails 5.2.6 and Rails 6.1.4
|
8
8
|
|
@@ -39,23 +39,28 @@ At version 0.3.0, `db2query.yml` only use DSN connection config:
|
|
39
39
|
```yml
|
40
40
|
development:
|
41
41
|
dsn: ARUNIT
|
42
|
+
idle: 5
|
42
43
|
pool: 5
|
43
44
|
timeout: 5
|
44
45
|
test:
|
45
46
|
dsn: ARUNIT
|
47
|
+
idle: 5
|
46
48
|
pool: 5
|
47
49
|
timeout: 5
|
48
50
|
production:
|
49
51
|
dsn: ARUNIT
|
52
|
+
idle: 5
|
50
53
|
pool: 5
|
51
54
|
timeout: 5
|
52
55
|
```
|
53
56
|
|
57
|
+
Note: `idle` is a `db_client` idle maximum limit value (in minutes) to avoid the client disconnected by the host server. Setting this value to zero will lead to an "ODBC driver Communication Link Failure. Comm rc 10054 . [CWBCO1047](https://www.ibm.com/support/pages/cwbco1047-any-function-uses-database-host-server)" error after a certain period of time.
|
58
|
+
|
54
59
|
Ensure that `unixodbc` have been installed and test your connection first by using `isql` commands.
|
55
60
|
|
56
61
|
Example:
|
57
62
|
|
58
|
-
|
63
|
+
ARUNIT DSN connection test
|
59
64
|
```bash
|
60
65
|
$ isql -v ARUNIT
|
61
66
|
+---------------------------------------+
|
@@ -82,7 +87,8 @@ The `query` method must have 2 inputs:
|
|
82
87
|
1. Method name
|
83
88
|
2. Body (can be an SQL statement or lamda).
|
84
89
|
|
85
|
-
The lambda is used to facilitate us in using `built-in methods` as shown at
|
90
|
+
The lambda is used to facilitate us in using `built-in methods` as shown at examples below:
|
91
|
+
|
86
92
|
Example 1.
|
87
93
|
```ruby
|
88
94
|
class User < Db2Query::Base
|
@@ -94,7 +100,7 @@ end
|
|
94
100
|
```bash
|
95
101
|
irb(main):004:0> User.find_by 10000
|
96
102
|
SQL (3.2ms) SELECT * FROM LIBTEST.USERS WHERE id = ? [["id", 10000]]
|
97
|
-
=> #<
|
103
|
+
=> #<DB2Query::Result @records=[#<Record id: 10000, first_name: "Strange", last_name: "Stephen", email: "strange@marvel.universe.com">]>
|
98
104
|
```
|
99
105
|
Example 2.
|
100
106
|
```ruby
|
@@ -107,7 +113,7 @@ end
|
|
107
113
|
```bash
|
108
114
|
irb(main):003:0> User.id_greater_than 10000
|
109
115
|
SQL (3.2ms) SELECT * FROM LIBTEST.USERS WHERE id > ? [["id", 1000]]
|
110
|
-
=> #<Db2Query::Result [#<Record id: 10000, first_name: "
|
116
|
+
=> #<Db2Query::Result [#<Record id: 10000, first_name: "Strange", last_name: "Stephen", email: "strange@marvel.universe.com">...">]>
|
111
117
|
```
|
112
118
|
Example 3.
|
113
119
|
```ruby
|
@@ -120,6 +126,9 @@ class User < Db2query::Base
|
|
120
126
|
end
|
121
127
|
```
|
122
128
|
```bash
|
129
|
+
irb(main):002:0> User.insert_record [77777, "Ancient", "One", "ancientone@marvel.universe.com"]
|
130
|
+
SQL (3651.2ms) SELECT * FROM NEW TABLE (INSERT INTO users (id, first_name, last_name, email) VALUES (?, ?, ?, ?)) [["id,", 77777], ["first_name,", "Ancient"], ["last_name,", "One"], ["email)", "ancientone@marvel.universe.com"]]
|
131
|
+
=> #<Db2Query::Result [#<Record id: 77777, first_name: "Ancient", last_name: "One", email: "ancientone@marvel.universe.com">]>
|
123
132
|
```
|
124
133
|
|
125
134
|
### 2. Plain/normal method
|
@@ -134,7 +143,7 @@ end
|
|
134
143
|
Then we can call it by using `find_by` class method.
|
135
144
|
```bash
|
136
145
|
irb(main):001:0> User.find_by 10000
|
137
|
-
SQL Load (3.28ms) SELECT * FROM LIBTEST.USERS WHERE id = ? [[
|
146
|
+
SQL Load (3.28ms) SELECT * FROM LIBTEST.USERS WHERE id = ? [["id", 10000]]
|
138
147
|
=> #<DB2Query::Result @records=[#<Record id: 10000, first_name: "Strange", last_name: "Stephen", email: "strange@marvel.universe.com">]>
|
139
148
|
```
|
140
149
|
Or with hash arguments input
|
@@ -154,11 +163,12 @@ SQL Load (3.28ms) SELECT * FROM LIBTEST.USERS WHERE first_name = ? AND last_nam
|
|
154
163
|
For a reusable sql, we can extend it by using a combination of `extention` and `sql_with_extention` methods, with an `@extention` pointer at SQL statement.
|
155
164
|
```ruby
|
156
165
|
class User < Db2Query::Base
|
157
|
-
|
158
|
-
|
166
|
+
# reusable SQL
|
167
|
+
_SQL = -> extention {
|
159
168
|
sql_with_extention("SELECT * FROM LIBTEST.USERS WHERE @extention", extention)
|
160
169
|
}
|
161
|
-
|
170
|
+
|
171
|
+
# implementation
|
162
172
|
query :by_email, _SQL.("$email = ?")
|
163
173
|
end
|
164
174
|
```
|
data/lib/db2_query.rb
CHANGED
data/lib/db2_query/connection.rb
CHANGED
@@ -1,8 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative "error"
|
4
|
-
require "connection_pool"
|
5
|
-
|
6
3
|
module Db2Query
|
7
4
|
class Bind < Struct.new(:name, :value, :index)
|
8
5
|
end
|
@@ -56,11 +53,11 @@ module Db2Query
|
|
56
53
|
binds, args = extract_binds_from_sql(sql, args)
|
57
54
|
sql = db2_spec_sql(sql)
|
58
55
|
log(sql, "SQL", binds, args) do
|
59
|
-
run_query(formatters, sql,
|
56
|
+
run_query(formatters, sql, binds, args)
|
60
57
|
end
|
61
58
|
end
|
62
59
|
|
63
|
-
def run_query(formatters, sql, args = []
|
60
|
+
def run_query(formatters, sql, binds, args = [])
|
64
61
|
pool do |odbc_conn|
|
65
62
|
begin
|
66
63
|
if args.empty?
|
data/lib/db2_query/core.rb
CHANGED
@@ -6,20 +6,40 @@ module Db2Query
|
|
6
6
|
|
7
7
|
delegate :run, :do, to: :client
|
8
8
|
|
9
|
-
def initialize(
|
10
|
-
@dsn = dsn
|
11
|
-
@
|
9
|
+
def initialize(config)
|
10
|
+
@dsn = config[:dsn]
|
11
|
+
@idle_time_limit = config[:idle] || 5
|
12
|
+
@client = new_db_client
|
13
|
+
@last_active = Time.now
|
12
14
|
end
|
13
15
|
|
14
|
-
def
|
16
|
+
def expire?
|
17
|
+
Time.now - @last_active > 60 * @idle_time_limit
|
18
|
+
end
|
19
|
+
|
20
|
+
def active?
|
21
|
+
@client.connected?
|
22
|
+
end
|
23
|
+
|
24
|
+
def connected_and_persist?
|
25
|
+
active? && !expire?
|
26
|
+
end
|
27
|
+
|
28
|
+
def disconnect!
|
29
|
+
@client.drop_all
|
30
|
+
@client.disconnect if active?
|
31
|
+
@client = nil
|
32
|
+
end
|
33
|
+
|
34
|
+
def new_db_client
|
15
35
|
ODBC.connect(dsn)
|
16
36
|
end
|
17
37
|
|
18
38
|
def client
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
@client
|
39
|
+
return @client if connected_and_persist?
|
40
|
+
disconnect!
|
41
|
+
@last_active = Time.now
|
42
|
+
@client = new_db_client
|
23
43
|
end
|
24
44
|
end
|
25
45
|
|
@@ -46,7 +66,7 @@ module Db2Query
|
|
46
66
|
def create_connection
|
47
67
|
@@mutex.synchronize do
|
48
68
|
return @@connection if @@connection
|
49
|
-
@@connection = Connection.new(config) { DbClient.new(config
|
69
|
+
@@connection = Connection.new(config) { DbClient.new(config) }
|
50
70
|
end
|
51
71
|
end
|
52
72
|
|
data/lib/db2_query/logger.rb
CHANGED
@@ -12,14 +12,14 @@ module Db2Query
|
|
12
12
|
exception
|
13
13
|
end
|
14
14
|
|
15
|
-
def log(sql, name = "SQL", binds = [],
|
15
|
+
def log(sql, name = "SQL", binds = [], args = [], &block)
|
16
16
|
@instrumenter.instrument(
|
17
17
|
"sql.active_record",
|
18
18
|
sql: sql,
|
19
19
|
name: name,
|
20
20
|
binds: binds,
|
21
|
-
type_casted_binds:
|
22
|
-
statement_name:
|
21
|
+
type_casted_binds: args,
|
22
|
+
statement_name: nil,
|
23
23
|
connection_id: object_id,
|
24
24
|
connection: self) do
|
25
25
|
@lock.synchronize do
|
@@ -7,16 +7,19 @@ DB2_QUERY_DATABASE_TEMPLATE ||= <<-EOF
|
|
7
7
|
|
8
8
|
development:
|
9
9
|
dsn: TODO
|
10
|
+
idle: 5
|
10
11
|
pool: 5
|
11
12
|
timeout: 5
|
12
13
|
|
13
14
|
test:
|
14
15
|
dsn: TODO
|
16
|
+
idle: 5
|
15
17
|
pool: 5
|
16
18
|
timeout: 5
|
17
19
|
|
18
20
|
production:
|
19
21
|
dsn: TODO
|
22
|
+
idle: 5
|
20
23
|
pool: 5
|
21
24
|
timeout: 5
|
22
25
|
EOF
|
@@ -12,15 +12,15 @@ Db2Query::Base.initiation do |base|
|
|
12
12
|
end
|
13
13
|
|
14
14
|
# Example
|
15
|
-
class FirstNameFormatter < Db2Query::AbstractFormatter
|
16
|
-
def format(value)
|
17
|
-
"Dr." + value
|
18
|
-
end
|
19
|
-
end
|
15
|
+
#class FirstNameFormatter < Db2Query::AbstractFormatter
|
16
|
+
# def format(value)
|
17
|
+
# "Dr." + value
|
18
|
+
# end
|
19
|
+
#end
|
20
20
|
|
21
|
-
Db2Query::Formatter.registration do |format|
|
22
|
-
format.register(:first_name_formatter, FirstNameFormatter)
|
23
|
-
end
|
21
|
+
#Db2Query::Formatter.registration do |format|
|
22
|
+
# format.register(:first_name_formatter, FirstNameFormatter)
|
23
|
+
#end
|
24
24
|
EOF
|
25
25
|
|
26
26
|
namespace :db2query do
|
data/lib/db2_query/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: db2_query
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- yohanes_l
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-07-
|
11
|
+
date: 2021-07-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubocop
|
@@ -84,42 +84,30 @@ dependencies:
|
|
84
84
|
name: rails
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- - "~>"
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: 6.1.3
|
90
87
|
- - ">="
|
91
88
|
- !ruby/object:Gem::Version
|
92
|
-
version:
|
89
|
+
version: '0'
|
93
90
|
type: :development
|
94
91
|
prerelease: false
|
95
92
|
version_requirements: !ruby/object:Gem::Requirement
|
96
93
|
requirements:
|
97
|
-
- - "~>"
|
98
|
-
- !ruby/object:Gem::Version
|
99
|
-
version: 6.1.3
|
100
94
|
- - ">="
|
101
95
|
- !ruby/object:Gem::Version
|
102
|
-
version:
|
96
|
+
version: '0'
|
103
97
|
- !ruby/object:Gem::Dependency
|
104
98
|
name: connection_pool
|
105
99
|
requirement: !ruby/object:Gem::Requirement
|
106
100
|
requirements:
|
107
|
-
- - "~>"
|
108
|
-
- !ruby/object:Gem::Version
|
109
|
-
version: '2.2'
|
110
101
|
- - ">="
|
111
102
|
- !ruby/object:Gem::Version
|
112
|
-
version:
|
103
|
+
version: '0'
|
113
104
|
type: :runtime
|
114
105
|
prerelease: false
|
115
106
|
version_requirements: !ruby/object:Gem::Requirement
|
116
107
|
requirements:
|
117
|
-
- - "~>"
|
118
|
-
- !ruby/object:Gem::Version
|
119
|
-
version: '2.2'
|
120
108
|
- - ">="
|
121
109
|
- !ruby/object:Gem::Version
|
122
|
-
version:
|
110
|
+
version: '0'
|
123
111
|
- !ruby/object:Gem::Dependency
|
124
112
|
name: ruby-odbc
|
125
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -134,7 +122,8 @@ dependencies:
|
|
134
122
|
- - ">="
|
135
123
|
- !ruby/object:Gem::Version
|
136
124
|
version: '0'
|
137
|
-
description: A Rails Db2
|
125
|
+
description: A Rails 5 & Rails 6 plugin for handling Db2 SQL SIUD statement (SELECT,
|
126
|
+
INSERT, UPDATE, DELETE) by using ODBC connection.
|
138
127
|
email:
|
139
128
|
- yohanes.lumentut@gmail.com
|
140
129
|
executables: []
|
@@ -183,5 +172,5 @@ requirements: []
|
|
183
172
|
rubygems_version: 3.0.3
|
184
173
|
signing_key:
|
185
174
|
specification_version: 4
|
186
|
-
summary: Db2 ODBC
|
175
|
+
summary: Rails Db2 ODBC plugin
|
187
176
|
test_files: []
|