bun

package module
v1.2.17 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2026 License: BSD-2-Clause Imports: 21 Imported by: 2,045

README

Bun: SQL-first Golang ORM

build workflow PkgGoDev Documentation Chat Gurubase

Lightweight, SQL-first Golang ORM for PostgreSQL, MySQL, MSSQL, SQLite, and Oracle

Bun is a modern ORM that embraces SQL rather than hiding it. Write complex queries in Go with type safety, powerful scanning capabilities, and database-agnostic code that works across multiple SQL databases.

✨ Key Features

  • SQL-first approach - Write elegant, readable queries that feel like SQL
  • Multi-database support - PostgreSQL, MySQL/MariaDB, MSSQL, SQLite, and Oracle
  • Type-safe operations - Leverage Go's static typing for compile-time safety
  • Flexible scanning - Query results into structs, maps, scalars, or slices
  • Performance optimized - Built on database/sql with minimal overhead
  • Rich relationships - Define complex table relationships with struct tags
  • Production ready - Migrations, fixtures, soft deletes, and OpenTelemetry support

🚀 Quick Start

go get github.com/uptrace/bun
Basic Example
package main

import (
    "context"
    "database/sql"
    "fmt"

    "github.com/uptrace/bun"
    "github.com/uptrace/bun/dialect/sqlitedialect"
    "github.com/uptrace/bun/driver/sqliteshim"
)

func main() {
    ctx := context.Background()

    // Open database
    sqldb, err := sql.Open(sqliteshim.ShimName, "file::memory:")
    if err != nil {
        panic(err)
    }

    // Create Bun instance
    db := bun.NewDB(sqldb, sqlitedialect.New())

    // Define model
    type User struct {
        ID   int64  `bun:",pk,autoincrement"`
        Name string `bun:",notnull"`
    }

    // Create table
    db.NewCreateTable().Model((*User)(nil)).Exec(ctx)

    // Insert user
    user := &User{Name: "John Doe"}
    db.NewInsert().Model(user).Exec(ctx)

    // Query user
    err = db.NewSelect().Model(user).Where("id = ?", user.ID).Scan(ctx)
    fmt.Printf("User: %+v\n", user)
}

🎯 Why Choose Bun?

Elegant Complex Queries

Write sophisticated queries that remain readable and maintainable:

regionalSales := db.NewSelect().
    ColumnExpr("region").
    ColumnExpr("SUM(amount) AS total_sales").
    TableExpr("orders").
    GroupExpr("region")

topRegions := db.NewSelect().
    ColumnExpr("region").
    TableExpr("regional_sales").
    Where("total_sales > (SELECT SUM(total_sales) / 10 FROM regional_sales)")

var results []struct {
    Region       string `bun:"region"`
    Product      string `bun:"product"`
    ProductUnits int    `bun:"product_units"`
    ProductSales int    `bun:"product_sales"`
}

err := db.NewSelect().
    With("regional_sales", regionalSales).
    With("top_regions", topRegions).
    ColumnExpr("region, product").
    ColumnExpr("SUM(quantity) AS product_units").
    ColumnExpr("SUM(amount) AS product_sales").
    TableExpr("orders").
    Where("region IN (SELECT region FROM top_regions)").
    GroupExpr("region, product").
    Scan(ctx, &results)
Flexible Result Scanning

Scan query results into various Go types:

// Into structs
var users []User
db.NewSelect().Model(&users).Scan(ctx)

// Into maps
var userMaps []map[string]interface{}
db.NewSelect().Table("users").Scan(ctx, &userMaps)

// Into scalars
var count int
db.NewSelect().Table("users").ColumnExpr("COUNT(*)").Scan(ctx, &count)

// Into individual variables
var id int64
var name string
db.NewSelect().Table("users").Column("id", "name").Limit(1).Scan(ctx, &id, &name)

📊 Database Support

Database Driver Dialect
PostgreSQL github.com/uptrace/bun/driver/pgdriver pgdialect.New()
MySQL/MariaDB github.com/go-sql-driver/mysql mysqldialect.New()
SQLite github.com/uptrace/bun/driver/sqliteshim sqlitedialect.New()
SQL Server github.com/denisenkom/go-mssqldb mssqldialect.New()
Oracle github.com/sijms/go-ora/v2 oracledialect.New()

🔧 Advanced Features

Table Relationships

Define complex relationships with struct tags:

type User struct {
    ID      int64   `bun:",pk,autoincrement"`
    Name    string  `bun:",notnull"`
    Posts   []Post  `bun:"rel:has-many,join:id=user_id"`
    Profile Profile `bun:"rel:has-one,join:id=user_id"`
}

type Post struct {
    ID     int64 `bun:",pk,autoincrement"`
    Title  string
    UserID int64
    User   *User `bun:"rel:belongs-to,join:user_id=id"`
}

// Load users with their posts
var users []User
err := db.NewSelect().
    Model(&users).
    Relation("Posts").
    Scan(ctx)
Bulk Operations

Efficient bulk operations for large datasets:

// Bulk insert
users := []User{{Name: "John"}, {Name: "Jane"}, {Name: "Bob"}}
_, err := db.NewInsert().Model(&users).Exec(ctx)

// Bulk update with CTE
_, err = db.NewUpdate().
    Model(&users).
    Set("updated_at = NOW()").
    Where("active = ?", true).
    Exec(ctx)

// Bulk delete
_, err = db.NewDelete().
    Model((*User)(nil)).
    Where("created_at < ?", time.Now().AddDate(-1, 0, 0)).
    Exec(ctx)
Migrations

Version your database schema:

import "github.com/uptrace/bun/migrate"

migrations := migrate.NewMigrations()

migrations.MustRegister(func(ctx context.Context, db *bun.DB) error {
    _, err := db.NewCreateTable().Model((*User)(nil)).Exec(ctx)
    return err
}, func(ctx context.Context, db *bun.DB) error {
    _, err := db.NewDropTable().Model((*User)(nil)).Exec(ctx)
    return err
})

migrator := migrate.NewMigrator(db, migrations)
err := migrator.Init(ctx)
err = migrator.Up(ctx)

📈 Monitoring & Observability

Debug Queries

Enable query logging for development:

import "github.com/uptrace/bun/extra/bundebug"

db.AddQueryHook(bundebug.NewQueryHook(
    bundebug.WithVerbose(true),
))
OpenTelemetry Integration

Production-ready observability with distributed tracing:

import "github.com/uptrace/bun/extra/bunotel"

db.AddQueryHook(bunotel.NewQueryHook(
    bunotel.WithDBName("myapp"),
))

Monitoring made easy: Bun is brought to you by ⭐ uptrace/uptrace. Uptrace is an open-source APM tool that supports distributed tracing, metrics, and logs. You can use it to monitor applications and set up automatic alerts to receive notifications via email, Slack, Telegram, and others.

See OpenTelemetry example which demonstrates how you can use Uptrace to monitor Bun.

📚 Documentation & Resources

🤝 Contributing

We welcome contributions! Please see our Contributing Guide for details on how to get started.

Thanks to all our contributors:

Contributors
Star ⭐ this repo if you find Bun useful!
Join our community on Discord • Follow updates on GitHub

Documentation

Index

Constants

View Source
const (
	// OrderAsc sorts values in ascending order.
	OrderAsc = schema.OrderAsc
	// OrderAscNullsFirst sorts ascending with NULL values first.
	OrderAscNullsFirst = schema.OrderAscNullsFirst
	// OrderAscNullsLast sorts ascending with NULL values last.
	OrderAscNullsLast = schema.OrderAscNullsLast
	// OrderDesc sorts values in descending order.
	OrderDesc = schema.OrderDesc
	// OrderDescNullsFirst sorts descending with NULL values first.
	OrderDescNullsFirst = schema.OrderDescNullsFirst
	// OrderDescNullsLast sorts descending with NULL values last.
	OrderDescNullsLast = schema.OrderDescNullsLast
)

Variables

This section is empty.

Functions

func In deprecated

func In(slice any) schema.QueryAppender

In wraps a slice so it can be used with the IN clause.

Deprecated: Use bun.List or bun.Tuple instead.

func IsReadOnlyQuery added in v1.2.10

func IsReadOnlyQuery(query Query) bool

IsReadOnlyQuery reports whether the provided query and its CTEs are SELECT-only.

func NullZero added in v1.1.15

func NullZero(value any) schema.QueryAppender

NullZero forces zero values to be treated as NULL when building queries.

func SafeQuery added in v1.2.2

func SafeQuery(query string, args ...any) schema.QueryWithArgs

SafeQuery wraps a raw query string and arguments and marks it safe for Bun.

func SetLogger added in v1.0.0

func SetLogger(logger internal.Logging)

SetLogger overwrites default Bun logger.

func Version added in v0.4.0

func Version() string

Version is the current release version.

func WithComment added in v1.2.10

func WithComment(ctx context.Context, comment string) context.Context

WithComment returns a context that includes a comment that may be included in a query for debugging

If a context with an attached query is used, a comment set by the Comment("...") API will be overwritten.

Types

type AddColumnQuery

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

AddColumnQuery builds ALTER TABLE ... ADD COLUMN statements.

func NewAddColumnQuery

func NewAddColumnQuery(db *DB) *AddColumnQuery

NewAddColumnQuery creates an AddColumnQuery bound to the provided DB.

func (*AddColumnQuery) AppendNamedArg added in v0.4.0

func (q *AddColumnQuery) AppendNamedArg(gen schema.QueryGen, b []byte, name string) ([]byte, bool)

func (*AddColumnQuery) AppendQuery

func (q *AddColumnQuery) AppendQuery(gen schema.QueryGen, b []byte) (_ []byte, err error)

func (*AddColumnQuery) Apply added in v1.1.10

func (q *AddColumnQuery) Apply(fns ...func(*AddColumnQuery) *AddColumnQuery) *AddColumnQuery

Apply calls each function in fns, passing the AddColumnQuery as an argument.

func (*AddColumnQuery) ColumnExpr

func (q *AddColumnQuery) ColumnExpr(query string, args ...any) *AddColumnQuery

func (*AddColumnQuery) Comment added in v1.2.8

func (q *AddColumnQuery) Comment(comment string) *AddColumnQuery

Comment adds a comment to the query, wrapped by /* ... */.

func (*AddColumnQuery) Conn added in v0.1.14

func (q *AddColumnQuery) Conn(db IConn) *AddColumnQuery

func (*AddColumnQuery) DB

func (q *AddColumnQuery) DB() *DB

func (*AddColumnQuery) Dialect added in v1.0.21

func (q *AddColumnQuery) Dialect() schema.Dialect

func (*AddColumnQuery) Err added in v1.1.10

func (q *AddColumnQuery) Err(err error) *AddColumnQuery

func (*AddColumnQuery) Exec

func (q *AddColumnQuery) Exec(ctx context.Context, dest ...any) (sql.Result, error)

func (*AddColumnQuery) GetModel added in v0.2.0

func (q *AddColumnQuery) GetModel() Model

func (*AddColumnQuery) GetTableName added in v1.0.9

func (q *AddColumnQuery) GetTableName() string

func (*AddColumnQuery) IfNotExists added in v1.0.15

func (q *AddColumnQuery) IfNotExists() *AddColumnQuery

func (*AddColumnQuery) Model

func (q *AddColumnQuery) Model(model any) *AddColumnQuery

func (*AddColumnQuery) ModelTableExpr

func (q *AddColumnQuery) ModelTableExpr(query string, args ...any) *AddColumnQuery

func (*AddColumnQuery) NewAddColumn added in v1.0.21

func (q *AddColumnQuery) NewAddColumn() *AddColumnQuery

func (*AddColumnQuery) NewCreateIndex added in v1.0.21

func (q *AddColumnQuery) NewCreateIndex() *CreateIndexQuery

func (*AddColumnQuery) NewCreateTable added in v1.0.21

func (q *AddColumnQuery) NewCreateTable() *CreateTableQuery

func (*AddColumnQuery) NewDelete added in v1.0.21

func (q *AddColumnQuery) NewDelete() *DeleteQuery

func (*AddColumnQuery) NewDropColumn added in v1.0.21

func (q *AddColumnQuery) NewDropColumn() *DropColumnQuery

func (*AddColumnQuery) NewDropIndex added in v1.0.21

func (q *AddColumnQuery) NewDropIndex() *DropIndexQuery

func (*AddColumnQuery) NewDropTable added in v1.0.21

func (q *AddColumnQuery) NewDropTable() *DropTableQuery

func (*AddColumnQuery) NewInsert added in v1.0.21

func (q *AddColumnQuery) NewInsert() *InsertQuery

func (*AddColumnQuery) NewRaw added in v1.1.8

func (q *AddColumnQuery) NewRaw(query string, args ...any) *RawQuery

func (*AddColumnQuery) NewSelect added in v1.0.21

func (q *AddColumnQuery) NewSelect() *SelectQuery

func (*AddColumnQuery) NewTruncateTable added in v1.0.21

func (q *AddColumnQuery) NewTruncateTable() *TruncateTableQuery

func (*AddColumnQuery) NewUpdate added in v1.0.21

func (q *AddColumnQuery) NewUpdate() *UpdateQuery

func (*AddColumnQuery) NewValues added in v1.0.21

func (q *AddColumnQuery) NewValues(model any) *ValuesQuery

func (*AddColumnQuery) Operation added in v1.0.5

func (q *AddColumnQuery) Operation() string

func (*AddColumnQuery) Table

func (q *AddColumnQuery) Table(tables ...string) *AddColumnQuery

func (*AddColumnQuery) TableExpr

