genorm

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2024 License: MIT Imports: 7 Imported by: 1

README

GenORM

MIT License

SQL Builder to prevent SQL mistakes using the Golang generics

document

Feature

By mapping SQL expressions to appropriate golang types using generics, you can discover many SQL mistakes at the time of compilation that are not prevented by traditional Golang ORMs or query builders.

For example:

  • Compilation error occurs when using values of different Go types in SQL for = etc. comparisons or updating values in UPDATE statements
  • Compile error occurs when using column names of unavailable tables

It also supports many CRUD syntaxes in SQL.

Example

Example 1

String column users.name can be compared to a string value, but comparing it to an int value will result in a compile error.

// correct
userValues, err := genorm.
	Select(orm.User()).
	Where(genorm.EqLit(user.NameExpr, genorm.Wrap("name"))).
	GetAll(db)

// compile error
userValues, err := genorm.
	Select(orm.User()).
	Where(genorm.EqLit(user.NameExpr, genorm.Wrap(1))).
	GetAll(db)
Example 2

You can use an id column from the users table in a SELECT statement that retrieves data from the users table, but using an id column from the messages table will result in a compile error.

// correct
userValues, err := genorm.
	Select(orm.User()).
	Where(genorm.EqLit(user.IDExpr, uuid.New())).
	GetAll(db)

// compile error
userValues, err := genorm.
	Select(orm.User()).
	Where(genorm.EqLit(message.IDExpr, uuid.New())).
	GetAll(db)

Install

GenORM uses the CLI to generate code. The genormpackage is used to invoke queries. For this reason, both the CLI and Package must be install.

CLI
go install github.com/mazrean/genorm/cmd/genorm@v1.0.0
Package
go get -u github.com/mazrean/genorm
Configuration
Example

The users table can join the messages table.

import "github.com/mazrean/genorm"

type User struct {
    // Column Information
    Message genorm.Ref[Message]
}

func (*User) TableName() string {
    return "users"
}

type Message struct {
    // Column Information
}

func (*Message) TableName() string {
    return "messages"
}

Usage

Connecting to a Database
import (
  "database/sql"
  _ "github.com/go-sql-driver/mysql"
)

db, err := sql.Open("mysql", "user:pass@tcp(host:port)/database?parseTime=true&loc=Asia%2FTokyo&charset=utf8mb4")
Insert
// INSERT INTO users (id, name, created_at) VALUES ({{uuid.New()}}, "name1", {{time.Now()}}), ({{uuid.New()}}, "name2", {{time.Now()}})
affectedRows, err := genorm.
    Insert(orm.User()).
    Values(&orm.UserTable{
        ID: uuid.New(),
        Name: genorm.Wrap("name1"),
        CreatedAt: genorm.Wrap(time.Now()),
    }, &orm.UserTable{
        ID: uuid.New(),
        Name: genorm.Wrap("name2"),
        CreatedAt: genorm.Wrap(time.Now()),
    }).
    Do(db)
Select
// SELECT id, name, created_at FROM users
// userValues: []orm.UserTable
userValues, err := genorm.
	Select(orm.User()).
	GetAll(db)

// SELECT id, name, created_at FROM users LIMIT 1
// userValue: orm.UserTable
userValue, err := genorm.
	Select(orm.User()).
	Get(db)

// SELECT id FROM users
// userIDs: []uuid.UUID
userIDs, err := genorm.
	Pluck(orm.User(), user.IDExpr).
	GetAll(db)

// SELECT COUNT(id) AS result FROM users LIMIT 1
// userNum: int64
userNum, err := genorm.
	Pluck(orm.User(), genorm.Count(user.IDExpr, false)).
	Get(db)
Update
// UPDATE users SET name="name"
affectedRows, err = genorm.
    Update(orm.User()).
    Set(
        genorm.AssignLit(user.Name, genorm.Wrap("name")),
    ).
    Do(db)
Delete
// DELETE FROM users
affectedRows, err = genorm.
    Delete(orm.User()).
    Do(db)
Join
Select
// SELECT users.name, messages.content FROM users INNER JOIN messages ON users.id = messages.user_id
// messageUserValues: []orm.MessageUserTable
userID := orm.MessageUserParseExpr(user.ID)
userName := orm.MessageUserParse(user.Name)
messageUserID := orm.MessageUserParseExpr(message.UserID)
messageContent := orm.MessageUserParse(message.Content)
messageUserValues, err := genorm.
	Select(orm.User().
		Message().Join(genorm.Eq(userID, messageUserID))).
	Fields(userName, messageContent).
	GetAll(db)
Update
// UPDATE users INNER JOIN messages ON users.id = messages.id SET content="hello world"
userIDColumn := orm.MessageUserParseExpr(user.ID)
messageUserIDColumn := orm.MessageUserParseExpr(message.UserID)
messageContent := orm.MessageUserParse(message.Content)
affectedRows, err := genorm.
  Update(orm.User().
		Message().Join(genorm.Eq(userID, messageUserID))).
  Set(genorm.AssignLit(messageContent, genorm.Wrap("hello world"))).
  Do(db)
Transaction
tx, err := db.Begin()
if err != nil {
    log.Fatal(err)
}

_, err = genorm.
    Insert(orm.User()).
    Values(&orm.UserTable{
        ID: uuid.New(),
        Name: genorm.Wrap("name1"),
        CreatedAt: genorm.Wrap(time.Now()),
    }, &orm.UserTable{
        ID: uuid.New(),
        Name: genorm.Wrap("name2"),
        CreatedAt: genorm.Wrap(time.Now()),
    }).
    Do(db)
if err != nil {
    _ = tx.Rollback()
    log.Fatal(err)
}

err = tx.Commit()
if err != nil {
    log.Fatal(err)
}
Context
// SELECT id, name, created_at FROM users
// userValues: []orm.UserTable
userValues, err := genorm.
	Select(orm.User()).
	GetAllCtx(context.Background(), db)
// INSERT INTO users (id, name, created_at) VALUES ({{uuid.New()}}, "name", {{time.Now()}})
affectedRows, err := genorm.
    Insert(orm.User()).
    Values(&orm.UserTable{
        ID: uuid.New(),
        Name: genorm.Wrap("name"),
        CreatedAt: genorm.Wrap(time.Now()),
    }).
    DoCtx(context.Background(), db)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRecordNotFound = errors.New("record not found")
	ErrNullValue      = errors.New("null value")
)

Functions

This section is empty.

Types

type BasicTable

type BasicTable interface {
	Table
	// TableName table_name
	TableName() string
}

type Column

type Column interface {
	Expr
	// SQLColumnName table_name.column_name
	SQLColumnName() string
	// TableName table_name
	TableName() string
	// ColumnName column_name
	ColumnName() string
}

type ColumnFieldExprType

type ColumnFieldExprType interface {
	sql.Scanner
	driver.Valuer
}

type ColumnFieldExprTypePointer added in v1.1.1

type ColumnFieldExprTypePointer[T ExprType] interface {
	ColumnFieldExprType
	*T
}

type Context

type Context[T Table] struct {
	// contains filtered or unexported fields
}

func (*Context[T]) Errors

func (c *Context[T]) Errors() []error

func (*Context[T]) Table

func (c *Context[T]) Table() T

type DB

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

type DeleteContext

type DeleteContext[T BasicTable] struct {
	*Context[T]
	// contains filtered or unexported fields
}

func Delete

func Delete[T BasicTable](table T) *DeleteContext[T]

func (*DeleteContext[T]) Do

func (c *DeleteContext[T]) Do(db DB) (rowsAffected int64, err error)

func (*DeleteContext[T]) DoCtx

func (c *DeleteContext[T]) DoCtx(ctx context.Context, db DB) (rowsAffected int64, err error)

func (*DeleteContext[T]) Limit

func (c *DeleteContext[T]) Limit(limit uint64) *DeleteContext[T]

func (*DeleteContext[T]) OrderBy

func (c *DeleteContext[T]) OrderBy(direction OrderDirection, expr TableExpr[T]) *DeleteContext[T]

func (*DeleteContext[T]) Where

func (c *DeleteContext[T]) Where(
	condition TypedTableExpr[T, WrappedPrimitive[bool]],
) *DeleteContext[T]

