qb: using, modernize writeCql
- Add writePreamble function to handle USING vs AND usages - Use Fprintf to avoid string allocation
This commit is contained in:
committed by
Michal Jan Matczuk
parent
49993c1fbb
commit
96a8de1e1e
38
qb/using.go
38
qb/using.go
@@ -25,6 +25,7 @@ type using struct {
|
|||||||
ttlName string
|
ttlName string
|
||||||
timestamp int64
|
timestamp int64
|
||||||
timestampName string
|
timestampName string
|
||||||
|
using bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *using) TTL(d time.Duration) *using {
|
func (u *using) TTL(d time.Duration) *using {
|
||||||
@@ -55,38 +56,37 @@ func (u *using) TimestampNamed(name string) *using {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *using) writeCql(cql *bytes.Buffer) (names []string) {
|
func (u *using) writeCql(cql *bytes.Buffer) (names []string) {
|
||||||
hasTTL := false
|
u.using = false
|
||||||
|
|
||||||
if u.ttl != 0 {
|
if u.ttl != 0 {
|
||||||
hasTTL = true
|
|
||||||
if u.ttl == -1 {
|
if u.ttl == -1 {
|
||||||
u.ttl = 0
|
u.ttl = 0
|
||||||
}
|
}
|
||||||
cql.WriteString("USING TTL ")
|
u.writePreamble(cql)
|
||||||
cql.WriteString(fmt.Sprint(u.ttl))
|
fmt.Fprintf(cql, "TTL %d ", u.ttl)
|
||||||
cql.WriteByte(' ')
|
|
||||||
} else if u.ttlName != "" {
|
} else if u.ttlName != "" {
|
||||||
hasTTL = true
|
u.writePreamble(cql)
|
||||||
cql.WriteString("USING TTL ? ")
|
cql.WriteString("TTL ? ")
|
||||||
names = append(names, u.ttlName)
|
names = append(names, u.ttlName)
|
||||||
}
|
}
|
||||||
|
|
||||||
if u.timestamp != 0 {
|
if u.timestamp != 0 {
|
||||||
if hasTTL {
|
u.writePreamble(cql)
|
||||||
cql.WriteString("AND TIMESTAMP ")
|
fmt.Fprintf(cql, "TIMESTAMP %d ", u.timestamp)
|
||||||
} else {
|
|
||||||
cql.WriteString("USING TIMESTAMP ")
|
|
||||||
}
|
|
||||||
cql.WriteString(fmt.Sprint(u.timestamp))
|
|
||||||
cql.WriteByte(' ')
|
|
||||||
} else if u.timestampName != "" {
|
} else if u.timestampName != "" {
|
||||||
if hasTTL {
|
u.writePreamble(cql)
|
||||||
cql.WriteString("AND TIMESTAMP ? ")
|
cql.WriteString("TIMESTAMP ? ")
|
||||||
} else {
|
|
||||||
cql.WriteString("USING TIMESTAMP ? ")
|
|
||||||
}
|
|
||||||
names = append(names, u.timestampName)
|
names = append(names, u.timestampName)
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *using) writePreamble(cql *bytes.Buffer) {
|
||||||
|
if u.using {
|
||||||
|
cql.WriteString("AND ")
|
||||||
|
} else {
|
||||||
|
cql.WriteString("USING ")
|
||||||
|
u.using = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user