Class: RailsRedshiftReplicator::Adapters::PostgreSQL

Inherits:
Generic
  • Object
show all
Defined in:
lib/rails_redshift_replicator/adapters/postgresql.rb

Instance Method Summary collapse

Methods inherited from Generic

#initialize

Constructor Details

This class inherits a constructor from RailsRedshiftReplicator::Adapters::Generic

Instance Method Details

#connectionObject



12
13
14
# File 'lib/rails_redshift_replicator/adapters/postgresql.rb', line 12

def connection
  @connection ||= @ar_client.instance_variable_get("@connection")
end

#last_record_query_command(sql) ⇒ Object



17
18
19
# File 'lib/rails_redshift_replicator/adapters/postgresql.rb', line 17

def last_record_query_command(sql)
  @ar_client.exec_query(sql).first['_last_record']
end

#query_command(sql) ⇒ Object

Executes query in stream mode to optimize memory usage, using pg driver.

Parameters:

  • sql (String)

    sql to execute



7
8
9
10
# File 'lib/rails_redshift_replicator/adapters/postgresql.rb', line 7

def query_command(sql)
  connection.send_query(sql)
  connection.set_single_row_mode
end

#write(file_path, query_result) ⇒ Integer

Writes query results to a file

Parameters:

  • file_path (String)

    path to output

  • query_result (#get_result)

    Resultset from the query_command

Returns:

  • (Integer)

    number of records



25
26
27
28
29
30
31
32
33
34
# File 'lib/rails_redshift_replicator/adapters/postgresql.rb', line 25

def write(file_path, query_result)
  line_number = 0
  CSV.open(file_path, "w") do |csv|
    query_result.get_result.stream_each do |row|
      csv << row.map{ |_,field| field.is_a?(String) ? field.gsub("\n", " ") : field }
      line_number+=1
    end
  end
  line_number
end