querybuilder

package
v0.0.0-...-ddc1a4a Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package querybuilder provides a fluent SQL generator. This package is derived from Squirrel, an open source fluent SQL generator for Go

Index

Constants

This section is empty.

Variables

View Source
var (
	// Question is a PlaceholderFormat instance that leaves placeholders as
	// question marks.
	Question = questionFormat{}

	// Dollar is a PlaceholderFormat instance that replaces placeholders with
	// dollar-prefixed positional placeholders (e.g. $1, $2, $3).
	Dollar = dollarFormat{}
)
View Source
var (
	// ErrMalformedSetClause indicates that the update is missing a set clause
	ErrMalformedSetClause = errors.New("update statements must have at least one Set clause")

	// ErrMissingTable indicates that the update is missing a target table
	ErrMissingTable = errors.New("update statements must specify a table")
)
View Source
var StatementBuilder = StatementBuilderType(builder.EmptyBuilder).PlaceholderFormat(Question)

StatementBuilder is a parent builder for other builders, e.g. SelectBuilder.

Functions

func IsUUID

func IsUUID(value interface{}) bool

IsUUID asserts if a value is of a UUID type

func Placeholders

func Placeholders(count int) string

Placeholders returns a string with count ? placeholders joined with commas.

Types

type And

type And conj

And is of type conj

func (And) ToSQL

func (a And) ToSQL() (string, []interface{}, error)

ToSQL converts to SQL string and args

type DeleteBuilder

type DeleteBuilder builder.Builder

DeleteBuilder builds SQL DELETE statements.

func Delete

func Delete(from string) DeleteBuilder

Delete returns a new DeleteBuilder with the given table name.

See DeleteBuilder.Table.

func (DeleteBuilder) From

func (b DeleteBuilder) From(from string) DeleteBuilder

From sets the table to be deleted from.

func (DeleteBuilder) GetData

func (b DeleteBuilder) GetData() StatementAccessor

GetData returns the underlying struct as an interface

func (DeleteBuilder) IsCAS

func (b DeleteBuilder) IsCAS() bool

IsCAS always return false

func (DeleteBuilder) PlaceholderFormat

func (b DeleteBuilder) PlaceholderFormat(f PlaceholderFormat) DeleteBuilder

PlaceholderFormat sets PlaceholderFormat (e.g. Question or Dollar) for the delete.

func (DeleteBuilder) StmtType

func (b DeleteBuilder) StmtType() StmtType

StmtType returns type of the statement

func (DeleteBuilder) ToSQL

func (b DeleteBuilder) ToSQL() (string, []interface{}, error)

ToSQL builds the delete into a SQL string and bound args.

func (DeleteBuilder) ToUql

func (b DeleteBuilder) ToUql() (query string, args []interface{},
	options map[string]interface{}, err error)

ToUql builds the delete into a UQL string and bound args. it also returns simple query options

func (DeleteBuilder) Using

func (b DeleteBuilder) Using(sql string, args ...interface{}) DeleteBuilder

Using adds an expression to the end of the query

func (DeleteBuilder) Where

func (b DeleteBuilder) Where(pred interface{}, args ...interface{}) DeleteBuilder

Where adds WHERE expressions to the delete.

See SelectBuilder.Where for more information.

type Eq

type Eq map[string]interface{}

Eq is syntactic sugar for use with Where/Having/Set methods. Ex:

.Where(Eq{"id": 1})

func (Eq) ToSQL

func (eq Eq) ToSQL() (sql string, args []interface{}, err error)

ToSQL converts to SQL string and args

type Gt

type Gt Lt

Gt is syntactic sugar for use with Where/Having/Set methods. Ex:

.Where(Gt{"id": 1}) == "id > 1"

func (Gt) ToSQL

func (gt Gt) ToSQL() (sql string, args []interface{}, err error)

ToSQL converts to SQL string and args

type GtOrEq

type GtOrEq Lt

GtOrEq is syntactic sugar for use with Where/Having/Set methods. Ex:

.Where(GtOrEq{"id": 1}) == "id >= 1"

func (GtOrEq) ToSQL

func (gtOrEq GtOrEq) ToSQL() (sql string, args []interface{}, err error)

ToSQL converts to SQL string and args

type InsertBuilder

type InsertBuilder builder.Builder

InsertBuilder builds SQL INSERT statements.

func Insert

func Insert(into string) InsertBuilder

Insert returns a new InsertBuilder with the given table name.

See InsertBuilder.Into.

func (InsertBuilder) AddSTApplyMetadata

func (b InsertBuilder) AddSTApplyMetadata(value []byte) InsertBuilder

AddSTApplyMetadata adds a value for the special st_apply_metadata column.

func (InsertBuilder) Columns

func (b InsertBuilder) Columns(columns ...string) InsertBuilder

Columns adds insert columns to the query.

func (InsertBuilder) GetData

func (b InsertBuilder) GetData() StatementAccessor

GetData returns the underlying struct as an interface

func (InsertBuilder) IfNotExist

func (b InsertBuilder) IfNotExist() InsertBuilder

IfNotExist performs the insert only if the value does not exist.

func (InsertBuilder) Into

func (b InsertBuilder) Into(from string) InsertBuilder

Into sets the INTO clause of the query.

func (InsertBuilder) IsCAS

func (b InsertBuilder) IsCAS() bool

IsCAS returns true is the insert statement has a compare-and-set part

func (InsertBuilder) PlaceholderFormat

func (b InsertBuilder) PlaceholderFormat(f PlaceholderFormat) InsertBuilder

PlaceholderFormat sets PlaceholderFormat (e.g. Question or Dollar) for the query.

func (InsertBuilder) SetMap

func (b InsertBuilder) SetMap(clauses map[string]interface{}) InsertBuilder

SetMap set columns and values for insert builder from a map of column name and value note that it will reset all previous columns and values was set if any

func (InsertBuilder) StmtType

func (b InsertBuilder) StmtType() StmtType

StmtType returns type of the statement

func (InsertBuilder) ToSQL

func (b InsertBuilder) ToSQL() (string, []interface{}, error)

ToSQL builds the query into a SQL string and bound args.

func (InsertBuilder) ToUql

func (b InsertBuilder) ToUql() (query string, args []interface{},
	options map[string]interface{}, err error)

ToUql builds the query into a UQL string and bound args. As an runtime optimization, it also returns query options

func (InsertBuilder) Using

func (b InsertBuilder) Using(sql string, args ...interface{}) InsertBuilder

Using adds an expression to the end of the query

func (InsertBuilder) Values

func (b InsertBuilder) Values(values ...interface{}) InsertBuilder

Values adds a single row's values to the query.

type Lt

type Lt map[string]interface{}

Lt is syntactic sugar for use with Where/Having/Set methods. Ex:

.Where(Lt{"id": 1})

func (Lt) ToSQL

func (lt Lt) ToSQL() (sql string, args []interface{}, err error)

ToSQL converts to SQL string and args

type LtOrEq

type LtOrEq Lt

LtOrEq is syntactic sugar for use with Where/Having/Set methods. Ex:

.Where(LtOrEq{"id": 1}) == "id <= 1"

func (LtOrEq) ToSQL

func (ltOrEq LtOrEq) ToSQL() (sql string, args []interface{}, err error)

ToSQL converts to SQL string and args

type NotEq

type NotEq Eq

NotEq is syntactic sugar for use with Where/Having/Set methods. Ex:

.Where(NotEq{"id": 1}) == "id <> 1"

func (NotEq) ToSQL

func (neq NotEq) ToSQL() (sql string, args []interface{}, err error)

ToSQL converts to SQL string and args

type Or

type Or conj

Or is of type conj

func (Or) ToSQL

func (o Or) ToSQL() (string, []interface{}, error)

ToSQL converts to SQL string and args

type PlaceholderFormat

type PlaceholderFormat interface {
	ReplacePlaceholders(sql string) (string, error)
}

PlaceholderFormat is the interface that wraps the ReplacePlaceholders method.

ReplacePlaceholders takes a SQL statement and replaces each question mark placeholder with a (possibly different) SQL placeholder.

type SelectBuilder

type SelectBuilder builder.Builder

SelectBuilder builds SQL SELECT statements.

func Select

func Select(columns ...string) SelectBuilder

Select returns a new SelectBuilder, optionally setting some result columns.

See SelectBuilder.Columns.

func (SelectBuilder) Column

func (b SelectBuilder) Column(column interface{}, args ...interface{}) SelectBuilder

Column adds a result column to the query. Unlike Columns, Column accepts args which will be bound to placeholders in the columns string, for example:

Column("IF(col IN ("+squirrel.Placeholders(3)+"), 1, 0) as col", 1, 2, 3)

func (SelectBuilder) Columns

func (b SelectBuilder) Columns(columns ...string) SelectBuilder

Columns adds result columns to the query.

func (SelectBuilder) Distinct

func (b SelectBuilder) Distinct() SelectBuilder

Distinct adds a DISTINCT clause to the query.

func (SelectBuilder) From

func (b SelectBuilder) From(from string) SelectBuilder

From sets the FROM clause of the query.

func (SelectBuilder) FromSelect

func (b SelectBuilder) FromSelect(from SelectBuilder, aliasString string) SelectBuilder

FromSelect sets a subquery into the FROM clause of the query.

func (SelectBuilder) GetData

func (b SelectBuilder) GetData() StatementAccessor

GetData returns the underlying struct as an interface

func (SelectBuilder) GetPageSize

func (b SelectBuilder) GetPageSize() int

GetPageSize returns true the size of the page

func (SelectBuilder) GetPagingState

func (b SelectBuilder) GetPagingState() []byte

GetPagingState returns true the continuation token

func (SelectBuilder) GroupBy

func (b SelectBuilder) GroupBy(groupBys ...string) SelectBuilder

GroupBy adds GROUP BY expressions to the query.

func (SelectBuilder) Having

func (b SelectBuilder) Having(pred interface{}, rest ...interface{}) SelectBuilder

Having adds an expression to the HAVING clause of the query.

See Where.

func (SelectBuilder) IsCAS

func (b SelectBuilder) IsCAS() bool

IsCAS always return false

func (SelectBuilder) IsDisableAutoPaging

func (b SelectBuilder) IsDisableAutoPaging() bool

IsDisableAutoPaging returns true if the select statement disable auto-paging

func (SelectBuilder) Join

func (b SelectBuilder) Join(join string, rest ...interface{}) SelectBuilder

Join adds a JOIN clause to the query.

func (SelectBuilder) JoinClause

func (b SelectBuilder) JoinClause(pred interface{}, args ...interface{}) SelectBuilder

JoinClause adds a join clause to the query.

func (SelectBuilder) LeftJoin

func (b SelectBuilder) LeftJoin(join string, rest ...interface{}) SelectBuilder

LeftJoin adds a LEFT JOIN clause to the query.

func (SelectBuilder) Limit

func (b SelectBuilder) Limit(limit uint64) SelectBuilder

Limit sets a LIMIT clause on the query.

func (SelectBuilder) Offset

func (b SelectBuilder) Offset(offset uint64) SelectBuilder

Offset sets a OFFSET clause on the query.

func (SelectBuilder) OrderBy

func (b SelectBuilder) OrderBy(orderBys ...string) SelectBuilder

OrderBy adds ORDER BY expressions to the query.

func (SelectBuilder) PageSize

func (b SelectBuilder) PageSize(pageSize int) SelectBuilder

PageSize sets size of rows the query should fetch a time

func (SelectBuilder) PagingState

func (b SelectBuilder) PagingState(pagingState []byte) SelectBuilder

PagingState sets a continuation token on where the query will resume for the next page.

func (SelectBuilder) PlaceholderFormat

func (b SelectBuilder) PlaceholderFormat(f PlaceholderFormat) SelectBuilder

PlaceholderFormat sets PlaceholderFormat (e.g. Question or Dollar) for the query.

func (SelectBuilder) RightJoin

func (b SelectBuilder) RightJoin(join string, rest ...interface{}) SelectBuilder

RightJoin adds a RIGHT JOIN clause to the query.

func (SelectBuilder) StmtType

func (b SelectBuilder) StmtType() StmtType

StmtType returns type of the statement

func (SelectBuilder) ToSQL

func (b SelectBuilder) ToSQL() (string, []interface{}, error)

ToSQL builds the query into a SQL string and bound args.

func (SelectBuilder) ToUql

func (b SelectBuilder) ToUql() (query string, args []interface{},
	options map[string]interface{}, err error)

ToUql builds the query into a UQL string and bound args. As an runtime optimization, it also returns query options

func (SelectBuilder) Where

func (b SelectBuilder) Where(pred interface{}, args ...interface{}) SelectBuilder

Where adds an expression to the WHERE clause of the query.

Expressions are ANDed together in the generated SQL.

Where accepts several types for its pred argument:

nil OR "" - ignored.

string - SQL expression. If the expression has SQL placeholders then a set of arguments must be passed as well, one for each placeholder.

map[string]interface{} OR Eq - map of SQL expressions to values. Each key is transformed into an expression like "<key> = ?", with the corresponding value bound to the placeholder. If the value is nil, the expression will be "<key> IS NULL". If the value is an array or slice, the expression will be "<key> IN (?,?,...)", with one placeholder for each item in the value. These expressions are ANDed together.

Where will panic if pred isn't any of the above types.

type Sqlizer

type Sqlizer interface {
	ToSQL() (string, []interface{}, error)
}

Sqlizer is the interface that wraps the ToSql method. ToSQL returns a SQL representation of the Sqlizer, along with a slice of args as passed to e.g. database/sql.Exec. It can also return an error.

type StatementAccessor

type StatementAccessor interface {
	// GetWhereParts gets the where clause of this statement
	GetWhereParts() []Sqlizer
	// GetResource gets the resource type (that is, the table) on which this statement operates
	GetResource() string
	// GetColumns returns the selected columns and is applicable for select stmt only. Other types
	// of statements will return nil
	GetColumns() []Sqlizer
}

StatementAccessor provides an interface to access statement internals

type StatementBuilderType

type StatementBuilderType builder.Builder

StatementBuilderType is the type of StatementBuilder.

func (StatementBuilderType) Delete

func (b StatementBuilderType) Delete(from string) DeleteBuilder

Delete returns a DeleteBuilder for this StatementBuilderType.

func (StatementBuilderType) Insert

func (b StatementBuilderType) Insert(into string) InsertBuilder

Insert returns a InsertBuilder for this StatementBuilderType.

func (StatementBuilderType) PlaceholderFormat

PlaceholderFormat sets the PlaceholderFormat field for any child builders.

func (StatementBuilderType) Select

func (b StatementBuilderType) Select(columns ...string) SelectBuilder

Select returns a SelectBuilder for this StatementBuilderType.

func (StatementBuilderType) Update

func (b StatementBuilderType) Update(table string) UpdateBuilder

Update returns a UpdateBuilder for this StatementBuilderType.

type StmtType

type StmtType int

StmtType indicates the type of statement

const (
	SelectStmtType StmtType = iota
	InsertStmtType
	UpdateStmtType
	DeleteStmtType
)

Constants for statement type

type UUID

type UUID struct {
	gocql.UUID
}

UUID represents the cassandra uuid data type

func ParseUUID

func ParseUUID(input string) (UUID, error)

ParseUUID creates an UUID object from a string

type UpdateBuilder

type UpdateBuilder builder.Builder

UpdateBuilder builds SQL UPDATE statements.

func Update

func Update(table string) UpdateBuilder

Update returns a new UpdateBuilder with the given table name.

See UpdateBuilder.Table.

func (UpdateBuilder) Add

func (b UpdateBuilder) Add(column string, value interface{}) UpdateBuilder

Add appends a value to a list column.

func (UpdateBuilder) AddSTApplyMetadata

func (b UpdateBuilder) AddSTApplyMetadata(value []byte) UpdateBuilder

AddSTApplyMetadata adds a value for the special st_apply_metadata column.

func (UpdateBuilder) GetData

func (b UpdateBuilder) GetData() StatementAccessor

GetData returns the underlying struct as an interface

func (UpdateBuilder) IfOnly

func (b UpdateBuilder) IfOnly(pred interface{}, rest ...interface{}) UpdateBuilder

IfOnly represents a LWT

func (UpdateBuilder) IsCAS

func (b UpdateBuilder) IsCAS() bool

IsCAS returns true is the update statement has a compare-and-set part

func (UpdateBuilder) PlaceholderFormat

func (b UpdateBuilder) PlaceholderFormat(f PlaceholderFormat) UpdateBuilder

PlaceholderFormat sets PlaceholderFormat (e.g. Question or Dollar) for the update.

func (UpdateBuilder) Remove

func (b UpdateBuilder) Remove(column string, value interface{}) UpdateBuilder

Remove discards a value from a list column

func (UpdateBuilder) Set

func (b UpdateBuilder) Set(column string, value interface{}) UpdateBuilder

Set adds SET clauses to the update.

func (UpdateBuilder) SetMap

func (b UpdateBuilder) SetMap(clauses map[string]interface{}) UpdateBuilder

SetMap is a convenience method which calls .Set for each key/value pair in clauses.

func (UpdateBuilder) StmtType

func (b UpdateBuilder) StmtType() StmtType

StmtType returns type of the statement

func (UpdateBuilder) Table

func (b UpdateBuilder) Table(table string) UpdateBuilder

Table sets the table to be updated.

func (UpdateBuilder) ToSQL

func (b UpdateBuilder) ToSQL() (string, []interface{}, error)

ToSQL builds the update into a SQL string and bound args.

func (UpdateBuilder) ToUql

func (b UpdateBuilder) ToUql() (query string, args []interface{},
	options map[string]interface{}, err error)

ToUql builds the query into a UQL string and bound args. As an runtime optimization, it also returns query options

func (UpdateBuilder) Using

func (b UpdateBuilder) Using(sql string, args ...interface{}) UpdateBuilder

Using adds an expression to the end of the update.

func (UpdateBuilder) Where

func (b UpdateBuilder) Where(pred interface{}, args ...interface{}) UpdateBuilder

Where adds WHERE expressions to the update.

See SelectBuilder.Where for more information.

Jump to

Keyboard shortcuts

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