github.com/altipla-consulting/ravendb-go-client@v0.1.3/from_token.go (about) 1 package ravendb 2 3 import "strings" 4 5 var _ queryToken = &fromToken{} 6 7 type fromToken struct { 8 collectionName string 9 indexName string 10 isDynamic bool 11 alias string 12 } 13 14 func createFromToken(indexName string, collectionName string, alias string) *fromToken { 15 return &fromToken{ 16 collectionName: collectionName, 17 indexName: indexName, 18 isDynamic: collectionName != "", 19 alias: alias, 20 } 21 } 22 23 func (t *fromToken) writeTo(writer *strings.Builder) error { 24 if t.indexName == "" && t.collectionName == "" { 25 return newIllegalStateError("Either indexName or collectionName must be specified") 26 } 27 28 if t.isDynamic { 29 writer.WriteString("from ") 30 31 hasWhitespace := strings.ContainsAny(t.collectionName, " \t\r\n") 32 if hasWhitespace { 33 err := throwIfInvalidCollectionName(t.collectionName) 34 if err != nil { 35 return err 36 } 37 writer.WriteString(`"`) 38 writer.WriteString(t.collectionName) 39 writer.WriteString(`"`) 40 } else { 41 writeQueryTokenField(writer, t.collectionName) 42 } 43 } else { 44 writer.WriteString("from index '") 45 writer.WriteString(t.indexName) 46 writer.WriteString("'") 47 } 48 49 if t.alias != "" { 50 writer.WriteString(" as ") 51 writer.WriteString(t.alias) 52 } 53 return nil 54 } 55 56 func throwIfInvalidCollectionName(collectionName string) error { 57 if strings.Contains(collectionName, "\"") { 58 return newIllegalArgumentError("Collection name cannot contain a quote, but was: " + collectionName) 59 } 60 return nil 61 }