func (q *AddColumnQuery) TableExpr(query string, args ...any) *AddColumnQuery

type AfterCreateTableHook added in v0.2.0

type AfterCreateTableHook interface {
	AfterCreateTable(ctx context.Context, query *CreateTableQuery) error
}

AfterCreateTableHook is invoked after executing CREATE TABLE queries.

type AfterDeleteHook

type AfterDeleteHook interface {
	AfterDelete(ctx context.Context, query *DeleteQuery) error
}

AfterDeleteHook is invoked after executing DELETE queries.

type AfterDropTableHook added in v0.2.0

type AfterDropTableHook interface {
	AfterDropTable(ctx context.Context, query *DropTableQuery) error
}

AfterDropTableHook is invoked after executing DROP TABLE queries.

type AfterInsertHook

type AfterInsertHook interface {
	AfterInsert(ctx context.Context, query *InsertQuery) error
}

AfterInsertHook is invoked after executing INSERT queries.

type AfterScanRowHook added in v1.0.13

type AfterScanRowHook = schema.AfterScanRowHook

AfterScanRowHook runs after scanning an individual row.

type AfterSelectHook

type AfterSelectHook interface {
	AfterSelect(ctx context.Context, query *SelectQuery) error
}

AfterSelectHook is invoked after executing SELECT queries.

type AfterUpdateHook

type AfterUpdateHook interface {
	AfterUpdate(ctx context.Context, query *UpdateQuery) error
}

AfterUpdateHook is invoked after executing UPDATE queries.

type BaseModel

type BaseModel = schema.BaseModel

BaseModel provides default metadata embedded into user models.

type BeforeAppendModelHook added in v1.0.13

type BeforeAppendModelHook = schema.BeforeAppendModelHook

BeforeAppendModelHook is called before a model is appended to a query.

type BeforeCreateTableHook added in v0.2.0

type BeforeCreateTableHook interface {
	BeforeCreateTable(ctx context.Context, query *CreateTableQuery) error
}

BeforeCreateTableHook is invoked before executing CREATE TABLE queries.

type BeforeDeleteHook

type BeforeDeleteHook interface {
	BeforeDelete(ctx context.Context, query *DeleteQuery) error
}

BeforeDeleteHook is invoked before executing DELETE queries.

type BeforeDropTableHook added in v0.2.0

type BeforeDropTableHook interface {
	BeforeDropTable(ctx context.Context, query *DropTableQuery) error
}

BeforeDropTableHook is invoked before executing DROP TABLE queries.

type BeforeInsertHook

type BeforeInsertHook interface {
	BeforeInsert(ctx context.Context, query *InsertQuery) error
}

BeforeInsertHook is invoked before executing INSERT queries.

type BeforeScanRowHook added in v1.0.13

type BeforeScanRowHook = schema.BeforeScanRowHook

BeforeScanRowHook runs before scanning an individual row.

type BeforeSelectHook added in v0.2.0

type BeforeSelectHook interface {
	BeforeSelect(ctx context.Context, query *SelectQuery) error
}

BeforeSelectHook is invoked before executing SELECT queries.

type BeforeUpdateHook

type BeforeUpdateHook interface {
	BeforeUpdate(ctx context.Context, query *UpdateQuery) error
}

BeforeUpdateHook is invoked before executing UPDATE queries.

type Conn

type Conn struct {
	*sql.Conn
	// contains filtered or unexported fields
}

Conn wraps *sql.Conn so queries continue to use Bun features and hooks.

func (Conn) BeginTx added in v1.1.6

func (c Conn) BeginTx(ctx context.Context, opts *sql.TxOptions) (Tx, error)

BeginTx starts a transaction on this connection with the given options.

func (Conn) Dialect added in v1.0.20

func (c Conn) Dialect() schema.Dialect

Dialect returns the database dialect for this connection.

func (Conn) ExecContext

func (c Conn) ExecContext(
	ctx context.Context, query string, args ...any,
) (sql.Result, error)

ExecContext executes a query without returning rows on this connection.

func (Conn) NewAddColumn added in v0.1.12

func (c Conn) NewAddColumn() *AddColumnQuery

NewAddColumn creates an ALTER TABLE ADD COLUMN query bound to this connection.

func (Conn) NewCreateIndex added in v0.1.12

func (c Conn) NewCreateIndex() *CreateIndexQuery

NewCreateIndex creates a CREATE INDEX query bound to this connection.

func (Conn) NewCreateTable added in v0.1.12

func (c Conn) NewCreateTable() *CreateTableQuery

NewCreateTable creates a CREATE TABLE query bound to this connection.

func (Conn) NewDelete added in v0.1.12

func (c Conn) NewDelete() *DeleteQuery

NewDelete creates a DELETE query bound to this connection.

func (Conn) NewDropColumn added in v0.1.12

func (c Conn) NewDropColumn() *DropColumnQuery

NewDropColumn creates an ALTER TABLE DROP COLUMN query bound to this connection.

func (Conn) NewDropIndex added in v0.1.12

func (c Conn) NewDropIndex() *DropIndexQuery

NewDropIndex creates a DROP INDEX query bound to this connection.

func (Conn) NewDropTable added in v0.1.12

func (c Conn) NewDropTable() *DropTableQuery

NewDropTable creates a DROP TABLE query bound to this connection.

func (Conn) NewInsert added in v0.1.12

func (c Conn) NewInsert() *InsertQuery

NewInsert creates an INSERT query bound to this connection.

func (Conn) NewMerge added in v1.1.10

func (c Conn) NewMerge() *MergeQuery

NewMerge creates a MERGE query bound to this connection.

func (Conn) NewRaw added in v1.1.8

func (c Conn) NewRaw(query string, args ...any) *RawQuery

NewRaw creates a raw SQL query bound to this connection.

func (Conn) NewSelect added in v0.1.12

func (c Conn) NewSelect() *SelectQuery

NewSelect creates a SELECT query bound to this connection.

func (Conn) NewTruncateTable added in v0.1.12

func (c Conn) NewTruncateTable() *TruncateTableQuery

NewTruncateTable creates a TRUNCATE TABLE query bound to this connection.

func (Conn) NewUpdate added in v0.1.12

func (c Conn) NewUpdate() *UpdateQuery

NewUpdate creates an UPDATE query bound to this connection.

func (Conn) NewValues added in v0.1.12

func (c Conn) NewValues(model any) *ValuesQuery

NewValues creates a VALUES query bound to this connection.

func (Conn) QueryContext

func (c Conn) QueryContext(
	ctx context.Context, query string, args ...any,
) (*sql.Rows, error)

QueryContext executes a query returning rows on this connection.

func (Conn) QueryRowContext

func (c Conn) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row

QueryRowContext executes a query expected to return at most one row on this connection.

func (Conn) RunInTx added in v1.1.6

func (c Conn) RunInTx(
	ctx context.Context, opts *sql.TxOptions, fn func(ctx context.Context, tx Tx) error,
) error

RunInTx runs the function in a transaction. If the function returns an error, the transaction is rolled back. Otherwise, the transaction is committed.

type ConnResolver added in v1.2.9

type ConnResolver interface {
	ResolveConn(ctx context.Context, query Query) IConn
	Close() error
}

ConnResolver enables routing queries to multiple databases.

type CreateIndexQuery

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

CreateIndexQuery builds CREATE INDEX statements.

func NewCreateIndexQuery

func NewCreateIndexQuery(db *DB) *CreateIndexQuery

NewCreateIndexQuery returns a CreateIndexQuery tied to the provided DB.

func (*CreateIndexQuery) AppendQuery

func (q *CreateIndexQuery) AppendQuery(gen schema.QueryGen, b []byte) (_ []byte, err error)

func (*CreateIndexQuery) Column

func (q *CreateIndexQuery) Column(columns ...string) *CreateIndexQuery

func (*CreateIndexQuery) ColumnExpr

func (q *CreateIndexQuery) ColumnExpr(query string, args ...any) *CreateIndexQuery

func (*CreateIndexQuery) Comment added in v1.2.8

func (q *CreateIndexQuery) Comment(comment string) *CreateIndexQuery

Comment adds a comment to the query, wrapped by /* ... */.

func (*CreateIndexQuery) Concurrently

func (q *CreateIndexQuery) Concurrently() *CreateIndexQuery

func (*CreateIndexQuery) Conn added in v0.1.14

func (*CreateIndexQuery) Err added in v1.1.10

func (*CreateIndexQuery) ExcludeColumn

func (q *CreateIndexQuery) ExcludeColumn(columns ...string) *CreateIndexQuery

func (*CreateIndexQuery) Exec

func (q *CreateIndexQuery) Exec(ctx context.Context, dest ...any) (sql.Result, error)

func (*CreateIndexQuery) IfNotExists

func (q *CreateIndexQuery) IfNotExists() *CreateIndexQuery

func (*CreateIndexQuery) Include

func (q *CreateIndexQuery) Include(columns ...string) *CreateIndexQuery

func (*CreateIndexQuery) IncludeExpr

func (q *CreateIndexQuery) IncludeExpr(query string, args ...any) *CreateIndexQuery

func (*CreateIndexQuery) Index

func (q *CreateIndexQuery) Index(query string) *CreateIndexQuery

func (*CreateIndexQuery) IndexExpr

func (q *CreateIndexQuery) IndexExpr(query string, args ...any) *CreateIndexQuery

func (*CreateIndexQuery) Model

func (q *CreateIndexQuery) Model(model any) *CreateIndexQuery

func (*CreateIndexQuery) ModelTableExpr

func (q *CreateIndexQuery) ModelTableExpr(query string, args ...any) *CreateIndexQuery

func (*CreateIndexQuery) Operation added in v1.0.5

func (q *CreateIndexQuery) Operation() string

func (*CreateIndexQuery) Table

func (q *CreateIndexQuery) Table(tables ...string) *CreateIndexQuery

func (*CreateIndexQuery) TableExpr

func (q *CreateIndexQuery) TableExpr(query string, args ...any) *CreateIndexQuery

func (*CreateIndexQuery) Unique

func (q *CreateIndexQuery) Unique() *CreateIndexQuery

func (*CreateIndexQuery) Using

func (q *CreateIndexQuery) Using(query string, args ...any) *CreateIndexQuery

func (*CreateIndexQuery) Where

func (q *CreateIndexQuery) Where(query string, args ...any) *CreateIndexQuery

func (*CreateIndexQuery) WhereOr

func (q *CreateIndexQuery) WhereOr(query string, args ...any) *CreateIndexQuery

type CreateTableQuery

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

CreateTableQuery builds CREATE TABLE statements.

func NewCreateTableQuery

func NewCreateTableQuery(db *DB) *CreateTableQuery

NewCreateTableQuery returns a CreateTableQuery bound to the provided DB.

func (*CreateTableQuery) AppendNamedArg added in v0.4.0

func (q *CreateTableQuery) AppendNamedArg(gen schema.QueryGen, b []byte, name string) ([]byte, bool)

func (*CreateTableQuery) AppendQuery

func (q *CreateTableQuery) AppendQuery(gen schema.QueryGen, b []byte) (_ []byte, err error)

func (*CreateTableQuery) ColumnExpr added in v1.0.9

func (q *CreateTableQuery) ColumnExpr(query string, args ...any) *CreateTableQuery

func (*CreateTableQuery) Comment added in v1.2.8

func (q *CreateTableQuery) Comment(comment string) *CreateTableQuery

Comment adds a comment to the query, wrapped by /* ... */.

func (*CreateTableQuery) Conn added in v0.1.14

func (*CreateTableQuery) DB

func (q *CreateTableQuery) DB() *DB

func (*CreateTableQuery) Dialect added in v1.0.21

func (q *CreateTableQuery) Dialect() schema.Dialect

func (*CreateTableQuery) Err added in v1.1.10

func (*CreateTableQuery) Exec

func (q *CreateTableQuery) Exec(ctx context.Context, dest ...any) (sql.Result, error)

func (*CreateTableQuery) ForeignKey added in v0.1.13

func (q *CreateTableQuery) ForeignKey(query string, args ...any) *CreateTableQuery

func (*CreateTableQuery) GetModel added in v0.2.0

func (q *CreateTableQuery) GetModel() Model

func (*CreateTableQuery) GetTableName added in v1.0.9

func (q *CreateTableQuery) GetTableName() string

func (*CreateTableQuery) IfNotExists

func (q *CreateTableQuery) IfNotExists() *CreateTableQuery

func (*CreateTableQuery) Model

func (q *CreateTableQuery) Model(model any) *CreateTableQuery

func (*CreateTableQuery) ModelTableExpr

func (q *CreateTableQuery) ModelTableExpr(query string, args ...any) *CreateTableQuery

func (*CreateTableQuery) NewAddColumn added in v1.0.21

func (q *CreateTableQuery) NewAddColumn() *AddColumnQuery

func (*CreateTableQuery) NewCreateIndex added in v1.0.21

func (q *CreateTableQuery) NewCreateIndex() *CreateIndexQuery

func (*CreateTableQuery) NewCreateTable added in v1.0.21

func (q *CreateTableQuery) NewCreateTable() *CreateTableQuery

func (*CreateTableQuery) NewDelete added in v1.0.21

func (q *CreateTableQuery) NewDelete() *DeleteQuery

func (*CreateTableQuery) NewDropColumn added in v1.0.21

func (q *CreateTableQuery) NewDropColumn() *DropColumnQuery

func (*CreateTableQuery) NewDropIndex added in v1.0.21

func (q *CreateTableQuery) NewDropIndex() *DropIndexQuery

func (*CreateTableQuery) NewDropTable added in v1.0.21

func (q *CreateTableQuery) NewDropTable() *DropTableQuery

func (*CreateTableQuery) NewInsert added in v1.0.21

func (q *CreateTableQuery) NewInsert() *InsertQuery

func (*CreateTableQuery) NewRaw added in v1.1.8

func (q *CreateTableQuery) NewRaw(query string, args ...any) *RawQuery

func (*CreateTableQuery) NewSelect added in v1.0.21

func (q *CreateTableQuery) NewSelect() *SelectQuery

func (*CreateTableQuery) NewTruncateTable added in v1.0.21

func (q *CreateTableQuery) NewTruncateTable() *TruncateTableQuery

func (*CreateTableQuery) NewUpdate added in v1.0.21

func (q *CreateTableQuery) NewUpdate() *UpdateQuery

func (*CreateTableQuery) NewValues added in v1.0.21

func (q *CreateTableQuery) NewValues(model any) *ValuesQuery

func (*CreateTableQuery) Operation added in v1.0.5

func (q *CreateTableQuery) Operation() string

func (*CreateTableQuery) PartitionBy added in v1.0.18

func (q *CreateTableQuery) PartitionBy(query string, args ...any) *CreateTableQuery

func (*CreateTableQuery) String added in v1.1.17

func (q *CreateTableQuery) String() string

String returns the generated SQL query string. The CreateTableQuery instance must not be modified during query generation to ensure multiple calls to String() return identical results.

func (*CreateTableQuery) Table

func (q *CreateTableQuery) Table(tables ...string) *CreateTableQuery

func (*CreateTableQuery) TableExpr

func (q *CreateTableQuery) TableExpr(query string, args ...any) *CreateTableQuery

func (*CreateTableQuery) TableSpace added in v1.0.18

func (q *CreateTableQuery) TableSpace(tablespace string) *CreateTableQuery

func (*CreateTableQuery) Temp

func (*CreateTableQuery) Varchar

func (q *CreateTableQuery) Varchar(n int) *CreateTableQuery

Varchar sets default length for VARCHAR columns.

func (*CreateTableQuery) WithForeignKeys added in v1.0.23

func (q *CreateTableQuery) WithForeignKeys() *CreateTableQuery

WithForeignKeys adds a FOREIGN KEY clause for each of the model's existing relations.

type DB

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

DB is the central access point for building and executing Bun queries.

func NewDB

func NewDB(sqldb *sql.DB, dialect schema.Dialect, opts ...DBOption) *DB

NewDB wraps an existing *sql.DB with Bun using the given dialect and options.

func (*DB) AddQueryHook

func (db *DB) AddQueryHook(hook QueryHook)

DEPRECATED: use WithQueryHook instead

func (*DB) Begin

func (db *DB) Begin() (Tx, error)

Begin starts a transaction with default options using a background context.

func (*DB) BeginTx

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

BeginTx starts a transaction with the given options.

func (*DB) Close added in v1.2.9

func (db *DB) Close() error

Close closes the database connection and any registered connection resolver. It returns the first error encountered during closure.

func (*DB) Conn

func (db *DB) Conn(ctx context.Context) (Conn, error)

Conn returns a Conn wrapping a dedicated *sql.Conn from the connection pool. Query hooks and dialect features remain active on the returned connection.

func (*DB) DBStats added in v0.1.13

func (db *DB) DBStats() DBStats

DBStats returns aggregated query statistics including total queries and errors.

func (*DB) Dialect

func (db *DB) Dialect() schema.Dialect

Dialect returns the database dialect being used (e.g., PostgreSQL, MySQL, SQLite).

func (*DB) Exec

func (db *DB) Exec(query string, args ...any) (sql.Result, error)

Exec executes a query without returning rows using a background context. Arguments are formatted using the dialect's placeholder syntax.

func (*DB) ExecContext

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

ExecContext executes a query without returning rows. Arguments are formatted using the dialect's placeholder syntax. Query hooks are invoked before and after execution.

func (*DB) HasFeature added in v1.0.18

func (db *DB) HasFeature(feat feature.Feature) bool

HasFeature uses feature package to report whether the underlying DBMS supports this feature.

func (*DB) NewAddColumn

func (db *DB) NewAddColumn() *AddColumnQuery

NewAddColumn creates an ALTER TABLE ADD COLUMN DDL query builder.

func (*DB) NewCreateIndex

func (db *DB) NewCreateIndex() *CreateIndexQuery

NewCreateIndex creates a CREATE INDEX DDL query builder.

func (*DB) NewCreateTable

func (db *DB) NewCreateTable() *CreateTableQuery

NewCreateTable creates a CREATE TABLE DDL query builder.

func (*DB) NewDelete

func (db *DB) NewDelete() *DeleteQuery

NewDelete creates a DELETE query builder.

func (*DB) NewDropColumn

func (db *DB) NewDropColumn() *DropColumnQuery

NewDropColumn creates an ALTER TABLE DROP COLUMN DDL query builder.

func (*DB) NewDropIndex

func (db *DB) NewDropIndex() *DropIndexQuery

NewDropIndex creates a DROP INDEX DDL query builder.

func (*DB) NewDropTable

func (db *DB) NewDropTable() *DropTableQuery

NewDropTable creates a DROP TABLE DDL query builder.

func (*DB) NewInsert

func (db *DB) NewInsert() *InsertQuery

NewInsert creates an INSERT query builder.

func (*DB) NewMerge added in v1.1.10

func (db *DB) NewMerge() *MergeQuery

NewMerge creates a MERGE (UPSERT) query for insert-or-update operations.

func (*DB) NewRaw added in v1.1.8

func (db *DB) NewRaw(query string, args ...any) *RawQuery

NewRaw creates a raw SQL query with the given query string and arguments.

func (*DB) NewSelect

func (db *DB) NewSelect() *SelectQuery

NewSelect creates a SELECT query builder.

func (*DB) NewTruncateTable

func (db *DB) NewTruncateTable() *TruncateTableQuery

NewTruncateTable creates a TRUNCATE TABLE DDL query builder.

func (*DB) NewUpdate

func (db *DB) NewUpdate() *UpdateQuery

NewUpdate creates an UPDATE query builder.

func (*DB) NewValues

func (db *DB) NewValues(model any) *ValuesQuery

NewValues creates a VALUES query for inserting multiple rows efficiently.

func (*DB) Prepare

func (db *DB) Prepare(query string) (Stmt, error)

Prepare creates a prepared statement using a background context.

func (*DB) PrepareContext

func (db *DB) PrepareContext(ctx context.Context, query string) (Stmt, error)

PrepareContext creates a prepared statement for repeated execution.

func (*DB) Query

func (db *DB) Query(query string, args ...any) (*sql.Rows, error)

Query executes a query returning rows using a background context. Arguments are formatted using the dialect's placeholder syntax.

func (*DB) QueryContext

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

QueryContext executes a query returning rows. Arguments are formatted using the dialect's placeholder syntax. Query hooks are invoked before and after execution.

func (*DB) QueryGen added in v1.2.16

func (db *DB) QueryGen() schema.QueryGen

QueryGen returns the query generator used for formatting SQL queries.

func (*DB) QueryRow

func (db *DB) QueryRow(query string, args ...any) *sql.Row

QueryRow executes a query expected to return at most one row using a background context. Arguments are formatted using the dialect's placeholder syntax.

func (*DB) QueryRowContext

func (db *DB) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row

QueryRowContext executes a query expected to return at most one row. Arguments are formatted using the dialect's placeholder syntax. Query hooks are invoked before and after execution.

func (*DB) RegisterModel

func (db *DB) RegisterModel(models ...any)

RegisterModel registers models by name so they can be referenced in table relations and fixtures.

func (*DB) ResetModel added in v0.1.6

func (db *DB) ResetModel(ctx context.Context, models ...any) error

ResetModel drops and recreates tables for the given models. This is useful for testing and development but should not be used in production.

func (*DB) ResetQueryHooks added in v1.2.15

func (db *DB) ResetQueryHooks()

DEPRECATED: use WithQueryHook instead

func (*DB) RunInTx added in v0.2.5

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

RunInTx runs the function in a transaction. If the function returns an error, the transaction is rolled back. Otherwise, the transaction is committed.

func (*DB) ScanRow

func (db *DB) ScanRow(ctx context.Context, rows *sql.Rows, dest ...any) error

ScanRow scans a single row from the result set into the destination values.

func (*DB) ScanRows

func (db *DB) ScanRows(ctx context.Context, rows *sql.Rows, dest ...any) error

ScanRows scans all rows from the result set into the destination values. It closes the rows when complete.

func (*DB) String added in v0.3.5

func (db *DB) String() string

String returns a string representation of the DB showing its dialect.

func (*DB) Table

func (db *DB) Table(typ reflect.Type) *schema.Table

Table returns the schema table metadata for the given type.

func (*DB) UpdateFQN added in v1.0.19

func (db *DB) UpdateFQN(alias, column string) Ident

UpdateFQN returns a fully qualified column name. For MySQL, it returns the column name with the table alias. For other RDBMS, it returns just the column name.

func (*DB) WithNamedArg

func (db *DB) WithNamedArg(name string, value any) *DB

WithNamedArg returns a copy of the DB with an additional named argument bound into its query generator. Named arguments can later be referenced in SQL queries using placeholders (e.g. ?name). This method does not mutate the original DB instance but instead creates a cloned copy.

func (*DB) WithQueryHook added in v1.2.16

func (db *DB) WithQueryHook(hook QueryHook) *DB

WithQueryHook returns a copy of the DB with the provided query hook attached. A query hook allows inspection or modification of queries before/after execution (e.g. for logging, tracing, metrics). If the hook implements queryHookIniter, its Init method is invoked with the current DB before cloning. Like other modifiers, this method leaves the original DB unmodified.

type DBOption

type DBOption func(db *DB)

DBOption mutates DB configuration during construction.

func WithConnResolver added in v1.2.9

func WithConnResolver(resolver ConnResolver) DBOption

WithConnResolver registers a connection resolver that chooses a connection per query.

func WithDiscardUnknownColumns added in v0.1.3

func WithDiscardUnknownColumns() DBOption

WithDiscardUnknownColumns ignores columns returned by queries that are not present in models.

func WithOptions added in v1.2.9

func WithOptions(opts ...DBOption) DBOption

WithOptions applies multiple DBOption values at once.

type DBStats

type DBStats struct {
	Queries uint32
	Errors  uint32
}

DBStats tracks aggregate query counters collected by Bun.

type DeleteQuery

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

DeleteQuery builds SQL DELETE statements.

func NewDeleteQuery

func NewDeleteQuery(db *DB) *DeleteQuery

NewDeleteQuery returns a DeleteQuery associated with the provided DB.

func (*DeleteQuery) AppendQuery

func (q *DeleteQuery) AppendQuery(gen schema.QueryGen, b []byte) (_ []byte, err error)

func (*DeleteQuery) Apply

func (q *DeleteQuery) Apply(fns ...func(*DeleteQuery) *DeleteQuery) *DeleteQuery

Apply calls each function in fns, passing the DeleteQuery as an argument.

func (*DeleteQuery) ApplyQueryBuilder added in v1.1.4

func (q *DeleteQuery) ApplyQueryBuilder(fn func(QueryBuilder) QueryBuilder) *DeleteQuery

func (*DeleteQuery) Comment added in v1.2.8

func (q *DeleteQuery) Comment(comment string) *DeleteQuery

Comment adds a comment to the query, wrapped by /* ... */.

func (*DeleteQuery) Conn added in v0.1.14

func (q *DeleteQuery) Conn(db IConn) *DeleteQuery

func (*DeleteQuery) Err added in v1.1.10

func (q *DeleteQuery) Err(err error) *DeleteQuery

func (*DeleteQuery) Exec

func (q *DeleteQuery) Exec(ctx context.Context, dest ...any) (sql.Result, error)

func (*DeleteQuery) ForceDelete

func (q *DeleteQuery) ForceDelete() *DeleteQuery

func (*DeleteQuery) Limit added in v1.2.6

func (q *DeleteQuery) Limit(n int) *DeleteQuery

------------------------------------------------------------------------------

func (*DeleteQuery) Model

func (q *DeleteQuery) Model(model any) *DeleteQuery

func (*DeleteQuery) ModelTableExpr

func (q *DeleteQuery) ModelTableExpr(query string, args ...any) *DeleteQuery

func (*DeleteQuery) Operation added in v1.0.5

func (q *DeleteQuery) Operation() string

func (*DeleteQuery) Order added in v1.2.6

func (q *DeleteQuery) Order(orders ...string) *DeleteQuery

func (*DeleteQuery) OrderExpr added in v1.2.6

func (q *DeleteQuery) OrderExpr(query string, args ...any) *DeleteQuery

func (*DeleteQuery) QueryBuilder added in v1.1.4

func (q *DeleteQuery) QueryBuilder() QueryBuilder

func (*DeleteQuery) Returning

func (q *DeleteQuery) Returning(query string, args ...any) *DeleteQuery

Returning adds a RETURNING clause to the query.

To suppress the auto-generated RETURNING clause, use `Returning("NULL")`.

func (*DeleteQuery) Scan added in v1.1.10

func (q *DeleteQuery) Scan(ctx context.Context, dest ...any) error

func (*DeleteQuery) String added in v1.1.6

func (q *DeleteQuery) String() string

String returns the generated SQL query string. The DeleteQuery instance must not be modified during query generation to ensure multiple calls to String() return identical results.

func (*DeleteQuery) Table

func (q *DeleteQuery) Table(tables ...string) *DeleteQuery

func (*DeleteQuery) TableExpr

