class Dbsketch::Automation::DatabaseProxy

Public Class Methods

new(connection_details, output_pathame = nil) click to toggle source
# File lib/dbsketch/automation/database_proxy.rb, line 14
def initialize connection_details, output_pathame = nil
        ### Preconditions
        raise ArgumentError, "connection_details is not a Dbsketch::Automation::DatabaseConnectionDetails" unless connection_details.is_a? DatabaseConnectionDetails
        ###
        @connection_details = connection_details
        @db = nil
        @execution_output_pathname = output_pathame + "execution_result.txt" if nil != output_pathame
end

Public Instance Methods

[](table_name) click to toggle source
# File lib/dbsketch/automation/database_proxy.rb, line 39
def [](table_name)
        ### Preconditions
        raise ArgumentError, "table_name is not a Symbol. If you want to use a qualified name (schema.name), please use the fetch function." unless table_name.is_a? Symbol
        # @db[object] returns all records if object is a string, regardless of further filter or where statements (in: @db['name'].where(condition), where(condition) is ignored)
        ###
        @db[table_name]
end
call_procedure(procedure, args = {}) click to toggle source
# File lib/dbsketch/automation/database_proxy.rb, line 35
def call_procedure procedure, args = {}
        @db.call_mssql_sproc procedure, :args => args
end
establish_connection() click to toggle source
# File lib/dbsketch/automation/database_proxy.rb, line 23
def establish_connection
        @db = Sequel.connect(
                :adapter   => 'ado',  #mssql
                :host              => @connection_details.full_host,
                :database  => @connection_details.database,
                :user              => @connection_details.user,                #not needed via SSO
                :password  => @connection_details.password          #not needed via SSO
                #:encoding => Encoding::UTF_8,     #only MySQL
        )
        @db.test_connection #force exception if problem occured
end
execute_file(file_pathname) click to toggle source
# File lib/dbsketch/automation/database_proxy.rb, line 62
def execute_file file_pathname
        cmd =
                "sqlcmd -d #{@connection_details.database} -S #{@connection_details.full_host} -i #{file_pathname.to_s.gsub(/\//, '\\')}" +
                " -P #{@connection_details.password} -U #{@connection_details.user} -o #{@execution_output_pathname.to_s.gsub(/\//, '\\')}"

        status = system cmd
        output = File.readlines(@execution_output_pathname)

        if not status or output.find { |o| o.match(/Msg \d+, Level \d+, State \d+/) } then
                raise AutomationError, "Error while executing file #{file_pathname}! Extract from #{@execution_output_pathname}:\n #{output[0].strip}\n #{output[1].strip}"
        end
        status
end
fetch(query) click to toggle source
# File lib/dbsketch/automation/database_proxy.rb, line 47
def fetch query
        ### Preconditions
        raise ArgumentError, "query is not a String" unless query.is_a? String
        ###
        @db.fetch query
end
run(query) click to toggle source
# File lib/dbsketch/automation/database_proxy.rb, line 54
def run query
        ### Preconditions
        raise ArgumentError, "query is not a String" unless query.is_a? String
        ###
        @db.run query # raise an error if something goes wrong, otherwise returns nil
        true
end