From 25b14743be0224dcebdbe3ece9722f3edb299004 Mon Sep 17 00:00:00 2001 From: Aneurin Barker Snook Date: Sun, 8 Oct 2023 22:50:24 +0100 Subject: [PATCH] add query appendf --- query.go | 13 +++++++++++++ query_test.go | 10 ++++++++++ 2 files changed, 23 insertions(+) diff --git a/query.go b/query.go index dec3f9c..e909ae4 100644 --- a/query.go +++ b/query.go @@ -1,6 +1,7 @@ package arango import ( + "fmt" "strings" ) @@ -36,6 +37,18 @@ func (query *Query) Append(line string, values ...any) *Query { return query.BindMap(params) } +// Appendf adds a line to the query, replacing values directly into the string using standard formatting syntax. +// This is a shorthand for: +// +// // query *Query +// query.Append(fmt.Sprintf("my format (%s)", "example")) +// +// This method does not attach values to bind parameters. +func (query *Query) Appendf(line string, a ...any) *Query { + line = fmt.Sprintf(line, a...) + return query.Append(line) +} + // Bind binds a value to a bind parameter. func (query *Query) Bind(name string, value any) *Query { query.Params[name] = value diff --git a/query_test.go b/query_test.go index 9218e81..46dc2ff 100644 --- a/query_test.go +++ b/query_test.go @@ -77,6 +77,16 @@ RETURN doc`, "title": "Spaghetti", }, }, + // Append and Appendf + { + Input: NewQuery(). + Appendf("FOR doc IN %s FILTER doc.title == @title RETURN doc", "recipes"). + Bind("title", "Spaghetti"), + ExpectedStr: `FOR doc IN recipes FILTER doc.title == @title RETURN doc`, + ExpectedParams: map[string]any{ + "title": "Spaghetti", + }, + }, } for n, tc := range testCases {