rsconn 0.0.1 → 0.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1c30c89eb0d8df0325b2e030f9456e007b73bd91
4
- data.tar.gz: f92be0b7307b051a9ae335b9b23d2d5428bd730a
3
+ metadata.gz: 4c73efb8028b4d0dff9f7eb0a69c9ebded1bf2da
4
+ data.tar.gz: c81d08bff44458589a05d2969b7125b4c3f3043a
5
5
  SHA512:
6
- metadata.gz: 974295630b65b75e56c3c46540fa4201b158dbd2b1abd1e0f78fecf0d45bd06e4adff49bb1c9ed636e00e875c9d51535cd821622028540e0d336fffa27cffa5c
7
- data.tar.gz: 17a74642754c11403981bb81105c1fa1a6877914fead77a2c0c71f39a3107cdff04cbd35d3997693cb340a4b0424fcd3fff0aa52c2d4f57989795f3f31ea7a70
6
+ metadata.gz: 0e6875b2bb4e679d072ea8c50daa57153468748b0c2399f80772146b4280e5d74c777c8ac46fdadeb579e2090659cdd14a130fd3e651338616c5e0b9b7ee9613
7
+ data.tar.gz: f243366883222b1b1afac47dbee1742e407ecc3964566b324d39f5040697f38a06b84e4877400e3f2a91bac92c71cd3049e1dd4efaa88f9094ed4be17b298df4
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # rsconn
2
2
 
3
- TODO: Write a gem description
3
+ A convenience wrapper to make interaction with a Redshift cluster easier.
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,7 +18,32 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ This is an example of connecting to and using a Redshift database using a JDBC URL.
22
+
23
+ require 'rsconn'
24
+
25
+ url = Rsconn::JdbcUrl.new(ENV['RSCONN_JDBC_URL'])
26
+
27
+ db_conn = Rsconn::Redshift.new(
28
+ url.host,
29
+ url.port,
30
+ url.database,
31
+ ENV['RSCONN_USER'],
32
+ ENV['RSCONN_PASSWORD'],
33
+ )
34
+
35
+ # For updates, inserts, deletes, etc., use #execute.
36
+ db_conn.execute('CREATE TABLE reg.students (id integer, name varchar(100))')
37
+ db_conn.execute("INSERT INTO reg.students VALUES (1, 'Dean')")
38
+
39
+ # Queries return an easy to use data structure - there's no need to manually
40
+ # create and iterate through cursors
41
+ rows = db_conn.query('SELECT * FROM reg.students WHERE id = 1')
42
+
43
+ puts "Use the column name: #{rows.first['name']}"
44
+ puts "Or use the column index: {rows.first[1]}"
45
+
46
+ # To run non-query SQL statements from a script, use #execute_script
22
47
 
23
48
  ## Contributing
24
49
 
@@ -27,3 +52,7 @@ TODO: Write usage instructions here
27
52
  3. Commit your changes (`git commit -am 'Add some feature'`)
28
53
  4. Push to the branch (`git push origin my-new-feature`)
29
54
  5. Create a new Pull Request
55
+
56
+ ### Running tests
57
+
58
+ The tests need to connect to a Redshift cluster. Set the environment variables `RSCONN_JDBC_URL`, `RSCONN_USER`, `RSCONN_PASSWORD` and make sure the cluster has a `test` schema.
data/lib/rsconn.rb CHANGED
@@ -5,7 +5,7 @@ module Rsconn
5
5
  class Redshift < Postgres
6
6
  attr_reader :query_group, :query_slot_count
7
7
 
8
- def initialize(jdbc_url, user, password, options={})
8
+ def initialize(host, port, database, user, password, options={})
9
9
  super
10
10
  @query_group = options.fetch(:query_group, nil)
11
11
  @query_slot_count = options.fetch(:query_slot_count, nil)
@@ -6,7 +6,7 @@ module Rsconn
6
6
  @url = url
7
7
  @host = @url.split('//').last.split(':').first
8
8
  @port = @url.split(':').last.split('/').first.to_i
9
- @database = @url.split('/').last
9
+ @database = @url.split('/').last.split('?').first
10
10
 
