Class: Dbsketch::Automation::DatabaseProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/dbsketch/automation/database_proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection_details, output_pathame = nil) ⇒ DatabaseProxy

Returns a new instance of DatabaseProxy.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
# 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

Instance Method Details

#[](table_name) ⇒ Object

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
# 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 = {}) ⇒ Object



35
36
37
# File 'lib/dbsketch/automation/database_proxy.rb', line 35

def call_procedure procedure, args = {}
  @db.call_mssql_sproc procedure, :args => args
end

#establish_connectionObject



23
24
25
26
27
28
29
30
31
32
33
# 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) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object

Raises:

  • (ArgumentError)


47
48
49
50
51
52
# 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) ⇒ Object

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
# 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