Skip to content

Commit 125717b

Browse files
authored
chore: add documentation for public API using LLM (#1304)
1 parent 98099ce commit 125717b

19 files changed

+89
-11
lines changed

bun.go

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,78 +8,107 @@ import (
88
)
99

1010
type (
11-
Safe = schema.Safe
12-
Name = schema.Name
11+
// Safe marks a SQL fragment as trusted and prevents further escaping.
12+
Safe = schema.Safe
13+
// Name represents a SQL identifier such as a column or table name.
14+
Name = schema.Name
15+
// Ident is a fully qualified SQL identifier.
1316
Ident = schema.Ident
17+
// Order denotes sorting direction used in ORDER BY clauses.
1418
Order = schema.Order
1519

16-
NullTime = schema.NullTime
20+
// NullTime is a nullable time value compatible with Bun.
21+
NullTime = schema.NullTime
22+
// BaseModel provides default metadata embedded into user models.
1723
BaseModel = schema.BaseModel
18-
Query = schema.Query
24+
// Query is implemented by all Bun query builders.
25+
Query = schema.Query
1926

27+
// BeforeAppendModelHook is called before a model is appended to a query.
2028
BeforeAppendModelHook = schema.BeforeAppendModelHook
2129

30+
// BeforeScanRowHook runs before scanning an individual row.
2231
BeforeScanRowHook = schema.BeforeScanRowHook
23-
AfterScanRowHook = schema.AfterScanRowHook
32+
// AfterScanRowHook runs after scanning an individual row.
33+
AfterScanRowHook = schema.AfterScanRowHook
2434
)
2535

2636
const (
27-
OrderAsc = schema.OrderAsc
28-
OrderAscNullsFirst = schema.OrderDesc
29-
OrderAscNullsLast = schema.OrderAscNullsLast
30-
OrderDesc = schema.OrderDesc
37+
// OrderAsc sorts values in ascending order.
38+
OrderAsc = schema.OrderAsc
39+
// OrderAscNullsFirst sorts ascending with NULL values first.
40+
OrderAscNullsFirst = schema.OrderDesc
41+
// OrderAscNullsLast sorts ascending with NULL values last.
42+
OrderAscNullsLast = schema.OrderAscNullsLast
43+
// OrderDesc sorts values in descending order.
44+
OrderDesc = schema.OrderDesc
45+
// OrderDescNullsFirst sorts descending with NULL values first.
3146
OrderDescNullsFirst = schema.OrderDescNullsFirst
32-
OrderDescNullsLast = schema.OrderDescNullsLast
47+
// OrderDescNullsLast sorts descending with NULL values last.
48+
OrderDescNullsLast = schema.OrderDescNullsLast
3349
)
3450

51+
// SafeQuery wraps a raw query string and arguments and marks it safe for Bun.
3552
func SafeQuery(query string, args ...any) schema.QueryWithArgs {
3653
return schema.SafeQuery(query, args)
3754
}
3855

56+
// BeforeSelectHook is invoked before executing SELECT queries.
3957
type BeforeSelectHook interface {
4058
BeforeSelect(ctx context.Context, query *SelectQuery) error
4159
}
4260

61+
// AfterSelectHook is invoked after executing SELECT queries.
4362
type AfterSelectHook interface {
4463
AfterSelect(ctx context.Context, query *SelectQuery) error
4564
}
4665

66+
// BeforeInsertHook is invoked before executing INSERT queries.
4767
type BeforeInsertHook interface {
4868
BeforeInsert(ctx context.Context, query *InsertQuery) error
4969
}
5070

71+
// AfterInsertHook is invoked after executing INSERT queries.
5172
type AfterInsertHook interface {
5273
AfterInsert(ctx context.Context, query *InsertQuery) error
5374
}
5475

76+
// BeforeUpdateHook is invoked before executing UPDATE queries.
5577
type BeforeUpdateHook interface {
5678
BeforeUpdate(ctx context.Context, query *UpdateQuery) error
5779
}
5880

81+
// AfterUpdateHook is invoked after executing UPDATE queries.
5982
type AfterUpdateHook interface {
6083
AfterUpdate(ctx context.Context, query *UpdateQuery) error
6184
}
6285

86+
// BeforeDeleteHook is invoked before executing DELETE queries.
6387
type BeforeDeleteHook interface {
6488
BeforeDelete(ctx context.Context, query *DeleteQuery) error
6589
}
6690

91+
// AfterDeleteHook is invoked after executing DELETE queries.
6792
type AfterDeleteHook interface {
6893
AfterDelete(ctx context.Context, query *DeleteQuery) error
6994
}
7095

96+
// BeforeCreateTableHook is invoked before executing CREATE TABLE queries.
7197
type BeforeCreateTableHook interface {
7298
BeforeCreateTable(ctx context.Context, query *CreateTableQuery) error
7399
}
74100

101+
// AfterCreateTableHook is invoked after executing CREATE TABLE queries.
75102
type AfterCreateTableHook interface {
76103
AfterCreateTable(ctx context.Context, query *CreateTableQuery) error
77104
}
78105

106+
// BeforeDropTableHook is invoked before executing DROP TABLE queries.
79107
type BeforeDropTableHook interface {
80108
BeforeDropTable(ctx context.Context, query *DropTableQuery) error
81109
}
82110

111+
// AfterDropTableHook is invoked after executing DROP TABLE queries.
83112
type AfterDropTableHook interface {
84113
AfterDropTable(ctx context.Context, query *DropTableQuery) error
85114
}
@@ -89,10 +118,12 @@ func SetLogger(logger internal.Logging) {
89118
internal.SetLogger(logger)
90119
}
91120

121+
// In wraps a slice so it can be used with the IN clause.
92122
func In(slice any) schema.QueryAppender {
93123
return schema.In(slice)
94124
}
95125

126+
// NullZero forces zero values to be treated as NULL when building queries.
96127
func NullZero(value any) schema.QueryAppender {
97128
return schema.NullZero(value)
98129
}

db.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ const (
1919
discardUnknownColumns internal.Flag = 1 << iota
2020
)
2121

22+
// DBStats tracks aggregate query counters collected by Bun.
2223
type DBStats struct {
2324
Queries uint32
2425
Errors uint32
2526
}
2627

28+
// DBOption mutates DB configuration during construction.
2729
type DBOption func(db *DB)
2830

31+
// WithOptions applies multiple DBOption values at once.
2932
func WithOptions(opts ...DBOption) DBOption {
3033
return func(db *DB) {
3134
for _, opt := range opts {
@@ -34,6 +37,7 @@ func WithOptions(opts ...DBOption) DBOption {
3437
}
3538
}
3639

40+
// WithDiscardUnknownColumns ignores columns returned by queries that are not present in models.
3741
func WithDiscardUnknownColumns() DBOption {
3842
return func(db *DB) {
3943
db.flags = db.flags.Set(discardUnknownColumns)
@@ -46,12 +50,14 @@ type ConnResolver interface {
4650
Close() error
4751
}
4852

53+
// WithConnResolver registers a connection resolver that chooses a connection per query.
4954
func WithConnResolver(resolver ConnResolver) DBOption {
5055
return func(db *DB) {
5156
db.resolver = resolver
5257
}
5358
}
5459

60+
// DB is the central access point for building and executing Bun queries.
5561
type DB struct {
5662
// Must be a pointer so we copy the whole state, not individual fields.
5763
*noCopyState
@@ -73,6 +79,7 @@ type noCopyState struct {
7379
stats DBStats
7480
}
7581

82+
// NewDB wraps an existing *sql.DB with Bun using the given dialect and options.
7683
func NewDB(sqldb *sql.DB, dialect schema.Dialect, opts ...DBOption) *DB {
7784
dialect.Init(sqldb)
7885

@@ -355,6 +362,7 @@ func (db *DB) format(query string, args []any) string {
355362

356363
//------------------------------------------------------------------------------
357364

365+
// Conn wraps *sql.Conn so queries continue to use Bun features and hooks.
358366
type Conn struct {
359367
db *DB
360368
*sql.Conn
@@ -501,6 +509,7 @@ func (c Conn) BeginTx(ctx context.Context, opts *sql.TxOptions) (Tx, error) {
501509

502510
//------------------------------------------------------------------------------
503511

512+
// Stmt wraps *sql.Stmt so prepared statements participate in Bun logging.
504513
type Stmt struct {
505514
*sql.Stmt
506515
}
@@ -519,6 +528,7 @@ func (db *DB) PrepareContext(ctx context.Context, query string) (Stmt, error) {
519528

520529
//------------------------------------------------------------------------------
521530

531+
// Tx wraps *sql.Tx and preserves Bun-specific context such as hooks and dialect.
522532
type Tx struct {
523533
ctx context.Context
524534
db *DB

hook.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"unicode"
1010
)
1111

12+
// QueryEvent captures information about a query execution for hooks.
1213
type QueryEvent struct {
1314
DB *DB
1415

@@ -25,6 +26,7 @@ type QueryEvent struct {
2526
Stash map[any]any
2627
}
2728

29+
// Operation returns the SQL operation name such as SELECT or UPDATE.
2830
func (e *QueryEvent) Operation() string {
2931
if e.IQuery != nil {
3032
return e.IQuery.Operation()
@@ -44,6 +46,7 @@ func queryOperation(query string) string {
4446
return queryOp
4547
}
4648

49+
// QueryHook allows observing queries before and after execution.
4750
type QueryHook interface {
4851
BeforeQuery(context.Context, *QueryEvent) context.Context
4952
AfterQuery(context.Context, *QueryEvent)

model.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ var (
1818
bytesType = reflect.TypeFor[[]byte]()
1919
)
2020

21+
// Model is implemented by all Bun models.
2122
type Model = schema.Model
2223

2324
type rowScanner interface {
2425
ScanRow(ctx context.Context, rows *sql.Rows) error
2526
}
2627

28+
// TableModel describes models that map to database tables and support query lifecycle hooks.
2729
type TableModel interface {
2830
Model
2931

query_base.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const (
2222
allWithDeletedFlag
2323
)
2424

25+
// WithQuery defines a common table expression used by another query.
2526
type WithQuery struct {
2627
name string
2728
query Query
@@ -76,7 +77,7 @@ var (
7677
_ IDB = (*Tx)(nil)
7778
)
7879

79-
// QueryBuilder is used for common query methods
80+
// QueryBuilder exposes shared filtering helpers across query builders.
8081
type QueryBuilder interface {
8182
Query
8283
Where(query string, args ...any) QueryBuilder
@@ -257,6 +258,7 @@ func (q *baseQuery) isSoftDelete() bool {
257258

258259
//------------------------------------------------------------------------------
259260

261+
// NewWithQuery creates a CTE with the given name and underlying query.
260262
func NewWithQuery(name string, query Query) *WithQuery {
261263
return &WithQuery{
262264
name: name,
@@ -1568,6 +1570,7 @@ func (q *orderLimitOffsetQuery) appendLimitOffset(gen schema.QueryGen, b []byte)
15681570
return b, nil
15691571
}
15701572

1573+
// IsReadOnlyQuery reports whether the provided query and its CTEs are SELECT-only.
15711574
func IsReadOnlyQuery(query Query) bool {
15721575
sel, ok := query.(*SelectQuery)
15731576
if !ok {

query_column_add.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/uptrace/bun/schema"
1111
)
1212

13+
// AddColumnQuery builds ALTER TABLE ... ADD COLUMN statements.
1314
type AddColumnQuery struct {
1415
baseQuery
1516

@@ -19,6 +20,7 @@ type AddColumnQuery struct {
1920

2021
var _ Query = (*AddColumnQuery)(nil)
2122

23+
// NewAddColumnQuery creates an AddColumnQuery bound to the provided DB.
2224
func NewAddColumnQuery(db *DB) *AddColumnQuery {
2325
q := &AddColumnQuery{
2426
baseQuery: baseQuery{

query_column_drop.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/uptrace/bun/schema"
1010
)
1111

12+
// DropColumnQuery builds ALTER TABLE ... DROP COLUMN statements.
1213
type DropColumnQuery struct {
1314
baseQuery
1415

@@ -17,6 +18,7 @@ type DropColumnQuery struct {
1718

1819
var _ Query = (*DropColumnQuery)(nil)
1920

21+
// NewDropColumnQuery creates a DropColumnQuery bound to the provided DB.
2022
func NewDropColumnQuery(db *DB) *DropColumnQuery {
2123
q := &DropColumnQuery{
2224
baseQuery: baseQuery{

query_delete.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/uptrace/bun/schema"
1212
)
1313

14+
// DeleteQuery builds SQL DELETE statements.
1415
type DeleteQuery struct {
1516
whereBaseQuery
1617
orderLimitOffsetQuery
@@ -21,6 +22,7 @@ type DeleteQuery struct {
2122

2223
var _ Query = (*DeleteQuery)(nil)
2324

25+
// NewDeleteQuery returns a DeleteQuery associated with the provided DB.
2426
func NewDeleteQuery(db *DB) *DeleteQuery {
2527
q := &DeleteQuery{
2628
whereBaseQuery: whereBaseQuery{

query_index_create.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/uptrace/bun/schema"
99
)
1010

11+
// CreateIndexQuery builds CREATE INDEX statements.
1112
type CreateIndexQuery struct {
1213
whereBaseQuery
1314

@@ -25,6 +26,7 @@ type CreateIndexQuery struct {
2526

2627
var _ Query = (*CreateIndexQuery)(nil)
2728

29+
// NewCreateIndexQuery returns a CreateIndexQuery tied to the provided DB.
2830
func NewCreateIndexQuery(db *DB) *CreateIndexQuery {
2931
q := &CreateIndexQuery{
3032
whereBaseQuery: whereBaseQuery{

query_index_drop.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/uptrace/bun/schema"
99
)
1010

11+
// DropIndexQuery builds DROP INDEX statements.
1112
type DropIndexQuery struct {
1213
baseQuery
1314
cascadeQuery
@@ -21,6 +22,7 @@ type DropIndexQuery struct {
2122

2223
var _ Query = (*DropIndexQuery)(nil)
2324

25+
// NewDropIndexQuery returns a DropIndexQuery associated with the provided DB.
2426
func NewDropIndexQuery(db *DB) *DropIndexQuery {
2527
q := &DropIndexQuery{
2628
baseQuery: baseQuery{

0 commit comments

Comments
 (0)