forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrom_token.go
61 lines (52 loc) · 1.43 KB
/
from_token.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
57
58
59
60
61
package ravendb
import "strings"
var _ queryToken = &fromToken{}
type fromToken struct {
collectionName string
indexName string
isDynamic bool
alias string
}
func createFromToken(indexName string, collectionName string, alias string) *fromToken {
return &fromToken{
collectionName: collectionName,
indexName: indexName,
isDynamic: collectionName != "",
alias: alias,
}
}
func (t *fromToken) writeTo(writer *strings.Builder) error {
if t.indexName == "" && t.collectionName == "" {
return newIllegalStateError("Either indexName or collectionName must be specified")
}
if t.isDynamic {
writer.WriteString("from ")
hasWhitespace := strings.ContainsAny(t.collectionName, " \t\r\n")
if hasWhitespace {
err := throwIfInvalidCollectionName(t.collectionName)
if err != nil {
return err
}
writer.WriteString(`"`)
writer.WriteString(t.collectionName)
writer.WriteString(`"`)
} else {
writeQueryTokenField(writer, t.collectionName)
}
} else {
writer.WriteString("from index '")
writer.WriteString(t.indexName)
writer.WriteString("'")
}
if t.alias != "" {
writer.WriteString(" as ")
writer.WriteString(t.alias)
}
return nil
}
func throwIfInvalidCollectionName(collectionName string) error {
if strings.Contains(collectionName, "\"") {
return newIllegalArgumentError("Collection name cannot contain a quote, but was: " + collectionName)
}
return nil
}