connectator 0.0.3 → 0.0.4

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.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ connectator
2
+ ===========
3
+
4
+ Multi-Connection proxy for DBI
5
+
6
+ == Notes
7
+
8
+ *This gem is currently using the older DBI gem.*
9
+ https://github.com/erikh/ruby-dbi
10
+
11
+ This will at some point in the future change to use the new rdbi gem.
12
+ https://github.com/RDBI/rdbi
13
+
14
+ === Oracle
15
+
16
+ * Uses the ruby-oci8 gem directly
17
+ * Note that this gem does not directly require the ruby-oci8 gem. If you need
18
+ to use Oracle, include the ruby-oci8 gem in your project.
19
+
20
+ === Sql Server/Sybase
21
+
22
+ * Uses DBI:ODBC
23
+ * In order to use SQL Server/Sybase with Connectator, you will need to set up
24
+ Unix ODBC and FreeTDS. Unix ODBC will need to reference FreeTDS as one of its
25
+ drivers.
26
+
27
+ === MySQL
28
+
29
+ * Uses DBI:ODBC
30
+ * In order to use MySQL with Connectator, you will need to set up Unix ODBC and
31
+ the MySQL client. Unix ODBC will need to reference the MySQL driver as one of
32
+ its drivers.
33
+
34
+ === DB2
35
+
36
+ * Uses DBI:DB2
37
+ * In order to use DB2 with Connectator, you will need to set up Unix ODBC and
38
+ the DB2 client. Unix ODBC will need to reference the DB2 driver as one of its
39
+ drivers.
40
+
41
+ == UNIX ODBC
42
+
43
+ See example_odbcinst.ini for a sample of what the /etc/odbcinst.ini should look like.
@@ -0,0 +1,19 @@
1
+ [FreeTDS]
2
+ Description = TDS driver (Sybase/MS SQL)
3
+ Driver = /usr/lib/odbc/libtdsodbc.so
4
+ Setup = /usr/lib/odbc/libtdsS.so
5
+ CPTimeout =
6
+ CPReuse =
7
+ FileUsage = 1
8
+
9
+ [MySQL]
10
+ Description = MySQL
11
+ Driver = /usr/lib64/libmyodbc5.so
12
+ Setup = /usr/lib64/libodbcmyS.so
13
+ FileUsage = 1
14
+
15
+ [DB2]
16
+ Description = DB2 DRIVER (DB2)
17
+ Driver = /path/to/your/clidriver/lib/libdb2o.so
18
+ FileUsage = 1
19
+ DontDLClose = 1
data/lib/connectator.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'connectator/base/connection'
2
2
  require 'connectator/db2/connection'
3
+ require 'connectator/mysql/connection'
3
4
  require 'connectator/oracle/connection'
4
5
  require 'connectator/sql/connection'
5
6
  require 'connectator/sql/connection/server_instance_string'
@@ -19,16 +19,17 @@ module Connectator
19
19
  @connection_params ||= OpenStruct.new
20
20
  end
21
21
 
22
- def dbi
23
- @dbi ||= open
22
+ def system_connection
23
+ @system_connection ||= DBIProxy.new(open)
24
24
  end
25
25
 
26
- def close
27
- dbi.disconnect
26
+ # proxies all other method calls to the DBIProxy
27
+ def method_missing(method, *args, &blk)
28
+ system_connection.send(method, *args, &blk)
28
29
  end
29
30
 
30
31
  def valid?
31
- ping? && valid_dbi?
32
+ ping? && valid_system_connection?
32
33
  end
33
34
 
34
35
  def ping?
@@ -40,18 +41,13 @@ module Connectator
40
41
  end
41
42
  end
42
43
 
43
- def valid_dbi?
44
- dbi
44
+ def valid_system_connection?
45
+ system_connection.connection
45
46
  true
46
- rescue DBI::DatabaseError => e
47
+ rescue => e
47
48
  @connection_error = e.message
48
49
  false
49
50
  end
