chsql

package
v0.19.1 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package chsql provides fluent Clickhouse SQL query builder.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CollectTokens added in v0.15.0

func CollectTokens[S ~string | ~[]byte](s S, cb func(s S) bool)

CollectTokens iterates over tokens in given string.

func PutPrinter

func PutPrinter(*Printer)

PutPrinter returns Printer to pool.

Types

type Expr

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

Expr is a Clickhouse expression.

func And

func And(left, right Expr) Expr

And returns new `AND` operation.

func Array

func Array(args ...Expr) Expr

Array returns `array(<args>...)` function call expression.

func ArrayConcat

func ArrayConcat(args ...Expr) Expr

ArrayConcat returns `arrayConcat(<args>...)` function call expression.

func ArrayJoin

func ArrayJoin(args ...Expr) Expr

ArrayJoin returns `arrayJoin(<args>...)` function call expression.

func Bool

func Bool(v bool) Expr

Bool returns bool literal.

func Cast added in v0.14.0

func Cast(x Expr, typ string) Expr

Cast returns `CAST(<x>, <typ>)` function call expression.

func Coalesce

func Coalesce(args ...Expr) Expr

Coalesce returns `coalesce(<args>...)` function call expression.

func ColumnEq

func ColumnEq[V litValue](column string, right V) Expr

ColumnEq returns new `=` operation on column and literal.

func Contains

func Contains(column, needle string) Expr

Contains returns boolean expression to filter strings containing needle.

func Eq

func Eq(left, right Expr) Expr

Eq returns new `=` operation.

func Float

func Float[I litFloat](v I) Expr

Float returns float literal.

func Function

func Function(name string, args ...Expr) Expr

Function reutrns function call expression.

func Gt

func Gt(left, right Expr) Expr

Gt returns new `>` operation.

func Gte

func Gte(left, right Expr) Expr

Gte returns new `>=` operation.

func Has

func Has(arr, elem Expr) Expr

Has returns `has(<arr>, <elem>)` function call expression.

func HasToken

func HasToken(haystack Expr, token string) Expr

HasToken returns `hasToken(<haystack>, <token>)` function call expression.

func Hex

func Hex(arg Expr) Expr

Hex returns `hex(<arg>)` function call expression.

func Ident

func Ident(tok string) Expr

Ident returns identifier.

func In

func In(left, right Expr) Expr

In returns new `IN` operation.

func InTimeRange

func InTimeRange(column string, start, end time.Time) Expr

InTimeRange returns boolean expression to filter by [start:end].

func Integer

func Integer[I litInt](v I) Expr

Integer returns integer literal.

func JSONExtract

func JSONExtract(from Expr, typ string) Expr

JSONExtract returns `JSONExtract(<from>, <typ>)` function call expression.

func JSONExtractField

func JSONExtractField(from Expr, field, typ string) Expr

JSONExtractField returns `JSONExtract(<from>, <field>, <typ>)` function call expression.

func JSONExtractKeys

func JSONExtractKeys(from Expr) Expr

JSONExtractKeys returns `JSONExtractKeys(<from>)` function call expression.

func JSONExtractString

func JSONExtractString(from Expr, field string) Expr

JSONExtractString returns `JSONExtractString(<from>, <field>)` function call expression.

func JoinAnd

func JoinAnd(args ...Expr) Expr

JoinAnd joins given expressions using AND op.

  • If len(args) == 0, returns `true` literal.
  • If len(args) == 1, returns first argument.
  • Otherwise, joins arguments with AND.

func JoinOr

func JoinOr(args ...Expr) Expr

JoinOr joins given expressions using OR op.

  • If len(args) == 0, returns `true` literal.
  • If len(args) == 1, returns first argument.
  • Otherwise, joins arguments with OR.

func Length

func Length(arg Expr) Expr

Length returns `length(<arg>)` function call expression.

func Like

func Like(haystack, pattern Expr) Expr

Like returns `like(<haystack>, <pattern>)` function call expression.

func Lt

func Lt(left, right Expr) Expr

Lt returns new `<` operation.

func Lte

func Lte(left, right Expr) Expr

Lte returns new `<=` operation.

func Map

func Map(args ...Expr) Expr

Map returns `map(<args>...)` function call expression.

func MapConcat

func MapConcat(args ...Expr) Expr

MapConcat returns `mapConcat(<args>...)` function call expression.

func Match

func Match(haystack, pattern Expr) Expr

Match returns `match(<haystack>, <pattern>)` function call expression.

func Minus

func Minus(left, right Expr) Expr

Minus returns new `-` operation.

func Not

func Not(sub Expr) Expr

Not returns new `NOT` operation.

func NotEq

func NotEq(left, right Expr) Expr

NotEq returns new `!=` operation.

func Or

func Or(left, right Expr) Expr

Or returns new `OR` operation.

func Position

func Position(haystack, needle Expr) Expr

Position returns `position(<haystack>, <needle>)` function call expression.

func PositionUTF8

func PositionUTF8(haystack, needle Expr) Expr

PositionUTF8 returns `positionUTF8(<haystack>, <needle>)` function call expression.

func SimpleJSONHas

func SimpleJSONHas(json Expr, field string) Expr

SimpleJSONHas returns `simpleJSONHas(<json>, <field>)` function call expression.

func String

func String(v string) Expr

String returns string literal.

func SubQuery

func SubQuery(q Query) Expr

