class SqlClass

Wrapper class to interact with sql server

Public Class Methods

new(user:, password:, host:, database:, azure:, port: nil) click to toggle source

Initialize Class

# 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

Public Instance Methods

close_connection() click to toggle source

Close the connection to the sql client

# 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_client() click to toggle source

Return Client object

# File lib/miq_utilities/sql.rb, line 40
def return_client
  @client
end
run_sql_query(sql) click to toggle source

Run Sql query

# 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