50
-
51
- def method_missing(method, *args, &blk)
52
- puts method
53
- dbi.send(method, *args, &blk)
54
- end
55
51
 
56
52
  def connection_string
57
53
  raise 'Abstract Method'
@@ -0,0 +1,33 @@
1
+ module Connectator
2
+ module Base
3
+ class DBIProxy
4
+
5
+ def initialize(connection)
6
+ @connection = connection
7
+ end
8
+
9
+ def connection
10
+ @connection
11
+ end
12
+
13
+ def close
14
+ connection.disconnect
15
+ end
16
+
17
+ # Do not use this method on queries that return lots of data
18
+ # because it would use a lot of memory.
19
+ def select_all_to_hash(query)
20
+ result = []
21
+ sth = connection.execute(query)
22
+ sth.fetch_hash do |row|
23
+ result << row
24
+ end
25
+ result
26
+ end
27
+
28
+ def method_missing(method, *args, &blk)
29
+ connection.send(method, *args, &blk)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,32 @@
1
+ module Connectator
2
+ module Mysql
3
+ class Connection < Base::Connection
4
+
5
+ def initialize(opts = {})
6
+ connection_params.driver = 'MySQL'
7
+ connection_params.option = opts[:option] || '3'
8
+ connection_params.port = opts[:port] || '3306'
9
+ connection_params.database = opts[:database] || ''
10
+ super(opts)
11
+ end
12
+
13
+ def connection_string
14
+ "DBI:ODBC:#{connection_params_list}"
15
+ end
16
+
17
+ private
18
+
19
+ def connection_params_hash
20
+ {
21
+ "DRIVER" => connection_params.driver,
22
+ "Server" => connection_params.server,
23
+ "Port" => connection_params.port,
24
+ "Database" => connection_params.database,
25
+ "User" => connection_params.username,
26
+ "Password" => connection_params.password,
27
+ "Option" => connection_params.option
28
+ }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -3,9 +3,9 @@ module Connectator
3
3
  class Connection < Base::Connection
4
4
 
5
5
  def initialize(opts = {})
6
- connection_params.tds_version = '8.0'
7
- connection_params.database = 'master'
8
6
  connection_params.driver = 'FreeTDS'
7
+ connection_params.tds_version = opts[:tds_version] || '8.0'
8
+ connection_params.database = opts[:database] || 'master'
9
9
  super(opts)
10
10
  end
11
11
 
@@ -3,9 +3,9 @@ module Connectator
3
3
  class Connection < Base::Connection
4
4
 
5
5
  def initialize(opts = {})
6
- connection_params.tds_version = '5.0'
7
- connection_params.database = 'master'
8
6
  connection_params.driver = 'FreeTDS'
7
+ connection_params.tds_version = opts[:tds_version] || '5.0'
8
+ connection_params.database = opts[:database] || 'master'
9
9
  super(opts)
10
10
  end
