This repository has been archived on 2026-10-03. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
aql/query.go
T

94 lines
2.4 KiB
Go
Raw Normal View History

2023-10-07 10:22:56 +01:00
package arango
import (
2023-10-08 22:50:24 +01:00
"fmt"
2023-10-07 10:22:56 +01:00
"strings"
)
// Query provides a simple way to build ArangoDB queries.
type Query struct {
Lines []string
Params map[string]any
}
// Append adds a line to the query, attaching (optional) values to bind parameters.
//
// Any variables given are assigned to bind parameters in the same order as they appear in the line.
// Keep in mind the following behaviours:
//
// - If more variables are provided than there are parameters in the line, leftover variables are discarded
// - If more parameters are used than variables are given, remaining parameters are left unmapped
// - Reused parameters are overwritten
func (query *Query) Append(line string, values ...any) *Query {
var params map[string]any = nil
names := ReadParams(line)
if len(names) > 0 {
params = map[string]any{}
for i, name := range names {
2023-10-08 15:55:20 +01:00
if i == len(values) {
2023-10-07 10:22:56 +01:00
break
}
params[name] = values[i]
}
}
query.Lines = append(query.Lines, line)
2023-10-08 15:55:20 +01:00
return query.BindMap(params)
2023-10-07 10:22:56 +01:00
}
2023-10-08 22:50:24 +01:00
// 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)
}
2023-10-08 15:55:20 +01:00
// Bind binds a value to a bind parameter.
func (query *Query) Bind(name string, value any) *Query {
2023-10-07 10:22:56 +01:00
query.Params[name] = value
return query
}
2023-10-08 15:55:20 +01:00
// BindMap assigns values to bind parameters.
func (query *Query) BindMap(params map[string]any) *Query {
2023-11-25 16:51:25 +00:00
for name, value := range params {
query.Params[name] = value
2023-10-07 10:22:56 +01:00
}
return query
}
// Copy creates a copy of the query.
func (query *Query) Copy() *Query {
newQuery := NewQuery()
2023-11-25 16:51:25 +00:00
newQuery.Lines = append(newQuery.Lines, query.Lines...)
2023-10-07 10:22:56 +01:00
for name, value := range query.Params {
newQuery.Params[name] = value
}
return newQuery
}
2023-10-13 08:07:06 +01:00
// String returns the query formatted across multiple lines.
2023-10-07 10:22:56 +01:00
func (query *Query) String() string {
return strings.Join(query.Lines, "\n")
}
2023-10-13 08:07:06 +01:00
// StringLine returns the query formatted on a single line.
// This reduces readability but may be more suitable for logging.
func (query *Query) StringLine() string {
return strings.Join(query.Lines, " ")
}
2023-10-08 16:22:44 +01:00
// NewQuery creates a new, empty Query.
2023-10-07 10:22:56 +01:00
func NewQuery() *Query {
return &Query{
Lines: []string{},
Params: map[string]any{},
}
}