redshift-client 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -8
- data/lib/redshift/client.rb +1 -0
- data/lib/redshift/client/configuration.rb +4 -2
- data/lib/redshift/client/connection.rb +23 -0
- data/lib/redshift/client/connection_handling.rb +28 -35
- data/lib/redshift/client/version.rb +1 -1
- data/redshift-client.gemspec +1 -1
- data/spec/redshift/client_spec.rb +2 -2
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 07134341d34da433891d4a6fabe9c092d09973a1
|
4
|
+
data.tar.gz: 1fffad0d484638978060447997fdc977d0cb2aa3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c4f6be049d2d7d848367c892624fa67dc64e63f725186a9005dea738d63d7ca5ab2d4f73957ee3f13ea24654b4bfafc9368414b70900c6d86fd11593e8ca983
|
7
|
+
data.tar.gz: 1266a3424362a90776b32832de19008f8be05b161bd5205e43ab28f4adab02dd0599e527d1c09d476c98b2f743700bbe754b9ca1891eb83d65066cecb7c0a117
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Redshift::Client [![Build Status](https://travis-ci.org/dakatsuka/redshift-client.svg)](https://travis-ci.org/dakatsuka/redshift-client) [![Gem Version](https://badge.fury.io/rb/redshift-client.svg)](https://badge.fury.io/rb/redshift-client)
|
2
2
|
|
3
|
-
|
3
|
+
This gem provides a way to connect to AWS Redshift using the [ruby-pg](https://github.com/ged/ruby-pg).
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -12,7 +12,7 @@ gem 'redshift-client'
|
|
12
12
|
|
13
13
|
And then execute:
|
14
14
|
|
15
|
-
$ bundle
|
15
|
+
$ bundle install
|
16
16
|
|
17
17
|
Or install it yourself as:
|
18
18
|
|
@@ -20,19 +20,27 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
|
23
|
+
### Using with your application
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'rubygems'
|
27
|
+
require 'redshift/client'
|
28
|
+
```
|
29
|
+
|
30
|
+
After that you configure the url of Redshift:
|
24
31
|
|
25
32
|
```
|
26
33
|
$ export REDSHIFT_URL="redshift://user:password@*****.region.redshift.amazonaws.com:5439/dbname"
|
27
34
|
```
|
28
35
|
|
29
|
-
|
30
|
-
require 'redshift/client'
|
36
|
+
Please refer to the following code:
|
31
37
|
|
38
|
+
```ruby
|
32
39
|
Redshift::Client.establish_connection
|
33
|
-
Redshift::Client.connection # instance of PG::Connection
|
34
40
|
|
35
|
-
Redshift::Client.connection
|
41
|
+
Redshift::Client.connection
|
42
|
+
Redshift::Client.connection.exec("SELECT GETDATE()").first # delegate to PG::Connection
|
43
|
+
# => {"getdate"=>"2015-10-08 05:17:40"}
|
36
44
|
```
|
37
45
|
|
38
46
|
## Development
|
@@ -43,7 +51,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
43
51
|
|
44
52
|
## Contributing
|
45
53
|
|
46
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
54
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/dakatsuka/redshift-client. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
47
55
|
|
48
56
|
|
49
57
|
## License
|
data/lib/redshift/client.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
1
3
|
module Redshift
|
2
4
|
module Client
|
3
5
|
class Configuration
|
4
6
|
attr_reader :host, :port, :user, :password, :dbname
|
5
7
|
|
6
|
-
def self.
|
8
|
+
def self.resolve(config = {})
|
7
9
|
if config.empty?
|
8
10
|
url = URI.parse(ENV["REDSHIFT_URL"])
|
9
11
|
self.new(url.host, url.port, url.user, url.password, url.path[1..-1])
|
@@ -22,7 +24,7 @@ module Redshift
|
|
22
24
|
@dbname = dbname
|
23
25
|
end
|
24
26
|
|
25
|
-
def
|
27
|
+
def params
|
26
28
|
{
|
27
29
|
host: @host,
|
28
30
|
port: @port,
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'pg'
|
2
|
+
require 'active_support/core_ext/module'
|
3
|
+
|
4
|
+
module Redshift
|
5
|
+
module Client
|
6
|
+
class Connection
|
7
|
+
attr_reader :original
|
8
|
+
|
9
|
+
def initialize(configuration)
|
10
|
+
@original = PG.connect(configuration.params)
|
11
|
+
end
|
12
|
+
|
13
|
+
delegate \
|
14
|
+
:exec,
|
15
|
+
:exec_params,
|
16
|
+
:escape,
|
17
|
+
:escape_string,
|
18
|
+
:escape_literal,
|
19
|
+
:close,
|
20
|
+
to: :original
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -1,61 +1,54 @@
|
|
1
|
-
require '
|
2
|
-
require 'uri'
|
1
|
+
require 'thread'
|
3
2
|
require 'active_support/lazy_load_hooks'
|
4
3
|
|
5
4
|
module Redshift
|
6
5
|
module Client
|
7
6
|
module ConnectionHandling
|
8
|
-
def establish_connection(config
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
def establish_connection(config= {})
|
8
|
+
clear_connection!
|
9
|
+
clear_thread!
|
10
|
+
|
11
|
+
current[:configuration] = Configuration.resolve(config)
|
12
12
|
end
|
13
13
|
|
14
|
-
def
|
15
|
-
if connected?
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
def connection
|
15
|
+
return current[:connection] if connected?
|
16
|
+
|
17
|
+
ActiveSupport.run_load_hooks :redshift_client_connection
|
18
|
+
|
19
|
+
check_established!
|
20
|
+
current[:connection] = Connection.new(current[:configuration])
|
21
|
+
current[:connection]
|
19
22
|
end
|
20
23
|
|
21
|
-
def
|
22
|
-
|
24
|
+
def disconnect
|
25
|
+
clear_connection!
|
23
26
|
end
|
24
27
|
|
25
28
|
def established?
|
26
|
-
!!
|
29
|
+
!! current[:configuration]
|
27
30
|
end
|
28
31
|
|
29
|
-
def
|
30
|
-
|
31
|
-
|
32
|
-
if connected?
|
33
|
-
thread[:connection]
|
34
|
-
else
|
35
|
-
connect!
|
36
|
-
end
|
32
|
+
def connected?
|
33
|
+
!! current[:connection]
|
37
34
|
end
|
38
35
|
|
39
36
|
private
|
40
|
-
def
|
41
|
-
|
42
|
-
end
|
43
|
-
|
44
|
-
def cleanup!
|
45
|
-
Thread.current[:redshift][:connection] = nil
|
37
|
+
def check_established!
|
38
|
+
raise ConnectionNotEstablished.new unless established?
|
46
39
|
end
|
47
40
|
|
48
|
-
def
|
49
|
-
|
41
|
+
def clear_connection!
|
42
|
+
current[:connection] && current[:connection].close
|
43
|
+
current[:connection] = nil
|
50
44
|
end
|
51
45
|
|
52
|
-
def
|
53
|
-
|
54
|
-
thread[:connection] = PG.connect(thread[:configurations].to_hash)
|
46
|
+
def clear_thread!
|
47
|
+
Thread.current[:redshift] = nil
|
55
48
|
end
|
56
49
|
|
57
|
-
def
|
58
|
-
|
50
|
+
def current
|
51
|
+
Thread.current[:redshift] ||= {}
|
59
52
|
end
|
60
53
|
end
|
61
54
|
end
|
data/redshift-client.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.email = ["d.akatsuka@gmail.com"]
|
10
10
|
|
11
11
|
spec.summary = %q{The ruby client for AWS Redshift.}
|
12
|
-
spec.description = %q{
|
12
|
+
spec.description = %q{This gem provides a way to connect to AWS Redshift using the ruby-pg.}
|
13
13
|
spec.homepage = "https://github.com/dakatsuka/redshift-client"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
@@ -59,8 +59,8 @@ describe Redshift::Client do
|
|
59
59
|
Redshift::Client.establish_connection
|
60
60
|
end
|
61
61
|
|
62
|
-
it "returns
|
63
|
-
expect(Redshift::Client.connection).to be_instance_of
|
62
|
+
it "returns an instance of connection" do
|
63
|
+
expect(Redshift::Client.connection).to be_instance_of Redshift::Client::Connection
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redshift-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dai Akatsuka
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pg
|
@@ -80,7 +80,7 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
-
description:
|
83
|
+
description: This gem provides a way to connect to AWS Redshift using the ruby-pg.
|
84
84
|
email:
|
85
85
|
- d.akatsuka@gmail.com
|
86
86
|
executables: []
|
@@ -98,6 +98,7 @@ files:
|
|
98
98
|
- bin/setup
|
99
99
|
- lib/redshift/client.rb
|
100
100
|
- lib/redshift/client/configuration.rb
|
101
|
+
- lib/redshift/client/connection.rb
|
101
102
|
- lib/redshift/client/connection_handling.rb
|
102
103
|
- lib/redshift/client/errors.rb
|
103
104
|
- lib/redshift/client/version.rb
|