-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
56 lines (49 loc) · 1.18 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package force
import (
"errors"
"fmt"
"net/url"
)
type QueryResult struct {
Done bool `json:"done"`
TotalSize int `json:"totalSize"`
QueryLocator string `json:"nextRecordsUrl"`
Records []interface{} `json:"records"`
}
func validateConn(conn *Conn) error {
if conn == nil || len(conn.SessionId) == 0 || len(conn.ServiceUrl) == 0 {
return errors.New("invalid connection")
}
return nil
}
func Query(conn *Conn, query string) (*QueryResult, error) {
err := validateConn(conn)
if err != nil {
return nil, err
}
url := conn.ServiceUrl + "/query?q=" + url.QueryEscape(query)
fmt.Println(url)
queryResult := QueryResult{}
err = restGetJSON(conn, url, &queryResult)
if err != nil {
return nil, err
}
return &queryResult, nil
}
func QueryMore(conn *Conn, queryLocator string) (*QueryResult, error) {
err := validateConn(conn)
if err != nil {
return nil, err
}
if len(queryLocator) == 0 {
return nil, fmt.Errorf("empty query locator")
}
url := conn.InstanceUrl + queryLocator
fmt.Println(url)
queryResult := QueryResult{}
err = restGetJSON(conn, url, &queryResult)
if err != nil {
return nil, err
}
return &queryResult, nil
}