xsql

package
v1.0.11 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package xsql provides wrappers around the standard database/sql package to allow the generated code to interact with a statically-typed API.

Index

Constants

View Source
const (
	ForGroupBy ForWhat = "FOR GROUP BY"
	ForOrderBy ForWhat = "FOR ORDER BY"
	ForJoin    ForWhat = "FOR JOIN"
	ForDefault ForWhat = ""

	USE    IndexAction = "USE INDEX"
	IGNORE IndexAction = "IGNORE INDEX"
	FORCE  IndexAction = "FORCE INDEX"
)

Variables

This section is empty.

Functions

func As

func As(ident string, as string) string

As suffixed the given column with an alias (`a` AS `b`).

func Asc

func Asc(column string) string

Asc adds the ASC suffix for the given column.

func Avg

func Avg(ident string) string

Avg wraps the ident with the AVG aggregation function.

func Count

func Count(ident string) string

Count wraps the ident with the COUNT aggregation function.

func Desc

func Desc(column string) string

Desc adds the DESC suffix for the given column.

func Distinct

func Distinct(idents ...string) string

Distinct prefixed the given columns with the `DISTINCT` keyword (DISTINCT `id`).

func Int64

func Int64(ctx context.Context, query Querier, eq ExecQuerier) (int64, error)

Int64 scans and returns an int64 from the rows columns.

func Int64s

func Int64s(ctx context.Context, query Querier, eq ExecQuerier) ([]int64, error)

Int64s scans and returns an int64 slice form the rows columns

func Lower

func Lower(ident string) string

Lower wraps the given column with the LOWER function.

P().EQ(sql.Lower("name"), "a8m")

func Max

func Max(ident string) string

Max wraps the ident with the MAX aggregation function.

func Min

func Min(ident string) string

Min wraps the ident with the MIN aggregation function.

func ScanInt

func ScanInt(rows ColumnScanner) (int, error)

ScanInt scans and returns an int from the rows columns.

func ScanInt64

func ScanInt64(rows ColumnScanner) (int64, error)

ScanInt64 scans and returns an int64 from the rows columns.

func ScanOne

func ScanOne(rows ColumnScanner, v interface{}) error

ScanOne scans one row to the given value. It fails if the rows holds more than 1 row.

func ScanSlice

func ScanSlice(rows ColumnScanner, v interface{}) error

ScanSlice scans the given ColumnScanner (basically, sql.Row or sql.Rows) into the given slice.

func ScanString

func ScanString(rows ColumnScanner) (string, error)

ScanString scans and returns a string from the rows columns.

func ScanValue

func ScanValue(rows ColumnScanner) (driver.Value, error)

ScanValue scans and returns a driver.Value from the rows columns.

func Shrink added in v1.0.4

func String

func String(ctx context.Context, query Querier, eq ExecQuerier) (string, error)

String scans and returns an string from the rows columns.

func Strings

func Strings(ctx context.Context, query Querier, eq ExecQuerier) ([]string, error)

Strings scans and returns an string slice form the rows columns

func Sum

func Sum(ident string) string

Sum wraps the ident with the SUM aggregation function.

Types

type Builder

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

Builder is the base query builder for the sql dsl.

func (*Builder) AddError

func (b *Builder) AddError(err error) *Builder

AddError appends an error to the builder errors.

func (*Builder) Arg

func (b *Builder) Arg(a interface{}) *Builder

Arg appends an input argument to the builder.

func (*Builder) Args

func (b *Builder) Args(a ...interface{}) *Builder

Args appends a list of arguments to the builder.

func (*Builder) Comma

func (b *Builder) Comma() *Builder

Comma adds a comma to the query.

func (Builder) Dialect

func (b Builder) Dialect() string

Dialect returns the dialect of the builder.

func (*Builder) Err

func (b *Builder) Err() error

Err returns a concatenated error of all errors encountered during the query-building, or were added manually by calling AddError.

func (*Builder) Ident

func (b *Builder) Ident(s string) *Builder

Ident appends the given string as an identifier.

func (*Builder) IdentComma

func (b *Builder) IdentComma(s ...string) *Builder

IdentComma calls Ident on all arguments and adds a comma between them.

func (*Builder) Join

func (b *Builder) Join(qs ...Querier) *Builder

Join joins a list of Queries to the builder.

func (*Builder) JoinComma

func (b *Builder) JoinComma(qs ...Querier) *Builder

JoinComma joins a list of Queries and adds comma between them.

func (*Builder) Len

func (b *Builder) Len() int

Len returns the number of accumulated bytes.

func (*Builder) Nested

func (b *Builder) Nested(f func(*Builder)) *Builder

Nested gets a callback, and wraps its result with parentheses.

func (*Builder) Pad

func (b *Builder) Pad() *Builder

Pad adds a space to the query.

func (Builder) Query

func (b Builder) Query() (string, []interface{})

Query implements the Querier interface.

func (*Builder) Quote

func (b *Builder) Quote(ident string) string

Quote quotes the given identifier with the characters based on the configured dialect. It defaults to "`".

func (*Builder) Reset

func (b *Builder) Reset() *Builder

Reset resets the Builder to be empty.

func (*Builder) SetDialect

func (b *Builder) SetDialect(dialect string)

SetDialect sets the builder dialect. It's used for garnering dialect specific queries.

func (*Builder) SetTotal

func (b *Builder) SetTotal(total int)

SetTotal sets the value of the total arguments. Used to pass this information between sub queries/expressions.

func (*Builder) String

func (b *Builder) String() string

String returns the accumulated string.

func (Builder) Total

func (b Builder) Total() int

Total returns the total number of arguments so far.

func (*Builder) WriteByte

func (b *Builder) WriteByte(c byte) *Builder

WriteByte wraps the Buffer.WriteByte to make it chainable with other methods.

func (*Builder) WriteOp

func (b *Builder) WriteOp(op Op) *Builder

WriteOp writes an operator to the builder.

func (*Builder) WriteString

func (b *Builder) WriteString(s string) *Builder

WriteString wraps the Buffer.WriteString to make it chainable with other methods.

type ColumnScanner

type ColumnScanner interface {
	Close() error
	ColumnTypes() ([]*sql.ColumnType, error)
	Columns() ([]string, error)
	Err() error
	Next() bool
	NextResultSet() bool
	Scan(dest ...interface{}) error
}

type Config added in v1.0.4

type Config struct {
	DSN          string        // write data source name.
	ReadDSN      []string      // read data source name.
	Active       int           // pool
	Idle         int           // pool
	IdleTimeout  time.Duration // connect max life time.
	QueryTimeout time.Duration // query sql timeout
	ExecTimeout  time.Duration // execute sql timeout
}

type DB added in v1.0.4

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

func NewMySQL added in v1.0.4

func NewMySQL(c *Config) (*DB, error)

func (*DB) BeginTx added in v1.0.4

func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)

func (*DB) ExecContext added in v1.0.4

func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (*DB) Master added in v1.0.4

func (db *DB) Master() *sql.DB

func (*DB) PingContext added in v1.0.4

func (db *DB) PingContext(ctx context.Context) error

func (*DB) QueryContext added in v1.0.4

func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

type DBI added in v1.0.4

type DBI interface {
	ExecQuerier
	Begin() (*sql.Tx, error)
	BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
}

type DebugDB

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

func Debug

func Debug(db DBI) *DebugDB

func (*DebugDB) Begin

func (d *DebugDB) Begin() (*DebugTx, error)

func (*DebugDB) BeginTx added in v1.0.4

func (d *DebugDB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*DebugTx, error)

func (*DebugDB) ExecContext

func (d *DebugDB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (*DebugDB) QueryContext

func (d *DebugDB) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

type DebugTx

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

func (*DebugTx) ExecContext

func (d *DebugTx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (*DebugTx) QueryContext

func (d *DebugTx) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

type DeleteBuilder

type DeleteBuilder struct {
	Builder
	// contains filtered or unexported fields
}

DeleteBuilder is a builder for `DELETE` statement.

func Delete

func Delete(table string) *DeleteBuilder

Delete creates a builder for the `DELETE` statement.

Delete("users").
	Where(
		Or(
			EQ("name", "foo").And().EQ("age", 10),
			EQ("name", "bar").And().EQ("age", 20),
			And(
				EQ("name", "qux"),
				EQ("age", 1).Or().EQ("age", 2),
			),
		),
	)

func (*DeleteBuilder) FromSelect

func (d *DeleteBuilder) FromSelect(s *Selector) *DeleteBuilder

FromSelect makes it possible to delete a sub query.

func (*DeleteBuilder) Query

func (d *DeleteBuilder) Query() (string, []interface{})

Query returns query representation of a `DELETE` statement.

func (*DeleteBuilder) Schema

func (d *DeleteBuilder) Schema(name string) *DeleteBuilder

Schema sets the database name for the table whose row will be deleted.

func (*DeleteBuilder) Table

func (d *DeleteBuilder) Table(name string) *DeleteBuilder

Table sets the table name for the table whose row will be deleted.

func (*DeleteBuilder) Where

func (d *DeleteBuilder) Where(p *Predicate) *DeleteBuilder

Where appends a where predicate to the `DELETE` statement.

type ExecQuerier

type ExecQuerier interface {
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
}

type ForWhat

type ForWhat string

type Func

type Func struct {
	Builder
	// contains filtered or unexported fields
}

Func represents an SQL function.

func (*Func) Append

func (f *Func) Append(fn func(*Builder)) *Func

Append appends a new function to the function callbacks. The callback list are executed on call to String.

func (*Func) Avg

func (f *Func) Avg(ident string)

Avg wraps the ident with the AVG aggregation function.

func (*Func) Count

func (f *Func) Count(ident string)

Count wraps the ident with the COUNT aggregation function.

func (*Func) Lower

func (f *Func) Lower(ident string)

Lower wraps the given ident with the LOWER function.

func (*Func) Max

func (f *Func) Max(ident string)

Max wraps the ident with the MAX aggregation function.

func (*Func) Min

func (f *Func) Min(ident string)

Min wraps the ident with the MIN aggregation function.

func (*Func) String

func (f *Func) String() string

String implements the fmt.Stringer.

func (*Func) Sum

func (f *Func) Sum(ident string)

Sum wraps the ident with the SUM aggregation function.

type IndexAction

type IndexAction string

type IndexOptions

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

type InsertBuilder

type InsertBuilder struct {
	Builder
	// contains filtered or unexported fields
}

InsertBuilder is a builder for `INSERT INTO` statement.

func Insert

func Insert(table string) *InsertBuilder

Insert creates a builder for the `INSERT INTO` statement.

Insert("users").
	Columns("name", "age").
	Values("a8m", 10).
	Values("foo", 20)

Note: Insert inserts all values in one batch.

func (*InsertBuilder) Columns

func (i *InsertBuilder) Columns(columns ...string) *InsertBuilder

Columns sets the columns of the insert statement.

func (*InsertBuilder) OnDuplicateKeyUpdate

func (i *InsertBuilder) OnDuplicateKeyUpdate(columns ...string) *InsertBuilder

OnDuplicateKeyUpdate UpdateColumns generate like "ON DUPLICATE KEY UPDATE `id` = VALUES (`id`) " sql statement

func (*InsertBuilder) OnDuplicateKeyUpdateExpr

func (i *InsertBuilder) OnDuplicateKeyUpdateExpr(q ...Querier) *InsertBuilder

OnDuplicateKeyUpdateExpr OnDuplicateKeyUpdateExpr generate like "ON DUPLICATE KEY UPDATE `c` = VALUES(`a`) + Values(`b`)"

func (*InsertBuilder) Query

func (i *InsertBuilder) Query() (string, []interface{})

Query returns query representation of an `INSERT INTO` statement.

func (*InsertBuilder) Schema

func (i *InsertBuilder) Schema(name string) *InsertBuilder

Schema sets the database name for the insert table.

func (*InsertBuilder) Set

func (i *InsertBuilder) Set(column string, v interface{}) *InsertBuilder

Set is a syntactic sugar API for inserting only one row.

func (*InsertBuilder) Table

func (i *InsertBuilder) Table(name string) *InsertBuilder

Table sets the table name for the insert table.

func (*InsertBuilder) Values

func (i *InsertBuilder) Values(values ...interface{}) *InsertBuilder

Values append a value tuple for the insert statement.

type LockAction

type LockAction string

LockAction tells the transaction what to do in case of requesting a row that is locked by other transaction.

const (
	// NoWait means never wait and returns an error.
	NoWait LockAction = "NOWAIT"
	// SkipLocked means never wait and skip.
	SkipLocked LockAction = "SKIP LOCKED"
)

type LockOption

type LockOption func(*LockOptions)

LockOption allows configuring the LockConfig using functional options.

func WithLockAction

func WithLockAction(action LockAction) LockOption

WithLockAction sets the Action of the lock.

type LockOptions

type LockOptions struct {
	// Strength of the lock.
	Strength LockStrength
	// Action of the lock.
	Action LockAction
}

LockOptions defines a SELECT statement lock for protecting concurrent updates.

type LockStrength

type LockStrength string

LockStrength defines the strength of the lock (see the list below).

const (
	LockShare  LockStrength = "SHARE"
	LockUpdate LockStrength = "UPDATE"
)

A list of all locking clauses.

type Logger added in v1.0.4

type Logger interface {
	Printf(format string, v ...interface{})
}

type Op

type Op int

An Op represents a predicate operator.

const (
	OpEQ      Op = iota // logical and.
	OpNEQ               // <>
	OpGT                // >
	OpGTE               // >=
	OpLT                // <
	OpLTE               // <=
	OpIn                // IN
	OpNotIn             // NOT IN
	OpLike              // LIKE
	OpIsNull            // IS NULL
	OpNotNull           // IS NOT NULL
)

Predicate operators

func VailedOp added in v1.0.4

func VailedOp(op string) (vailed bool, t Op)

type ParamFormatter

type ParamFormatter interface {
	// The FormatParam function lets users to define
	// custom placeholder formatting for their types.
	// For example, formatting the default placeholder
	// from '?' to 'ST_GeomFromWKB(?)' for MySQL dialect.
	FormatParam(placeholder string, info *StmtInfo) string
}

ParamFormatter wraps the FormatPram function.

type Predicate

type Predicate struct {
	Builder
	// contains filtered or unexported fields
}

Predicate is a where predicate.

func And

func And(preds ...*Predicate) *Predicate

And combines all given predicates with AND between them.

func CompositeGT

func CompositeGT(columns []string, args ...interface{}) *Predicate

CompositeGT returns a comiposite ">" predicate

func CompositeLT

func CompositeLT(columns []string, args ...interface{}) *Predicate

CompositeLT returns a comiposite "<" predicate

func Contains

func Contains(col, sub string) *Predicate

Contains is a helper predicate that checks substring using the LIKE predicate.

func EQ

func EQ(col string, value interface{}) *Predicate

EQ returns a "=" predicate.

func EqualFold

func EqualFold(col, sub string) *Predicate

EqualFold is a helper predicate that applies the "=" predicate with case-folding.

func ExprP

func ExprP(exr string, args ...interface{}) *Predicate

ExprP creates a new predicate from the given expression.

ExprP("A = ? AND B > ?", args...)

func False

func False() *Predicate

False appends the FALSE keyword to the predicate.

Delete().From("users").Where(False())

func GT

func GT(col string, value interface{}) *Predicate

GT returns a ">" predicate.

func GTE

func GTE(col string, value interface{}) *Predicate

GTE returns a ">=" predicate.

func GenP added in v1.0.4

func GenP(field, op, value string) (*Predicate, error)

func HasPrefix

func HasPrefix(col, prefix string) *Predicate

HasPrefix is a helper predicate that checks prefix using the LIKE predicate.

func HasSuffix

func HasSuffix(col, suffix string) *Predicate

HasSuffix is a helper predicate that checks suffix using the LIKE predicate.

func In

func In(col string, args ...interface{}) *Predicate

In returns the `IN` predicate.

func InInts

func InInts(col string, args ...int) *Predicate

InInts returns the `IN` predicate for ints.

func InValues

func InValues(col string, args ...driver.Value) *Predicate

InValues adds the `IN` predicate for slice of driver.Value.

func IsNull

func IsNull(col string) *Predicate

IsNull returns the `IS NULL` predicate.

func LT

func LT(col string, value interface{}) *Predicate

LT returns a "<" predicate.

func LTE

func LTE(col string, value interface{}) *Predicate

LTE returns a "<=" predicate.

func Like

func Like(col, pattern string) *Predicate

Like returns the `LIKE` predicate.

func NEQ

func NEQ(col string, value interface{}) *Predicate

NEQ returns a "<>" predicate.

func Not

func Not(pred *Predicate) *Predicate

Not wraps the given predicate with the not predicate.

Not(Or(EQ("name", "foo"), EQ("name", "bar")))

func NotIn

func NotIn(col string, args ...interface{}) *Predicate

NotIn returns the `Not IN` predicate.

func NotNull

func NotNull(col string) *Predicate

NotNull returns the `IS NOT NULL` predicate.

func Or

func Or(preds ...*Predicate) *Predicate

Or combines all given predicates with OR between them.

Or(EQ("name", "foo"), EQ("name", "bar"))

func P

func P(fns ...func(*Builder)) *Predicate

P creates a new predicate.

P().EQ("name", "a8m").And().EQ("age", 30)

func (*Predicate) Append

func (p *Predicate) Append(f func(*Builder)) *Predicate

Append appends a new function to the predicate callbacks. The callback list are executed on call to ry.

func (*Predicate) CompositeGT

func (p *Predicate) CompositeGT(columns []string, args ...interface{}) *Predicate

CompositeGT returns a composite ">" predicate.

func (*Predicate) CompositeLT

func (p *Predicate) CompositeLT(columns []string, args ...interface{}) *Predicate

CompositeLT appends a composite "<" predicate.

func (*Predicate) Contains

func (p *Predicate) Contains(col, sub string) *Predicate

Contains is a helper predicate that checks substring using the LIKE predicate.

func (*Predicate) EQ

func (p *Predicate) EQ(col string, arg interface{}) *Predicate

EQ appends a "=" predicate.

func (*Predicate) EqualFold

func (p *Predicate) EqualFold(col, sub string) *Predicate

EqualFold is a helper predicate that applies the "=" predicate with case-folding.

func (*Predicate) False

func (p *Predicate) False() *Predicate

False appends FALSE to the predicate.

func (*Predicate) GT

func (p *Predicate) GT(col string, arg interface{}) *Predicate

GT appends a ">" predicate.

func (*Predicate) GTE

func (p *Predicate) GTE(col string, arg interface{}) *Predicate

GTE appends a ">=" predicate.

func (*Predicate) HasPrefix

func (p *Predicate) HasPrefix(col, prefix string) *Predicate

HasPrefix is a helper predicate that checks prefix using the LIKE predicate.

func (*Predicate) HasSuffix

func (p *Predicate) HasSuffix(col, suffix string) *Predicate

HasSuffix is a helper predicate that checks suffix using the LIKE predicate.

func (*Predicate) In

func (p *Predicate) In(col string, args ...interface{}) *Predicate

In appends the `IN` predicate.

func (*Predicate) InInts

func (p *Predicate) InInts(col string, args ...int) *Predicate

InInts adds the `IN` predicate for ints.

func (*Predicate) InValues

func (p *Predicate) InValues(col string, args ...driver.Value) *Predicate

InValues adds the `IN` predicate for slice of driver.Value.

func (*Predicate) IsNull

func (p *Predicate) IsNull(col string) *Predicate

IsNull appends the `IS NULL` predicate.

func (*Predicate) LT

func (p *Predicate) LT(col string, arg interface{}) *Predicate

LT appends a "<" predicate.

func (*Predicate) LTE

func (p *Predicate) LTE(col string, arg interface{}) *Predicate

LTE appends a "<=" predicate.

func (*Predicate) Like

func (p *Predicate) Like(col, pattern string) *Predicate

Like appends the `LIKE` predicate.

func (*Predicate) NEQ

func (p *Predicate) NEQ(col string, arg interface{}) *Predicate

NEQ appends a "<>" predicate.

func (*Predicate) Not

func (p *Predicate) Not() *Predicate

Not appends NOT to the predicate.

func (*Predicate) NotIn

func (p *Predicate) NotIn(col string, args ...interface{}) *Predicate

NotIn appends the `Not IN` predicate.

func (*Predicate) NotNull

func (p *Predicate) NotNull(col string) *Predicate

NotNull appends the `IS NOT NULL` predicate.

func (*Predicate) Query

func (p *Predicate) Query() (string, []interface{})

Query returns query representation of a predicate.

type Querier

type Querier interface {
	// Query returns the query representation of the element
	// and its arguments (if any).
	Query() (string, []interface{})
}

Querier wraps the basic Query method that is implemented by the different builders in this file.

func Expr

func Expr(exr string, args ...interface{}) Querier

Expr returns an SQL expression that implements the Querier interface.

func Raw

func Raw(s string) Querier

Raw returns a raw SQL query that is placed as-is in the query.

type Queries

type Queries []Querier

Queries are list of queries join with space between them.

func (Queries) Query

func (n Queries) Query() (string, []interface{})

Query returns query representation of Queriers.

type SelectTable

type SelectTable struct {
	Builder
	// contains filtered or unexported fields
}

SelectTable is a table selector.

func Table

func Table(name string) *SelectTable

Table returns a new table selector.

t1 := Table("users").As("u")
return Select(t1.C("name"))

func (*SelectTable) As

func (s *SelectTable) As(alias string) *SelectTable

As adds the AS clause to the table selector.

func (*SelectTable) C

func (s *SelectTable) C(column string) string

C returns a formatted string for the table column.

func (*SelectTable) Columns

func (s *SelectTable) Columns(columns ...string) []string

Columns returns a list of formatted strings for the table columns.

func (*SelectTable) Schema

func (s *SelectTable) Schema(name string) *SelectTable

Schema sets the schema name of the table.

func (*SelectTable) Unquote

func (s *SelectTable) Unquote() *SelectTable

Unquote makes the table name to be formatted as raw string (unquoted). It is useful whe you don't want to query tables under the current database. For example: "INFORMATION_SCHEMA.TABLE_CONSTRAINTS" in MySQL.

type Selector

type Selector struct {
	Builder
	// contains filtered or unexported fields
}

Selector is a builder for the `SELECT` statement.

func Select

func Select(columns ...string) *Selector

Select returns a new selector for the `SELECT` statement.

t1 := Table("users").As("u")
t2 := Select().From(Table("groups")).Where(EQ("user_id", 10)).As("g")
return Select(t1.C("id"), t2.C("name")).
		From(t1).
		Join(t2).
		On(t1.C("id"), t2.C("user_id"))

func (*Selector) As

func (s *Selector) As(alias string) *Selector

As give this selection an alias.

func (*Selector) C

func (s *Selector) C(column string) string

C returns a formatted string for a selected column from this statement.

func (*Selector) Clone

func (s *Selector) Clone() *Selector

Clone returns a duplicate of the selector, including all associated steps. It can be used to prepare common SELECT statements and use them differently after the clone is made.

func (*Selector) Columns

func (s *Selector) Columns(columns ...string) []string

Columns returns a list of formatted strings for a selected columns from this statement.

func (*Selector) Context

func (s *Selector) Context() context.Context

Context returns the Selector context or Background if nil.

func (*Selector) Count

func (s *Selector) Count(columns ...string) *Selector

Count sets the Select statement to be a `SELECT COUNT(*)`.

func (*Selector) Distinct

func (s *Selector) Distinct() *Selector

Distinct adds the DISTINCT keyword to the `SELECT` statement.

func (*Selector) For

func (s *Selector) For(l LockStrength, opts ...LockOption) *Selector

For sets the lock configuration for suffixing the `SELECT` statement with the `FOR [SHARE | UPDATE] ...` clause.

func (*Selector) ForShare

func (s *Selector) ForShare(opts ...LockOption) *Selector

ForShare sets the lock configuration for suffixing the `SELECT` statement with the `FOR SHARE` clause.

func (*Selector) ForUpdate

func (s *Selector) ForUpdate(opts ...LockOption) *Selector

ForUpdate sets the lock configuration for suffixing the `SELECT` statement with the `FOR UPDATE` clause.

func (*Selector) ForceIndex

func (s *Selector) ForceIndex(indexName ...string) *Selector

ForceIndex generate FORCE INDEX (`indexName`)

func (*Selector) ForceIndexFor

func (s *Selector) ForceIndexFor(forwhat ForWhat, indexName ...string) *Selector

ForceIndexFor generate FORCE INDEX FOR xxx (`indexName`)

func (*Selector) From

func (s *Selector) From(t TableView) *Selector

From sets the source of `FROM` clause.

func (*Selector) FromSelect

func (s *Selector) FromSelect(s2 *Selector) *Selector

FromSelect copies the predicate from a selector.

func (*Selector) GroupBy

func (s *Selector) GroupBy(columns ...string) *Selector

GroupBy appends the `GROUP BY` clause to the `SELECT` statement.

func (*Selector) Having

func (s *Selector) Having(p *Predicate) *Selector

Having appends a predicate for the `HAVING` clause.

func (*Selector) IgnoreIndex

func (s *Selector) IgnoreIndex(indexName ...string) *Selector

IgnoreIndex generate IGNORE INDEX (`indexName`)

func (*Selector) IgnoreIndexFor

func (s *Selector) IgnoreIndexFor(forwhat ForWhat, indexName ...string) *Selector

IgnoreIndexFor generate IGNORE INDEX FOR xxx (`indexName`)

func (*Selector) Join

func (s *Selector) Join(t TableView) *Selector

Join appends a `JOIN` clause to the statement.

func (*Selector) LeftJoin

func (s *Selector) LeftJoin(t TableView) *Selector

LeftJoin appends a `LEFT JOIN` clause to the statement.

func (*Selector) Limit

func (s *Selector) Limit(limit int) *Selector

Limit adds the `LIMIT` clause to the `SELECT` statement.

func (*Selector) Not

func (s *Selector) Not() *Selector

Not sets the next coming predicate with not.

func (*Selector) Offset

func (s *Selector) Offset(offset int) *Selector

Offset adds the `OFFSET` clause to the `SELECT` statement.

func (*Selector) On

func (s *Selector) On(c1, c2 string) *Selector

On sets the `ON` clause for the `JOIN` operation.

func (*Selector) OnP

func (s *Selector) OnP(p *Predicate) *Selector

OnP sets or appends the given predicate for the `ON` clause of the statement.

func (*Selector) Or

func (s *Selector) Or() *Selector

Or sets the next coming predicate with OR operator (disjunction).

func (*Selector) OrderBy

func (s *Selector) OrderBy(columns ...string) *Selector

OrderBy appends the `ORDER BY` clause to the `SELECT` statement.

func (*Selector) OrderExpr

func (s *Selector) OrderExpr(exprs ...Querier) *Selector

OrderExpr appends the `ORDER BY` clause to the `SELECT` statement with custom list of expressions.

func (*Selector) P

func (s *Selector) P() *Predicate

P returns the predicate of a selector.

func (*Selector) Query

func (s *Selector) Query() (string, []interface{})

Query returns query representation of a `SELECT` statement.

func (*Selector) RightJoin

func (s *Selector) RightJoin(t TableView) *Selector

RightJoin appends a `RIGHT JOIN` clause to the statement.

func (*Selector) Select

func (s *Selector) Select(columns ...string) *Selector

Select changes the columns selection of the SELECT statement. Empty selection means all columns *.

func (*Selector) SelectColumnsLen

func (s *Selector) SelectColumnsLen() int

SelectColumnsLen returns len of select columns

func (*Selector) SelectedColumns added in v1.0.9

func (s *Selector) SelectedColumns() []string

func (*Selector) SetDistinct

func (s *Selector) SetDistinct(v bool) *Selector

SetDistinct sets explicitly if the returned rows are distinct or indistinct.

func (*Selector) SetP

func (s *Selector) SetP(p *Predicate) *Selector

SetP sets explicitly the predicate function for the selector and clear its previous state.

func (*Selector) Table

func (s *Selector) Table() *SelectTable

Table returns the selected table.

func (*Selector) UseIndex

func (s *Selector) UseIndex(indexName ...string) *Selector

UseIndex generate USE INDEX (`indexName`)

func (*Selector) UseIndexFor

func (s *Selector) UseIndexFor(forwhat ForWhat, indexName ...string) *Selector

UseIndexFor generate USE INDEX FOR xxx (`indexName`)

func (*Selector) Where

func (s *Selector) Where(p *Predicate) *Selector

Where sets or appends the given predicate to the statement.

func (*Selector) WithContext

func (s *Selector) WithContext(ctx context.Context) *Selector

WithContext sets the context into the *Selector.

type StmtInfo

type StmtInfo struct {
	// The Dialect of the SQL driver.
	Dialect string
}

StmtInfo holds an information regarding the statement

type TableView

type TableView interface {
	// contains filtered or unexported methods
}

TableView is a view that returns a table view. Can ne a Table, Selector or a View (WITH statement).

type UpdateBuilder

type UpdateBuilder struct {
	Builder
	// contains filtered or unexported fields
}

UpdateBuilder is a builder for `UPDATE` statement.

func Update

func Update(table string) *UpdateBuilder

Update creates a builder for the `UPDATE` statement.

Update("users").Set("name", "foo").Set("age", 10)

func (*UpdateBuilder) Add

func (u *UpdateBuilder) Add(column string, v interface{}) *UpdateBuilder

Add adds a numeric value to the given column.

func (*UpdateBuilder) Empty

func (u *UpdateBuilder) Empty() bool

Empty reports whether this builder does not contain update changes.

func (*UpdateBuilder) FromSelect

func (u *UpdateBuilder) FromSelect(s *Selector) *UpdateBuilder

FromSelect makes it possible to update entities that match the sub-query.

func (*UpdateBuilder) Query

func (u *UpdateBuilder) Query() (string, []interface{})

Query returns query representation of an `UPDATE` statement.

func (*UpdateBuilder) Schema

func (u *UpdateBuilder) Schema(name string) *UpdateBuilder

Schema sets the database name for the updated table.

func (*UpdateBuilder) Set

func (u *UpdateBuilder) Set(column string, v interface{}) *UpdateBuilder

Set sets a column and a its value.

func (*UpdateBuilder) SetNull

func (u *UpdateBuilder) SetNull(column string) *UpdateBuilder

SetNull sets a column as null value.

func (*UpdateBuilder) Table

func (u *UpdateBuilder) Table(name string) *UpdateBuilder

Table sets the table name for the updated table.

func (*UpdateBuilder) Where

func (u *UpdateBuilder) Where(p *Predicate) *UpdateBuilder

Where adds a where predicate for update statement.

Jump to

Keyboard shortcuts

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