jdbc-helper 0.3.0 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,19 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
# Junegunn Choi (junegunn.c@gmail.com)
|
3
3
|
|
4
|
+
module JDBCHelper
|
5
|
+
class Connector
|
6
|
+
private
|
7
|
+
def self.ensure_close conn
|
8
|
+
begin
|
9
|
+
yield conn
|
10
|
+
ensure
|
11
|
+
conn.close rescue nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end#Connector
|
15
|
+
end#JDBCHelper
|
16
|
+
|
4
17
|
require 'jdbc-helper/connector/oracle_connector'
|
5
18
|
require 'jdbc-helper/connector/mysql_connector'
|
6
19
|
|
@@ -3,7 +3,7 @@
|
|
3
3
|
|
4
4
|
module JDBCHelper
|
5
5
|
# Shortcut connector for MySQL
|
6
|
-
class MySQLConnector
|
6
|
+
class MySQLConnector < Connector
|
7
7
|
include Constants
|
8
8
|
include Constants::Connector
|
9
9
|
|
@@ -16,19 +16,21 @@ class MySQLConnector
|
|
16
16
|
# @return [JDBCHelper::Connection]
|
17
17
|
def self.connect(host, user, password, db,
|
18
18
|
timeout = DEFAULT_LOGIN_TIMEOUT,
|
19
|
-
extra_params = DEFAULT_PARAMETERS[:mysql])
|
19
|
+
extra_params = DEFAULT_PARAMETERS[:mysql], &block)
|
20
20
|
|
21
21
|
if extra_params && extra_params.is_a?(Hash) == false
|
22
22
|
raise ArgumentError.new('extra_params must be a hash')
|
23
23
|
end
|
24
24
|
|
25
|
-
Connection.new(
|
25
|
+
conn = Connection.new(
|
26
26
|
(extra_params || {}).merge(
|
27
27
|
:driver => JDBC_DRIVER[:mysql],
|
28
28
|
:url => "jdbc:mysql://#{host}/#{db}",
|
29
29
|
:user => user,
|
30
30
|
:password => password,
|
31
31
|
:timeout => timeout))
|
32
|
+
|
33
|
+
block_given? ? ensure_close(conn, &block) : conn
|
32
34
|
end
|
33
35
|
end#MySQLConnector
|
34
36
|
end#JDBCHelper
|
@@ -3,7 +3,7 @@
|
|
3
3
|
|
4
4
|
module JDBCHelper
|
5
5
|
# Shortcut connector for Oracle
|
6
|
-
|
6
|
+
class OracleConnector < Connector
|
7
7
|
include Constants
|
8
8
|
include Constants::Connector
|
9
9
|
|
@@ -13,13 +13,15 @@ module OracleConnector
|
|
13
13
|
# @param [String] service_name
|
14
14
|
# @param [Fixnum] timeout
|
15
15
|
# @return [JDBCHelper::Connection]
|
16
|
-
def self.connect(host, user, password, service_name, timeout = DEFAULT_LOGIN_TIMEOUT)
|
17
|
-
Connection.new(
|
16
|
+
def self.connect(host, user, password, service_name, timeout = DEFAULT_LOGIN_TIMEOUT, &block)
|
17
|
+
conn = Connection.new(
|
18
18
|
:driver => JDBC_DRIVER[:oracle],
|
19
19
|
:url => "jdbc:oracle:thin:@#{host}/#{service_name}",
|
20
20
|
:user => user,
|
21
21
|
:password => password,
|
22
22
|
:timeout => timeout)
|
23
|
+
|
24
|
+
block_given? ? ensure_close(conn, &block) : conn
|
23
25
|
end
|
24
26
|
|
25
27
|
# @param [String] host
|
@@ -28,13 +30,15 @@ module OracleConnector
|
|
28
30
|
# @param [String] sid
|
29
31
|
# @param [Fixnum] timeout
|
30
32
|
# @return [JDBCHelper::Connection]
|
31
|
-
def self.connect_by_sid(host, user, password, sid, timeout = DEFAULT_LOGIN_TIMEOUT)
|
32
|
-
Connection.new(
|
33
|
+
def self.connect_by_sid(host, user, password, sid, timeout = DEFAULT_LOGIN_TIMEOUT, &block)
|
34
|
+
conn = Connection.new(
|
33
35
|
:driver => JDBC_DRIVER[:oracle],
|
34
36
|
:url => "jdbc:oracle:thin:@#{host}:#{sid}",
|
35
37
|
:user => user,
|
36
38
|
:password => password,
|
37
39
|
:timeout => timeout)
|
40
|
+
|
41
|
+
block_given? ? ensure_close(conn, &block) : conn
|
38
42
|
end
|
39
43
|
end#OracleConnector
|
40
44
|
end#JDBCHelper
|
data/test/database.yml
CHANGED
data/test/test_connectors.rb
CHANGED
@@ -19,6 +19,15 @@ class TestConnectors < Test::Unit::TestCase
|
|
19
19
|
assert conn.closed? == false
|
20
20
|
conn.close
|
21
21
|
assert conn.closed?
|
22
|
+
|
23
|
+
@conn = nil
|
24
|
+
ret = JDBCHelper::MySQLConnector.connect(host, conn_info['user'], conn_info['password'], db) do |conn|
|
25
|
+
assert conn.closed? == false
|
26
|
+
@conn = conn
|
27
|
+
1
|
28
|
+
end
|
29
|
+
assert @conn.closed?
|
30
|
+
assert_equal 1, ret
|
22
31
|
elsif conn_info['driver'] =~ /oracle/
|
23
32
|
host = conn_info['url'].match(%r{@(.*?)/})[1]
|
24
33
|
svc = conn_info['url'].match(%r{/([^/?]*?)(\?.*)?$})[1]
|
@@ -27,6 +36,15 @@ class TestConnectors < Test::Unit::TestCase
|
|
27
36
|
assert conn.closed? == false
|
28
37
|
conn.close
|
29
38
|
assert conn.closed?
|
39
|
+
|
40
|
+
@conn = nil
|
41
|
+
ret = JDBCHelper::OracleConnector.connect(host, conn_info['user'], conn_info['password'], svc) do |conn|
|
42
|
+
assert conn.closed? == false
|
43
|
+
@conn = conn
|
44
|
+
1
|
45
|
+
end
|
46
|
+
assert @conn.closed?
|
47
|
+
assert_equal 1, ret
|
30
48
|
end
|
31
49
|
end
|
32
50
|
end
|
data/test/test_performance.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: jdbc-helper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.3.
|
5
|
+
version: 0.3.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Junegunn Choi
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-05-
|
13
|
+
date: 2011-05-25 00:00:00 +09:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|