Class: GoodData::CloudResources::PostgresClient
- Inherits:
-
CloudResourceClient
- Object
- CloudResourceClient
- GoodData::CloudResources::PostgresClient
- Defined in:
- lib/gooddata/cloud_resources/postgresql/postgresql_client.rb
Constant Summary collapse
- JDBC_POSTGRES_PATTERN =
%r{jdbc:postgresql:\/\/([^:^\/]+)(:([0-9]+))?(\/)?}
- POSTGRES_DEFAULT_PORT =
5432
- JDBC_POSTGRES_PROTOCOL =
'jdbc:postgresql://'
- SSL_JAVA_FACTORY =
'&sslfactory=org.postgresql.ssl.DefaultJavaSSLFactory'
- VERIFY_FULL =
'verify-full'
- PREFER =
'prefer'
- REQUIRE =
'require'
- POSTGRES_SET_SCHEMA_COMMAND =
"set search_path to"
- POSTGRES_FETCH_SIZE =
1000
Class Method Summary collapse
Instance Method Summary collapse
- #build_url(url) ⇒ Object
- #connect ⇒ Object
-
#initialize(options = {}) ⇒ PostgresClient
constructor
A new instance of PostgresClient.
- #realize_query(query, _params) ⇒ Object
Methods inherited from CloudResourceClient
Constructor Details
#initialize(options = {}) ⇒ PostgresClient
Returns a new instance of PostgresClient.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/gooddata/cloud_resources/postgresql/postgresql_client.rb', line 37 def initialize( = {}) raise("Data Source needs a client to Postgres to be able to query the storage but 'postgresql_client' is empty.") unless ['postgresql_client'] if ['postgresql_client']['connection'].is_a?(Hash) @database = ['postgresql_client']['connection']['database'] @schema = ['postgresql_client']['connection']['schema'] || 'public' @authentication = ['postgresql_client']['connection']['authentication'] @ssl_mode = ['postgresql_client']['connection']['sslMode'] raise "SSL Mode should be prefer, require and verify-full" unless @ssl_mode == 'prefer' || @ssl_mode == 'require' || @ssl_mode == 'verify-full' @url = build_url(['postgresql_client']['connection']['url']) else raise('Missing connection info for Postgres client') end Java.org.postgresql.Driver end |
Class Method Details
.accept?(type) ⇒ Boolean
32 33 34 |
# File 'lib/gooddata/cloud_resources/postgresql/postgresql_client.rb', line 32 def accept?(type) type == 'postgresql' end |
Instance Method Details
#build_url(url) ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/gooddata/cloud_resources/postgresql/postgresql_client.rb', line 95 def build_url(url) matches = url.scan(JDBC_POSTGRES_PATTERN) raise 'Cannot reach the url' unless matches host = matches[0][0] port = matches[0][2]&.to_i || POSTGRES_DEFAULT_PORT "#{JDBC_POSTGRES_PROTOCOL}#{host}:#{port}/#{@database}?sslmode=#{@ssl_mode}#{VERIFY_FULL == @ssl_mode ? SSL_JAVA_FACTORY : ''}" end |
#connect ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/gooddata/cloud_resources/postgresql/postgresql_client.rb', line 81 def connect GoodData.logger.info "Setting up connection to Postgresql #{@url}" prop = java.util.Properties.new prop.setProperty('user', @authentication['basic']['userName']) prop.setProperty('password', @authentication['basic']['password']) prop.setProperty('schema', @schema) @connection = java.sql.DriverManager.getConnection(@url, prop) statement = @connection.create_statement statement.execute("#{POSTGRES_SET_SCHEMA_COMMAND} #{@schema}") @connection.set_auto_commit(false) end |
#realize_query(query, _params) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/gooddata/cloud_resources/postgresql/postgresql_client.rb', line 55 def realize_query(query, _params) GoodData.gd_logger.info("Realize SQL query: type=postgresql status=started") connect filename = "#{SecureRandom.urlsafe_base64(6)}_#{Time.now.to_i}.csv" measure = Benchmark.measure do statement = @connection.create_statement statement.set_fetch_size(POSTGRES_FETCH_SIZE) has_result = statement.execute(query) if has_result result = statement.get_result_set = result. col_count = .column_count CSV.open(filename, 'wb') do |csv| csv << Array(1..col_count).map { |i| .get_column_name(i) } # build the header csv << Array(1..col_count).map { |i| result.get_string(i)&.to_s } while result.next end end end GoodData.gd_logger.info("Realize SQL query: type=postgresql status=finished duration=#{measure.real}") filename ensure @connection&.close @connection = nil end |