Class: Outhad::Integrations::Destination::MicrosoftSql::Client

Inherits:
DestinationConnector
  • Object
show all
Defined in:
lib/outhad/integrations/destination/microsoft_sql/client.rb

Instance Method Summary collapse

Instance Method Details

#check_connection(connection_config) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/outhad/integrations/destination/microsoft_sql/client.rb', line 9

def check_connection(connection_config)
  connection_config = connection_config.with_indifferent_access
  db_client = create_connection(connection_config)

  if db_client.active?
    ConnectionStatus.new(
      status: ConnectionStatusType["succeeded"]
    ).to_outhad_message
  else
    ConnectionStatus.new(
      status: ConnectionStatusType["failed"], message: "MS SQL connection not active"
    ).to_outhad_message
  end
rescue TinyTds::Error => e
  ConnectionStatus.new(
    status: ConnectionStatusType["failed"], message: e.message
  ).to_outhad_message
end

#discover(connection_config) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/outhad/integrations/destination/microsoft_sql/client.rb', line 28

def discover(connection_config)
  connection_config = connection_config.with_indifferent_access
  query = "SELECT table_name, column_name, data_type, is_nullable
           FROM information_schema.columns
           WHERE table_schema = '#{connection_config[:schema]}' AND table_catalog = '#{connection_config[:database]}'
           ORDER BY table_name, ordinal_position;"

  db_client = create_connection(connection_config)
  records = db_client.execute(query) do |result|
    result.map do |row|
      row
    end
  end
  catalog = Catalog.new(streams: create_streams(records))
  catalog.to_outhad_message
rescue StandardError => e
  handle_exception(e, {
                     context: "MSSQL:DISCOVER:EXCEPTION",
                     type: "error"
                   })
ensure
  db_client&.close
end

#write(sync_config, records, action = "destination_insert") ⇒ Object



52
53
54
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
80
81
82
83
84
85
86
87
88
89
# File 'lib/outhad/integrations/destination/microsoft_sql/client.rb', line 52

def write(sync_config, records, action = "destination_insert")
  connection_config = sync_config.destination.connection_specification.with_indifferent_access
  table_name = sync_config.stream.name
  primary_key = sync_config.model.primary_key
  log_message_array = []
  db = create_connection(connection_config)

  write_success = 0
  write_failure = 0

  records.each do |record|
    query = Outhad::Integrations::Core::QueryBuilder.perform(action, table_name, record, primary_key)
    logger.debug("MSSQL:WRITE:QUERY query = #{query} sync_id = #{sync_config.sync_id} sync_run_id = #{sync_config.sync_run_id}")
    begin
      response = db.execute(query)
      response.do
      write_success += 1
      log_message_array << log_request_response("info", query, response)
    rescue StandardError => e
      handle_exception(e, {
                         context: "MSSQL:RECORD:WRITE:EXCEPTION",
                         type: "error",
                         sync_id: sync_config.sync_id,
                         sync_run_id: sync_config.sync_run_id
                       })
      write_failure += 1
      log_message_array << log_request_response("error", query, e.message)
    end
  end
  tracking_message(write_success, write_failure, log_message_array)
rescue StandardError => e
  handle_exception(e, {
                     context: "MSSQL:RECORD:WRITE:EXCEPTION",
                     type: "error",
                     sync_id: sync_config.sync_id,
                     sync_run_id: sync_config.sync_run_id
                   })
end