func (q *DeleteQuery) TableExpr(query string, args ...any) *DeleteQuery

func (*DeleteQuery) Where

func (q *DeleteQuery) Where(query string, args ...any) *DeleteQuery

func (*DeleteQuery) WhereAllWithDeleted

func (q *DeleteQuery) WhereAllWithDeleted() *DeleteQuery

func (*DeleteQuery) WhereDeleted

func (q *DeleteQuery) WhereDeleted() *DeleteQuery

func (*DeleteQuery) WhereGroup

func (q *DeleteQuery) WhereGroup(sep string, fn func(*DeleteQuery) *DeleteQuery) *DeleteQuery

func (*DeleteQuery) WhereOr

func (q *DeleteQuery) WhereOr(query string, args ...any) *DeleteQuery

func (*DeleteQuery) WherePK

func (q *DeleteQuery) WherePK(cols ...string) *DeleteQuery

func (*DeleteQuery) With

func (q *DeleteQuery) With(name string, query Query) *DeleteQuery

func (*DeleteQuery) WithQuery added in v1.2.16

func (q *DeleteQuery) WithQuery(query *WithQuery) *DeleteQuery

func (*DeleteQuery) WithRecursive added in v1.1.9

func (q *DeleteQuery) WithRecursive(name string, query Query) *DeleteQuery

type DropColumnQuery

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

DropColumnQuery builds ALTER TABLE ... DROP COLUMN statements.

func NewDropColumnQuery

func NewDropColumnQuery(db *DB) *DropColumnQuery

NewDropColumnQuery creates a DropColumnQuery bound to the provided DB.

func (*DropColumnQuery) AppendNamedArg added in v0.4.0

func (q *DropColumnQuery) AppendNamedArg(gen schema.QueryGen, b []byte, name string) ([]byte, bool)

func (*DropColumnQuery) AppendQuery

func (q *DropColumnQuery) AppendQuery(gen schema.QueryGen, b []byte) (_ []byte, err error)

func (*DropColumnQuery) Apply added in v1.1.10

Apply calls each function in fns, passing the DropColumnQuery as an argument.

func (*DropColumnQuery) Column

func (q *DropColumnQuery) Column(columns ...string) *DropColumnQuery

func (*DropColumnQuery) ColumnExpr

func (q *DropColumnQuery) ColumnExpr(query string, args ...any) *DropColumnQuery

func (*DropColumnQuery) Comment added in v1.2.8

func (q *DropColumnQuery) Comment(comment string) *DropColumnQuery

Comment adds a comment to the query, wrapped by /* ... */.

func (*DropColumnQuery) Conn added in v0.1.14

func (q *DropColumnQuery) Conn(db IConn) *DropColumnQuery

func (*DropColumnQuery) DB

func (q *DropColumnQuery) DB() *DB

func (*DropColumnQuery) Dialect added in v1.0.21

func (q *DropColumnQuery) Dialect() schema.Dialect

func (*DropColumnQuery) Err added in v1.1.10

func (q *DropColumnQuery) Err(err error) *DropColumnQuery

func (*DropColumnQuery) Exec

func (q *DropColumnQuery) Exec(ctx context.Context, dest ...any) (sql.Result, error)

func (*DropColumnQuery) GetModel added in v0.2.0

func (q *DropColumnQuery) GetModel() Model

func (*DropColumnQuery) GetTableName added in v1.0.9

func (q *DropColumnQuery) GetTableName() string

func (*DropColumnQuery) Model

func (q *DropColumnQuery) Model(model any) *DropColumnQuery

func (*DropColumnQuery) ModelTableExpr

func (q *DropColumnQuery) ModelTableExpr(query string, args ...any) *DropColumnQuery

func (*DropColumnQuery) NewAddColumn added in v1.0.21

func (q *DropColumnQuery) NewAddColumn() *AddColumnQuery

func (*DropColumnQuery) NewCreateIndex added in v1.0.21

func (q *DropColumnQuery) NewCreateIndex() *CreateIndexQuery

func (*DropColumnQuery) NewCreateTable added in v1.0.21

func (q *DropColumnQuery) NewCreateTable() *CreateTableQuery

func (*DropColumnQuery) NewDelete added in v1.0.21

func (q *DropColumnQuery) NewDelete() *DeleteQuery

func (*DropColumnQuery) NewDropColumn added in v1.0.21

func (q *DropColumnQuery) NewDropColumn() *DropColumnQuery

func (*DropColumnQuery) NewDropIndex added in v1.0.21

func (q *DropColumnQuery) NewDropIndex() *DropIndexQuery

func (*DropColumnQuery) NewDropTable added in v1.0.21

func (q *DropColumnQuery) NewDropTable() *DropTableQuery

func (*DropColumnQuery) NewInsert added in v1.0.21

func (q *DropColumnQuery) NewInsert() *InsertQuery

func (*DropColumnQuery) NewRaw added in v1.1.8

func (q *DropColumnQuery) NewRaw(query string, args ...any) *RawQuery

func (*DropColumnQuery) NewSelect added in v1.0.21

func (q *DropColumnQuery) NewSelect() *SelectQuery

func (*DropColumnQuery) NewTruncateTable added in v1.0.21

func (q *DropColumnQuery) NewTruncateTable() *TruncateTableQuery

func (*DropColumnQuery) NewUpdate added in v1.0.21

func (q *DropColumnQuery) NewUpdate() *UpdateQuery

func (*DropColumnQuery) NewValues added in v1.0.21

func (q *DropColumnQuery) NewValues(model any) *ValuesQuery

func (*DropColumnQuery) Operation added in v1.0.5

func (q *DropColumnQuery) Operation() string

func (*DropColumnQuery) Table

func (q *DropColumnQuery) Table(tables ...string) *DropColumnQuery

func (*DropColumnQuery) TableExpr

func (q *DropColumnQuery) TableExpr(query string, args ...any) *DropColumnQuery

type DropIndexQuery

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

DropIndexQuery builds DROP INDEX statements.

func NewDropIndexQuery

func NewDropIndexQuery(db *DB) *DropIndexQuery

NewDropIndexQuery returns a DropIndexQuery associated with the provided DB.

func (*DropIndexQuery) AppendNamedArg added in v0.4.0

func (q *DropIndexQuery) AppendNamedArg(gen schema.QueryGen, b []byte, name string) ([]byte, bool)

func (*DropIndexQuery) AppendQuery

func (q *DropIndexQuery) AppendQuery(gen schema.QueryGen, b []byte) (_ []byte, err error)

func (*DropIndexQuery) Cascade added in v1.0.20

func (q *DropIndexQuery) Cascade() *DropIndexQuery

func (*DropIndexQuery) Comment added in v1.2.8

func (q *DropIndexQuery) Comment(comment string) *DropIndexQuery

Comment adds a comment to the query, wrapped by /* ... */.

func (*DropIndexQuery) Concurrently

func (q *DropIndexQuery) Concurrently() *DropIndexQuery

func (*DropIndexQuery) Conn added in v0.1.14

func (q *DropIndexQuery) Conn(db IConn) *DropIndexQuery

func (*DropIndexQuery) DB

func (q *DropIndexQuery) DB() *DB

func (*DropIndexQuery) Dialect added in v1.0.21

func (q *DropIndexQuery) Dialect() schema.Dialect

func (*DropIndexQuery) Err added in v1.1.10

func (q *DropIndexQuery) Err(err error) *DropIndexQuery

func (*DropIndexQuery) Exec

func (q *DropIndexQuery) Exec(ctx context.Context, dest ...any) (sql.Result, error)

func (*DropIndexQuery) GetModel added in v0.2.0

func (q *DropIndexQuery) GetModel() Model

func (*DropIndexQuery) GetTableName added in v1.0.9

func (q *DropIndexQuery) GetTableName() string

func (*DropIndexQuery) IfExists

func (q *DropIndexQuery) IfExists() *DropIndexQuery

func (*DropIndexQuery) Index

func (q *DropIndexQuery) Index(query string, args ...any) *DropIndexQuery

func (*DropIndexQuery) Model

func (q *DropIndexQuery) Model(model any) *DropIndexQuery

func (*DropIndexQuery) NewAddColumn added in v1.0.21

func (q *DropIndexQuery) NewAddColumn() *AddColumnQuery

func (*DropIndexQuery) NewCreateIndex added in v1.0.21

func (q *DropIndexQuery) NewCreateIndex() *CreateIndexQuery

func (*DropIndexQuery) NewCreateTable added in v1.0.21

func (q *DropIndexQuery) NewCreateTable() *CreateTableQuery

func (*DropIndexQuery) NewDelete added in v1.0.21

func (q *DropIndexQuery) NewDelete() *DeleteQuery

func (*DropIndexQuery) NewDropColumn added in v1.0.21

func (q *DropIndexQuery) NewDropColumn() *DropColumnQuery

func (*DropIndexQuery) NewDropIndex added in v1.0.21

func (q *DropIndexQuery) NewDropIndex() *DropIndexQuery

func (*DropIndexQuery) NewDropTable added in v1.0.21

func (q *DropIndexQuery) NewDropTable() *DropTableQuery

func (*DropIndexQuery) NewInsert added in v1.0.21

func (q *DropIndexQuery) NewInsert() *InsertQuery

func (*DropIndexQuery) NewRaw added in v1.1.8

func (q *DropIndexQuery) NewRaw(query string, args ...any) *RawQuery

func (*DropIndexQuery) NewSelect added in v1.0.21

func (q *DropIndexQuery) NewSelect() *SelectQuery

func (*DropIndexQuery) NewTruncateTable added in v1.0.21

func (q *DropIndexQuery) NewTruncateTable() *TruncateTableQuery

func (*DropIndexQuery) NewUpdate added in v1.0.21

func (q *DropIndexQuery) NewUpdate() *UpdateQuery

func (*DropIndexQuery) NewValues added in v1.0.21

func (q *DropIndexQuery) NewValues(model any) *ValuesQuery

func (*DropIndexQuery) Operation added in v1.0.5

func (q *DropIndexQuery) Operation() string

func (*DropIndexQuery) Restrict

func (q *DropIndexQuery) Restrict() *DropIndexQuery

type DropTableQuery

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

DropTableQuery builds DROP TABLE statements.

func NewDropTableQuery

func NewDropTableQuery(db *DB) *DropTableQuery

NewDropTableQuery returns a DropTableQuery tied to the provided DB.

func (*DropTableQuery) AppendNamedArg added in v0.4.0

func (q *DropTableQuery) AppendNamedArg(gen schema.QueryGen, b []byte, name string) ([]byte, bool)

func (*DropTableQuery) AppendQuery

func (q *DropTableQuery) AppendQuery(gen schema.QueryGen, b []byte) (_ []byte, err error)

func (*DropTableQuery) Cascade added in v1.0.20

func (q *DropTableQuery) Cascade() *DropTableQuery

func (*DropTableQuery) Comment added in v1.2.8

func (q *DropTableQuery) Comment(comment string) *DropTableQuery

Comment adds a comment to the query, wrapped by /* ... */.

func (*DropTableQuery) Conn added in v0.1.14

func (q *DropTableQuery) Conn(db IConn) *DropTableQuery

func (*DropTableQuery) DB

func (q *DropTableQuery) DB() *DB

func (*DropTableQuery) Dialect added in v1.0.21

func (q *DropTableQuery) Dialect() schema.Dialect

func (*DropTableQuery) Err added in v1.1.10

func (q *DropTableQuery) Err(err error) *DropTableQuery

func (*DropTableQuery) Exec

func (q *DropTableQuery) Exec(ctx context.Context, dest ...any) (sql.Result, error)

func (*DropTableQuery) GetModel added in v0.2.0

func (q *DropTableQuery) GetModel() Model

func (*DropTableQuery) GetTableName added in v1.0.9

func (q *DropTableQuery) GetTableName() string

func (*DropTableQuery) IfExists

func (q *DropTableQuery) IfExists() *DropTableQuery

func (*DropTableQuery) Model

func (q *DropTableQuery) Model(model any) *DropTableQuery

func (*DropTableQuery) ModelTableExpr added in v0.4.1

func (q *DropTableQuery) ModelTableExpr(query string, args ...any) *DropTableQuery

func (*DropTableQuery) NewAddColumn added in v1.0.21

func (q *DropTableQuery) NewAddColumn() *AddColumnQuery

func (*DropTableQuery) NewCreateIndex added in v1.0.21

func (q *DropTableQuery) NewCreateIndex() *CreateIndexQuery

func (*DropTableQuery) NewCreateTable added in v1.0.21

func (q *DropTableQuery) NewCreateTable() *CreateTableQuery

func (*DropTableQuery) NewDelete added in v1.0.21

func (q *DropTableQuery) NewDelete() *DeleteQuery

func (*DropTableQuery) NewDropColumn added in v1.0.21

func (q *DropTableQuery) NewDropColumn() *DropColumnQuery

func (*DropTableQuery) NewDropIndex added in v1.0.21

func (q *DropTableQuery) NewDropIndex() *DropIndexQuery

func (*DropTableQuery) NewDropTable added in v1.0.21

func (q *DropTableQuery) NewDropTable() *DropTableQuery

func (*DropTableQuery) NewInsert added in v1.0.21

func (q *DropTableQuery) NewInsert() *InsertQuery

func (*DropTableQuery) NewRaw added in v1.1.8

func (q *DropTableQuery) NewRaw(query string, args ...any) *RawQuery

func (*DropTableQuery) NewSelect added in v1.0.21

func (q *DropTableQuery) NewSelect() *SelectQuery

func (*DropTableQuery) NewTruncateTable added in v1.0.21

func (q *DropTableQuery) NewTruncateTable() *TruncateTableQuery

func (*DropTableQuery) NewUpdate added in v1.0.21

func (q *DropTableQuery) NewUpdate() *UpdateQuery

func (*DropTableQuery) NewValues added in v1.0.21

func (q *DropTableQuery) NewValues(model any) *ValuesQuery

func (*DropTableQuery) Operation added in v1.0.5

func (q *DropTableQuery) Operation() string

func (*DropTableQuery) Restrict

func (q *DropTableQuery) Restrict() *DropTableQuery

func (*DropTableQuery) String added in v1.2.6

func (q *DropTableQuery) String() string

String returns the generated SQL query string. The DropTableQuery instance must not be modified during query generation to ensure multiple calls to String() return identical results.

func (*DropTableQuery) Table

func (q *DropTableQuery) Table(tables ...string) *DropTableQuery

func (*DropTableQuery) TableExpr

func (q *DropTableQuery) TableExpr(query string, args ...any) *DropTableQuery

type IConn added in v0.2.1

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

IConn is a common interface for *sql.DB, *sql.Conn, and *sql.Tx.

type IDB added in v0.2.1

type IDB interface {
	IConn
	Dialect() schema.Dialect

	NewValues(model any) *ValuesQuery
	NewSelect() *SelectQuery
	NewInsert() *InsertQuery
	NewUpdate() *UpdateQuery
	NewDelete() *DeleteQuery
	NewMerge() *MergeQuery
	NewRaw(query string, args ...any) *RawQuery
	NewCreateTable() *CreateTableQuery
	NewDropTable() *DropTableQuery
	NewCreateIndex() *CreateIndexQuery
	NewDropIndex() *DropIndexQuery
	NewTruncateTable() *TruncateTableQuery
	NewAddColumn() *AddColumnQuery
	NewDropColumn() *DropColumnQuery

	BeginTx(ctx context.Context, opts *sql.TxOptions) (Tx, error)
	RunInTx(ctx context.Context, opts *sql.TxOptions, f func(ctx context.Context, tx Tx) error) error
}

IDB is a common interface for *bun.DB, bun.Conn, and bun.Tx.

type Ident

type Ident = schema.Ident

Ident is a fully qualified SQL identifier.

type InsertQuery

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

InsertQuery builds SQL INSERT statements.

func NewInsertQuery

func NewInsertQuery(db *DB) *InsertQuery

NewInsertQuery returns an InsertQuery tied to the provided DB.

func (*InsertQuery) AppendQuery

func (q *InsertQuery) AppendQuery(gen schema.QueryGen, b []byte) (_ []byte, err error)

func (*InsertQuery) Apply

func (q *InsertQuery) Apply(fns ...func(*InsertQuery) *InsertQuery) *InsertQuery

Apply calls each function in fns, passing the InsertQuery as an argument.

func (*InsertQuery) Column

func (q *InsertQuery) Column(columns ...string) *InsertQuery

func (*InsertQuery) ColumnExpr added in v1.0.12

func (q *InsertQuery) ColumnExpr(query string, args ...any) *InsertQuery

func (*InsertQuery) Comment added in v1.2.8

func (q *InsertQuery) Comment(comment string) *InsertQuery

Comment adds a comment to the query, wrapped by /* ... */.

func (*InsertQuery) Conn added in v0.1.14

func (q *InsertQuery) Conn(db IConn) *InsertQuery

func (*InsertQuery) Err added in v1.1.10

func (q *InsertQuery) Err(err error) *InsertQuery

func (*InsertQuery) ExcludeColumn added in v0.2.0

func (q *InsertQuery) ExcludeColumn(columns ...string) *InsertQuery

func (*InsertQuery) Exec

func (q *InsertQuery) Exec(ctx context.Context, dest ...any) (sql.Result, error)

func (*InsertQuery) Ignore added in v0.1.9

func (q *InsertQuery) Ignore() *InsertQuery

Ignore generates different queries depending on the DBMS:

  • On MySQL, it generates `INSERT IGNORE INTO`.
  • On PostgreSQL, it generates `ON CONFLICT DO NOTHING`.

func (*InsertQuery) Model

func (q *InsertQuery) Model(model any) *InsertQuery

func (*InsertQuery) ModelTableExpr

func (q *InsertQuery) ModelTableExpr(query string, args ...any) *InsertQuery

func (*InsertQuery) On added in v0.1.9

func (q *InsertQuery) On(s string, args ...any) *InsertQuery

func (*InsertQuery) Operation added in v1.0.5

func (q *InsertQuery) Operation() string

func (*InsertQuery) Replace added in v0.1.9

func (q *InsertQuery) Replace() *InsertQuery

Replaces generates a `REPLACE INTO` query (MySQL and MariaDB).

func (*InsertQuery) Returning

func (q *InsertQuery) Returning(query string, args ...any) *InsertQuery

Returning adds a RETURNING clause to the query.

To suppress the auto-generated RETURNING clause, use `Returning("")`.

func (*InsertQuery) Scan added in v1.1.10

func (q *InsertQuery) Scan(ctx context.Context, dest ...any) error

func (*InsertQuery) Set

func (q *InsertQuery) Set(query string, args ...any) *InsertQuery

func (*InsertQuery) SetValues added in v1.2.16

func (q *InsertQuery) SetValues(values *ValuesQuery) *InsertQuery

func (*InsertQuery) String added in v1.1.6

func (q *InsertQuery) String() string

String returns the generated SQL query string. The InsertQuery instance must not be modified during query generation to ensure multiple calls to String() return identical results.

func (*InsertQuery) Table

func (q *InsertQuery) Table(tables ...string) *InsertQuery

func (*InsertQuery) TableExpr

func (q *InsertQuery) TableExpr(query string, args ...any) *InsertQuery

func (*InsertQuery) Value

func (q *InsertQuery) Value(column string, expr string, args ...any) *InsertQuery

Value overwrites model value for the column.

func (*InsertQuery) Where

func (q *InsertQuery) Where(query string, args ...any) *InsertQuery

func (*InsertQuery) WhereOr

func (q *InsertQuery) WhereOr(query string, args ...any) *InsertQuery

func (*InsertQuery) With

func (q *InsertQuery) With(name string, query Query) *InsertQuery

func (*InsertQuery) WithQuery added in v1.2.16

func (q *InsertQuery) WithQuery(query *WithQuery) *InsertQuery

func (*InsertQuery) WithRecursive added in v1.1.9

func (q *InsertQuery) WithRecursive(name string, query Query) *InsertQuery

type ListValues added in v1.2.17

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

ListValues formats a Go slice as a comma-separated SQL list (e.g., "1, 2, 3").

func List added in v1.2.17

func List(slice any) ListValues

List creates a ListValues from a Go slice for use in SQL IN expressions.

func (ListValues) AppendQuery added in v1.2.17

func (in ListValues) AppendQuery(gen schema.QueryGen, b []byte) (_ []byte, err error)

AppendQuery appends the comma-separated list values to the byte slice.

type MergeQuery added in v1.1.10

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

MergeQuery builds MERGE statements for dialects that support them.

func NewMergeQuery added in v1.1.10

func NewMergeQuery(db *DB) *MergeQuery

NewMergeQuery creates a MergeQuery associated with the provided DB.

func (*MergeQuery) AppendNamedArg added in v1.1.10

func (q *MergeQuery) AppendNamedArg(gen schema.QueryGen, b []byte, name string) ([]byte, bool)

func (*MergeQuery) AppendQuery added in v1.1.10

func (q *MergeQuery) AppendQuery(gen schema.QueryGen, b []byte) (_ []byte, err error)

func (*MergeQuery) Apply added in v1.1.10

func (q *MergeQuery) Apply(fns ...func(*MergeQuery) *MergeQuery) *MergeQuery

Apply calls each function in fns, passing the MergeQuery as an argument.

func (*MergeQuery) Comment added in v1.2.8

func (q *MergeQuery) Comment(comment string) *MergeQuery

Comment adds a comment to the query, wrapped by /* ... */.

func (*MergeQuery) Conn added in v1.1.10

func (q *MergeQuery) Conn(db IConn) *MergeQuery

func (*MergeQuery) DB added in v1.1.10

func (q *MergeQuery) DB() *DB

func (*MergeQuery) Dialect added in v1.1.10

func (q *MergeQuery) Dialect() schema.Dialect

func (*MergeQuery) Err added in v1.1.10

func (q *MergeQuery) Err(err error) *MergeQuery

func (*MergeQuery) Exec added in v1.1.10

func (q *MergeQuery) Exec(ctx context.Context, dest ...any) (sql.Result, error)

func (*MergeQuery) GetModel added in v1.1.10

func (q *MergeQuery) GetModel() Model

func (*MergeQuery) GetTableName added in v1.1.10

func (q *MergeQuery) GetTableName() string

func (*MergeQuery) Model added in v1.1.10

func (q *MergeQuery) Model(model any) *MergeQuery

func (*MergeQuery) ModelTableExpr added in v1.1.10

func (q *MergeQuery) ModelTableExpr(query string, args ...any) *MergeQuery

func (*MergeQuery) NewAddColumn added in v1.1.10

func (q *MergeQuery) NewAddColumn() *AddColumnQuery

func (*MergeQuery) NewCreateIndex added in v1.1.10

func (q *MergeQuery) NewCreateIndex() *CreateIndexQuery

func (*MergeQuery) NewCreateTable added in v1.1.10

func (q *MergeQuery) NewCreateTable() *CreateTableQuery

func (*MergeQuery) NewDelete added in v1.1.10

func (q *MergeQuery) NewDelete() *DeleteQuery

func (*MergeQuery) NewDropColumn added in v1.1.10

func (q *MergeQuery) NewDropColumn() *DropColumnQuery

func (*MergeQuery) NewDropIndex added in v1.1.10

func (q *MergeQuery) NewDropIndex() *DropIndexQuery

func (*MergeQuery) NewDropTable added in v1.1.10

func (q *MergeQuery) NewDropTable() *DropTableQuery

func (*MergeQuery) NewInsert added in v1.1.10

func (q *MergeQuery) NewInsert() *InsertQuery

func (*MergeQuery) NewRaw added in v1.1.10

func (q *MergeQuery) NewRaw(query string, args ...any) *RawQuery

func (*MergeQuery) NewSelect added in v1.1.10

func (q *MergeQuery) NewSelect() *SelectQuery

func (*MergeQuery) NewTruncateTable added in v1.1.10

func (q *MergeQuery) NewTruncateTable() *TruncateTableQuery

func (*MergeQuery) NewUpdate added in v1.1.10

func (q *MergeQuery) NewUpdate() *UpdateQuery

func (*MergeQuery) NewValues added in v1.1.10

func (q *MergeQuery) NewValues(model any) *ValuesQuery

func (*MergeQuery) On added in v1.1.10

func (q *MergeQuery) On(s string, args ...any) *MergeQuery

func (*MergeQuery) Operation added in v1.1.10

func (q *MergeQuery) Operation() string

func (*MergeQuery) Returning added in v1.1.10

func (q *MergeQuery) Returning(query string, args ...any) *MergeQuery

Returning adds a RETURNING clause to the query.

To suppress the auto-generated RETURNING clause, use `Returning("NULL")`. Supported for PostgreSQL 17+ and MSSQL (via OUTPUT clause)

func (*MergeQuery) Scan added in v1.1.10

func (q *MergeQuery) Scan(ctx context.Context, dest ...any) error

func (*MergeQuery) String added in v1.1.10

func (q *MergeQuery) String() string

String returns the generated SQL query string. The MergeQuery instance must not be modified during query generation to ensure multiple calls to String() return identical results.

func (*MergeQuery) Table added in v1.1.10

func (q *MergeQuery) Table(tables ...string) *MergeQuery

func (*MergeQuery) TableExpr added in v1.1.10

func (q *MergeQuery) TableExpr(query string, args ...any) *MergeQuery

func (*MergeQuery) Using added in v1.1.10

func (q *MergeQuery) Using(s string, args ...any) *MergeQuery

func (*MergeQuery) When added in v1.1.10

func (q *MergeQuery) When(expr string, args ...any) *MergeQuery

When for raw expression clause.

func (*MergeQuery) WhenDelete added in v1.1.10

func (q *MergeQuery) WhenDelete(expr string) *MergeQuery

WhenDelete for when delete clause.

func (*MergeQuery) WhenInsert added in v1.1.10

func (q *MergeQuery) WhenInsert(expr string, fn func(q *InsertQuery) *InsertQuery) *MergeQuery

WhenInsert for when insert clause.

func (*MergeQuery) WhenUpdate added in v1.1.10

func (q *MergeQuery) WhenUpdate(expr string, fn func(q *UpdateQuery) *UpdateQuery) *MergeQuery

WhenUpdate for when update clause.

func (*MergeQuery) With added in v1.1.10

func (q *MergeQuery) With(name string, query Query) *MergeQuery

func (*MergeQuery) WithQuery added in v1.2.16

func (q *MergeQuery) WithQuery(query *WithQuery) *MergeQuery

func (*MergeQuery) WithRecursive added in v1.1.10

func (q *MergeQuery) WithRecursive(name string, query Query) *MergeQuery

type Model added in v0.2.0

type Model = schema.Model

Model is implemented by all Bun models.

type Name added in v1.1.15

type Name = schema.Name

Name represents a SQL identifier such as a column or table name.

type NullTime

type NullTime = schema.NullTime

NullTime is a nullable time value compatible with Bun.

type Order added in v1.2.16

type Order = schema.Order

Order denotes sorting direction used in ORDER BY clauses.

type Query added in v1.0.13

type Query = schema.Query

Query is implemented by all Bun query builders.

type QueryBuilder added in v1.1.3

type QueryBuilder interface {
	Query
	Where(query string, args ...any) QueryBuilder
	WhereGroup(sep string, fn func(QueryBuilder) QueryBuilder) QueryBuilder
	WhereOr(query string, args ...any) QueryBuilder
	WhereDeleted() QueryBuilder
	WhereAllWithDeleted() QueryBuilder
	WherePK(cols ...string) QueryBuilder
	Unwrap() any
}

QueryBuilder exposes shared filtering helpers across query builders.

type QueryEvent

type QueryEvent struct {
	DB *DB

	IQuery        Query
	Query         string
	QueryTemplate string
	QueryArgs     []any
	Model         Model

	StartTime time.Time
	Result    sql.Result
	Err       error

	Stash map[any]any
}

QueryEvent captures information about a query execution for hooks.

func (*QueryEvent) Operation added in v1.0.5

func (e *QueryEvent) Operation() string

Operation returns the SQL operation name such as SELECT or UPDATE.

type QueryHook

type QueryHook interface {
	BeforeQuery(context.Context, *QueryEvent) context.Context
	AfterQuery(context.Context, *QueryEvent)
}

QueryHook allows observing queries before and after execution.

type RawQuery added in v1.1.6

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

RawQuery executes a plain SQL query using Bun formatting and hooks.

func NewRawQuery added in v1.1.8

func NewRawQuery(db *DB, query string, args ...any) *RawQuery

NewRawQuery creates a RawQuery with the provided SQL template and arguments.

func (*RawQuery) AppendNamedArg added in v1.1.6

func (q *RawQuery) AppendNamedArg(gen schema.QueryGen, b []byte, name string) ([]byte, bool)

func (*RawQuery) AppendQuery added in v1.1.6

func (q *RawQuery) AppendQuery(gen schema.QueryGen, b []byte) ([]byte, error)

func (*RawQuery) Comment added in v1.2.8

func (q *RawQuery) Comment(comment string) *RawQuery

Comment adds a comment to the query, wrapped by /* ... */.

func (*RawQuery) Conn added in v1.1.8

func (q *RawQuery) Conn(db IConn) *RawQuery

func (*RawQuery) DB added in v1.1.6

func (q *RawQuery) DB() *DB

func (*RawQuery) Dialect added in v1.1.6

func (q *RawQuery) Dialect() schema.Dialect

func (*RawQuery) Err added in v1.1.10

func (q *RawQuery) Err(err error) *RawQuery

func (*RawQuery) Exec added in v1.1.13

func (q *RawQuery) Exec(ctx context.Context, dest ...any) (sql.Result, error)

func (*RawQuery) GetModel added in v1.1.6

func (q *RawQuery) GetModel() Model

func (*RawQuery) GetTableName added in v1.1.6

func (q *RawQuery) GetTableName() string

func (*RawQuery) NewAddColumn added in v1.1.6

func (q *RawQuery) NewAddColumn() *AddColumnQuery

func (*RawQuery) NewCreateIndex added in v1.1.6

func (q *RawQuery) NewCreateIndex() *CreateIndexQuery

func (*RawQuery) NewCreateTable added in v1.1.6

func (q *RawQuery) NewCreateTable() *CreateTableQuery

func (*RawQuery) NewDelete added in v1.1.6

func (q *RawQuery) NewDelete() *DeleteQuery

func (*RawQuery) NewDropColumn added in v1.1.6

func (q *RawQuery) NewDropColumn() *DropColumnQuery

func (*RawQuery) NewDropIndex added in v1.1.6

func (q *RawQuery) NewDropIndex() *DropIndexQuery

func (*RawQuery) NewDropTable added in v1.1.6

func (q *RawQuery) NewDropTable() *DropTableQuery

func (*RawQuery) NewInsert added in v1.1.6

func (q *RawQuery) NewInsert() *InsertQuery

func (*RawQuery) NewRaw added in v1.1.8

func (q *RawQuery) NewRaw(query string, args ...any) *RawQuery

func (*RawQuery) NewSelect added in v1.1.6

func (q *RawQuery) NewSelect() *SelectQuery

func (*RawQuery) NewTruncateTable added in v1.1.6

func (q *RawQuery) NewTruncateTable() *TruncateTableQuery

func (*RawQuery) NewUpdate added in v1.1.6

func (q *RawQuery) NewUpdate() *UpdateQuery

func (*RawQuery) NewValues added in v1.1.6

func (q *RawQuery) NewValues(model any) *ValuesQuery

func (*RawQuery) Operation added in v1.1.6

func (q *RawQuery) Operation() string

func (*RawQuery) Scan added in v1.1.6

func (q *RawQuery) Scan(ctx context.Context, dest ...any) error

func (*RawQuery) String added in v1.2.6

func (q *RawQuery) String() string

String returns the generated SQL query string. The RawQuery instance must not be modified during query generation to ensure multiple calls to String() return identical results.

type RelationOpts added in v1.2.8

type RelationOpts struct {
	// Apply applies additional options to the relation.
	Apply func(*SelectQuery) *SelectQuery
	// AdditionalJoinOnConditions adds additional conditions to the JOIN ON clause.
	AdditionalJoinOnConditions []schema.QueryWithArgs
}

RelationOpts configures how a relation is joined in a SelectQuery.

type Safe

type Safe = schema.Safe

Safe marks a SQL fragment as trusted and prevents further escaping.

type SelectQuery

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

SelectQuery builds SQL SELECT statements.

func NewSelectQuery

func NewSelectQuery(db *DB) *SelectQuery

NewSelectQuery returns a SelectQuery attached to the provided DB.

func (*SelectQuery) AppendQuery

func (q *SelectQuery) AppendQuery(gen schema.QueryGen, b []byte) (_ []byte, err error)

func (*SelectQuery) Apply

func (q *SelectQuery) Apply(fns ...func(*SelectQuery) *SelectQuery) *SelectQuery

Apply calls each function in fns, passing the SelectQuery as an argument.

func (*SelectQuery) ApplyQueryBuilder added in v1.1.4

func (q *SelectQuery) ApplyQueryBuilder(fn func(QueryBuilder) QueryBuilder) *SelectQuery

ApplyQueryBuilder applies a function to a generic QueryBuilder and returns the modified SelectQuery.

func (*SelectQuery) Clone added in v1.2.10

func (q *SelectQuery) Clone() *SelectQuery

Clone creates a deep copy of the SelectQuery.

func (*SelectQuery) Column

func (q *SelectQuery) Column(columns ...string) *SelectQuery

Column adds columns to the SELECT clause.

func (*SelectQuery) ColumnExpr

func (q *SelectQuery) ColumnExpr(query string, args ...any) *SelectQuery

ColumnExpr adds a column expression to the SELECT clause with arguments.

func (*SelectQuery) Comment added in v1.2.8

func (q *SelectQuery) Comment(comment string) *SelectQuery

Comment adds a comment to the query, wrapped by /* ... */.

func (*SelectQuery) Conn added in v0.1.14

func (q *SelectQuery) Conn(db IConn) *SelectQuery

Conn sets the database connection for this query.

func (*SelectQuery) Count

func (q *SelectQuery) Count(ctx context.Context) (int, error)

Count executes the query and returns the number of rows that match.

func (*SelectQuery) Distinct

func (q *SelectQuery) Distinct() *SelectQuery

Distinct adds a DISTINCT clause to eliminate duplicate rows.

func (*SelectQuery) DistinctOn

func (q *SelectQuery) DistinctOn(query string, args ...any) *SelectQuery

DistinctOn adds a DISTINCT ON clause for PostgreSQL-specific distinct behavior.

func (*SelectQuery) Err added in v1.1.10

func (q *SelectQuery) Err(err error) *SelectQuery

Err sets an error on the query, causing subsequent operations to fail.

func (*SelectQuery) Except

func (q *SelectQuery) Except(other *SelectQuery) *SelectQuery

Except returns rows in this query that are not in another (removes duplicates).

func (*SelectQuery) ExceptAll

func (q *SelectQuery) ExceptAll(other *SelectQuery) *SelectQuery

ExceptAll returns rows in this query that are not in another (keeps duplicates).

func (*SelectQuery) ExcludeColumn

func (q *SelectQuery) ExcludeColumn(columns ...string) *SelectQuery

ExcludeColumn excludes specific columns from being selected.

func (*SelectQuery) Exec

func (q *SelectQuery) Exec(ctx context.Context, dest ...any) (res sql.Result, err error)

Exec executes the query and optionally scans results into dest.

func (*SelectQuery) Exists added in v1.0.7

func (q *SelectQuery) Exists(ctx context.Context) (bool, error)

Exists checks whether any rows match the query.

func (*SelectQuery) For

func (q *SelectQuery) For(s string, args ...any) *SelectQuery

For adds a FOR clause for row locking (e.g., "UPDATE", "SHARE").

func (*SelectQuery) ForceIndex added in v1.1.6

func (q *SelectQuery) ForceIndex(indexes ...string) *SelectQuery

ForceIndex adds a FORCE INDEX hint for MySQL to require index usage.

func (*SelectQuery) ForceIndexForGroupBy added in v1.1.6

func (q *SelectQuery) ForceIndexForGroupBy(indexes ...string) *SelectQuery

ForceIndexForGroupBy adds a FORCE INDEX FOR GROUP BY hint for MySQL.

func (*SelectQuery) ForceIndexForJoin added in v1.1.6

func (q *SelectQuery) ForceIndexForJoin(indexes ...string) *SelectQuery

ForceIndexForJoin adds a FORCE INDEX FOR JOIN hint for MySQL.

func (*SelectQuery) ForceIndexForOrderBy added in v1.1.6

func (q *SelectQuery) ForceIndexForOrderBy(indexes ...string) *SelectQuery

ForceIndexForOrderBy adds a FORCE INDEX FOR ORDER BY hint for MySQL.

func (*SelectQuery) Group

func (q *SelectQuery) Group(columns ...string) *SelectQuery

Group adds columns to the GROUP BY clause.

func (*SelectQuery) GroupExpr

func (q *SelectQuery) GroupExpr(group string, args ...any) *SelectQuery

GroupExpr adds a GROUP BY expression with optional arguments.

func (*SelectQuery) Having

func (q *SelectQuery) Having(having string, args ...any) *SelectQuery

Having adds a HAVING clause condition to filter grouped results.

func (*SelectQuery) IgnoreIndex added in v1.1.6

func (q *SelectQuery) IgnoreIndex(indexes ...string) *SelectQuery

IgnoreIndex adds an IGNORE INDEX hint for MySQL to prevent index usage.

func (*SelectQuery) IgnoreIndexForGroupBy added in v1.1.6

func (q *SelectQuery) IgnoreIndexForGroupBy(indexes ...string) *SelectQuery

IgnoreIndexForGroupBy adds an IGNORE INDEX FOR GROUP BY hint for MySQL.

func (*SelectQuery) IgnoreIndexForJoin added in v1.1.6

func (q *SelectQuery) IgnoreIndexForJoin(indexes ...string) *SelectQuery

IgnoreIndexForJoin adds an IGNORE INDEX FOR JOIN hint for MySQL.

func (*SelectQuery) IgnoreIndexForOrderBy added in v1.1.6

func (q *SelectQuery) IgnoreIndexForOrderBy(indexes ...string) *SelectQuery

IgnoreIndexForOrderBy adds an IGNORE INDEX FOR ORDER BY hint for MySQL.

func (*SelectQuery) Intersect

func (q *SelectQuery) Intersect(other *SelectQuery) *SelectQuery

Intersect returns rows that appear in both this query and another (removes duplicates).

func (*SelectQuery) IntersectAll

func (q *SelectQuery) IntersectAll(other *SelectQuery) *SelectQuery

IntersectAll returns rows that appear in both this query and another (keeps duplicates).

func (*SelectQuery) Join

func (q *SelectQuery) Join(join string, args ...any) *SelectQuery

Join adds a JOIN clause with the specified join expression.

func (*SelectQuery) JoinOn

func (q *SelectQuery) JoinOn(cond string, args ...any) *SelectQuery

JoinOn adds an ON condition to the most recent JOIN, combined with AND.

func (*SelectQuery) JoinOnOr

func (q *SelectQuery) JoinOnOr(cond string, args ...any) *SelectQuery

JoinOnOr adds an ON condition to the most recent JOIN, combined with OR.

func (*SelectQuery) Limit

func (q *SelectQuery) Limit(n int) *SelectQuery

Limit sets the maximum number of rows to return.

func (*SelectQuery) Model

func (q *SelectQuery) Model(model any) *SelectQuery

Model sets the model to select into and generates SELECT and FROM clauses.

func (*SelectQuery) ModelTableExpr

func (q *SelectQuery) ModelTableExpr(query string, args ...any) *SelectQuery

ModelTableExpr overrides the table name derived from the model.

func (*SelectQuery) Offset

func (q *SelectQuery) Offset(n int) *SelectQuery

Offset sets the number of rows to skip before returning results.

func (*SelectQuery) Operation added in v1.0.5

func (q *SelectQuery) Operation() string

Operation returns the query operation name ("SELECT").

func (*SelectQuery) Order

func (q *SelectQuery) Order(orders ...string) *SelectQuery

Order adds columns to the ORDER BY clause.

func (*SelectQuery) OrderBy added in v1.2.16

func (q *SelectQuery) OrderBy(colName string, sortDir Order) *SelectQuery

OrderBy adds an ORDER BY clause with explicit sort direction.

func (*SelectQuery) OrderExpr

func (q *SelectQuery) OrderExpr(query string, args ...any) *SelectQuery

OrderExpr adds an ORDER BY expression with optional arguments.

func (*SelectQuery) QueryBuilder added in v1.1.4

func (q *SelectQuery) QueryBuilder() QueryBuilder

QueryBuilder wraps the SelectQuery in a generic QueryBuilder interface.

func (*SelectQuery) Relation

func (q *SelectQuery) Relation(name string, apply ...func(*SelectQuery) *SelectQuery) *SelectQuery

Relation adds a relation to the query.

func (*SelectQuery) RelationWithOpts added in v1.2.8

func (q *SelectQuery) RelationWithOpts(name string, opts RelationOpts) *SelectQuery

RelationWithOpts adds a relation to the query with additional options.

func (*SelectQuery) Rows

func (q *SelectQuery) Rows(ctx context.Context) (*sql.Rows, error)

Rows executes the query and returns the result rows for manual scanning.

func (*SelectQuery) Scan

func (q *SelectQuery) Scan(ctx context.Context, dest ...any) error

Scan executes the query and scans the results into dest.

func (*SelectQuery) ScanAndCount

func (q *SelectQuery) ScanAndCount(ctx context.Context, dest ...any) (int, error)

ScanAndCount executes the query, scans results into dest, and returns the total count.

func (*SelectQuery) String added in v1.1.6

func (q *SelectQuery) String() string

String returns the generated SQL query string. The SelectQuery instance must not be modified during query generation to ensure multiple calls to String() return identical results.

func (*SelectQuery) Table

func (q *SelectQuery) Table(tables ...string) *SelectQuery

Table specifies the table(s) to select from.

func (*SelectQuery) TableExpr

func (q *SelectQuery) TableExpr(query string, args ...any) *SelectQuery

TableExpr adds a table expression to the FROM clause with arguments.

func (*SelectQuery) Union

func (q *SelectQuery) Union(other *SelectQuery) *SelectQuery

Union combines this query with another using UNION (removes duplicates).

func (*SelectQuery) UnionAll

func (q *SelectQuery) UnionAll(other *SelectQuery) *SelectQuery

UnionAll combines this query with another using UNION ALL (keeps duplicates).

func (*SelectQuery) UseIndex added in v1.1.6

func (q *SelectQuery) UseIndex(indexes ...string) *SelectQuery

UseIndex adds a USE INDEX hint for MySQL to suggest index usage.

func (*SelectQuery) UseIndexForGroupBy added in v1.1.6

func (q *SelectQuery) UseIndexForGroupBy(indexes ...string) *SelectQuery

UseIndexForGroupBy adds a USE INDEX FOR GROUP BY hint for MySQL.

func (*SelectQuery) UseIndexForJoin added in v1.1.6

func (q *SelectQuery) UseIndexForJoin(indexes ...string) *SelectQuery

UseIndexForJoin adds a USE INDEX FOR JOIN hint for MySQL.

func (*SelectQuery) UseIndexForOrderBy added in v1.1.6

func (q *SelectQuery) UseIndexForOrderBy(indexes ...string) *SelectQuery

UseIndexForOrderBy adds a USE INDEX FOR ORDER BY hint for MySQL.

func (*SelectQuery) Where

func (q *SelectQuery) Where(query string, args ...any) *SelectQuery

Where adds a WHERE condition combined with AND.

func (*SelectQuery) WhereAllWithDeleted

func (q *SelectQuery) WhereAllWithDeleted() *SelectQuery

WhereAllWithDeleted includes both active and soft-deleted rows.

func (*SelectQuery) WhereDeleted

func (q *SelectQuery) WhereDeleted() *SelectQuery

WhereDeleted adds a WHERE condition to select soft-deleted rows only.

func (*SelectQuery) WhereGroup

func (q *SelectQuery) WhereGroup(sep string, fn func(*SelectQuery) *SelectQuery) *SelectQuery

WhereGroup groups WHERE conditions with the given separator (AND/OR).

func (*SelectQuery) WhereOr

func (q *SelectQuery) WhereOr(query string, args ...any) *SelectQuery

WhereOr adds a WHERE condition combined with OR.

func (*SelectQuery) WherePK

func (q *SelectQuery) WherePK(cols ...string) *SelectQuery

WherePK adds a WHERE condition on the model's primary key columns.

func (*SelectQuery) With

func (q *SelectQuery) With(name string, query Query) *SelectQuery

With adds a WITH clause (Common Table Expression) to the query.

func (*SelectQuery) WithQuery added in v1.2.16

func (q *SelectQuery) WithQuery(query *WithQuery) *SelectQuery

WithQuery adds a pre-configured WITH clause to the query.

func (*SelectQuery) WithRecursive added in v1.1.9

func (q *SelectQuery) WithRecursive(name string, query Query) *SelectQuery

WithRecursive adds a WITH RECURSIVE clause to the query.

type Stmt

type Stmt struct {
	*sql.Stmt
}

Stmt wraps *sql.Stmt so prepared statements participate in Bun logging.

type TableModel added in v1.0.9

type TableModel interface {
	Model

	schema.BeforeAppendModelHook
	schema.BeforeScanRowHook
	schema.AfterScanRowHook
	ScanColumn(column string, src any) error

	Table() *schema.Table
	Relation() *schema.Relation
	// contains filtered or unexported methods
}

TableModel describes models that map to database tables and support query lifecycle hooks.

type TruncateTableQuery

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

TruncateTableQuery builds TRUNCATE TABLE statements.

func NewTruncateTableQuery

func NewTruncateTableQuery(db *DB) *TruncateTableQuery

NewTruncateTableQuery creates a TruncateTableQuery attached to the given DB.

func (*TruncateTableQuery) AppendNamedArg added in v0.4.0

func (q *TruncateTableQuery) AppendNamedArg(gen schema.QueryGen, b []byte, name string) ([]byte, bool)

func (*TruncateTableQuery) AppendQuery

func (q *TruncateTableQuery) AppendQuery(
	gen schema.QueryGen, b []byte,
) (_ []byte, err error)

func (*TruncateTableQuery) Cascade added in v1.0.20

func (q *TruncateTableQuery) Cascade() *TruncateTableQuery

func (*TruncateTableQuery) Comment added in v1.2.8

func (q *TruncateTableQuery) Comment(comment string) *TruncateTableQuery

Comment adds a comment to the query, wrapped by /* ... */.

func (*TruncateTableQuery) Conn added in v0.1.14

func (*TruncateTableQuery) ContinueIdentity

func (q *TruncateTableQuery) ContinueIdentity() *TruncateTableQuery

func (*TruncateTableQuery) DB

func (q *TruncateTableQuery) DB() *DB

func (*TruncateTableQuery) Dialect added in v1.0.21

func (q *TruncateTableQuery) Dialect() schema.Dialect

func (*TruncateTableQuery) Err added in v1.1.10

func (*TruncateTableQuery) Exec

func (q *TruncateTableQuery) Exec(ctx context.Context, dest ...any) (sql.Result, error)

func (*TruncateTableQuery) GetModel added in v0.2.0

func (q *TruncateTableQuery) GetModel() Model

func (*TruncateTableQuery) GetTableName added in v1.0.9

func (q *TruncateTableQuery) GetTableName() string

func (*TruncateTableQuery) Model

func (q *TruncateTableQuery) Model(model any) *TruncateTableQuery

func (*TruncateTableQuery) ModelTableExpr added in v1.2.2

func (q *TruncateTableQuery) ModelTableExpr(query string, args ...any) *TruncateTableQuery

func (*TruncateTableQuery) NewAddColumn added in v1.0.21

func (q *TruncateTableQuery) NewAddColumn() *AddColumnQuery

func (*TruncateTableQuery) NewCreateIndex added in v1.0.21

func (q *TruncateTableQuery) NewCreateIndex() *CreateIndexQuery

func (*TruncateTableQuery) NewCreateTable added in v1.0.21

func (q *TruncateTableQuery) NewCreateTable() *CreateTableQuery

func (*TruncateTableQuery) NewDelete added in v1.0.21

func (q *TruncateTableQuery) NewDelete() *DeleteQuery

func (*TruncateTableQuery) NewDropColumn added in v1.0.21

func (q *TruncateTableQuery) NewDropColumn() *DropColumnQuery

func (*TruncateTableQuery) NewDropIndex added in v1.0.21

func (q *TruncateTableQuery) NewDropIndex() *DropIndexQuery

func (*TruncateTableQuery) NewDropTable added in v1.0.21

func (q *TruncateTableQuery) NewDropTable() *DropTableQuery

func (*TruncateTableQuery) NewInsert added in v1.0.21

func (q *TruncateTableQuery) NewInsert() *InsertQuery

func (*TruncateTableQuery) NewRaw added in v1.1.8

func (q *TruncateTableQuery) NewRaw(query string, args ...any) *RawQuery

func (*TruncateTableQuery) NewSelect added in v1.0.21

func (q *TruncateTableQuery) NewSelect() *SelectQuery

func (*TruncateTableQuery) NewTruncateTable added in v1.0.21

func (q *TruncateTableQuery) NewTruncateTable() *TruncateTableQuery

func (*TruncateTableQuery) NewUpdate added in v1.0.21

func (q *TruncateTableQuery) NewUpdate() *UpdateQuery

func (*TruncateTableQuery) NewValues added in v1.0.21

func (q *TruncateTableQuery) NewValues(model any) *ValuesQuery

func (*TruncateTableQuery) Operation added in v1.0.5

func (q *TruncateTableQuery) Operation() string

func (*TruncateTableQuery) Restrict

func (q *TruncateTableQuery) Restrict() *TruncateTableQuery

func (*TruncateTableQuery) Table

func (q *TruncateTableQuery) Table(tables ...string) *TruncateTableQuery

func (*TruncateTableQuery) TableExpr

func (q *TruncateTableQuery) TableExpr(query string, args ...any) *TruncateTableQuery

type TupleValues added in v1.2.17

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

TupleValues formats a Go slice as a parenthesized SQL tuple (e.g., "(1, 2, 3)").

func Tuple added in v1.2.17

func Tuple(slice any) TupleValues

Tuple creates a TupleValues from a slice for use in SQL expressions.

func (TupleValues) AppendQuery added in v1.2.17

func (in TupleValues) AppendQuery(gen schema.QueryGen, b []byte) (_ []byte, err error)

AppendQuery appends the parenthesized tuple to the byte slice.

type Tx

type Tx struct {
	*sql.Tx
	// contains filtered or unexported fields
}

Tx wraps *sql.Tx and preserves Bun-specific context such as hooks and dialect.

func (Tx) Begin added in v1.1.6

func (tx Tx) Begin() (Tx, error)

Begin creates a savepoint, effectively starting a nested transaction.

func (Tx) BeginTx added in v1.1.6

func (tx Tx) BeginTx(ctx context.Context, _ *sql.TxOptions) (Tx, error)

BeginTx will save a point in the running transaction.

func (Tx) Commit added in v1.0.16

func (tx Tx) Commit() error

Commit commits the transaction or releases the savepoint if this is a nested transaction.

func (Tx) Dialect added in v1.0.20

func (tx Tx) Dialect() schema.Dialect

Dialect returns the database dialect for this transaction.

func (Tx) Exec added in v0.1.3

func (tx Tx) Exec(query string, args ...any) (sql.Result, error)

Exec executes a query without returning rows within this transaction.

func (Tx) ExecContext added in v0.1.3

func (tx Tx) ExecContext(
	ctx context.Context, query string, args ...any,
) (sql.Result, error)

ExecContext executes a query without returning rows within this transaction.

func (Tx) NewAddColumn added in v0.1.12

func (tx Tx) NewAddColumn() *AddColumnQuery

NewAddColumn creates an ALTER TABLE ADD COLUMN query bound to this transaction.

func (Tx) NewCreateIndex added in v0.1.12

func (tx Tx) NewCreateIndex() *CreateIndexQuery

NewCreateIndex creates a CREATE INDEX query bound to this transaction.

func (Tx) NewCreateTable added in v0.1.12

func (tx Tx) NewCreateTable() *CreateTableQuery

NewCreateTable creates a CREATE TABLE query bound to this transaction.

func (Tx) NewDelete added in v0.1.12

func (tx Tx) NewDelete() *DeleteQuery

NewDelete creates a DELETE query bound to this transaction.

func (Tx) NewDropColumn added in v0.1.12

func (tx Tx) NewDropColumn() *DropColumnQuery

NewDropColumn creates an ALTER TABLE DROP COLUMN query bound to this transaction.

func (Tx) NewDropIndex added in v0.1.12

func (tx Tx) NewDropIndex() *DropIndexQuery

NewDropIndex creates a DROP INDEX query bound to this transaction.

func (Tx) NewDropTable added in v0.1.12

func (tx Tx) NewDropTable() *DropTableQuery

NewDropTable creates a DROP TABLE query bound to this transaction.

func (Tx) NewInsert added in v0.1.12

func (tx Tx) NewInsert() *InsertQuery

NewInsert creates an INSERT query bound to this transaction.

func (Tx) NewMerge added in v1.1.10

func (tx Tx) NewMerge() *MergeQuery

NewMerge creates a MERGE query bound to this transaction.

func (Tx) NewRaw added in v1.1.8

func (tx Tx) NewRaw(query string, args ...any) *RawQuery

NewRaw creates a raw SQL query bound to this transaction.

func (Tx) NewSelect added in v0.1.12

func (tx Tx) NewSelect() *SelectQuery

NewSelect creates a SELECT query bound to this transaction.

func (Tx) NewTruncateTable added in v0.1.12

