From 96a8de1e1e6e4b8d2c4bf9c20055ee2eee630ae3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Matczuk?= Date: Tue, 3 Aug 2021 11:48:49 +0200 Subject: [PATCH] qb: using, modernize writeCql - Add writePreamble function to handle USING vs AND usages - Use Fprintf to avoid string allocation --- qb/using.go | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/qb/using.go b/qb/using.go index fc1ab13..2f811a0 100644 --- a/qb/using.go +++ b/qb/using.go @@ -25,6 +25,7 @@ type using struct { ttlName string timestamp int64 timestampName string + using bool } 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) { - hasTTL := false + u.using = false if u.ttl != 0 { - hasTTL = true if u.ttl == -1 { u.ttl = 0 } - cql.WriteString("USING TTL ") - cql.WriteString(fmt.Sprint(u.ttl)) - cql.WriteByte(' ') + u.writePreamble(cql) + fmt.Fprintf(cql, "TTL %d ", u.ttl) } else if u.ttlName != "" { - hasTTL = true - cql.WriteString("USING TTL ? ") + u.writePreamble(cql) + cql.WriteString("TTL ? ") names = append(names, u.ttlName) } if u.timestamp != 0 { - if hasTTL { - cql.WriteString("AND TIMESTAMP ") - } else { - cql.WriteString("USING TIMESTAMP ") - } - cql.WriteString(fmt.Sprint(u.timestamp)) - cql.WriteByte(' ') + u.writePreamble(cql) + fmt.Fprintf(cql, "TIMESTAMP %d ", u.timestamp) } else if u.timestampName != "" { - if hasTTL { - cql.WriteString("AND TIMESTAMP ? ") - } else { - cql.WriteString("USING TIMESTAMP ? ") - } + u.writePreamble(cql) + cql.WriteString("TIMESTAMP ? ") names = append(names, u.timestampName) } return } + +func (u *using) writePreamble(cql *bytes.Buffer) { + if u.using { + cql.WriteString("AND ") + } else { + cql.WriteString("USING ") + u.using = true + } +}