type Expr

type Expr interface {
	Expr() (string, []ExprType, []error)
}

type ExprPrimitive

type ExprPrimitive interface {
	bool |
		int | int8 | int16 | int32 | int64 |
		uint | uint8 | uint16 | uint32 | uint64 |
		float32 | float64 |
		string | time.Time
}

type ExprStruct

type ExprStruct[T Table, S ExprType] struct {
	// contains filtered or unexported fields
}

func RawExpr

func RawExpr[T Table, S ExprType](query string, args ...ExprType) *ExprStruct[T, S]

func (*ExprStruct[_, _]) Expr

func (es *ExprStruct[_, _]) Expr() (string, []ExprType, []error)

func (*ExprStruct[T, _]) TableExpr

func (es *ExprStruct[T, _]) TableExpr(T) (string, []ExprType, []error)

func (*ExprStruct[_, S]) TypedExpr

func (es *ExprStruct[_, S]) TypedExpr(S) (string, []ExprType, []error)

type ExprType

type ExprType interface {
	driver.Valuer
}

type FindContext added in v1.1.1

type FindContext[S Table, T TuplePointer[U], U any] struct {
	*Context[S]
	// contains filtered or unexported fields
}

func Find added in v1.1.1

func Find[T Table, S TuplePointer[U], U any](table T, tuple S) *FindContext[T, S, U]

func (*FindContext[S, T, U]) Distinct added in v1.1.1

func (c *FindContext[S, T, U]) Distinct() *FindContext[S, T, U]

func (*FindContext[S, T, U]) Get added in v1.1.1

func (c *FindContext[S, T, U]) Get(db DB) (T, error)

func (*FindContext[S, T, U]) GetAll added in v1.1.1

func (c *FindContext[S, T, U]) GetAll(db DB) ([]T, error)

func (*FindContext[S, T, U]) GetAllCtx added in v1.1.1

func (c *FindContext[S, T, U]) GetAllCtx(ctx context.Context, db DB) ([]T, error)

func (*FindContext[S, T, U]) GetCtx added in v1.1.1

func (c *FindContext[S, T, U]) GetCtx(ctx context.Context, db DB) (T, error)

func (*FindContext[S, T, U]) GroupBy added in v1.1.1

func (c *FindContext[S, T, U]) GroupBy(exprs ...TableExpr[S]) *FindContext[S, T, U]

func (*FindContext[S, T, U]) Having added in v1.1.1

func (c *FindContext[S, T, U]) Having(
	condition TypedTableExpr[S, WrappedPrimitive[bool]],
) *FindContext[S, T, U]

func (*FindContext[S, T, U]) Limit added in v1.1.1

func (c *FindContext[S, T, U]) Limit(limit uint64) *FindContext[S, T, U]

func (*FindContext[S, T, U]) Lock added in v1.1.1

func (c *FindContext[S, T, U]) Lock(lockType LockType) *FindContext[S, T, U]

func (*FindContext[S, T, U]) Offset added in v1.1.1

func (c *FindContext[S, T, U]) Offset(offset uint64) *FindContext[S, T, U]

func (*FindContext[S, T, U]) OrderBy added in v1.1.1

func (c *FindContext[S, T, U]) OrderBy(direction OrderDirection, expr TableExpr[S]) *FindContext[S, T, U]

func (*FindContext[S, T, U]) Where added in v1.1.1

func (c *FindContext[S, T, U]) Where(
	condition TypedTableExpr[S, WrappedPrimitive[bool]],
) *FindContext[S, T, U]

type InsertContext

type InsertContext[T BasicTable] struct {
	*Context[T]
	// contains filtered or unexported fields
}

func Insert

func Insert[T BasicTable](table T) *InsertContext[T]

func (*InsertContext[T]) Do

func (c *InsertContext[T]) Do(db DB) (rowsAffected int64, err error)

func (*InsertContext[T]) DoCtx

func (c *InsertContext[T]) DoCtx(ctx context.Context, db DB) (rowsAffected int64, err error)

func (*InsertContext[T]) Fields

func (c *InsertContext[T]) Fields(fields ...TableColumns[T]) *InsertContext[T]

func (*InsertContext[T]) Values

func (c *InsertContext[T]) Values(tableBases ...T) *InsertContext[T]

type JoinedTable

type JoinedTable interface {
	Table
	BaseTables() []BasicTable
	AddError(error)
}

type LockType

type LockType uint8
const (
	ForUpdate LockType
	ForShare
)

type OrderDirection

type OrderDirection uint8
const (
	Asc OrderDirection = iota + 1
	Desc
)

type PluckContext

type PluckContext[T Table, S ExprType] struct {
	*Context[T]
	// contains filtered or unexported fields
}

func Pluck

func Pluck[T Table, S ExprType](table T, field TypedTableExpr[T, S]) *PluckContext[T, S]

func (*PluckContext[T, S]) Distinct

func (c *PluckContext[T, S]) Distinct() *PluckContext[T, S]

func (*PluckContext[T, S]) Get

func (c *PluckContext[T, S]) Get(db DB) (S, error)

func (*PluckContext[T, S]) GetAll

func (c *PluckContext[T, S]) GetAll(db DB) ([]S, error)

func (*PluckContext[T, S]) GetAllCtx

func (c *PluckContext[T, S]) GetAllCtx(ctx context.Context, db DB) ([]S, error)

func (*PluckContext[T, S]) GetCtx

func (c *PluckContext[T, S]) GetCtx(ctx context.Context, db DB) (S, error)

func (*PluckContext[T, S]) GroupBy

func (c *PluckContext[T, S]) GroupBy(exprs ...TableExpr[T]) *PluckContext[T, S]

func (*PluckContext[T, S]) Having

func (c *PluckContext[T, S]) Having(
	condition TypedTableExpr[T, WrappedPrimitive[bool]],
) *PluckContext[T, S]

func (*PluckContext[T, S]) Limit

func (c *PluckContext[T, S]) Limit(limit uint64) *PluckContext[T, S]

func (*PluckContext[T, S]) Lock

func (c *PluckContext[T, S]) Lock(lockType LockType) *PluckContext[T, S]

func (*PluckContext[T, S]) Offset

func (c *PluckContext[T, S]) Offset(offset uint64) *PluckContext[T, S]

func (*PluckContext[T, S]) OrderBy

func (c *PluckContext[T, S]) OrderBy(direction OrderDirection, expr TableExpr[T]) *PluckContext[T, S]

func (*PluckContext[T, S]) Where

func (c *PluckContext[T, S]) Where(
	condition TypedTableExpr[T, WrappedPrimitive[bool]],
) *PluckContext[T, S]

type Ref

type Ref[T any] struct{}

type SelectContext

type SelectContext[S any, T TablePointer[S]] struct {
	*Context[T]
	// contains filtered or unexported fields
}

func Select

func Select[S any, T TablePointer[S]](table T) *SelectContext[S, T]

func (*SelectContext[S, T]) Distinct

func (c *SelectContext[S, T]) Distinct() *SelectContext[S, T]

func (*SelectContext[S, T]) Fields

func (c *SelectContext[S, T]) Fields(fields ...TableColumns[T]) *SelectContext[S, T]

func (*SelectContext[S, T]) Get

func (c *SelectContext[S, T]) Get(db DB) (T, error)

func (*SelectContext[S, T]) GetAll

func (c *SelectContext[S, T]) GetAll(db DB) ([]T, error)

func (*SelectContext[S, T]) GetAllCtx

func (c *SelectContext[S, T]) GetAllCtx(ctx context.Context, db DB) ([]T, error)

func (*SelectContext[S, T]) GetCtx

func (c *SelectContext[S, T]) GetCtx(ctx context.Context, db DB) (T, error)

func (*SelectContext[S, T]) GroupBy

func (c *SelectContext[S, T]) GroupBy(exprs ...TableExpr[T]) *SelectContext[S, T]

func (*SelectContext[S, T]) Having

func (c *SelectContext[S, T]) Having(
	condition TypedTableExpr[T, WrappedPrimitive[bool]],
) *SelectContext[S, T]

func (*SelectContext[S, T]) Limit

func (c *SelectContext[S, T]) Limit(limit uint64) *SelectContext[S, T]

func (*SelectContext[S, T]) Lock

func (c *SelectContext[S, T]) Lock(lockType LockType) *SelectContext[S, T]

func (*SelectContext[S, T]) Offset

func (c *SelectContext[S, T]) Offset(offset uint64) *SelectContext[S, T]

func (*SelectContext[S, T]) OrderBy

func (c *SelectContext[S, T]) OrderBy(direction OrderDirection, expr TableExpr[T]) *SelectContext[S, T]

func (*SelectContext[S, T]) Where

func (c *SelectContext[S, T]) Where(
	condition TypedTableExpr[T, WrappedPrimitive[bool]],
) *SelectContext[S, T]

type Table

type Table interface {
	Expr
	Columns() []Column
	// ColumnMap key: table_name.column_name
	ColumnMap() map[string]ColumnFieldExprType
	GetErrors() []error
}

type TableAssignExpr

type TableAssignExpr[T Table] struct {
	// contains filtered or unexported fields
}

func Assign

func Assign[T Table, S ExprType](
	expr1 TypedTableColumns[T, S],
	expr2 TypedTableExpr[T, S],
) *TableAssignExpr[T]

Assign expr1 = expr2

func AssignLit

func AssignLit[T Table, S ExprType](
	expr TypedTableColumns[T, S],
	literal S,
) *TableAssignExpr[T]

AssignLit expr = literal

func (*TableAssignExpr[_]) AssignExpr

func (tae *TableAssignExpr[_]) AssignExpr() (string, []ExprType, []error)

type TableColumns

type TableColumns[T Table] interface {
	Column
	TableExpr[T]
}

type TableExpr

type TableExpr[T Table] interface {
	Expr
	TableExpr(T) (string, []ExprType, []error)
}

type TablePointer added in v1.1.1

type TablePointer[T any] interface {
	Table
	*T
}

type Tuple added in v1.1.1

type Tuple interface {
	Exprs() []Expr
	Columns() []ColumnFieldExprType
}

type Tuple2Struct added in v1.1.1

type Tuple2Struct[
	S Table,
	T1 ExprType, U1 ColumnFieldExprTypePointer[T1],
	T2 ExprType, U2 ColumnFieldExprTypePointer[T2],
] struct {
	// contains filtered or unexported fields
}

func Tuple2 added in v1.1.1

func Tuple2[
	S Table,
	T1 ExprType, U1 ColumnFieldExprTypePointer[T1],
	T2 ExprType, U2 ColumnFieldExprTypePointer[T2],
](expr1 TypedTableExpr[S, T1], expr2 TypedTableExpr[S, T2]) *Tuple2Struct[S, T1, U1, T2, U2]

func (*Tuple2Struct[_, _, U1, _, U2]) Columns added in v1.1.1

func (t *Tuple2Struct[_, _, U1, _, U2]) Columns() []ColumnFieldExprType

func (*Tuple2Struct[_, _, _, _, _]) Exprs added in v1.1.1

func (t *Tuple2Struct[_, _, _, _, _]) Exprs() []Expr

func (*Tuple2Struct[_, T1, _, T2, _]) Values added in v1.1.1

func (t *Tuple2Struct[_, T1, _, T2, _]) Values() (T1, T2)

type Tuple3Struct added in v1.1.1

type Tuple3Struct[
	S Table,
	T1 ExprType, U1 ColumnFieldExprTypePointer[T1],
	T2 ExprType, U2 ColumnFieldExprTypePointer[T2],
	T3 ExprType, U3 ColumnFieldExprTypePointer[T3],
] struct {
	// contains filtered or unexported fields
}

func Tuple3 added in v1.1.1

func Tuple3[
	S Table,
	T1 ExprType, U1 ColumnFieldExprTypePointer[T1],
	T2 ExprType, U2 ColumnFieldExprTypePointer[T2],
	T3 ExprType, U3 ColumnFieldExprTypePointer[T3],
](expr1 TypedTableExpr[S, T1], expr2 TypedTableExpr[S, T2], expr3 TypedTableExpr[S, T3]) *Tuple3Struct[S, T1, U1, T2, U2, T3, U3]

