tdquery

package module
v0.0.0-...-6628553 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 5, 2022 License: MIT Imports: 15 Imported by: 0

README

tdquery

A simple go client for TDengine

Installation

go get githhub.com/snownd/tdquery

Usage


	client := tdquery.NewClient(
		tdquery.WithBrokers([]string{"localhost"}),
		tdquery.WithPort(6041),
		tdquery.WithBasicAuth("root", "taosdata"),
    // tdquery.WithUrlDatabase() use this when TDengine version is greater than 2.2.0.0
	)
 	if err := client.Connect(context.Background()); err != nil {
		panic(err)
	}
  	ret := make([]Data, 0)
	qb := client.NewSelectQueryBuilder().UseDatabase(db)
	err = qb.SelectColumnWithAlias("MAX(value)", "value").
		FromSTable(stable).
		WithTimeScope(time.Now().Add(-1*time.Hour), time.Now()).
		Where(tdquery.Equals("city_code", 1002)).
		Interval(tdquery.NewInterval("3s")).
		Desc().
		Limit(3).
    // GetRaw() will have much better performance than GetResult()
		GetResult(context.TODO(), &ret)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%+v\n", ret)

You can check example for more usage.


TODO

  • [] Add test cases, and use github Action do tests
  • [] Add more examples
  • [] Add Insert Builder
  • [] Add more Condition for TDengine SQL aggregation functions
  • [] Add Support for JOIN
  • [] Add Support for UNION ALL
  • [] HTTP keepalive
  • [] Taosd token authentication

Documentation

Index

Constants

View Source
const (
	QueryErrCodeTableNotExist = 866
)

Variables

View Source
var ErrEmptyFrom = errors.New("tdquery: table and stable are both empty")
View Source
var ErrEmptySelect = errors.New("tdquery: select columns is empty")
View Source
var ErrInvalidCondition = errors.New("tdquery: invalid condition")
View Source
var ErrorInvalidQueryArgs = errors.New("tdquery: invalid query args")
View Source
var ErrorInvalidQueryArgsNumber = errors.New("tdquery: query param number not match")
View Source
var ErrorNoAvailableBroker = errors.New("tdquery: no available broker")

Functions

func IsValidPeriod

func IsValidPeriod(period string) bool

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(opts ...Option) *Client

func (*Client) Close

func (c *Client) Close(ctx context.Context) error

func (*Client) Connect

func (c *Client) Connect(ctx context.Context) error

func (*Client) NewSelectQueryBuilder

func (c *Client) NewSelectQueryBuilder() *SelectQueryBuilder

func (*Client) Query

func (c *Client) Query(ctx context.Context, sql string, params ...interface{}) (*QueryResult, error)

type Condition

type Condition struct {
	ColumnName string
	Operator   string
	Value      interface{}
}

func Equals

func Equals(column string, value interface{}) *Condition

func Greater

func Greater(column string, value interface{}) *Condition

func GreaterEqual

func GreaterEqual(column string, value interface{}) *Condition

func IsNotNull

func IsNotNull(column string) *Condition

func IsNull

func IsNull(column string) *Condition

func Less

func Less(column string, value interface{}) *Condition

func LessEqual

func LessEqual(column string, value interface{}) *Condition

func NewCondition

func NewCondition(column, operator string, value interface{}) *Condition

func NotEquals

func NotEquals(column string, value interface{}) *Condition

func (*Condition) IsValid

func (c *Condition) IsValid() bool

func (*Condition) String

func (c *Condition) String() string

type Fill

type Fill struct {
	// contains filtered or unexported fields
}

func FillLinear

func FillLinear() *Fill

func FillNext

func FillNext() *Fill

func FillNull

func FillNull() *Fill

func FillPrev

func FillPrev() *Fill

func FillValue

func FillValue(value string) *Fill

func (*Fill) String

func (f *Fill) String() string

type FillType

type FillType int
const (
	FillTypeValue FillType = iota + 1
	FillTypePrevious
	FillTypeNull
	FillTypeLinear
	FillTypeNext
)

func (FillType) String

func (f FillType) String() string

type Interval

type Interval struct {
	// contains filtered or unexported fields
}

func NewInterval

func NewInterval(period string) *Interval

func (*Interval) String

func (i *Interval) String() string

func (*Interval) WithOffset

func (i *Interval) WithOffset(n int) *Interval

type Option

type Option func(c *Client)

func WithBasicAuth

func WithBasicAuth(username, password string) Option

func WithBrokers

func WithBrokers(brokers []string) Option

func WithDatabase

func WithDatabase(db string) Option

func WithPort

func WithPort(port int) Option

func WithQueryTimeout

func WithQueryTimeout(timeout time.Duration) Option

func WithUrlDatabase

func WithUrlDatabase() Option

WithUrlDatabase choose database in url like `/rest/sqlt/dbname`. Should only be used when TDengine version is greater than 2.2.0.0

type Order

type Order int
const (
	ASC Order = iota
	DESC
)

func (Order) String

func (o Order) String() string

type QueryBuilder

