• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stephenafamo / bob / 15243731916

26 May 2025 12:43AM UTC coverage: 30.915% (-0.08%) from 30.996%
15243731916

push

github

web-flow
Merge pull request #428 from stephenafamo/pk-columns

Added a `PrimaryKey` method to `{dialect}.Table`

16 of 38 new or added lines in 7 files covered. (42.11%)

6690 of 21640 relevant lines covered (30.91%)

117.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/dialect/sqlite/table.go
1
package sqlite
2

3
import (
4
        "context"
5
        "reflect"
6

7
        "github.com/stephenafamo/bob"
8
        "github.com/stephenafamo/bob/dialect/sqlite/dialect"
9
        "github.com/stephenafamo/bob/dialect/sqlite/dm"
10
        "github.com/stephenafamo/bob/dialect/sqlite/im"
11
        "github.com/stephenafamo/bob/dialect/sqlite/um"
12
        "github.com/stephenafamo/bob/internal/mappings"
13
        "github.com/stephenafamo/bob/orm"
14
)
15

16
type setter[T any] interface {
17
        orm.Setter[T, *dialect.InsertQuery, *dialect.UpdateQuery]
18
}
19

20
func NewTable[T orm.Model, Tset setter[T]](schema, tableName string) *Table[T, []T, Tset] {
×
21
        return NewTablex[T, []T, Tset](schema, tableName)
×
22
}
×
23

24
func NewTablex[T orm.Model, Tslice ~[]T, Tset setter[T]](schema, tableName string) *Table[T, Tslice, Tset] {
×
NEW
25
        setMapping := mappings.GetMappings(reflect.TypeOf(*new(Tset)))
×
26
        view, mappings := newView[T, Tslice](schema, tableName)
×
27
        t := &Table[T, Tslice, Tset]{
×
28
                View:          view,
×
NEW
29
                pkCols:        orm.NewColumns(mappings.PKs...).WithParent(schema, tableName),
×
30
                setterMapping: setMapping,
×
31
        }
×
32

×
33
        return t
×
34
}
×
35

36
// The table contains extract information from the struct and contains
37
// caches ???
38
type Table[T orm.Model, Tslice ~[]T, Tset setter[T]] struct {
39
        *View[T, Tslice]
40
        pkCols        orm.Columns
41
        setterMapping mappings.Mapping
42

43
        BeforeInsertHooks bob.Hooks[Tset, bob.SkipModelHooksKey]
44
        AfterInsertHooks  bob.Hooks[Tslice, bob.SkipModelHooksKey]
45

46
        BeforeUpdateHooks bob.Hooks[Tslice, bob.SkipModelHooksKey]
47
        AfterUpdateHooks  bob.Hooks[Tslice, bob.SkipModelHooksKey]
48

49
        BeforeDeleteHooks bob.Hooks[Tslice, bob.SkipModelHooksKey]
50
        AfterDeleteHooks  bob.Hooks[Tslice, bob.SkipModelHooksKey]
51

52
        InsertQueryHooks bob.Hooks[*dialect.InsertQuery, bob.SkipQueryHooksKey]
53
        UpdateQueryHooks bob.Hooks[*dialect.UpdateQuery, bob.SkipQueryHooksKey]
54
        DeleteQueryHooks bob.Hooks[*dialect.DeleteQuery, bob.SkipQueryHooksKey]
55
}
56

57
// Returns the primary key columns for this table.
NEW
58
func (t *Table[T, Tslice, Tset]) PrimaryKey() orm.Columns {
×
NEW
59
        return t.pkCols
×
NEW
60
}
×
61

62
// Starts an insert query for this table
63
func (t *Table[T, Tslice, Tset]) Insert(queryMods ...bob.Mod[*dialect.InsertQuery]) *orm.Query[*dialect.InsertQuery, T, Tslice] {
×
64
        q := &orm.Query[*dialect.InsertQuery, T, Tslice]{
×
65
                ExecQuery: orm.ExecQuery[*dialect.InsertQuery]{
×
66
                        BaseQuery: Insert(im.Into(t.NameAs())),
×
67
                        Hooks:     &t.InsertQueryHooks,
×
68
                },
×
69
                Scanner: t.scanner,
×
70
        }
×
71

×
72
        q.Expression.AppendContextualModFunc(
×
73
                func(ctx context.Context, q *dialect.InsertQuery) (context.Context, error) {
×
74
                        if !q.HasReturning() {
×
75
                                q.AppendReturning(t.returningCols)
×
76
                        }
×
77
                        return ctx, nil
×
78
                },
79
        )
80

81
        q.Apply(queryMods...)
×
82

×
83
        return q
×
84
}
85

86
// Starts an Update query for this table
87
func (t *Table[T, Tslice, Tset]) Update(queryMods ...bob.Mod[*dialect.UpdateQuery]) *orm.Query[*dialect.UpdateQuery, T, Tslice] {
×
88
        q := &orm.Query[*dialect.UpdateQuery, T, Tslice]{
×
89
                ExecQuery: orm.ExecQuery[*dialect.UpdateQuery]{
×
90
                        BaseQuery: Update(um.Table(t.NameAs())),
×
91
                        Hooks:     &t.UpdateQueryHooks,
×
92
                },
×
93
                Scanner: t.scanner,
×
94
        }
×
95

×
96
        q.Expression.AppendContextualModFunc(
×
97
                func(ctx context.Context, q *dialect.UpdateQuery) (context.Context, error) {
×
98
                        if !q.HasReturning() {
×
99
                                q.AppendReturning(t.returningCols)
×
100
                        }
×
101
                        return ctx, nil
×
102
                },
103
        )
104

105
        q.Apply(queryMods...)
×
106

×
107
        return q
×
108
}
109

110
// Starts a Delete query for this table
111
func (t *Table[T, Tslice, Tset]) Delete(queryMods ...bob.Mod[*dialect.DeleteQuery]) *orm.Query[*dialect.DeleteQuery, T, Tslice] {
×
112
        q := &orm.Query[*dialect.DeleteQuery, T, Tslice]{
×
113
                ExecQuery: orm.ExecQuery[*dialect.DeleteQuery]{
×
114
                        BaseQuery: Delete(dm.From(t.NameAs())),
×
115
                        Hooks:     &t.DeleteQueryHooks,
×
116
                },
×
117
                Scanner: t.scanner,
×
118
        }
×
119

×
120
        q.Expression.AppendContextualModFunc(
×
121
                func(ctx context.Context, q *dialect.DeleteQuery) (context.Context, error) {
×
122
                        if !q.HasReturning() {
×
123
                                q.AppendReturning(t.returningCols)
×
124
                        }
×
125
                        return ctx, nil
×
126
                },
127
        )
128

129
        q.Apply(queryMods...)
×
130

×
131
        return q
×
132
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc