Class: Rsconn::Postgres

Inherits:
Object
  • Object
show all
Defined in:
lib/rsconn/postgres.rb

Direct Known Subclasses

Redshift

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, database, user, password, options = {}) ⇒ Postgres

Returns a new instance of Postgres.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rsconn/postgres.rb', line 18

def initialize(host, port, database, user, password, options={})
  fail_if_invalid_connection_credentials(host, port, database, user, password)

  @host = host
  @port = port
  @database = database
  @user = user
  @password = password
  @abort_on_error = options.fetch(:abort_on_error, true)
  @max_retries = options.fetch(:max_retries, 3)
  @quiet = options.fetch(:quiet, false)
  @print_backtrace_for_all_errors = options.fetch(:print_backtrace_for_all_errors, false)
  @error_occurred = false
  @retry_count = 0

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

  init_connection
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



16
17
18
# File 'lib/rsconn/postgres.rb', line 16

def conn
  @conn
end

Instance Method Details

#clear_error_stateObject



79
80
81
# File 'lib/rsconn/postgres.rb', line 79

def clear_error_state
  @error_occurred = false
end

#drop_table_if_exists(table, options = {}) ⇒ Object



83
84
85
86
# File 'lib/rsconn/postgres.rb', line 83

def drop_table_if_exists(table, options={})
  cascade = options.delete(:cascade) ? ' CASCADE' : ''
  execute("DROP TABLE IF EXISTS #{table}#{cascade};", options)
end

#drop_view_if_exists(view, options = {}) ⇒ Object



88
89
90
91
# File 'lib/rsconn/postgres.rb', line 88

def drop_view_if_exists(view, options={})
  cascade = options.delete(:cascade) ? ' CASCADE' : ''
  execute("DROP VIEW IF EXISTS #{view}#{cascade};", options)
end

#error_occurred?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/rsconn/postgres.rb', line 75

def error_occurred?
  @error_occurred
end

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rsconn/postgres.rb', line 39

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

  log "\n#{sql}\n" unless quiet
  result = with_error_handling { @conn.exec(sql) }
  return nil if @error_occurred

  cmd = result.cmd_status.split.first
  affected_rows = result.cmd_tuples

  if %w(INSERT UPDATE DELETE MOVE FETCH).include?(cmd)
    log "Affected #{affected_rows} row(s)." unless quiet
  end

  affected_rows
end

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



66
67
68
69
70
71
72
73
# File 'lib/rsconn/postgres.rb', line 66

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



56
57
58
59
60
61
62
63
64
# File 'lib/rsconn/postgres.rb', line 56

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

  log "\n#{sql}\n" unless quiet
  result = with_error_handling { @conn.exec(sql) }
  return nil if @error_occurred

  TypecastResult.new(result).result
end

#table_exists?(schema, table) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
# File 'lib/rsconn/postgres.rb', line 93

def table_exists?(schema, table)
  sql = "    SELECT count(*) FROM pg_tables\n    WHERE schemaname = '\#{schema}' AND tablename = '\#{table}'\n    ;\n  SQL\n  query(sql, :quiet => true).first['count'] == 1\nend\n"