Documentation
¶
Overview ¶
Package chsql provides fluent Clickhouse SQL query builder.
Index ¶
- func CollectTokens[S ~string | ~[]byte](s S, cb func(s S) bool)
- func IsSingleToken[S ~string | ~[]byte](s S) bool
- func PutPrinter(*Printer)
- type Expr
- func And(left, right Expr) Expr
- func ArgMax(arg, val Expr) Expr
- func ArgMin(arg, val Expr) Expr
- func Array(args ...Expr) Expr
- func ArrayConcat(args ...Expr) Expr
- func ArrayElement(arr, index Expr) Expr
- func ArrayJoin(args ...Expr) Expr
- func Bool(v bool) Expr
- func Cast(x Expr, typ string) Expr
- func Coalesce(args ...Expr) Expr
- func ColumnEq[V litValue](column string, right V) Expr
- func Contains(column, needle string) Expr
- func DateTime(t time.Time) Expr
- func DateTime64(t time.Time, prec proto.Precision) Expr
- func Eq(left, right Expr) Expr
- func FirstValue(arg Expr) Expr
- func Float[I litFloat](v I) Expr
- func Function(name string, args ...Expr) Expr
- func Gt(left, right Expr) Expr
- func Gte(left, right Expr) Expr
- func Has(arr, elem Expr) Expr
- func HasToken(haystack Expr, token string) Expr
- func Hex(arg Expr) Expr
- func Hop(arg Expr, hopInterval, windowInterval time.Duration) Expr
- func HopEnd(arg Expr, hopInterval, windowInterval time.Duration) Expr
- func HopStart(arg Expr, hopInterval, windowInterval time.Duration) Expr
- func Ident(tok string) Expr
- func In(left, right Expr) Expr
- func InTimeRange(column string, start, end time.Time, prec proto.Precision) Expr
- func Integer[I litInt](v I) Expr
- func Interval(d time.Duration) Expr
- func JSONExtract(from Expr, typ string) Expr
- func JSONExtractField(from Expr, field, typ string) Expr
- func JSONExtractKeys(from Expr) Expr
- func JSONExtractString(from Expr, field string) Expr
- func JoinAnd(args ...Expr) Expr
- func JoinOr(args ...Expr) Expr
- func LastValue(arg Expr) Expr
- func Length(arg Expr) Expr
- func Like(haystack, pattern Expr) Expr
- func Lt(left, right Expr) Expr
- func Lte(left, right Expr) Expr
- func Map(args ...Expr) Expr
- func MapConcat(args ...Expr) Expr
- func Match(haystack, pattern Expr) Expr
- func Minus(left, right Expr) Expr
- func Not(sub Expr) Expr
- func NotEq(left, right Expr) Expr
- func Or(left, right Expr) Expr
- func Position(haystack, needle Expr) Expr
- func PositionUTF8(haystack, needle Expr) Expr
- func PrefixedIdent(prefix, tok string) Expr
- func SimpleJSONHas(json Expr, field string) Expr
- func String(v string) Expr
- func SubQuery(q Query) Expr
- func ToDateTime(arg Expr) Expr
- func ToDateTime64(arg Expr, prec proto.Precision) Expr
- func ToFloat64(arg Expr) Expr
- func ToInt64(arg Expr) Expr
- func ToStartOfHour(arg Expr) Expr
- func ToString(arg Expr) Expr
- func ToUInt64(arg Expr) Expr
- func ToUnixTimestamp64Nano(arg Expr) Expr
- func Tumble(arg Expr, hopInterval time.Duration) Expr
- func TumbleEnd(arg Expr, hopInterval time.Duration) Expr
- func TumbleStart(arg Expr, hopInterval time.Duration) Expr
- func Tuple(args ...Expr) Expr
- func TupleValues[V litValue](vals ...V) Expr
- func Unhex(arg Expr) Expr
- func UnixNano(t time.Time) Expr
- func Value[V litValue](v V) Expr
- type OnResult
- type Order
- type Printer
- func (p *Printer) And()
- func (p *Printer) Asc()
- func (p *Printer) By()
- func (p *Printer) CloseParen()
- func (p *Printer) Comma()
- func (p *Printer) Cross()
- func (p *Printer) Desc()
- func (p *Printer) Distinct()
- func (p *Printer) Dot()
- func (p *Printer) Final()
- func (p *Printer) From()
- func (p *Printer) Full()
- func (p *Printer) Group()
- func (p *Printer) Having()
- func (p *Printer) Ident(tok string)
- func (p *Printer) Inner()
- func (p *Printer) Join()
- func (p *Printer) Left()
- func (p *Printer) Limit()
- func (p *Printer) Literal(lit string)
- func (p *Printer) On()
- func (p *Printer) OpenParen()
- func (p *Printer) Order()
- func (p *Printer) Outer()
- func (p *Printer) Prewhere()
- func (p *Printer) Right()
- func (p *Printer) Select()
- func (p *Printer) String() string
- func (p *Printer) Where()
- func (p *Printer) With()
- func (p *Printer) WriteExpr(e Expr) error
- type Query
- type ResultColumn
- type SelectQuery
- func (q *SelectQuery) Alias(alias string) *SelectQuery
- func (q *SelectQuery) Distinct(b bool) *SelectQuery
- func (q *SelectQuery) Final(b bool) *SelectQuery
- func (q *SelectQuery) GroupBy(groups ...Expr) *SelectQuery
- func (q *SelectQuery) Having(filters ...Expr) *SelectQuery
- func (q *SelectQuery) InnerJoin(table, alias string, on Expr) *SelectQuery
- func (q *SelectQuery) Limit(n int) *SelectQuery
- func (q *SelectQuery) Order(e Expr, order Order) *SelectQuery
- func (q *SelectQuery) Prepare(onResult OnResult) (ch.Query, error)
- func (q *SelectQuery) Prewhere(filters ...Expr) *SelectQuery
- func (q *SelectQuery) Results() (r proto.Results)
- func (q *SelectQuery) Where(filters ...Expr) *SelectQuery
- func (q *SelectQuery) With(name string, expr Expr) *SelectQuery
- func (q *SelectQuery) WriteSQL(p *Printer) error
- type WithColumn
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CollectTokens ¶ added in v0.15.0
CollectTokens iterates over tokens in given string.
func IsSingleToken ¶
IsSingleToken whether if given string is a single token.
See https://clickhouse.com/docs/en/sql-reference/functions/string-search-functions#hastoken. See https://github.com/ClickHouse/ClickHouse/blob/755b73f3fc99847f40ac4d9186bb19116e709c37/src/Interpreters/ITokenExtractor.cpp#L84.
Types ¶
type Expr ¶
type Expr struct {
// contains filtered or unexported fields
}
Expr is a Clickhouse expression.
func ArrayConcat ¶
ArrayConcat returns `arrayConcat(<args>...)` function call expression.
func ArrayElement ¶ added in v0.34.1
ArrayElement returns `arrayElement(<arr>, <index>)` function call expression.
func DateTime64 ¶ added in v0.32.0
DateTime64 returns an expression representing time.Time with given precision as DateTime64.
func FirstValue ¶ added in v0.34.1
FirstValue returns `first_value(<arg>)` function call expression.
func Hop ¶ added in v0.34.1
Hop returns `hop(<time_attr>, <hop_interval>, <window_interval>)` function call expression.
func HopEnd ¶ added in v0.34.1
HopEnd returns `hopEnd(<time_attr>, <hop_interval>, <window_interval>)` function call expression.
func HopStart ¶ added in v0.34.1
HopStart returns `hopStart(<time_attr>, <hop_interval>, <window_interval>)` function call expression.
func InTimeRange ¶
InTimeRange returns boolean expression to filter by [start:end].
func Interval ¶ added in v0.34.1
Interval converts duration d to ClickHouse Interval expression with given precision.
func JSONExtract ¶
JSONExtract returns `JSONExtract(<from>, <typ>)` function call expression.
func JSONExtractField ¶
JSONExtractField returns `JSONExtract(<from>, <field>, <typ>)` function call expression.
func JSONExtractKeys ¶
JSONExtractKeys returns `JSONExtractKeys(<from>)` function call expression.
func JSONExtractString ¶
JSONExtractString returns `JSONExtractString(<from>, <field>)` function call expression.
func JoinAnd ¶
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 ¶
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 PositionUTF8 ¶
PositionUTF8 returns `positionUTF8(<haystack>, <needle>)` function call expression.
func PrefixedIdent ¶ added in v0.35.0
PrefixedIdent return identifier with prefix.
func SimpleJSONHas ¶
SimpleJSONHas returns `simpleJSONHas(<json>, <field>)` function call expression.
func ToDateTime ¶ added in v0.34.0
ToDateTime returns `toDateTime(<arg>)` function call expression.
func ToDateTime64 ¶ added in v0.32.0
ToDateTime64 returns `toDateTime64(<arg>, <prec>)` function call expression.
func ToStartOfHour ¶ added in v0.27.0
ToStartOfDay returns `toStartOfHour(<arg>)` function call expression.
func ToUnixTimestamp64Nano ¶
ToUnixTimestamp64Nano returns `toUnixTimestamp64Nano(<arg>)` function call expression.
func Tumble ¶ added in v0.34.1
Tumble returns `tumble(<time_attr>, <hop_interval>)` function call expression.
func TumbleEnd ¶ added in v0.34.1
TumbleEnd returns `tumbleEnd(<time_attr>, <hop_interval>)` function call expression.
func TumbleStart ¶ added in v0.34.1
TumbleStart returns `tumbleStart(<time_attr>, <hop_interval>)` function call expression.
func TupleValues ¶
func TupleValues[V litValue](vals ...V) Expr
TupleValues returns tuple of given values.
type OnResult ¶
OnResult defines ch.Query.OnResult callback type.
type Printer ¶
type Printer struct {
// contains filtered or unexported fields
}
Printer prints SQL query.
type ResultColumn ¶
ResultColumn defines a column result.
func Column ¶
func Column(name string, data proto.ColResult) ResultColumn
Column returns new ResultColumn.
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) Alias ¶ added in v0.35.0
func (q *SelectQuery) Alias(alias string) *SelectQuery
Alias sets alias for selected table.
func (*SelectQuery) Distinct ¶
func (q *SelectQuery) Distinct(b bool) *SelectQuery
Distinct sets if query is `DISTINCT`.
func (*SelectQuery) Final ¶ added in v0.30.0
func (q *SelectQuery) Final(b bool) *SelectQuery
Final sets if query is `FINAL`.
func (*SelectQuery) GroupBy ¶ added in v0.16.0
func (q *SelectQuery) GroupBy(groups ...Expr) *SelectQuery
GroupBy adds grouping to query.
func (*SelectQuery) Having ¶ added in v0.26.0
func (q *SelectQuery) Having(filters ...Expr) *SelectQuery
Having adds grouped filters to query.
func (*SelectQuery) InnerJoin ¶ added in v0.35.0
func (q *SelectQuery) InnerJoin(table, alias string, on Expr) *SelectQuery
InnerJoin adds an INNER JOIN 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) With ¶ added in v0.34.1
func (q *SelectQuery) With(name string, expr Expr) *SelectQuery
With adds common table expressions to a query.
func (*SelectQuery) WriteSQL ¶
func (q *SelectQuery) WriteSQL(p *Printer) error
WriteSQL writes SQL query.
type WithColumn ¶ added in v0.34.1
WithColumn defines a Common table expression.