11
11
  db_type = @url.split(':')[1]
12
12
 
@@ -14,15 +14,17 @@ module Rsconn
14
14
  class Postgres
15
15
  attr_reader :conn
16
16
 
17
- def initialize(jdbc_url, user, password, options={})
18
- fail_if_invalid_connection_credentials(jdbc_url, user, password)
17
+ def initialize(host, port, database, user, password, options={})
18
+ fail_if_invalid_connection_credentials(host, port, database, user, password)
19
19
 
20
+ @host = host
21
+ @port = port
22
+ @database = database
23
+ @user = user
24
+ @password = password
20
25
  @abort_on_error = options.fetch(:abort_on_error, true)
21
26
  @max_retries = options.fetch(:max_retries, 3)
22
27
  @quiet = options.fetch(:quiet, false)
23
- @jdbc_url = JdbcUrl.new(jdbc_url)
24
- @user = user
25
- @password = password
26
28
  @error_occurred = false
27
29
  @retry_count = 0
28
30
 
@@ -94,18 +96,20 @@ module Rsconn
94
96
 
95
97
  private
96
98
 
97
- def fail_if_invalid_connection_credentials(jdbc_url, user, password)
98
- fail ArgumentError, 'jdbc_url needs to be a string' unless jdbc_url.is_a?(String)
99
+ def fail_if_invalid_connection_credentials(host, port, database, user, password)
100
+ fail ArgumentError, 'host needs to be a string' unless host.is_a?(String)
101
+ fail ArgumentError, 'database needs to be a string' unless database.is_a?(String)
99
102
  fail ArgumentError, 'user needs to be a string' unless user.is_a?(String)
100
103
  fail ArgumentError, 'password needs to be a string' unless password.is_a?(String)
104
+ fail ArgumentError, 'port needs to be a integer' unless port.to_s =~ /\A\d+\z/
101
105
  end
102
106
 
103
107
  def init_connection
104
108
  with_error_handling do
105
109
  @conn = PGconn.open(
106
- :host => @jdbc_url.host,
107
- :port => @jdbc_url.port,
108
- :dbname => @jdbc_url.database,
110
+ :host => @host,
111
+ :port => @port,
112
+ :dbname => @database,
109
113
  :user => @user,
110
114
  :password => @password,
111
115
  )
@@ -1,3 +1,3 @@
1
1
  module Rsconn
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
data/spec/dev_redshift.rb CHANGED
@@ -5,8 +5,17 @@ module Rsconn
5
5
  class DevRedshift < Redshift
6
6
 
7
7
  def initialize(options={})
8
+ unless ENV['RSCONN_JDBC_URL'] && ENV['RSCONN_USER'] && ENV['RSCONN_PASSWORD']
9
+ fail 'The environment variables RSCONN_JDBC_URL, RSCONN_USER, and ' +
10
+ 'RSCONN_PASSWORD must be set to run the tests'
11
+ end
12
+
13
+ url = JdbcUrl.new(ENV['RSCONN_JDBC_URL'])
14
+
8
15
  super(
9
- ENV['RSCONN_JDBC_URL'],
16
+ url.host,
17
+ url.port,
18
+ url.database,
10
19
  ENV['RSCONN_USER'],
11
20
  ENV['RSCONN_PASSWORD'],
12
21
  options
@@ -7,7 +7,8 @@ module Rsconn
7
7
  HOST = 'host.redshift.amazonaws.com'
8
8
  PORT = 5439
9
9
  DATABASE = 'dbname'
10
- JDBC_URL = "jdbc:postgresql://#{HOST}:#{PORT}/#{DATABASE}"
10
+ OPTIONS = 'tcpKeepAlive=true'
11
+ JDBC_URL = "jdbc:postgresql://#{HOST}:#{PORT}/#{DATABASE}?#{OPTIONS}"
11
12
  MYSQL_URL = "jdbc:mysql://#{HOST}:#{PORT}/#{DATABASE}"
12
13
 
13
14
  describe '#new' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rsconn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dean Morin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-13 00:00:00.000000000 Z
11
+ date: 2014-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg