bqb

package module
v0.0.0-...-424d785 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: MIT Imports: 5 Imported by: 0

README

bqb

A query builder for BigQuery.

Why use this?

The most common way to write a BigQuery query is to combine a query string with a []bigquery.QueryParameter slice which contains the query parameter values. But that has some big downsides:

  • []bigquery.QueryParameter gets out of sync with the parameters in the query string. For example, you might have a query param in your query string that isn't in your []bigquery.QueryParameter.
  • No type checking for []bigquery.QueryParameter values.
  • Without a query builder, dynamic queries require error-prone string manipulation.

Examples

Basic

To create this query:

SELECT id, name, age,
FROM my_table
WHERE age > 50
  AND name = 'bob'

Write this code:

query, err := Build(
    &client,
    Query(
        Select("id", "name", "age"),
        From("my_table"),
        Where("age > 50"),
        Where("name = 'bob'"),
    ),
)
Parameterized queries

BigQuery supports parameterized queries. Parameter names have an @ prefix in the query (e.g. @age) and must have matching bigquery.QueryParameter object.

To create this query:

SELECT id, name, age,
FROM my_table
WHERE age > @age
  AND name = @name

Write this code:

query, err := Build(
    &client,
    Query(
        Select("id", "name", "age"),
        From("my_table"),
        Where("age > @age"),
        Where("name = @name"),
    ),
    ParamInt("age", 50),
    ParamStr("name", "bob"),
)

The Build function will automatically populate query.Parameters and check that all parameters in the query have been specified (e.g. using ParamInt).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Build

func Build(
	client *bigquery.Client,
	query errorableStringer,
	paramMods ...ParamMod,
) (*bigquery.Query, error)

Types

type FromMod

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

func From

func From(table string) FromMod

type FromQueryMod

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

func FromQuery

func FromQuery(alias string, query errorableStringer) FromQueryMod

type GroupByMod

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

func GroupBy

func GroupBy(columns ...string) []GroupByMod

type LimitMod

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

func Limit

func Limit(limit int) LimitMod

type OrderByMod

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

func OrderBy

func OrderBy(columns ...string) []OrderByMod

type ParamMod

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

func ParamBool

func ParamBool(name string, value bool) ParamMod

func ParamFloat64

func ParamFloat64(name string, value float64) ParamMod

func ParamInt

func ParamInt(name string, value int) ParamMod

func ParamInt64

func ParamInt64(name string, value int64) ParamMod

func ParamStr

func ParamStr(name string, value string) ParamMod

func ParamStrSlice

func ParamStrSlice(name string, value []string) ParamMod

func ParamStringer

func ParamStringer(name string, value fmt.Stringer) ParamMod

type QueryMod

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

func Query

func Query(mods ...any) QueryMod

func (*QueryMod) Limit

func (q *QueryMod) Limit(limit int)

func (QueryMod) String

func (q QueryMod) String() (string, error)

func (*QueryMod) Where

func (q *QueryMod) Where(clause string)

type SelectMod

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

func Select

func Select(columns ...string) []SelectMod

type UnionAllMod

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

func UnionAll

func UnionAll(queries ...QueryMod) UnionAllMod

func (UnionAllMod) String

func (u UnionAllMod) String() (string, error)

type WhereMod

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

func Where

func Where(clause string) WhereMod

func (WhereMod) String

func (w WhereMod) String() (string, error)

type WhereQueryMod

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

func WhereQuery

func WhereQuery(clause string, query QueryMod) WhereQueryMod

func (WhereQueryMod) String

func (w WhereQueryMod) String() (string, error)

Jump to

Keyboard shortcuts

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