Class: SqlClass

Inherits:
Object
  • Object
show all
Defined in:
lib/miq_utilities/sql.rb

Overview

Wrapper class to interact with sql server

Instance Method Summary collapse

Constructor Details

#initialize(user:, password:, host:, database:, azure:, port: nil) ⇒ SqlClass

Initialize Class



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/miq_utilities/sql.rb', line 19

def initialize(user:, password:, host:, database:, azure:, port: nil)
  # Logging
  @logger = LoggingClass.new(self.class.to_s)

  # Default port
  unless port.nil?
    @logger.log(level: 'info', message: 'Using default port 1433')
    port = '1433'
  end

  # Get connection to client
  client = TinyTds::Client.new(username: user,
                               password: password,
                               host: host,
                               port: port,
                               database: database,
                               azure: azure)
  @client = client
end

Instance Method Details

#close_connectionObject

Close the connection to the sql client



55
56
57
58
# File 'lib/miq_utilities/sql.rb', line 55

def close_connection
  @client.close unless @client.closed?
  @logger.log(level: 'info', message: 'Connection to SQL Client Closed Successfully.') if @client.closed?
end

#return_clientObject

Return Client object



40
41
42
# File 'lib/miq_utilities/sql.rb', line 40

def return_client
  @client
end

#run_sql_query(sql) ⇒ Object

Run Sql query



45
46
47
48
49
50
51
52
# File 'lib/miq_utilities/sql.rb', line 45

def run_sql_query(sql)
  if @client.nil?
    @logger.log(level: 'warn', message: 'SQL Server Connection Not Established! Query hasn\'t been run!')
  else
    @logger.log(level: 'info', message: "Executing Sql Query: #{sql}")
    @client.execute(sql)
  end
end