func (*Tuple3Struct[_, _, U1, _, U2, _, U3]) Columns added in v1.1.1

func (t *Tuple3Struct[_, _, U1, _, U2, _, U3]) Columns() []ColumnFieldExprType

func (*Tuple3Struct[_, _, _, _, _, _, _]) Exprs added in v1.1.1

func (t *Tuple3Struct[_, _, _, _, _, _, _]) Exprs() []Expr

func (*Tuple3Struct[_, T1, _, T2, _, T3, _]) Values added in v1.1.1

func (t *Tuple3Struct[_, T1, _, T2, _, T3, _]) Values() (T1, T2, T3)

type Tuple4Struct added in v1.1.1

type Tuple4Struct[
	S Table,
	T1 ExprType, U1 ColumnFieldExprTypePointer[T1],
	T2 ExprType, U2 ColumnFieldExprTypePointer[T2],
	T3 ExprType, U3 ColumnFieldExprTypePointer[T3],
	T4 ExprType, U4 ColumnFieldExprTypePointer[T4],
] struct {
	// contains filtered or unexported fields
}

func Tuple4 added in v1.1.1

func Tuple4[
	S Table,
	T1 ExprType, U1 ColumnFieldExprTypePointer[T1],
	T2 ExprType, U2 ColumnFieldExprTypePointer[T2],
	T3 ExprType, U3 ColumnFieldExprTypePointer[T3],
	T4 ExprType, U4 ColumnFieldExprTypePointer[T4],
](expr1 TypedTableExpr[S, T1], expr2 TypedTableExpr[S, T2], expr3 TypedTableExpr[S, T3], expr4 TypedTableExpr[S, T4]) *Tuple4Struct[S, T1, U1, T2, U2, T3, U3, T4, U4]

func (*Tuple4Struct[_, _, U1, _, U2, _, U3, _, U4]) Columns added in v1.1.1

func (t *Tuple4Struct[_, _, U1, _, U2, _, U3, _, U4]) Columns() []ColumnFieldExprType

func (*Tuple4Struct[_, _, _, _, _, _, _, _, _]) Exprs added in v1.1.1

func (t *Tuple4Struct[_, _, _, _, _, _, _, _, _]) Exprs() []Expr

func (*Tuple4Struct[_, T1, _, T2, _, T3, _, T4, _]) Values added in v1.1.1

func (t *Tuple4Struct[_, T1, _, T2, _, T3, _, T4, _]) Values() (T1, T2, T3, T4)

type Tuple5Struct added in v1.1.1

type Tuple5Struct[
	S Table,
	T1 ExprType, U1 ColumnFieldExprTypePointer[T1],
	T2 ExprType, U2 ColumnFieldExprTypePointer[T2],
	T3 ExprType, U3 ColumnFieldExprTypePointer[T3],
	T4 ExprType, U4 ColumnFieldExprTypePointer[T4],
	T5 ExprType, U5 ColumnFieldExprTypePointer[T5],
] struct {
	// contains filtered or unexported fields
}

func Tuple5 added in v1.1.1

func Tuple5[
	S Table,
	T1 ExprType, U1 ColumnFieldExprTypePointer[T1],
	T2 ExprType, U2 ColumnFieldExprTypePointer[T2],
	T3 ExprType, U3 ColumnFieldExprTypePointer[T3],
	T4 ExprType, U4 ColumnFieldExprTypePointer[T4],
	T5 ExprType, U5 ColumnFieldExprTypePointer[T5],
](expr1 TypedTableExpr[S, T1], expr2 TypedTableExpr[S, T2], expr3 TypedTableExpr[S, T3], expr4 TypedTableExpr[S, T4], expr5 TypedTableExpr[S, T5]) *Tuple5Struct[S, T1, U1, T2, U2, T3, U3, T4, U4, T5, U5]

func (*Tuple5Struct[_, _, U1, _, U2, _, U3, _, U4, _, U5]) Columns added in v1.1.1

func (t *Tuple5Struct[_, _, U1, _, U2, _, U3, _, U4, _, U5]) Columns() []ColumnFieldExprType

