Module: IFD_DBConnection

Defined in:
lib/helper/database_helper.rb

Class Method Summary collapse

Class Method Details

.close_database_connectionObject



28
29
30
31
32
33
# File 'lib/helper/database_helper.rb', line 28

def self.close_database_connection
  @connection.disconnect!
  if !@connection.connected?
    put_log "Disconnected to Database!"
  end
end

.create_database_connection(data) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/helper/database_helper.rb', line 15

def self.create_database_connection(data)
  unless data.hashes.empty?
    data = data.hashes[0]
    data = JSON.parse(data) unless data.is_a? Hash

    data.each_pair do |k, v|
      data[k] = Utils.check_dynamic_value(v)
    end
    @connection = IFD_DBConnection.new(data)
    put_log "Connect to database successfully!" if @connection
  end
end

.execute_script(script) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/helper/database_helper.rb', line 44

def self.execute_script(script)
  script = Utils.check_dynamic_value(script)
  if @connection
    @result = @connection.connection.execute(script)
  else
    raise "ERROR: Create connection to database first!"
  end
end

.execute_select(script) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/helper/database_helper.rb', line 35

def self.execute_select(script)
  script = Utils.check_dynamic_value(script)
  if @connection
    @result = @connection.connection.exec_query(script)
  else
    raise "ERROR: Create connection to database first!"
  end
end

.new(connection_params) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/helper/database_helper.rb', line 6

def self.new(connection_params)
  begin
    put_log ("Connecting to database...")
    return ActiveRecord::Base.establish_connection(connection_params)
  rescue Exception => e
    raise e.message
  end
end


53
54
55
56
57
58
59
60
61
# File 'lib/helper/database_helper.rb', line 53

def self.print_sql_result
  if @result.present?
    @result.each do |row|
      p row
    end
  else
    p "WARNING: No result from SQL statement"
  end
end

.store_sql_result_into_string(string) ⇒ Object



77
78
79
# File 'lib/helper/database_helper.rb', line 77

def self.store_sql_result_into_string(string)
  Utils.set_var(string, @result)
end

.verify_sql_result_with_json(json) ⇒ Object



63
64
65
66
67
68
# File 'lib/helper/database_helper.rb', line 63

def self.verify_sql_result_with_json(json)
  expected = JSON.parse(json)
  @result.each do |row|
    IFD_Assertion.assert_string_equal(expected,row)
  end
end

.verify_sql_result_with_json_node(json_node, value) ⇒ Object



70
71
72
73
74
75
# File 'lib/helper/database_helper.rb', line 70

def self.verify_sql_result_with_json_node(json_node, value)
  @result.each do|row|
    results = JsonPath.new(json_node).on(row).to_a.map(&:to_s)
    IFD_Assertion.assert_string_equal(results[0], value)
  end
end