SubQuery returns sub-query as an expression.

func ToFloat64

func ToFloat64(arg Expr) Expr

ToFloat64 returns `toFloat64(<arg>)` function call expression.

func ToString

func ToString(arg Expr) Expr

ToString returns `toString(<arg>)` function call expression.

func ToUnixTimestamp64Nano

func ToUnixTimestamp64Nano(arg Expr) Expr

ToUnixTimestamp64Nano returns `toUnixTimestamp64Nano(<arg>)` function call expression.

func Tuple

func Tuple(args ...Expr) Expr

Tuple returns tuple of given expressions.

func TupleValues

func TupleValues[V litValue](vals ...V) Expr

TupleValues returns tuple of given values.

func Unhex

func Unhex(arg Expr) Expr

Unhex returns `unhex(<arg>)` function call expression.

func UnixNano

func UnixNano(t time.Time) Expr

UnixNano returns time.Time as unix nano timestamp.

func Value

func Value[V litValue](v V) Expr

Value returns literal.

func (Expr) IsZero

func (e Expr) IsZero() bool

type OnResult

type OnResult = func(ctx context.Context, block proto.Block) error

OnResult defines ch.Query.OnResult callback type.

type Order

type Order uint8

Order defines sorting order.

const (
	Desc Order = iota + 1
	Asc
)

func (Order) String

func (o Order) String() string

String implements fmt.Stringer.

type Printer

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

Printer prints SQL query.

func GetPrinter

func GetPrinter() *Printer

GetPrinter creates a new Printer.

func (*Printer) And

func (p *Printer) And()

And writes `AND` ident.

func (*Printer) Asc

func (p *Printer) Asc()

Asc writes `ASC` ident.

func (*Printer) By

func (p *Printer) By()

By writes `BY` ident.

func (*Printer) CloseParen

func (p *Printer) CloseParen()

CloseParen writes a paren.

func (*Printer) Comma

func (p *Printer) Comma()

Comma writes a comma.

func (*Printer) Desc

func (p *Printer) Desc()

Desc writes `DESC` ident.

func (*Printer) Distinct

func (p *Printer) Distinct()

Distinct writes `DISTINCT` ident.

func (*Printer) From

func (p *Printer) From()

From writes `FROM` ident.

func (*Printer) Group added in v0.16.0

func (p *Printer) Group()

Group writes `GROUP` ident.

func (*Printer) Ident

func (p *Printer) Ident(tok string)

Ident writes an identifier.

func (*Printer) Limit

func (p *Printer) Limit()

Limit writes `LIMIT` ident.

func (*Printer) Literal

func (p *Printer) Literal(lit string)

Literal writes an literal.

func (*Printer) OpenParen

func (p *Printer) OpenParen()

OpenParen writes a paren.

func (*Printer) Order

func (p *Printer) Order()

Order writes `ORDER` ident.

func (*Printer) Prewhere

func (p *Printer) Prewhere()

Prewhere writes `PREWHERE` ident.

func (*Printer) Select

func (p *Printer) Select()

Select writes `SELECT` ident.

func (*Printer) String

func (p *Printer) String() string

String returns query.

func (*Printer) Where

func (p *Printer) Where()

Where writes `WHERE` ident.

func (*Printer) WriteExpr

func (p *Printer) WriteExpr(e Expr) error

WriteExpr writes given expression.

type Query

type Query interface {
	WriteSQL(p *Printer) error
}

Query is an query interface.

type ResultColumn

type ResultColumn struct {
	Name string
	Expr Expr
	Data proto.ColResult
}

ResultColumn defines a column result.

func Column

func Column(name string, data proto.ColResult) ResultColumn

Column returns new Result

type SelectQuery

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

SelectQuery is a SELECT query builder.

func Select

func Select(table string, columns ...ResultColumn) *SelectQuery

Select creates a new SelectQuery.

func SelectFrom

func SelectFrom(sub *SelectQuery, columns ...ResultColumn) *SelectQuery

SelectFrom creates a new SelectQuery from subquery.

func (*SelectQuery) Distinct

func (q *SelectQuery) Distinct(b bool) *SelectQuery

Distinct sets if query is `DISTINCT`.

func (*SelectQuery) GroupBy added in v0.16.0

func (q *SelectQuery) GroupBy(groups ...Expr) *SelectQuery

GroupBy adds grouping to query.

func (*SelectQuery) Limit

func (q *SelectQuery) Limit(n int) *SelectQuery

Limit sets query limit.

If n is equal to or less than zero, limit is ignored.

func (*SelectQuery) Order

func (q *SelectQuery) Order(e Expr, order Order) *SelectQuery

Order adds order to query.

func (*SelectQuery) Prepare

func (q *SelectQuery) Prepare(onResult OnResult) (ch.Query, error)

Prepare builds SQL query and passes columns to ch.Query.

func (*SelectQuery) Prewhere

func (q *SelectQuery) Prewhere(filters ...Expr) *SelectQuery

Prewhere adds filters to query.

func (*SelectQuery) Results

func (q *SelectQuery) Results() (r proto.Results)

Results returns list of result columns.

func (*SelectQuery) Where

func (q *SelectQuery) Where(filters ...Expr) *SelectQuery

Where adds filters to query.

func (*SelectQuery) WriteSQL

func (q *SelectQuery) WriteSQL(p *Printer) error

WriteSQL writes SQL query.

Jump to

Keyboard shortcuts

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