Class: Databasedotcom::Client

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

Instance Method Summary collapse

Instance Method Details

#count(classname) ⇒ Object



58
59
60
# File 'lib/ext/databasedotcom.rb', line 58

def count(classname)
  query("SELECT COUNT() FROM #{classname}").total_size
end

#fetch_multiple(classname, ids, batch_size = 100, field_list = nil) ⇒ Object

Fetches a collection of sobjects with given ids Useful in conjunction with get_updated / get_deleted calls



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ext/databasedotcom.rb', line 5

def fetch_multiple(classname, ids, batch_size = 100, field_list = nil)
  return [] unless ids.present?
  klass = find_or_materialize(classname)
  field_list ||= klass.field_list.split(",")
  field_list = field_list | ["Id"]
  field_list = field_list.join(",")
  ids.in_groups_of(batch_size).flat_map do |ids|
    query <<-EOQ
    SELECT #{field_list}
    FROM #{klass.sobject_name}
    WHERE id IN (#{ids.map {|id| "'%s'" % id}.join(',')})
    EOQ
  end
end

#get_deleted(classname, start_date, end_date = Time.current) ⇒ Object

Returns a list of updated sobject ids for provided date range reference: developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_getdeleted.htm?search_text=getUpdated



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ext/databasedotcom.rb', line 41

def get_deleted(classname, start_date, end_date = Time.current)
  result = http_get("/services/data/v#{self.version}/sobjects/#{classname}/deleted",
                    start: prepare_date_arg(start_date),
                    end: prepare_date_arg(end_date))
  JSON.parse(result.body)
rescue Databasedotcom::SalesForceError => e
  if e.message.include?("is not replicable")
    {}
  else
    raise
  end
end

#get_deleted_ids(classname, start_date, end_date = Time.current) ⇒ Object



54
55
56
# File 'lib/ext/databasedotcom.rb', line 54

def get_deleted_ids(classname, start_date, end_date = Time.current)
  get_deleted(classname, start_date, end_date).fetch("deletedRecords", []).map {|r| r["id"] }
end

#get_updated(classname, start_date, end_date = Time.current) ⇒ Object

Returns a list of updated sobject ids for provided date range reference: developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_getupdated.htm?search_text=getUpdated



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ext/databasedotcom.rb', line 22

def get_updated(classname, start_date, end_date = Time.current)
  result = http_get("/services/data/v#{self.version}/sobjects/#{classname}/updated",
                    start: prepare_date_arg(start_date),
                    end: prepare_date_arg(end_date))
  JSON.parse(result.body)
rescue Databasedotcom::SalesForceError => e
  if e.message.include?("is not replicable")
    {}
  else
    raise
  end
end

#get_updated_ids(classname, start_date, end_date = Time.current) ⇒ Object



35
36
37
# File 'lib/ext/databasedotcom.rb', line 35

def get_updated_ids(classname, start_date, end_date = Time.current)
  get_updated(classname, start_date, end_date).fetch("ids", [])
end