func (*Tuple5Struct[_, _, _, _, _, _, _, _, _, _, _]) Exprs added in v1.1.1

func (t *Tuple5Struct[_, _, _, _, _, _, _, _, _, _, _]) Exprs() []Expr

func (*Tuple5Struct[_, T1, _, T2, _, T3, _, T4, _, T5, _]) Values added in v1.1.1

func (t *Tuple5Struct[_, T1, _, T2, _, T3, _, T4, _, T5, _]) Values() (T1, T2, T3, T4, T5)

type TuplePointer added in v1.1.1

type TuplePointer[T any] interface {
	Exprs() []Expr
	Columns() []ColumnFieldExprType
	*T
}

type TypedColumns

type TypedColumns[S ExprType] interface {
	Column
	TypedExpr[S]
}

type TypedExpr

type TypedExpr[T ExprType] interface {
	Expr
	TypedExpr(T) (string, []ExprType, []error)
}

type TypedTableColumns

type TypedTableColumns[T Table, S ExprType] interface {
	TableColumns[T]
	TypedColumns[S]
}

type TypedTableExpr

type TypedTableExpr[T Table, S ExprType] interface {
	Expr
	TableExpr[T]
	TypedExpr[S]
}

func And

And (expr1 AND expr2)

func Avg

func Avg[T Table, S ExprType](expr TypedTableExpr[T, S], distinct bool) TypedTableExpr[T, WrappedPrimitive[float64]]

Avg if (distinct) {return AVG(DISTINCT expr)} else {return AVG(expr)}

func Count

func Count[T Table, S ExprType](expr TypedTableExpr[T, S], distinct bool) TypedTableExpr[T, WrappedPrimitive[int64]]

Count if (distinct) {return COUNT(DISTINCT expr)} else {return COUNT(expr)}

func Eq

func Eq[T Table, S ExprType](
	expr1 TypedTableExpr[T, S],
	expr2 TypedTableExpr[T, S],
) TypedTableExpr[T, WrappedPrimitive[bool]]

Eq (expr1 = expr2)

func EqLit

func EqLit[T Table, S ExprType](
	expr TypedTableExpr[T, S],
	literal S,
) TypedTableExpr[T, WrappedPrimitive[bool]]

EqLit (expr = literal)

func Geq

func Geq[T Table, S ExprType](
	expr1 TypedTableExpr[T, S],
	expr2 TypedTableExpr[T, S],
) TypedTableExpr[T, WrappedPrimitive[bool]]

Geq (expr1 >= expr2)

func GeqLit

func GeqLit[T Table, S ExprType](
	expr TypedTableExpr[T, S],
	literal S,
) TypedTableExpr[T, WrappedPrimitive[bool]]

GeqLit (expr >= literal)

func Gt

func Gt[T Table, S ExprType](
	expr1 TypedTableExpr[T, S],
	expr2 TypedTableExpr[T, S],
) TypedTableExpr[T, WrappedPrimitive[bool]]

Gt (expr1 > expr2)

func GtLit

func GtLit[T Table, S ExprType](
	expr TypedTableExpr[T, S],
	literal S,
) TypedTableExpr[T, WrappedPrimitive[bool]]

GtLit (expr > literal)

func In

func In[T Table, S ExprType](
	expr1 TypedTableExpr[T, S],
	exprs ...TypedTableExpr[T, S],
) TypedTableExpr[T, WrappedPrimitive[bool]]

In (expr1 IN (exprs[0], exprs[1], ...))

func InLit

func InLit[T Table, S ExprType](
	expr TypedTableExpr[T, S],
	literals ...S,
) TypedTableExpr[T, WrappedPrimitive[bool]]

InLit (expr IN (literals[0], literal[1], ...))

func IsNotNull

func IsNotNull[T Table, S ExprType](
	expr TypedTableExpr[T, S],
) TypedTableExpr[T, WrappedPrimitive[bool]]

IsNotNull (expr IS NOT NULL)

func IsNull

func IsNull[T Table, S ExprType](
	expr TypedTableExpr[T, S],
) TypedTableExpr[T, WrappedPrimitive[bool]]

IsNull (expr IS NULL)

func Leq

