This repository was archived by the owner on Dec 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.go
More file actions
executable file
·107 lines (87 loc) · 2.7 KB
/
search.go
File metadata and controls
executable file
·107 lines (87 loc) · 2.7 KB
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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// search
package rets
import (
"fmt"
"github.com/mlapping/rets/results"
"strconv"
)
type Count int
const (
COUNT_OFF Count = iota
COUNT_PREPEND_TO_RESULTS Count = iota
COUNT_IS_ONLY_RESULT Count = iota
)
type SearchQuery struct {
DMQL string
Limit int
Resource string
Class string
Offset int
Count Count
StandardNames bool
}
/*
Run a DMQL Search query on a Resource/Class pair
SearchType=Property&Class=A&QueryType=DMQL2&Query=%28LIST_15=%7COV61GOJ13C0%29&StandardNames=0&Limit=50
*/
func (sess *Session) Search(query *SearchQuery, addtlParams map[string]string) (*results.SearchReply, error) {
queryString := map[string]string{
"SearchType": query.Resource,
"Class": query.Class,
"QueryType": "DMQL2",
"Query": query.DMQL,
"Count": strconv.Itoa(int(query.Count)),
"StandardNames": strconv.Itoa(boolToInt(query.StandardNames)),
"Limit": strconv.Itoa(query.Limit),
"Format": "COMPACT-DECODED",
}
for key, value := range addtlParams {
queryString[key] = value
}
// fire off the query
searchReply := &results.SearchReply{}
err := sess.getResults("Search", "GET", sess.Capabilities.SearchUrl(), queryString, searchReply)
if err == nil && searchReply.Code != 0 {
return nil, fmt.Errorf("rets.%s: Error: %s", "Search", searchReply.Text)
}
return searchReply, err
}
// SearchType=Property&Class=A&Query=*&QueryType=DMQL2&Select=LIST_1,LIST_105&Limit=NONE&Format=COMPACT
/*
Using a special feature in RETS 1.7.2, you can get a key for every record in the database regardless of your search query hard-limit
*/
func (sess *Session) AllKeys(resource, class string) (*results.SearchReply, error) {
keyField, err := sess.KeyField(resource)
if err != nil {
return nil, fmt.Errorf("rets.AllKeys: Resource %s does not have a KeyField. This feature is not available for this resource.", resource)
}
queryString := map[string]string{
"SearchType": resource,
"Class": class,
"QueryType": "DMQL2",
"Query": "*",
"Limit": "NONE",
"Format": "COMPACT",
"Select": keyField,
}
// fire off the query
searchReply := &results.SearchReply{Data: make([]string, 0)}
err = sess.getResults("Search", "GET", sess.Capabilities.SearchUrl(), queryString, searchReply)
if err == nil && searchReply.Code != 0 {
return nil, fmt.Errorf("rets.%s: Error: %s", "Search", searchReply.Text)
}
delimiter, err := searchReply.GetDelimiter()
if err != nil {
return nil, err
}
for i, value := range searchReply.Data {
searchReply.Data[i] = results.DecodeData(value, delimiter)[1]
}
return searchReply, err
}
func boolToInt(b bool) int {
if b {
return 1
}
return 0
}