Class: SalesforceChunker::ManualChunkingQuery

Inherits:
Job
  • Object
show all
Defined in:
lib/salesforce_chunker/manual_chunking_query.rb

Constant Summary

Constants inherited from Job

Job::DEFAULT_RETRY_SECONDS, Job::DEFAULT_TIMEOUT_SECONDS, Job::QUERY_OPERATIONS

Instance Attribute Summary

Attributes inherited from Job

#batches_count

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Job

#close, #create_batch, #download_results, #get_batch_results, #get_batch_statuses, #get_completed_batches, #retrieve_batch_results, #retrieve_raw_results, #retrieve_results

Constructor Details

#initialize(connection:, object:, operation:, query:, **options) ⇒ ManualChunkingQuery

Returns a new instance of ManualChunkingQuery.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/salesforce_chunker/manual_chunking_query.rb', line 4

def initialize(connection:, object:, operation:, query:, **options)
  @log = options.delete(:logger) || Logger.new(options[:log_output])
  @log.progname = "salesforce_chunker"
  batch_size = options[:batch_size] || 100000
  where_clause = self.class.query_where_clause(query)

  @log.info "Using Manual Chunking"
  breakpoint_creation_job = SalesforceChunker::ManualChunkingBreakpointQuery.new(
    connection: connection,
    object: object,
    operation: operation,
    logger: @log,
    batch_size: batch_size,
    query: "Select Id From #{object} #{where_clause} Order By Id Asc",
  )
  breakpoints = breakpoint_creation_job.download_results(retry_seconds: 10).to_a

  super(connection: connection, object: object, operation: operation, logger: @log, **options)

  @log.info "Creating Query Batches"
  create_batches(query, breakpoints, where_clause)

  close
end

Class Method Details

.query_where_clause(query) ⇒ Object



44
45
46
# File 'lib/salesforce_chunker/manual_chunking_query.rb', line 44

def self.query_where_clause(query)
  query.partition(/where\s/i)[1..2].join
end

Instance Method Details

#create_batches(query, breakpoints, where_clause) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/salesforce_chunker/manual_chunking_query.rb', line 29

def create_batches(query, breakpoints, where_clause)
  if breakpoints.empty?
    create_batch(query)
  else
    query += where_clause.empty? ? " Where" : " And"

    create_batch("#{query} Id < '#{breakpoints.first}'")
    breakpoints.each_cons(2) do |first, second|
      create_batch("#{query} Id >= '#{first}' And Id < '#{second}'")
    end
    create_batch("#{query} Id >= '#{breakpoints.last}'")
  end
  @batches_count = breakpoints.length + 1
end