11
11
 
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe Connectator::Base::Connection do
4
+
5
+ describe "initalized with no params" do
6
+ Then { lambda { Connectator::Sql::Connection.new() }.should raise_error /Connection Options are required/ }
7
+ end
8
+
9
+ describe "initalized with basic params" do
10
+ Given(:connection) {
11
+ Connectator::Base::Connection.new(:server => 'Server',
12
+ :port => 'Port',
13
+ :username => 'User',
14
+ :password => 'Pass',
15
+ :instance => 'Instance')
16
+ }
17
+
18
+ Then { connection.connection_params.server.should == 'Server' }
19
+ Then { connection.connection_params.port.should == 'Port' }
20
+ Then { connection.connection_params.username.should == 'User' }
21
+ Then { connection.connection_params.password.should == 'Pass' }
22
+ Then { connection.connection_params.instance.should == 'Instance' }
23
+
24
+ Then { lambda { connection.connection_string }.should raise_error /Abstract Method/ }
25
+ Then { lambda { connection.send :connection_params_hash }.should raise_error /Abstract Method/ }
26
+ end
27
+
28
+ describe "initalized with a valid server" do
29
+ Given(:connection) {
30
+ Connectator::Base::Connection.new(:server => 'localhost')
31
+ }
32
+ Then { connection.connection_params.server.should == 'localhost' }
33
+ Then { connection.ping?.should be_true }
34
+
35
+ describe "initalized with a valid system connection" do
36
+ Given { connection.stub(:valid_system_connection?).and_return(true) }
37
+ Then { connection.valid?.should be_true }
38
+ end
39
+ end
40
+
41
+ describe "initalized with a valid dbi connection" do
42
+ Given(:connection) {
43
+ Connectator::Base::Connection.new(:server => 'localhost')
44
+ }
45
+ Then { connection.connection_params.server.should == 'localhost' }
46
+ Then { connection.ping?.should be_true }
47
+ end
48
+
49
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Connectator::DB2::Connection do
4
+
5
+ describe "initalized with no params" do
6
+ Then { lambda { Connectator::DB2::Connection.new() }.should raise_error /Connection Options are required/ }
7
+ end
8
+
9
+ describe "initalized with basic params" do
10
+ Given(:connection) {
11
+ Connectator::DB2::Connection.new(:server => 'Server',
12
+ :port => 'Port',
13
+ :username => 'User',
14
+ :password => 'Pass',
15
+ :instance => 'Instance',
16
+ :database => 'ABCDEFG')
17
+ }
18
+
19
+ Then { connection.connection_params.server.should == 'Server' }
20
+ Then { connection.connection_params.port.should == 'Port' }
21
+ Then { connection.connection_params.username.should == 'User' }
22
+ Then { connection.connection_params.password.should == 'Pass' }
23
+ Then { connection.connection_params.instance.should == 'Instance' }
24
+ Then { connection.connection_params.protocol.should == 'TCPIP' }
25
+ Then { connection.connection_params.driver.should == 'DB2' }
26
+ Then { connection.connection_params.database.should == 'ABCDEFG' }
27
+
28
+ Then { connection.connection_string.should ==
29
+ 'DBI:ODBC:DRIVER=DB2;PROTOCOL=TCPIP;HOSTNAME=Server;Port=Port;DATABASE=ABCDEFG;UID=User;PWD=Pass' }
30
+ end
31
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe Connectator::Mysql::Connection do
4
+
5
+ describe "initalized with no params" do
6
+ Then { lambda { Connectator::Mysql::Connection.new() }.should raise_error /Connection Options are required/ }
7
+ end
8
+
9
+ describe "initalized with basic params" do
10
+ Given(:connection) {
11
+ Connectator::Mysql::Connection.new(:server => 'Server',
12
+ :port => 'Port',
13
+ :username => 'User',
14
+ :password => 'Pass',
15
+ :instance => 'Instance')
16
+ }
17
+
18
+ Then { connection.connection_params.server.should == 'Server' }
19
+ Then { connection.connection_params.port.should == 'Port' }
20
+ Then { connection.connection_params.username.should == 'User' }
21
+ Then { connection.connection_params.password.should == 'Pass' }
22
+ Then { connection.connection_params.instance.should == 'Instance' }
23
+ Then { connection.connection_params.database.should == '' }
24
+ Then { connection.connection_params.option.should == '3' }
25
+
26
+ Then { connection.connection_string.should ==
27
+ "DBI:ODBC:DRIVER=MySQL;Server=Server;Port=Port;Database=;User=User;Password=Pass;Option=3" }
28
+
29
+ Then { connection.send(:connection_params_hash).should ==
30
+ {
31
+ "DRIVER" => 'MySQL',
32
+ "Server" => 'Server',
33
+ "Port" => 'Port',
34
+ "Database" => '',
35
+ "User" => 'User',
36
+ "Password" => 'Pass',
37
+ "Option" => '3'
38
+ }
39
+ }
40
+ end
41
+ end
42
+
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe Connectator::Oracle::Connection do
4
+
5
+ describe "initalized with no params" do
6
+ Then { lambda { Connectator::Oracle::Connection.new() }.should raise_error /Connection Options are required/ }
7
+ end
8
+
9
+ describe "initalized with basic params" do
10
+ Given(:connection) {
11
+ Connectator::Oracle::Connection.new(:server => 'Server',
12
+ :port => 'Port',
13
+ :username => 'User',
14
+ :password => 'Pass',
15
+ :instance => 'Instance')
16
+ }
17
+
18
+ Then { connection.connection_params.server.should == 'Server' }
19
+ Then { connection.connection_params.port.should == 'Port' }
20
+ Then { connection.connection_params.username.should == 'User' }
21
+ Then { connection.connection_params.password.should == 'Pass' }
22
+ Then { connection.connection_params.instance.should == 'Instance' }
23
+
24
+ Then { connection.connection_string.should == 'DBI:OCI8://Server:Port/Instance' }
25
+ end
26
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Connectator::Pinger do
4
+
5
+ Then { Connectator::Pinger.ping?('localhost').should be_true }
6
+ Then { Connectator::Pinger.ping?('iamnotahostname').should be_false }
7
+
8
+ describe "worse than the allowable timeout" do
9
+ Given { Connectator::Pinger.stub(:system_ping_command).and_return("sleep 1.000001") }
10
+ Then { Connectator::Pinger.ping?('localhost', 1).should be_false }
11
+ end
12
+
13
+ describe "better than the allowable timeout" do
14
+ Given { Connectator::Pinger.stub(:system_ping_command).and_return("sleep 0.01") }
15
+ Then { Connectator::Pinger.ping?('localhost', 1).should be_true }
16
+ end
17
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe Connectator::Sql::Connection do
4
+
5
+ describe "initalized with no params" do
6
+ Then { lambda { Connectator::Sql::Connection.new() }.should raise_error /Connection Options are required/ }
7
+ end
8
+
9
+ describe "initalized with basic params" do
10
+ Given(:connection) {
11
+ Connectator::Sql::Connection.new(:server => 'Server',
12
+ :port => 'Port',
13
+ :username => 'User',
14
+ :password => 'Pass',
15
+ :instance => 'Instance')
16
+ }
17
+
18
+ Then { connection.connection_params.server.should == 'Server' }
19
+ Then { connection.connection_params.port.should == 'Port' }
20
+ Then { connection.connection_params.username.should == 'User' }
21
+ Then { connection.connection_params.password.should == 'Pass' }
22
+ Then { connection.connection_params.instance.should == 'Instance' }
23
+ Then { connection.connection_params.tds_version.should == '8.0' }
24
+ Then { connection.connection_params.database.should == 'master' }
25
+
26
+ Then { connection.connection_string.should ==
27
+ "DBI:ODBC:DRIVER=FreeTDS;TDS_Version=8.0;SERVER=Server;Port=Port;DATABASE=master;UID=User;PWD=Pass" }
28
+ Then { connection.send(:connection_params_hash).should ==
29
+ {
30
+ "DRIVER" => 'FreeTDS',
31
+ "TDS_Version" => '8.0',
32
+ "SERVER" => 'Server',
33
+ "Port" => 'Port',
34
+ "DATABASE" => 'master',
35
+ "UID" => 'User',
36
+ "PWD" => 'Pass'
37
+ }
38
+ }
39
+ end
40
+
41
+ describe "with no instance" do
42
+ Given(:connection) { Connectator::Sql::Connection.new(:server => 'Server',
43
+ :instance => nil) }
44
+ Then { connection.send(:server_instance_string).should == 'Server' }
45
+ end
46
+
47
+ describe "with an instance of format X\Y" do
48
+ Given(:connection) { Connectator::Sql::Connection.new(:server => 'Server',
49
+ :instance => 'Host\Instance') }
50
+ Then { connection.send(:server_instance_string).should == 'Server\Instance' }
51
+ end
52
+
53
+ describe "with an instance of format X\Y$Z" do
54
+ Given(:connection) { Connectator::Sql::Connection.new(:server => 'Server',
55
+ :instance => 'Host\Instance$Foo') }
56
+ Then { connection.send(:server_instance_string).should == 'Server\Instance$Foo' }
57
+ end
58
+
59
+ describe "with an instance of format X\Y_Z" do
60
+ Given(:connection) { Connectator::Sql::Connection.new(:server => 'Server',
61
+ :instance => 'Host\Instance_Foo') }
62
+ Then { connection.send(:server_instance_string).should == 'Server\Instance_Foo' }
63
+ end
64
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe Connectator::Sybase::Connection do
4
+
5
+ describe "initalized with no params" do
6
+ Then { lambda { Connectator::Sybase::Connection.new() }.should raise_error /Connection Options are required/ }
7
+ end
8
+
9
+ describe "initalized with basic params" do
10
+ Given(:connection) {
11
+ Connectator::Sybase::Connection.new(:server => 'Server',
12
+ :port => 'Port',
13
+ :username => 'User',
14
+ :password => 'Pass',
15
+ :instance => 'Instance')
16
+ }
17
+
18
+ Then { connection.connection_params.server.should == 'Server' }
19
+ Then { connection.connection_params.port.should == 'Port' }
20
+ Then { connection.connection_params.username.should == 'User' }
21
+ Then { connection.connection_params.password.should == 'Pass' }
22
+ Then { connection.connection_params.instance.should == 'Instance' }
23
+ Then { connection.connection_params.tds_version.should == '5.0' }
24
+ Then { connection.connection_params.database.should == 'master' }
25
+
26
+ Then { connection.connection_string.should ==
27
+ "DBI:ODBC:DRIVER=FreeTDS;TDS_Version=5.0;SERVER=Server;Port=Port;DATABASE=master;UID=User;PWD=Pass" }
28
+
29
+ Then { connection.send(:connection_params_hash).should ==
30
+ {
31
+ "DRIVER" => 'FreeTDS',
32
+ "TDS_Version" => '5.0',
33
+ "SERVER" => 'Server',
34
+ "Port" => 'Port',
35
+ "DATABASE" => 'master',
36
+ "UID" => 'User',
37
+ "PWD" => 'Pass'
38
+ }
39
+ }
40
+ end
41
+ end
@@ -0,0 +1,4 @@
1
+ require 'rspec'
2
+ require 'rspec/given'
3
+
4
+ require 'connectator'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: connectator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-14 00:00:00.000000000 Z
12
+ date: 2013-02-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: dbi
@@ -118,12 +118,24 @@ files:
118
118
  - lib/connectator/factory.rb
