class GoodData::CloudResources::MSSQLClient

Constants

LOGIN_TIME_OUT
MSSQL_DEFAULT_PORT
MSSQL_FETCH_SIZE
MSSQL_SEPARATOR_PARAM
MSSQL_URL_PATTERN
PREFER
REQUIRE
VERIFY_FULL

Public Class Methods

accept?(type) click to toggle source
# File lib/gooddata/cloud_resources/mssql/mssql_client.rb, line 31
def accept?(type)
  type == 'mssql'
end
new(options = {}) click to toggle source
# File lib/gooddata/cloud_resources/mssql/mssql_client.rb, line 36
def initialize(options = {})
  raise("Data Source needs a client to MSSQL to be able to query the storage but 'mssql_client' is empty.") unless options['mssql_client']

  connection = options['mssql_client']['connection']
  if connection.is_a?(Hash)
    @database = connection['database']
    @schema = connection['schema']
    @authentication = connection['authentication']
    @ssl_mode = connection['sslMode']
    @url = connection['url']

    validate
  else
    raise('Missing connection info for MSSQL client')
  end

  Java.com.microsoft.sqlserver.jdbc.SQLServerDriver
end

Public Instance Methods

build_connection_string() click to toggle source
# File lib/gooddata/cloud_resources/mssql/mssql_client.rb, line 109
def build_connection_string
  encrypt = @ssl_mode != PREFER
  trust_server_certificate = @ssl_mode == REQUIRE

  "#{@url};" \
    "database=#{@database};" \
    "encrypt=#{encrypt};" \
    "trustServerCertificate=#{trust_server_certificate};" \
    "loginTimeout=#{LOGIN_TIME_OUT};" \
    "#{'authentication=ActiveDirectoryPassword;' if @authentication['activeDirectoryPassword']}"
end
connect() click to toggle source
# File lib/gooddata/cloud_resources/mssql/mssql_client.rb, line 83
def connect
  connection_string = build_connection_string
  GoodData.logger.info "Setting up connection to MSSQL #{connection_string}"

  authentication = @authentication['basic'] || @authentication['activeDirectoryPassword']

  prop = java.util.Properties.new
  prop.setProperty('userName', authentication['userName'])
  prop.setProperty('password', authentication['password'])

  @connection = java.sql.DriverManager.getConnection(connection_string, prop)
end
realize_query(query, _params) click to toggle source
# File lib/gooddata/cloud_resources/mssql/mssql_client.rb, line 55
def realize_query(query, _params)
  GoodData.gd_logger.info("Realize SQL query: type=mssql 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(MSSQL_FETCH_SIZE)
    has_result = statement.execute(query)
    if has_result
      result = statement.get_result_set
      metadata = result.get_meta_data
      col_count = metadata.column_count
      CSV.open(filename, 'wb') do |csv|
        csv << Array(1..col_count).map { |i| metadata.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=mssql status=finished duration=#{measure.real}")
  filename
ensure
  @connection&.close
  @connection = nil
end
validate() click to toggle source
# File lib/gooddata/cloud_resources/mssql/mssql_client.rb, line 96
def validate
  raise "SSL Mode should be prefer, require and verify-full" unless @ssl_mode == 'prefer' || @ssl_mode == 'require' || @ssl_mode == 'verify-full'

  raise "Instance name is not supported" if @url !~ /^[^\\]*$/

  raise "The connection url is invalid. Parameter is not supported." if @url.include? MSSQL_SEPARATOR_PARAM

  url_matches = @url.scan(MSSQL_URL_PATTERN)
  raise "Cannot reach the url" if url_matches.nil? || url_matches.length.zero?

  raise "The authentication method is not supported." unless @authentication['basic'] || @authentication['activeDirectoryPassword']
end