Class: Jredshift::JdbcDb

Inherits:
Object
  • Object
show all
Defined in:
lib/jredshift/jdbc_db.rb

Direct Known Subclasses

Redshift

Constant Summary collapse

ERROR_UPDATE_COUNT_OVERFLOW_REGEX =

Java uses an int for the updateCount field in ResultHandler. If the update count overflows an int, you’ll get this error message (but the records will be updated/inserted correctly).

/Unable to interpret the update count in command completion tag: /i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jdbc_url, user, password, options = {}) ⇒ JdbcDb

Returns a new instance of JdbcDb.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jredshift/jdbc_db.rb', line 18

def initialize(jdbc_url, user, password, options={})
  @abort_on_error = options.fetch(:abort_on_error, true)
  @quiet = options.fetch(:quiet, false)
  @jdbc_url = jdbc_url
  @user = user
  @password = password
  @error_occurred = false
  @retry_count = 0

  validate_connection_credentials

  secrets = options.fetch(:secrets, [])
  @logger = Logger.new(:secrets => secrets)

  init_connection
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



9
10
11
# File 'lib/jredshift/jdbc_db.rb', line 9

def conn
  @conn
end

Instance Method Details

#big_query(sql, options = {}) ⇒ Object

Returns a ResultSet (instead of an Array of Hashes). Requires caller to close ResultSet and set autocommit back to true



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/jredshift/jdbc_db.rb', line 64

def big_query(sql, options={})
  quiet = options.fetch(:quiet, @quiet)
  fetch_size = options.fetch(:fetch_size, 100000)

  @conn.set_auto_commit(false);
  stmt = @conn.create_statement
  stmt.set_fetch_size(fetch_size)
  stmt.execute_query(sql)

  log "\n#{sql}\n" unless quiet
  with_error_handling { stmt.execute_query(sql) }
end

#clear_error_stateObject



90
91
92
# File 'lib/jredshift/jdbc_db.rb', line 90

def clear_error_state
  @error_occurred = false
end

#error_occurred?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/jredshift/jdbc_db.rb', line 86

def error_occurred?
  @error_occurred
end

#execute(sql, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jredshift/jdbc_db.rb', line 35

def execute(sql, options={})
  errors_to_ignore = options.fetch(:errors_to_ignore, [])
  quiet = options.fetch(:quiet, @quiet)

  log "\n#{sql}\n" unless quiet

  affected_rows = with_error_handling(:errors_to_ignore => errors_to_ignore) do
    @statement.execute_update(sql)
  end

  unless transaction_statement?(sql) || quiet || affected_rows.nil?
    log "Affected #{affected_rows} row(s)."
  end

  affected_rows
end

#execute_script(filename, options = {}) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/jredshift/jdbc_db.rb', line 77

def execute_script(filename, options={})
  File.open(filename) do |fh|
    sql = fh.read
    sql = remove_comments(sql)
    sql = substitute_variables(sql)
    execute_each_statement(sql, options)
  end
end

#query(sql, options = {}) ⇒ Object

Returns an empty array for no results, or nil if an error occurred and



54
55
56
57
58
59
60
# File 'lib/jredshift/jdbc_db.rb', line 54

def query(sql, options={})
  quiet = options.fetch(:quiet, @quiet)

  log "\n#{sql}\n" unless quiet
  result_set = with_error_handling { @statement.execute_query(sql) }
  result_set ? ArrayOfHashesResult.new(result_set).result : nil
end