type QueryBuilder struct {
	// contains filtered or unexported fields
}

func (*QueryBuilder) Build

func (b *QueryBuilder) Build() (string, error)

func (*QueryBuilder) FromSTable

func (b *QueryBuilder) FromSTable(stable string) *QueryBuilder

func (*QueryBuilder) FromTables

func (b *QueryBuilder) FromTables(tables ...string) *QueryBuilder

func (*QueryBuilder) GetRaw

func (b *QueryBuilder) GetRaw(ctx context.Context, sql string, params ...interface{}) (*QueryResult, error)

func (*QueryBuilder) UseDatabase

func (b *QueryBuilder) UseDatabase(db string) *QueryBuilder

type QueryResult

type QueryResult struct {
	Code    int                      `json:"code"`
	Message string                   `json:"message,omitempty"`
	SQL     string                   `json:"sql,omitempty"`
	Data    []map[string]interface{} `json:"data"`
	Rows    int                      `json:"rows"`
	// 单位毫秒
	Cost int `json:"cost"`
}

func NewQueryResult

func NewQueryResult(raw *rawQueryResult, sql string, cost time.Duration) *QueryResult

type Select

type Select struct {
	ColumnName string
	Alias      string
}

type SelectQueryBuilder

type SelectQueryBuilder struct {
	QueryBuilder
	// contains filtered or unexported fields
}

func (*SelectQueryBuilder) AddSelect

func (b *SelectQueryBuilder) AddSelect(s Select) *SelectQueryBuilder

func (*SelectQueryBuilder) AndWhere

func (b *SelectQueryBuilder) AndWhere(conditions ...*Condition) *SelectQueryBuilder

func (*SelectQueryBuilder) Asc

order by time column with ASC order

func (*SelectQueryBuilder) Build

func (b *SelectQueryBuilder) Build() (string, error)

func (*SelectQueryBuilder) Desc

order by time column with DESC order

func (*SelectQueryBuilder) Fill

func (*SelectQueryBuilder) FromSTable

func (b *SelectQueryBuilder) FromSTable(stable string) *SelectQueryBuilder

func (*SelectQueryBuilder) FromSubQuery

func (b *SelectQueryBuilder) FromSubQuery(subQuery *SelectQueryBuilder) *SelectQueryBuilder

func (*SelectQueryBuilder) FromTables

func (b *SelectQueryBuilder) FromTables(tables ...string) *SelectQueryBuilder

func (*SelectQueryBuilder) GetRaw

func (s *SelectQueryBuilder) GetRaw(ctx context.Context) (*QueryResult, error)

func (*SelectQueryBuilder) GetResult

func (s *SelectQueryBuilder) GetResult(ctx context.Context, v interface{}) error

func (*SelectQueryBuilder) GroupBy

func (b *SelectQueryBuilder) GroupBy(columns ...string) *SelectQueryBuilder

func (*SelectQueryBuilder) Interval

func (b *SelectQueryBuilder) Interval(interval *Interval) *SelectQueryBuilder

func (*SelectQueryBuilder) Limit

func (b *SelectQueryBuilder) Limit(limit int) *SelectQueryBuilder

func (*SelectQueryBuilder) Offset

func (b *SelectQueryBuilder) Offset(offset int) *SelectQueryBuilder

func (*SelectQueryBuilder) OrderBy

func (b *SelectQueryBuilder) OrderBy(columns []string, order Order) *SelectQueryBuilder

OrderBy only support ASC or DESC with time column

func (*SelectQueryBuilder) SLimit

func (b *SelectQueryBuilder) SLimit(limit int) *SelectQueryBuilder

func (*SelectQueryBuilder) SOffset

func (b *SelectQueryBuilder) SOffset(offset int) *SelectQueryBuilder

func (*SelectQueryBuilder) Select

func (b *SelectQueryBuilder) Select(selects ...Select) *SelectQueryBuilder

func (*SelectQueryBuilder) SelectAll

func (b *SelectQueryBuilder) SelectAll() *SelectQueryBuilder

func (*SelectQueryBuilder) SelectColumn

func (b *SelectQueryBuilder) SelectColumn(columnName string) *SelectQueryBuilder

func (*SelectQueryBuilder) SelectColumnWithAlias

func (b *SelectQueryBuilder) SelectColumnWithAlias(columnName, alias string) *SelectQueryBuilder

func (*SelectQueryBuilder) UseDatabase

func (b *SelectQueryBuilder) UseDatabase(db string) *SelectQueryBuilder

func (*SelectQueryBuilder) Where

func (b *SelectQueryBuilder) Where(conditions ...*Condition) *SelectQueryBuilder

func (*SelectQueryBuilder) WithTimeScope

func (b *SelectQueryBuilder) WithTimeScope(start, end time.Time) *SelectQueryBuilder

WithTimeScope generate sql with BETWEEN: _co between start and end

type TDEngineError

type TDEngineError struct {
	Code    int
	Message string
}

func (*TDEngineError) Error

func (e *TDEngineError) Error() string

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL