Class: SalesforceBulkQuery::Api

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

Overview

Abstracts the whole library, class the user interacts with

Constant Summary collapse

CHECK_INTERVAL =
30
@@DEFAULT_API_VERSION =
'29.0'

Instance Method Summary collapse

Constructor Details

#initialize(client, options = {}) ⇒ Api

Constructor

Parameters:

  • client (Restforce)

    An instance of the Restforce client, that is used internally to access Salesforce api

  • options (defaults to: {})


20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/salesforce_bulk_query.rb', line 20

def initialize(client, options={})
  @logger = options[:logger]

  api_version = options[:api_version] || @@DEFAULT_API_VERSION

  # use our own logging middleware if logger passed
  if @logger && client.respond_to?(:middleware)
    client.middleware.use(SalesforceBulkQuery::Logger, @logger, options)
  end

  # initialize connection
  @connection = SalesforceBulkQuery::Connection.new(client, api_version, @logger, options[:filename_prefix])
end

Instance Method Details

#instance_urlObject

Get the Salesforce instance URL



35
36
37
38
39
40
# File 'lib/salesforce_bulk_query.rb', line 35

def instance_url
  # make sure it ends with /
  url = @connection.client.instance_url
  url += '/' if url[-1] != '/'
  return url
end

#query(sobject, soql, options = {}) ⇒ Object

Query the Salesforce API. It’s a blocking method - waits until the query is resolved can take quite some time

Parameters:

  • sobject

    Salesforce object, e.g. “Opportunity”

  • soql

    SOQL query, e.g. “SELECT Name FROM Opportunity”

Returns:

  • hash with :filenames and other useful stuff



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/salesforce_bulk_query.rb', line 49

def query(sobject, soql, options={})
  @logger.info "Running query: #{soql}. Gem version salesforce_bulk_query: #{SalesforceBulkQuery::VERSION}" if @logger
  check_interval = options[:check_interval] || CHECK_INTERVAL
  time_limit = options[:time_limit] # in seconds

  start_time = Time.now

  # start the machinery
  query = start_query(sobject, soql, options)
  results = nil

  loop do
    # get available results and check the status
    results = query.get_available_results(options)
    @logger.debug "get_available_results: #{results}"

    # if finished get the result and we're done
    if results[:succeeded]

      # we're done
      @logger.info "Query succeeded. Results: #{results}" if @logger
      break
    end

    # if we've run out of time limit, go away
    if time_limit && (Time.now - start_time > time_limit)
      @logger.warn "Ran out of time limit, downloading what's available and terminating" if @logger

      @logger.info "Downloaded the following files: #{results[:filenames]} The following didn't finish in time: #{results[:unfinished_subqueries]}." if @logger
      break
    end

    @logger.info "Sleeping #{check_interval}" if @logger
    @logger.info "Downloaded files: #{results[:filenames].length} Jobs in progress: #{query.jobs_in_progress.length}"
    sleep(check_interval)
  end

  # nice list of files to log
  if @logger && ! results[:filenames].empty?

    @logger.info "Download finished. Downloaded files in #{File.dirname(results[:filenames][0])}. Filename size [line count]:"
    @logger.info "\n" + results[:filenames].sort.map{|f| "#{File.basename(f)} #{File.size(f)} #{Utils.line_count(f) if options[:count_lines]}"}.join("\n")
  end
  return results
end

#start_query(sobject, soql, options = {}) ⇒ Object

Start the query (synchronous method)

Returns:

  • Query instance with the running query



98
99
100
101
102
103
# File 'lib/salesforce_bulk_query.rb', line 98

def start_query(sobject, soql, options={})
  # create the query, start it and return it
  query = SalesforceBulkQuery::Query.new(sobject, soql, @connection, {:logger => @logger}.merge(options))
  query.start(options)
  return query
end