func (tx Tx) NewTruncateTable() *TruncateTableQuery

NewTruncateTable creates a TRUNCATE TABLE query bound to this transaction.

func (Tx) NewUpdate added in v0.1.12

func (tx Tx) NewUpdate() *UpdateQuery

NewUpdate creates an UPDATE query bound to this transaction.

func (Tx) NewValues added in v0.1.12

func (tx Tx) NewValues(model any) *ValuesQuery

NewValues creates a VALUES query bound to this transaction.

func (Tx) Query added in v0.1.3

func (tx Tx) Query(query string, args ...any) (*sql.Rows, error)

Query executes a query returning rows within this transaction.

func (Tx) QueryContext added in v0.1.3

func (tx Tx) QueryContext(
	ctx context.Context, query string, args ...any,
) (*sql.Rows, error)

QueryContext executes a query returning rows within this transaction.

func (Tx) QueryRow added in v0.1.3

func (tx Tx) QueryRow(query string, args ...any) *sql.Row

QueryRow executes a query expected to return at most one row within this transaction.

func (Tx) QueryRowContext added in v0.1.3

func (tx Tx) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row

QueryRowContext executes a query expected to return at most one row within this transaction.

func (Tx) Rollback added in v1.0.16

func (tx Tx) Rollback() error

Rollback rolls back the transaction or rolls back to the savepoint if this is a nested transaction.

func (Tx) RunInTx added in v1.1.6

func (tx Tx) RunInTx(
	ctx context.Context, _ *sql.TxOptions, fn func(ctx context.Context, tx Tx) error,
) error

RunInTx creates a savepoint and runs the function within that savepoint. If the function returns an error, the savepoint is rolled back.

type UpdateQuery

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

UpdateQuery builds SQL UPDATE statements.

func NewUpdateQuery

func NewUpdateQuery(db *DB) *UpdateQuery

NewUpdateQuery returns a new UpdateQuery attached to the provided DB.

func (*UpdateQuery) AppendQuery

func (q *UpdateQuery) AppendQuery(gen schema.QueryGen, b []byte) (_ []byte, err error)

func (*UpdateQuery) Apply

func (q *UpdateQuery) Apply(fns ...func(*UpdateQuery) *UpdateQuery) *UpdateQuery

Apply calls each function in fns, passing the UpdateQuery as an argument.

func (*UpdateQuery) ApplyQueryBuilder added in v1.1.4

func (q *UpdateQuery) ApplyQueryBuilder(fn func(QueryBuilder) QueryBuilder) *UpdateQuery

func (*UpdateQuery) Bulk added in v0.2.8

func (q *UpdateQuery) Bulk() *UpdateQuery

func (*UpdateQuery) Column

func (q *UpdateQuery) Column(columns ...string) *UpdateQuery

func (*UpdateQuery) Comment added in v1.2.8

func (q *UpdateQuery) Comment(comment string) *UpdateQuery

Comment adds a comment to the query, wrapped by /* ... */.

func (*UpdateQuery) Conn added in v0.1.14

func (q *UpdateQuery) Conn(db IConn) *UpdateQuery

func (*UpdateQuery) Err added in v1.1.10

func (q *UpdateQuery) Err(err error) *UpdateQuery

func (*UpdateQuery) ExcludeColumn added in v0.2.0

func (q *UpdateQuery) ExcludeColumn(columns ...string) *UpdateQuery

func (*UpdateQuery) Exec

func (q *UpdateQuery) Exec(ctx context.Context, dest ...any) (sql.Result, error)

func (*UpdateQuery) FQN added in v0.1.3

func (q *UpdateQuery) FQN(column string) Ident

FQN returns a fully qualified column name, for example, table_name.column_name or table_alias.column_alias.

func (*UpdateQuery) ForceIndex added in v1.1.6

func (q *UpdateQuery) ForceIndex(indexes ...string) *UpdateQuery

func (*UpdateQuery) IgnoreIndex added in v1.1.6

func (q *UpdateQuery) IgnoreIndex(indexes ...string) *UpdateQuery

func (*UpdateQuery) Join added in v1.1.17

func (q *UpdateQuery) Join(join string, args ...any) *UpdateQuery

func (*UpdateQuery) JoinOn added in v1.1.17

func (q *UpdateQuery) JoinOn(cond string, args ...any) *UpdateQuery

func (*UpdateQuery) JoinOnOr added in v1.1.17

func (q *UpdateQuery) JoinOnOr(cond string, args ...any) *UpdateQuery

func (*UpdateQuery) Limit added in v1.2.6

func (q *UpdateQuery) Limit(n int) *UpdateQuery

func (*UpdateQuery) Model

func (q *UpdateQuery) Model(model any) *UpdateQuery

func (*UpdateQuery) ModelTableExpr

func (q *UpdateQuery) ModelTableExpr(query string, args ...any) *UpdateQuery

func (*UpdateQuery) OmitZero added in v1.0.10

func (q *UpdateQuery) OmitZero() *UpdateQuery

func (*UpdateQuery) Operation added in v1.0.5

func (q *UpdateQuery) Operation() string

func (*UpdateQuery) Order added in v1.2.6

func (q *UpdateQuery) Order(orders ...string) *UpdateQuery

------------------------------------------------------------------------------

func (*UpdateQuery) OrderExpr added in v1.2.6

func (q *UpdateQuery) OrderExpr(query string, args ...any) *UpdateQuery

func (*UpdateQuery) QueryBuilder added in v1.1.4

func (q *UpdateQuery) QueryBuilder() QueryBuilder

func (*UpdateQuery) Returning

func (q *UpdateQuery) Returning(query string, args ...any) *UpdateQuery

Returning adds a RETURNING clause to the query.

To suppress the auto-generated RETURNING clause, use `Returning("NULL")`.

func (*UpdateQuery) Scan added in v1.1.10

func (q *UpdateQuery) Scan(ctx context.Context, dest ...any) error

func (*UpdateQuery) Set

func (q *UpdateQuery) Set(query string, args ...any) *UpdateQuery

func (*UpdateQuery) SetColumn added in v1.1.0

func (q *UpdateQuery) SetColumn(column string, query string, args ...any) *UpdateQuery

func (*UpdateQuery) String added in v1.1.6

func (q *UpdateQuery) String() string

String returns the generated SQL query string. The UpdateQuery instance must not be modified during query generation to ensure multiple calls to String() return identical results.

func (*UpdateQuery) Table

func (q *UpdateQuery) Table(tables ...string) *UpdateQuery

func (*UpdateQuery) TableExpr

func (q *UpdateQuery) TableExpr(query string, args ...any) *UpdateQuery

func (*UpdateQuery) UseIndex added in v1.1.6

func (q *UpdateQuery) UseIndex(indexes ...string) *UpdateQuery

func (*UpdateQuery) Value

func (q *UpdateQuery) Value(column string, query string, args ...any) *UpdateQuery

Value overwrites model value for the column.

func (*UpdateQuery) Where

func (q *UpdateQuery) Where(query string, args ...any) *UpdateQuery

func (*UpdateQuery) WhereAllWithDeleted

func (q *UpdateQuery) WhereAllWithDeleted() *UpdateQuery

func (*UpdateQuery) WhereDeleted

func (q *UpdateQuery) WhereDeleted() *UpdateQuery

func (*UpdateQuery) WhereGroup

func (q *UpdateQuery) WhereGroup(sep string, fn func(*UpdateQuery) *UpdateQuery) *UpdateQuery

func (*UpdateQuery) WhereOr

func (q *UpdateQuery) WhereOr(query string, args ...any) *UpdateQuery

func (*UpdateQuery) WherePK

func (q *UpdateQuery) WherePK(cols ...string) *UpdateQuery

func (*UpdateQuery) With

func (q *UpdateQuery) With(name string, query Query) *UpdateQuery

func (*UpdateQuery) WithQuery added in v1.2.16

func (q *UpdateQuery) WithQuery(query *WithQuery) *UpdateQuery

func (*UpdateQuery) WithRecursive added in v1.1.9

func (q *UpdateQuery) WithRecursive(name string, query Query) *UpdateQuery

type ValuesQuery

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

ValuesQuery builds VALUES clauses that can be used as subqueries.

func NewValuesQuery

func NewValuesQuery(db *DB, model any) *ValuesQuery

NewValuesQuery creates a VALUES query for the given model.

func (*ValuesQuery) AppendColumns

func (q *ValuesQuery) AppendColumns(gen schema.QueryGen, b []byte) (_ []byte, err error)

AppendColumns appends the table columns. It is used by CTE.

func (*ValuesQuery) AppendNamedArg added in v0.4.0

func (q *ValuesQuery) AppendNamedArg(gen schema.QueryGen, b []byte, name string) ([]byte, bool)

func (*ValuesQuery) AppendQuery

func (q *ValuesQuery) AppendQuery(gen schema.QueryGen, b []byte) (_ []byte, err error)

func (*ValuesQuery) Column added in v1.0.22

func (q *ValuesQuery) Column(columns ...string) *ValuesQuery

func (*ValuesQuery) Comment added in v1.2.8

func (q *ValuesQuery) Comment(comment string) *ValuesQuery

Comment adds a comment to the query, wrapped by /* ... */.

func (*ValuesQuery) Conn added in v0.1.14

func (q *ValuesQuery) Conn(db IConn) *ValuesQuery

func (*ValuesQuery) DB

func (q *ValuesQuery) DB() *DB

func (*ValuesQuery) Dialect added in v1.0.21

func (q *ValuesQuery) Dialect() schema.Dialect

func (*ValuesQuery) Err added in v1.1.10

func (q *ValuesQuery) Err(err error) *ValuesQuery

func (*ValuesQuery) GetModel added in v0.2.0

func (q *ValuesQuery) GetModel() Model

func (*ValuesQuery) GetTableName added in v1.0.9

func (q *ValuesQuery) GetTableName() string

func (*ValuesQuery) NewAddColumn added in v1.0.21

func (q *ValuesQuery) NewAddColumn() *AddColumnQuery

func (*ValuesQuery) NewCreateIndex added in v1.0.21

func (q *ValuesQuery) NewCreateIndex() *CreateIndexQuery

func (*ValuesQuery) NewCreateTable added in v1.0.21

func (q *ValuesQuery) NewCreateTable() *CreateTableQuery

func (*ValuesQuery) NewDelete added in v1.0.21

func (q *ValuesQuery) NewDelete() *DeleteQuery

func (*ValuesQuery) NewDropColumn added in v1.0.21

func (q *ValuesQuery) NewDropColumn() *DropColumnQuery

func (*ValuesQuery) NewDropIndex added in v1.0.21

func (q *ValuesQuery) NewDropIndex() *DropIndexQuery

func (*ValuesQuery) NewDropTable added in v1.0.21

func (q *ValuesQuery) NewDropTable() *DropTableQuery

func (*ValuesQuery) NewInsert added in v1.0.21

func (q *ValuesQuery) NewInsert() *InsertQuery

func (*ValuesQuery) NewRaw added in v1.1.8

func (q *ValuesQuery) NewRaw(query string, args ...any) *RawQuery

func (*ValuesQuery) NewSelect added in v1.0.21

func (q *ValuesQuery) NewSelect() *SelectQuery

func (*ValuesQuery) NewTruncateTable added in v1.0.21

func (q *ValuesQuery) NewTruncateTable() *TruncateTableQuery

func (*ValuesQuery) NewUpdate added in v1.0.21

func (q *ValuesQuery) NewUpdate() *UpdateQuery

func (*ValuesQuery) NewValues added in v1.0.21

func (q *ValuesQuery) NewValues(model any) *ValuesQuery

func (*ValuesQuery) OmitZero added in v1.2.16

func (q *ValuesQuery) OmitZero() *ValuesQuery

func (*ValuesQuery) Operation added in v1.0.5

func (q *ValuesQuery) Operation() string

func (*ValuesQuery) Value added in v1.0.1

func (q *ValuesQuery) Value(column string, expr string, args ...any) *ValuesQuery

Value overwrites model value for the column.

func (*ValuesQuery) WithOrder

func (q *ValuesQuery) WithOrder() *ValuesQuery

type WithQuery added in v1.2.16

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

WithQuery defines a common table expression used by another query.

func NewWithQuery added in v1.2.16

func NewWithQuery(name string, query Query) *WithQuery

NewWithQuery creates a CTE with the given name and underlying query.

func (*WithQuery) Materialized added in v1.2.16

func (q *WithQuery) Materialized() *WithQuery

Materialized hints the planner to evaluate the CTE once and store the results.

func (*WithQuery) NotMaterialized added in v1.2.16

func (q *WithQuery) NotMaterialized() *WithQuery

NotMaterialized hints the planner to inline the CTE, potentially re-evaluating it.

func (*WithQuery) Recursive added in v1.2.16

func (q *WithQuery) Recursive() *WithQuery

Recursive marks the CTE as recursive, enabling self-referential queries.

Directories

Path Synopsis
dbfixture module
feature
Package feature defines flags that represent optional SQL dialect capabilities.
Package feature defines flags that represent optional SQL dialect capabilities.
mssqldialect module
mysqldialect module
oracledialect module
pgdialect module
sqlitedialect module
driver
pgdriver module
sqliteshim module
example
basic module
belongs-to module
custom-type module
fixture module
has-many module
has-one module
migrate module
model-hooks module
multi-tenant module
opentelemetry module
pg-listen module
pg-range module
placeholders module
rel-has-many module
rel-has-one module
trivial module
extra
bunbig module
bundebug module
bunotel module
bunrelic module
bunslog module
fixture module
dbtest module
testfixture module

Jump to

Keyboard shortcuts

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