func Leq[T Table, S ExprType](
	expr1 TypedTableExpr[T, S],
	expr2 TypedTableExpr[T, S],
) TypedTableExpr[T, WrappedPrimitive[bool]]

Leq (expr1 <= expr2)

func LeqLit

func LeqLit[T Table, S ExprType](
	expr TypedTableExpr[T, S],
	literal S,
) TypedTableExpr[T, WrappedPrimitive[bool]]

LeqLit (expr <= literal)

func Lt

func Lt[T Table, S ExprType](
	expr1 TypedTableExpr[T, S],
	expr2 TypedTableExpr[T, S],
) TypedTableExpr[T, WrappedPrimitive[bool]]

Lt (expr1 < expr2)

func LtLit

func LtLit[T Table, S ExprType](
	expr TypedTableExpr[T, S],
	literal S,
) TypedTableExpr[T, WrappedPrimitive[bool]]

LtLit (expr < literal)

func Max

func Max[T Table, S ExprType](expr TypedTableExpr[T, S]) TypedTableExpr[T, S]

Max MAX(expr)

func Min

func Min[T Table, S ExprType](expr TypedTableExpr[T, S]) TypedTableExpr[T, S]

Min MIN(expr)

func Neq

func Neq[T Table, S ExprType](
	expr1 TypedTableExpr[T, S],
	expr2 TypedTableExpr[T, S],
) TypedTableExpr[T, WrappedPrimitive[bool]]

Neq (expr1 != expr2)

func NeqLit

func NeqLit[T Table, S ExprType](
	expr TypedTableExpr[T, S],
	literal S,
) TypedTableExpr[T, WrappedPrimitive[bool]]

NeqLit (expr != literal)

func Not

Not (NOT expr)

func NotIn

func NotIn[T Table, S ExprType](
	expr1 TypedTableExpr[T, S],
	exprs ...TypedTableExpr[T, S],
) TypedTableExpr[T, WrappedPrimitive[bool]]

NotIn (expr1 NOT IN (exprs[0], exprs[1], ...))

func NotInLit

func NotInLit[T Table, S ExprType](
	expr TypedTableExpr[T, S],
	literals ...S,
) TypedTableExpr[T, WrappedPrimitive[bool]]

NotInLit (expr NOT IN (literals[0], literal[1], ...))

func Or

Or (expr1 OR expr2)

func Xor

Xor (expr1 XOR expr2)

type UpdateContext

type UpdateContext[T Table] struct {
	*Context[T]
	// contains filtered or unexported fields
}

func Update

func Update[T Table](table T) *UpdateContext[T]

func (*UpdateContext[T]) Do

func (c *UpdateContext[T]) Do(db DB) (rowsAffected int64, err error)

func (*UpdateContext[T]) DoCtx

func (c *UpdateContext[T]) DoCtx(ctx context.Context, db DB) (rowsAffected int64, err error)

func (*UpdateContext[T]) Limit

func (c *UpdateContext[T]) Limit(limit uint64) *UpdateContext[T]

func (*UpdateContext[T]) OrderBy

func (c *UpdateContext[T]) OrderBy(direction OrderDirection, expr TableExpr[T]) *UpdateContext[T]

func (*UpdateContext[T]) Set

func (c *UpdateContext[T]) Set(assignExprs ...*TableAssignExpr[T]) (res *UpdateContext[T])

func (*UpdateContext[T]) Where

func (c *UpdateContext[T]) Where(
	condition TypedTableExpr[T, WrappedPrimitive[bool]],
) *UpdateContext[T]

type WrappedPrimitive

type WrappedPrimitive[T ExprPrimitive] struct {
	// contains filtered or unexported fields
}

func Wrap

func Wrap[T ExprPrimitive](val T) WrappedPrimitive[T]

func (*WrappedPrimitive[T]) Scan

func (wp *WrappedPrimitive[T]) Scan(src any) error

func (WrappedPrimitive[T]) Val

func (wp WrappedPrimitive[T]) Val() (T, bool)

func (WrappedPrimitive[_]) Value

func (wp WrappedPrimitive[_]) Value() (driver.Value, error)

Jump to

Keyboard shortcuts

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