119
119
  - lib/connectator/oracle/connection.rb
120
120
  - lib/connectator/base/connection.rb
121
+ - lib/connectator/base/dbi_proxy.rb
121
122
  - lib/connectator/pinger.rb
122
123
  - lib/connectator/db2/connection.rb
123
124
  - lib/connectator/sybase/connection.rb
125
+ - lib/connectator/mysql/connection.rb
124
126
  - lib/connectator/sql/connection.rb
125
127
  - lib/connectator/sql/connection/server_instance_string.rb
126
- homepage: http://rubygems.org/gems/connectator
128
+ - spec/spec_helper.rb
129
+ - spec/connectator/oracle/connection_spec.rb
130
+ - spec/connectator/pinger_spec.rb
131
+ - spec/connectator/base/connection_spec.rb
132
+ - spec/connectator/db2/connection_spec.rb
133
+ - spec/connectator/sybase/connection_spec.rb
134
+ - spec/connectator/mysql/connection_spec.rb
135
+ - spec/connectator/sql/connection_spec.rb
136
+ - README.md
137
+ - example_odbcinst.ini
138
+ homepage: https://github.com/jamin4jc/connectator
127
139
  licenses: []
128
140
  post_install_message:
129
141
  rdoc_options: []
@@ -137,7 +149,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
137
149
  version: '0'
138
150
  segments:
139
151
  - 0
140
- hash: 2689105201860434706
152
+ hash: 4345797816152053971
141
153
  required_rubygems_version: !ruby/object:Gem::Requirement
142
154
  none: false
143
155
  requirements: