The super lightweight InfluxDB query builder implemented in Go.
go get -u github.com/benjamin658/influx-query-builder
builder := New()
query := builder.
Select("temperature", "humidity").
From("measurement).
Build()
Output:
SELECT "temperature","humidity" FROM "measurement"
builder := New()
query := builder.
Select("temperature AS temp", "humidity AS hum").
From("measurement).
Build()
Output:
SELECT "temperature" AS "temp","humidity" AS "hum" FROM "measurement"
builder := New()
query := builder.
Select(`MEAN("temperature")`, `SUM("humidity")`).
From("measurement").
Build()
Output:
SELECT MEAN("temperature"),SUM("humidity") FROM "measurement"
builder := New()
query := builder.
Select(`MEAN("temperature") AS mt`, `SUM("humidity") AS sh`).
From("measurement").
Build()
Output:
SELECT MEAN("temperature") AS "mt",SUM("humidity") AS "sh" FROM "measurement"
builder := New()
query := builder.
Select("temperature", "humidity").
From("measurement").
Where("time", ">", "2018-11-01T06:33:57.503Z").
And("time", "<", "2018-11-02T09:35:25Z").
Or("tag", "=", "t").
Build()
Output:
SELECT "temperature","humidity" FROM "measurement" WHERE "time" > '2018-11-01T06:33:57.503Z' AND "time" < '2018-11-02T09:35:25Z' OR "tag" = 't'
Noted: If you use Where
with WhereBrackets
, Where
will override the WhereBrackets
.
builder := New()
query := builder.
Select("temperature", "humidity").
From("measurement").
WhereBrackets(
// Passing a new builder as the param
New().
Where("time", ">", "2018-11-01T06:33:57.503Z").
And("time", "<", "2018-11-02T09:35:25Z").
).
Or("tag", "=", "t").
Build()
Output:
SELECT "temperature","humidity" FROM "measurement" WHERE ("time" > '2018-11-01T06:33:57.503Z' AND "time" < '2018-11-02T09:35:25Z') OR "tag" = 't'
builder := New()
query := builder.
Select("temperature", "humidity").
From("measurement").
Where("time", ">", "2018-11-01T06:33:57.503Z").
AndBrackets(
// Passing a new builder as the param
New().
Where("time", "<", "2018-11-02T09:35:25Z").
Or("tag", "=", "t"),
).
Build()
Output:
SELECT "temperature","humidity" FROM "measurement" WHERE "time" > '2018-11-01T06:33:57.503Z' AND ("time" < '2018-11-02T09:35:25Z' OR "tag" = 't')
builder := New()
query := builder.
Select("temperature", "humidity").
From("measurement").
Where("time", ">", "2018-11-01T06:33:57.503Z").
OrBrackets(
// Passing a new builder as the param
New().
Where("time", "<", "2018-11-02T09:35:25Z").
Or("tag", "=", "t"),
).
Build()
Output:
SELECT "temperature","humidity" FROM "measurement" WHERE "time" > '2018-11-01T06:33:57.503Z' OR ("time" < '2018-11-02T09:35:25Z' OR "tag" = 't')
builder := New()
duration := NewDuration()
query := builder.
Select("temperature", "humidity").
From("measurement").
GroupByTime(duration.Minute(10)).
Build()
Output:
SELECT "temperature","humidity" FROM "measurement" GROUP BY time(10m)
builder := New()
query := builder.
Select("temperature", "humidity").
From("measurement").
GroupByTag("sensorId").
Build()
Output:
SELECT "temperature","humidity" FROM "measurement" GROUP BY sensorId
builder := New()
query := builder.
Select("temperature", "humidity").
From("measurement").
Desc().
Build()
Output:
SELECT "temperature","humidity" FROM "measurement" ORDER BY time DESC
builder := New()
query := builder.
Select("temperature", "humidity").
From("measurement").
Limit(10).
Offset(5).
Build()
Output:
SELECT "temperature","humidity" FROM "measurement" LIMIT 10 OFFSET 5
builder := New()
// some code...
builder = builder.Clean()
builder := New()
query := builder.
Select("temperature", "humidity").
From("measurement").
Limit(100).
Offset(100).
Asc().
GetQueryStruct()
/*
type CurrentQuery struct {
Measurement string
Where Tag
And []Tag
Or []Tag
WhereBrackets QueryBuilder
AndBrackets []QueryBuilder
OrBrackets []QueryBuilder
Fields []string
GroupBy string
Limit uint
Offset uint
Order string
IsLimitSet bool
IsOffsetSet bool
}
*/
builder := New()
query := builder.
Select("temperature", "humidity").
From("measurement").
GroupBy("10m").
Build()
Output:
SELECT "temperature","humidity" FROM "measurement" GROUP BY time(10m)
© Ben Hu (benjamin658), 2018-NOW
Released under the MIT License