Skip to content

Latest commit

 

History

History
307 lines (238 loc) · 5.96 KB

README.md

File metadata and controls

307 lines (238 loc) · 5.96 KB

Influx Query Builder

Build Status Coverage Status Codacy Badge Go Report Card

The super lightweight InfluxDB query builder implemented in Go.

Installation

go get -u github.com/benjamin658/influx-query-builder

Query Builder Usage

Simple query

builder := New()
query := builder.
  Select("temperature", "humidity").
  From("measurement).
  Build()

Output:

SELECT "temperature","humidity" FROM "measurement"

Select AS

builder := New()
query := builder.
  Select("temperature AS temp", "humidity AS hum").
  From("measurement).
  Build()

Output:

SELECT "temperature" AS "temp","humidity" AS "hum" FROM "measurement"

Function query

builder := New()
query := builder.
  Select(`MEAN("temperature")`, `SUM("humidity")`).
  From("measurement").
  Build()

Output:

SELECT MEAN("temperature"),SUM("humidity") FROM "measurement"

Function AS

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"

Query with criteria

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'

Brackets criteria

Noted: If you use Where with WhereBrackets, Where will override the WhereBrackets.

Where Brackets

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'

And Brackets

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')

Or Brackets

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')

Group By time duration

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)

Group By Tag

builder := New()
query := builder.
  Select("temperature", "humidity").
  From("measurement").
  GroupByTag("sensorId").
  Build()

Output:

SELECT "temperature","humidity" FROM "measurement" GROUP BY sensorId

Order By time

builder := New()
query := builder.
  Select("temperature", "humidity").
  From("measurement").
  Desc().
  Build()

Output:

SELECT "temperature","humidity" FROM "measurement" ORDER BY time DESC

Limit and Offset

builder := New()
query := builder.
  Select("temperature", "humidity").
  From("measurement").
  Limit(10).
  Offset(5).
  Build()

Output:

SELECT "temperature","humidity" FROM "measurement" LIMIT 10 OFFSET 5

Reset builder and get a new one

builder := New()
// some code...
builder = builder.Clean()

Get current query struct

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
}
*/

Deprecated

Group By time

builder := New()
query := builder.
  Select("temperature", "humidity").
  From("measurement").
  GroupBy("10m").
  Build()

Output:

SELECT "temperature","humidity" FROM "measurement" GROUP BY time(10m)

License


© Ben Hu (benjamin658), 2018-NOW

Released under the MIT License