(core) Update golang dependencies

``
go get -u ./...
go mod tidy
```
Fixed SQLite related code to fit latest changes in  SQLite driver
This commit is contained in:
Alexandr Stelnykovych
2025-11-12 12:37:33 +02:00
parent 2009dcf9c8
commit f9105fc738
21 changed files with 1704 additions and 466 deletions

View File

@@ -0,0 +1,51 @@
// Code generated by BobGen sqlite v0.41.1. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package dberrors
import (
"errors"
"fmt"
"strings"
sqliteDriver "modernc.org/sqlite"
)
// ErrUniqueConstraint captures all unique constraint errors by explicitly leaving `s` empty.
var ErrUniqueConstraint = &UniqueConstraintError{s: ""}
type UniqueConstraintError struct {
// schema is the schema where the unique constraint is defined.
schema string
// table is the name of the table where the unique constraint is defined.
table string
// columns are the columns constituting the unique constraint.
columns []string
// s is a string uniquely identifying the constraint in the raw error message returned from the database.
s string
}
func (e *UniqueConstraintError) Error() string {
return e.s
}
func (e *UniqueConstraintError) Is(target error) bool {
var err *sqliteDriver.Error
if !errors.As(target, &err) {
return false
}
// 1555 is for Primary Key Constraint
// 2067 is for Unique Constraint
if err.Code() != 1555 && err.Code() != 2067 {
return false
}
for _, col := range e.columns {
if !strings.Contains(err.Error(), fmt.Sprintf("%s.%s", e.table, col)) {
return false
}
}
return true
}

View File

@@ -0,0 +1,9 @@
// Code generated by BobGen sqlite v0.41.1. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package dberrors
import "github.com/stephenafamo/bob"
// Set the testDB to enable tests that use the database
var testDB bob.Transactor[bob.Tx]

View File

@@ -0,0 +1,17 @@
// Code generated by BobGen sqlite v0.41.1. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package dberrors
var RecordErrors = &recordErrors{
ErrUniquePkMainRecords: &UniqueConstraintError{
schema: "",
table: "records",
columns: []string{"key"},
s: "pk_main_records",
},
}
type recordErrors struct {
ErrUniquePkMainRecords *UniqueConstraintError
}

View File

@@ -0,0 +1,80 @@
// Code generated by BobGen sqlite v0.41.1. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package dbinfo
import "github.com/aarondl/opt/null"
type Table[Cols columns, Idxs indexes, FKs foreignKeys, U uniques, C checks] struct {
Schema string
Name string
Columns Cols
Indexes Idxs
PrimaryKey *constraint
ForeignKeys FKs
Uniques U
Checks C
Comment string
}
type columns interface {
AsSlice() []column
}
type column struct {
Name string
DBType string
Default string
Comment string
Nullable bool
Generated bool
AutoIncr bool
}
type indexes interface {
AsSlice() []index
}
type index struct {
Type string
Name string
Columns []indexColumn
Unique bool
Comment string
Partial bool
}
type indexColumn struct {
Name string
Desc null.Val[bool]
IsExpression bool
}
type constraint struct {
Name string
Columns []string
Comment string
}
type foreignKeys interface {
AsSlice() []foreignKey
}
type foreignKey struct {
constraint
ForeignTable string
ForeignColumns []string
}
type uniques interface {
AsSlice() []constraint
}
type checks interface {
AsSlice() []check
}
type check struct {
constraint
Expression string
}

View File

@@ -0,0 +1,169 @@
// Code generated by BobGen sqlite v0.41.1. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package dbinfo
import "github.com/aarondl/opt/null"
var Records = Table[
recordColumns,
recordIndexes,
recordForeignKeys,
recordUniques,
recordChecks,
]{
Schema: "",
Name: "records",
Columns: recordColumns{
Key: column{
Name: "key",
DBType: "TEXT",
Default: "",
Comment: "",
Nullable: false,
Generated: false,
AutoIncr: false,
},
Format: column{
Name: "format",
DBType: "SMALLINT",
Default: "NULL",
Comment: "",
Nullable: true,
Generated: false,
AutoIncr: false,
},
Value: column{
Name: "value",
DBType: "BLOB",
Default: "NULL",
Comment: "",
Nullable: true,
Generated: false,
AutoIncr: false,
},
Created: column{
Name: "created",
DBType: "BIGINT",
Default: "",
Comment: "",
Nullable: false,
Generated: false,
AutoIncr: false,
},
Modified: column{
Name: "modified",
DBType: "BIGINT",
Default: "",
Comment: "",
Nullable: false,
Generated: false,
AutoIncr: false,
},
Expires: column{
Name: "expires",
DBType: "BIGINT",
Default: "0",
Comment: "",
Nullable: false,
Generated: false,
AutoIncr: false,
},
Deleted: column{
Name: "deleted",
DBType: "BIGINT",
Default: "0",
Comment: "",
Nullable: false,
Generated: false,
AutoIncr: false,
},
Secret: column{
Name: "secret",
DBType: "BOOLEAN",
Default: "FALSE",
Comment: "",
Nullable: false,
Generated: false,
AutoIncr: false,
},
Crownjewel: column{
Name: "crownjewel",
DBType: "BOOLEAN",
Default: "FALSE",
Comment: "",
Nullable: false,
Generated: false,
AutoIncr: false,
},
},
Indexes: recordIndexes{
SqliteAutoindexRecords1: index{
Type: "pk",
Name: "sqlite_autoindex_records_1",
Columns: []indexColumn{
{
Name: "key",
Desc: null.FromCond(false, true),
IsExpression: false,
},
},
Unique: true,
Comment: "",
Partial: false,
},
},
PrimaryKey: &constraint{
Name: "pk_main_records",
Columns: []string{"key"},
Comment: "",
},
Comment: "",
}
type recordColumns struct {
Key column
Format column
Value column
Created column
Modified column
Expires column
Deleted column
Secret column
Crownjewel column
}
func (c recordColumns) AsSlice() []column {
return []column{
c.Key, c.Format, c.Value, c.Created, c.Modified, c.Expires, c.Deleted, c.Secret, c.Crownjewel,
}
}
type recordIndexes struct {
SqliteAutoindexRecords1 index
}
func (i recordIndexes) AsSlice() []index {
return []index{
i.SqliteAutoindexRecords1,
}
}
type recordForeignKeys struct{}
func (f recordForeignKeys) AsSlice() []foreignKey {
return []foreignKey{}
}
type recordUniques struct{}
func (u recordUniques) AsSlice() []constraint {
return []constraint{}
}
type recordChecks struct{}
func (c recordChecks) AsSlice() []check {
return []check{}
}

View File

@@ -0,0 +1,29 @@
// Code generated by BobGen sqlite v0.41.1. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package factory
import "context"
type contextKey string
// Relationship Contexts for records
var recordWithParentsCascadingCtx = newContextual[bool]("recordWithParentsCascading")
// Contextual is a convienience wrapper around context.WithValue and context.Value
type contextual[V any] struct {
key contextKey
}
func newContextual[V any](key string) contextual[V] {
return contextual[V]{key: contextKey(key)}
}
func (k contextual[V]) WithValue(ctx context.Context, val V) context.Context {
return context.WithValue(ctx, k.key, val)
}
func (k contextual[V]) Value(ctx context.Context) (V, bool) {
v, ok := ctx.Value(k.key).(V)
return v, ok
}

View File

@@ -0,0 +1,59 @@
// Code generated by BobGen sqlite v0.41.1. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package factory
import (
"context"
"github.com/aarondl/opt/null"
models "github.com/safing/portmaster/base/database/storage/sqlite/models"
)
type Factory struct {
baseRecordMods RecordModSlice
}
func New() *Factory {
return &Factory{}
}
func (f *Factory) NewRecord(mods ...RecordMod) *RecordTemplate {
return f.NewRecordWithContext(context.Background(), mods...)
}
func (f *Factory) NewRecordWithContext(ctx context.Context, mods ...RecordMod) *RecordTemplate {
o := &RecordTemplate{f: f}
if f != nil {
f.baseRecordMods.Apply(ctx, o)
}
RecordModSlice(mods).Apply(ctx, o)
return o
}
func (f *Factory) FromExistingRecord(m *models.Record) *RecordTemplate {
o := &RecordTemplate{f: f, alreadyPersisted: true}
o.Key = func() string { return m.Key }
o.Format = func() null.Val[int64] { return m.Format }
o.Value = func() null.Val[[]byte] { return m.Value }
o.Created = func() int64 { return m.Created }
o.Modified = func() int64 { return m.Modified }
o.Expires = func() int64 { return m.Expires }
o.Deleted = func() int64 { return m.Deleted }
o.Secret = func() bool { return m.Secret }
o.Crownjewel = func() bool { return m.Crownjewel }
return o
}
func (f *Factory) ClearBaseRecordMods() {
f.baseRecordMods = nil
}
func (f *Factory) AddBaseRecordMod(mods ...RecordMod) {
f.baseRecordMods = append(f.baseRecordMods, mods...)
}

View File

@@ -0,0 +1,33 @@
// Code generated by BobGen sqlite v0.41.1. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package factory
import (
"context"
"testing"
)
func TestCreateRecord(t *testing.T) {
if testDB == nil {
t.Skip("skipping test, no DSN provided")
}
ctx, cancel := context.WithCancel(t.Context())
t.Cleanup(cancel)
tx, err := testDB.Begin(ctx)
if err != nil {
t.Fatalf("Error starting transaction: %v", err)
}
defer func() {
if err := tx.Rollback(ctx); err != nil {
t.Fatalf("Error rolling back transaction: %v", err)
}
}()
if _, err := New().NewRecordWithContext(ctx).Create(ctx, tx); err != nil {
t.Fatalf("Error creating Record: %v", err)
}
}

View File

@@ -0,0 +1,53 @@
// Code generated by BobGen sqlite v0.41.1. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package factory
import (
"strconv"
"strings"
"github.com/jaswdr/faker/v2"
)
var defaultFaker = faker.New()
func random___byte(f *faker.Faker, limits ...string) []byte {
if f == nil {
f = &defaultFaker
}
return []byte(random_string(f, limits...))
}
func random_bool(f *faker.Faker, limits ...string) bool {
if f == nil {
f = &defaultFaker
}
return f.Bool()
}
func random_int64(f *faker.Faker, limits ...string) int64 {
if f == nil {
f = &defaultFaker
}
return f.Int64()
}
func random_string(f *faker.Faker, limits ...string) string {
if f == nil {
f = &defaultFaker
}
val := strings.Join(f.Lorem().Words(f.IntBetween(1, 5)), " ")
if len(limits) == 0 {
return val
}
limitInt, _ := strconv.Atoi(limits[0])
if limitInt > 0 && limitInt < len(val) {
val = val[:limitInt]
}
return val
}

View File

@@ -0,0 +1,47 @@
// Code generated by BobGen sqlite v0.41.1. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package factory
import (
"bytes"
"testing"
"github.com/stephenafamo/bob"
)
// Set the testDB to enable tests that use the database
var testDB bob.Transactor[bob.Tx]
func TestRandom___byte(t *testing.T) {
t.Parallel()
val1 := random___byte(nil)
val2 := random___byte(nil)
if bytes.Equal(val1, val2) {
t.Fatalf("random___byte() returned the same value twice: %v", val1)
}
}
func TestRandom_int64(t *testing.T) {
t.Parallel()
val1 := random_int64(nil)
val2 := random_int64(nil)
if val1 == val2 {
t.Fatalf("random_int64() returned the same value twice: %v", val1)
}
}
func TestRandom_string(t *testing.T) {
t.Parallel()
val1 := random_string(nil)
val2 := random_string(nil)
if val1 == val2 {
t.Fatalf("random_string() returned the same value twice: %v", val1)
}
}

View File

@@ -0,0 +1,629 @@
// Code generated by BobGen sqlite v0.41.1. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package factory
import (
"context"
"testing"
"github.com/aarondl/opt/null"
"github.com/aarondl/opt/omit"
"github.com/aarondl/opt/omitnull"
"github.com/jaswdr/faker/v2"
models "github.com/safing/portmaster/base/database/storage/sqlite/models"
"github.com/stephenafamo/bob"
)
type RecordMod interface {
Apply(context.Context, *RecordTemplate)
}
type RecordModFunc func(context.Context, *RecordTemplate)
func (f RecordModFunc) Apply(ctx context.Context, n *RecordTemplate) {
f(ctx, n)
}
type RecordModSlice []RecordMod
func (mods RecordModSlice) Apply(ctx context.Context, n *RecordTemplate) {
for _, f := range mods {
f.Apply(ctx, n)
}
}
// RecordTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type RecordTemplate struct {
Key func() string
Format func() null.Val[int64]
Value func() null.Val[[]byte]
Created func() int64
Modified func() int64
Expires func() int64
Deleted func() int64
Secret func() bool
Crownjewel func() bool
f *Factory
alreadyPersisted bool
}
// Apply mods to the RecordTemplate
func (o *RecordTemplate) Apply(ctx context.Context, mods ...RecordMod) {
for _, mod := range mods {
mod.Apply(ctx, o)
}
}
// setModelRels creates and sets the relationships on *models.Record
// according to the relationships in the template. Nothing is inserted into the db
func (t RecordTemplate) setModelRels(o *models.Record) {}
// BuildSetter returns an *models.RecordSetter
// this does nothing with the relationship templates
func (o RecordTemplate) BuildSetter() *models.RecordSetter {
m := &models.RecordSetter{}
if o.Key != nil {
val := o.Key()
m.Key = omit.From(val)
}
if o.Format != nil {
val := o.Format()
m.Format = omitnull.FromNull(val)
}
if o.Value != nil {
val := o.Value()
m.Value = omitnull.FromNull(val)
}
if o.Created != nil {
val := o.Created()
m.Created = omit.From(val)
}
if o.Modified != nil {
val := o.Modified()
m.Modified = omit.From(val)
}
if o.Expires != nil {
val := o.Expires()
m.Expires = omit.From(val)
}
if o.Deleted != nil {
val := o.Deleted()
m.Deleted = omit.From(val)
}
if o.Secret != nil {
val := o.Secret()
m.Secret = omit.From(val)
}
if o.Crownjewel != nil {
val := o.Crownjewel()
m.Crownjewel = omit.From(val)
}
return m
}
// BuildManySetter returns an []*models.RecordSetter
// this does nothing with the relationship templates
func (o RecordTemplate) BuildManySetter(number int) []*models.RecordSetter {
m := make([]*models.RecordSetter, number)
for i := range m {
m[i] = o.BuildSetter()
}
return m
}
// Build returns an *models.Record
// Related objects are also created and placed in the .R field
// NOTE: Objects are not inserted into the database. Use RecordTemplate.Create
func (o RecordTemplate) Build() *models.Record {
m := &models.Record{}
if o.Key != nil {
m.Key = o.Key()
}
if o.Format != nil {
m.Format = o.Format()
}
if o.Value != nil {
m.Value = o.Value()
}
if o.Created != nil {
m.Created = o.Created()
}
if o.Modified != nil {
m.Modified = o.Modified()
}
if o.Expires != nil {
m.Expires = o.Expires()
}
if o.Deleted != nil {
m.Deleted = o.Deleted()
}
if o.Secret != nil {
m.Secret = o.Secret()
}
if o.Crownjewel != nil {
m.Crownjewel = o.Crownjewel()
}
o.setModelRels(m)
return m
}
// BuildMany returns an models.RecordSlice
// Related objects are also created and placed in the .R field
// NOTE: Objects are not inserted into the database. Use RecordTemplate.CreateMany
func (o RecordTemplate) BuildMany(number int) models.RecordSlice {
m := make(models.RecordSlice, number)
for i := range m {
m[i] = o.Build()
}
return m
}
func ensureCreatableRecord(m *models.RecordSetter) {
if !(m.Key.IsValue()) {
val := random_string(nil)
m.Key = omit.From(val)
}
if !(m.Created.IsValue()) {
val := random_int64(nil)
m.Created = omit.From(val)
}
if !(m.Modified.IsValue()) {
val := random_int64(nil)
m.Modified = omit.From(val)
}
}
// insertOptRels creates and inserts any optional the relationships on *models.Record
// according to the relationships in the template.
// any required relationship should have already exist on the model
func (o *RecordTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.Record) error {
var err error
return err
}
// Create builds a record and inserts it into the database
// Relations objects are also inserted and placed in the .R field
func (o *RecordTemplate) Create(ctx context.Context, exec bob.Executor) (*models.Record, error) {
var err error
opt := o.BuildSetter()
ensureCreatableRecord(opt)
m, err := models.Records.Insert(opt).One(ctx, exec)
if err != nil {
return nil, err
}
if err := o.insertOptRels(ctx, exec, m); err != nil {
return nil, err
}
return m, err
}
// MustCreate builds a record and inserts it into the database
// Relations objects are also inserted and placed in the .R field
// panics if an error occurs
func (o *RecordTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.Record {
m, err := o.Create(ctx, exec)
if err != nil {
panic(err)
}
return m
}
// CreateOrFail builds a record and inserts it into the database
// Relations objects are also inserted and placed in the .R field
// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs
func (o *RecordTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.Record {
tb.Helper()
m, err := o.Create(ctx, exec)
if err != nil {
tb.Fatal(err)
return nil
}
return m
}
// CreateMany builds multiple records and inserts them into the database
// Relations objects are also inserted and placed in the .R field
func (o RecordTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.RecordSlice, error) {
var err error
m := make(models.RecordSlice, number)
for i := range m {
m[i], err = o.Create(ctx, exec)
if err != nil {
return nil, err
}
}
return m, nil
}
// MustCreateMany builds multiple records and inserts them into the database
// Relations objects are also inserted and placed in the .R field
// panics if an error occurs
func (o RecordTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.RecordSlice {
m, err := o.CreateMany(ctx, exec, number)
if err != nil {
panic(err)
}
return m
}
// CreateManyOrFail builds multiple records and inserts them into the database
// Relations objects are also inserted and placed in the .R field
// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs
func (o RecordTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.RecordSlice {
tb.Helper()
m, err := o.CreateMany(ctx, exec, number)
if err != nil {
tb.Fatal(err)
return nil
}
return m
}
// Record has methods that act as mods for the RecordTemplate
var RecordMods recordMods
type recordMods struct{}
func (m recordMods) RandomizeAllColumns(f *faker.Faker) RecordMod {
return RecordModSlice{
RecordMods.RandomKey(f),
RecordMods.RandomFormat(f),
RecordMods.RandomValue(f),
RecordMods.RandomCreated(f),
RecordMods.RandomModified(f),
RecordMods.RandomExpires(f),
RecordMods.RandomDeleted(f),
RecordMods.RandomSecret(f),
RecordMods.RandomCrownjewel(f),
}
}
// Set the model columns to this value
func (m recordMods) Key(val string) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Key = func() string { return val }
})
}
// Set the Column from the function
func (m recordMods) KeyFunc(f func() string) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Key = f
})
}
// Clear any values for the column
func (m recordMods) UnsetKey() RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Key = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m recordMods) RandomKey(f *faker.Faker) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Key = func() string {
return random_string(f)
}
})
}
// Set the model columns to this value
func (m recordMods) Format(val null.Val[int64]) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Format = func() null.Val[int64] { return val }
})
}
// Set the Column from the function
func (m recordMods) FormatFunc(f func() null.Val[int64]) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Format = f
})
}
// Clear any values for the column
func (m recordMods) UnsetFormat() RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Format = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
// The generated value is sometimes null
func (m recordMods) RandomFormat(f *faker.Faker) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Format = func() null.Val[int64] {
if f == nil {
f = &defaultFaker
}
val := random_int64(f)
return null.From(val)
}
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
// The generated value is never null
func (m recordMods) RandomFormatNotNull(f *faker.Faker) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Format = func() null.Val[int64] {
if f == nil {
f = &defaultFaker
}
val := random_int64(f)
return null.From(val)
}
})
}
// Set the model columns to this value
func (m recordMods) Value(val null.Val[[]byte]) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Value = func() null.Val[[]byte] { return val }
})
}
// Set the Column from the function
func (m recordMods) ValueFunc(f func() null.Val[[]byte]) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Value = f
})
}
// Clear any values for the column
func (m recordMods) UnsetValue() RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Value = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
// The generated value is sometimes null
func (m recordMods) RandomValue(f *faker.Faker) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Value = func() null.Val[[]byte] {
if f == nil {
f = &defaultFaker
}
val := random___byte(f)
return null.From(val)
}
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
// The generated value is never null
func (m recordMods) RandomValueNotNull(f *faker.Faker) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Value = func() null.Val[[]byte] {
if f == nil {
f = &defaultFaker
}
val := random___byte(f)
return null.From(val)
}
})
}
// Set the model columns to this value
func (m recordMods) Created(val int64) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Created = func() int64 { return val }
})
}
// Set the Column from the function
func (m recordMods) CreatedFunc(f func() int64) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Created = f
})
}
// Clear any values for the column
func (m recordMods) UnsetCreated() RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Created = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m recordMods) RandomCreated(f *faker.Faker) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Created = func() int64 {
return random_int64(f)
}
})
}
// Set the model columns to this value
func (m recordMods) Modified(val int64) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Modified = func() int64 { return val }
})
}
// Set the Column from the function
func (m recordMods) ModifiedFunc(f func() int64) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Modified = f
})
}
// Clear any values for the column
func (m recordMods) UnsetModified() RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Modified = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m recordMods) RandomModified(f *faker.Faker) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Modified = func() int64 {
return random_int64(f)
}
})
}
// Set the model columns to this value
func (m recordMods) Expires(val int64) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Expires = func() int64 { return val }
})
}
// Set the Column from the function
func (m recordMods) ExpiresFunc(f func() int64) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Expires = f
})
}
// Clear any values for the column
func (m recordMods) UnsetExpires() RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Expires = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m recordMods) RandomExpires(f *faker.Faker) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Expires = func() int64 {
return random_int64(f)
}
})
}
// Set the model columns to this value
func (m recordMods) Deleted(val int64) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Deleted = func() int64 { return val }
})
}
// Set the Column from the function
func (m recordMods) DeletedFunc(f func() int64) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Deleted = f
})
}
// Clear any values for the column
func (m recordMods) UnsetDeleted() RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Deleted = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m recordMods) RandomDeleted(f *faker.Faker) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Deleted = func() int64 {
return random_int64(f)
}
})
}
// Set the model columns to this value
func (m recordMods) Secret(val bool) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Secret = func() bool { return val }
})
}
// Set the Column from the function
func (m recordMods) SecretFunc(f func() bool) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Secret = f
})
}
// Clear any values for the column
func (m recordMods) UnsetSecret() RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Secret = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m recordMods) RandomSecret(f *faker.Faker) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Secret = func() bool {
return random_bool(f)
}
})
}
// Set the model columns to this value
func (m recordMods) Crownjewel(val bool) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Crownjewel = func() bool { return val }
})
}
// Set the Column from the function
func (m recordMods) CrownjewelFunc(f func() bool) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Crownjewel = f
})
}
// Clear any values for the column
func (m recordMods) UnsetCrownjewel() RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Crownjewel = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m recordMods) RandomCrownjewel(f *faker.Faker) RecordMod {
return RecordModFunc(func(_ context.Context, o *RecordTemplate) {
o.Crownjewel = func() bool {
return random_bool(f)
}
})
}
func (m recordMods) WithParentsCascading() RecordMod {
return RecordModFunc(func(ctx context.Context, o *RecordTemplate) {
if isDone, _ := recordWithParentsCascadingCtx.Value(ctx); isDone {
return
}
ctx = recordWithParentsCascadingCtx.WithValue(ctx, true)
})
}

View File

@@ -0,0 +1,69 @@
// Code generated by BobGen sqlite v0.41.1. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package models
import (
"hash/maphash"
"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/clause"
"github.com/stephenafamo/bob/dialect/sqlite/dialect"
)
var (
SelectJoins = getJoins[*dialect.SelectQuery]
UpdateJoins = getJoins[*dialect.UpdateQuery]
)
type joinSet[Q interface{ aliasedAs(string) Q }] struct {
InnerJoin Q
LeftJoin Q
RightJoin Q
}
func (j joinSet[Q]) AliasedAs(alias string) joinSet[Q] {
return joinSet[Q]{
InnerJoin: j.InnerJoin.aliasedAs(alias),
LeftJoin: j.LeftJoin.aliasedAs(alias),
RightJoin: j.RightJoin.aliasedAs(alias),
}
}
type joins[Q dialect.Joinable] struct{}
func buildJoinSet[Q interface{ aliasedAs(string) Q }, C any, F func(C, string) Q](c C, f F) joinSet[Q] {
return joinSet[Q]{
InnerJoin: f(c, clause.InnerJoin),
LeftJoin: f(c, clause.LeftJoin),
RightJoin: f(c, clause.RightJoin),
}
}
func getJoins[Q dialect.Joinable]() joins[Q] {
return joins[Q]{}
}
type modAs[Q any, C interface{ AliasedAs(string) C }] struct {
c C
f func(C) bob.Mod[Q]
}
func (m modAs[Q, C]) Apply(q Q) {
m.f(m.c).Apply(q)
}
func (m modAs[Q, C]) AliasedAs(alias string) bob.Mod[Q] {
m.c = m.c.AliasedAs(alias)
return m
}
func randInt() int64 {
out := int64(new(maphash.Hash).Sum64())
if out < 0 {
return -out % 10000
}
return out % 10000
}

View File

@@ -0,0 +1,55 @@
// Code generated by BobGen sqlite v0.41.1. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package models
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/dialect/sqlite/dialect"
"github.com/stephenafamo/bob/orm"
)
var Preload = getPreloaders()
type preloaders struct{}
func getPreloaders() preloaders {
return preloaders{}
}
var (
SelectThenLoad = getThenLoaders[*dialect.SelectQuery]()
InsertThenLoad = getThenLoaders[*dialect.InsertQuery]()
UpdateThenLoad = getThenLoaders[*dialect.UpdateQuery]()
)
type thenLoaders[Q orm.Loadable] struct{}
func getThenLoaders[Q orm.Loadable]() thenLoaders[Q] {
return thenLoaders[Q]{}
}
func thenLoadBuilder[Q orm.Loadable, T any](name string, f func(context.Context, bob.Executor, T, ...bob.Mod[*dialect.SelectQuery]) error) func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] {
return func(queryMods ...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] {
return func(ctx context.Context, exec bob.Executor, retrieved any) error {
loader, isLoader := retrieved.(T)
if !isLoader {
return fmt.Errorf("object %T cannot load %q", retrieved, name)
}
err := f(ctx, exec, loader, queryMods...)
// Don't cause an issue due to missing relationships
if errors.Is(err, sql.ErrNoRows) {
return nil
}
return err
}
}
}

View File

@@ -1,131 +0,0 @@
// Code generated by BobGen sqlite v0.30.0. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package models
import (
"hash/maphash"
"strings"
"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/clause"
"github.com/stephenafamo/bob/dialect/sqlite"
"github.com/stephenafamo/bob/dialect/sqlite/dialect"
sqliteDriver "modernc.org/sqlite"
)
var TableNames = struct {
Records string
}{
Records: "records",
}
var ColumnNames = struct {
Records recordColumnNames
}{
Records: recordColumnNames{
Key: "key",
Format: "format",
Value: "value",
Created: "created",
Modified: "modified",
Expires: "expires",
Deleted: "deleted",
Secret: "secret",
Crownjewel: "crownjewel",
},
}
var (
SelectWhere = Where[*dialect.SelectQuery]()
InsertWhere = Where[*dialect.InsertQuery]()
UpdateWhere = Where[*dialect.UpdateQuery]()
DeleteWhere = Where[*dialect.DeleteQuery]()
)
func Where[Q sqlite.Filterable]() struct {
Records recordWhere[Q]
} {
return struct {
Records recordWhere[Q]
}{
Records: buildRecordWhere[Q](RecordColumns),
}
}
var (
SelectJoins = getJoins[*dialect.SelectQuery]
UpdateJoins = getJoins[*dialect.UpdateQuery]
)
type joinSet[Q interface{ aliasedAs(string) Q }] struct {
InnerJoin Q
LeftJoin Q
RightJoin Q
}
func (j joinSet[Q]) AliasedAs(alias string) joinSet[Q] {
return joinSet[Q]{
InnerJoin: j.InnerJoin.aliasedAs(alias),
LeftJoin: j.LeftJoin.aliasedAs(alias),
RightJoin: j.RightJoin.aliasedAs(alias),
}
}
type joins[Q dialect.Joinable] struct{}
func buildJoinSet[Q interface{ aliasedAs(string) Q }, C any, F func(C, string) Q](c C, f F) joinSet[Q] {
return joinSet[Q]{
InnerJoin: f(c, clause.InnerJoin),
LeftJoin: f(c, clause.LeftJoin),
RightJoin: f(c, clause.RightJoin),
}
}
func getJoins[Q dialect.Joinable]() joins[Q] {
return joins[Q]{}
}
type modAs[Q any, C interface{ AliasedAs(string) C }] struct {
c C
f func(C) bob.Mod[Q]
}
func (m modAs[Q, C]) Apply(q Q) {
m.f(m.c).Apply(q)
}
func (m modAs[Q, C]) AliasedAs(alias string) bob.Mod[Q] {
m.c = m.c.AliasedAs(alias)
return m
}
func randInt() int64 {
out := int64(new(maphash.Hash).Sum64())
if out < 0 {
return -out % 10000
}
return out % 10000
}
// ErrUniqueConstraint captures all unique constraint errors by explicitly leaving `s` empty.
var ErrUniqueConstraint = &UniqueConstraintError{s: ""}
type UniqueConstraintError struct {
// s is a string uniquely identifying the constraint in the raw error message returned from the database.
s string
}
func (e *UniqueConstraintError) Error() string {
return e.s
}
func (e *UniqueConstraintError) Is(target error) bool {
err, ok := target.(*sqliteDriver.Error)
if !ok {
return false
}
return err.Code() == 2067 && strings.Contains(err.Error(), e.s)
}

View File

@@ -1,9 +1,12 @@
// Code generated by BobGen sqlite v0.30.0. DO NOT EDIT.
// Code generated by BobGen sqlite v0.41.1. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package models
import "github.com/stephenafamo/bob"
// Set the testDB to enable tests that use the database
var testDB bob.Transactor[bob.Tx]
// Make sure the type Record runs hooks after queries
var _ bob.HookableType = &Record{}

View File

@@ -0,0 +1,27 @@
// Code generated by BobGen sqlite v0.41.1. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package models
import (
"github.com/stephenafamo/bob/clause"
"github.com/stephenafamo/bob/dialect/sqlite"
"github.com/stephenafamo/bob/dialect/sqlite/dialect"
)
var (
SelectWhere = Where[*dialect.SelectQuery]()
UpdateWhere = Where[*dialect.UpdateQuery]()
DeleteWhere = Where[*dialect.DeleteQuery]()
OnConflictWhere = Where[*clause.ConflictClause]() // Used in ON CONFLICT DO UPDATE
)
func Where[Q sqlite.Filterable]() struct {
Records recordWhere[Q]
} {
return struct {
Records recordWhere[Q]
}{
Records: buildRecordWhere[Q](Records.Columns),
}
}

View File

@@ -1,4 +1,4 @@
// Code generated by BobGen sqlite v0.30.0. DO NOT EDIT.
// Code generated by BobGen sqlite v0.41.1. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package models
@@ -22,7 +22,7 @@ import (
// Record is an object representing the database table.
type Record struct {
Key string `db:"key,pk" `
Format null.Val[int16] `db:"format" `
Format null.Val[int64] `db:"format" `
Value null.Val[[]byte] `db:"value" `
Created int64 `db:"created" `
Modified int64 `db:"modified" `
@@ -37,26 +37,31 @@ type Record struct {
type RecordSlice []*Record
// Records contains methods to work with the records table
var Records = sqlite.NewTablex[*Record, RecordSlice, *RecordSetter]("", "records")
var Records = sqlite.NewTablex[*Record, RecordSlice, *RecordSetter]("", "records", buildRecordColumns("records"))
// RecordsQuery is a query on the records table
type RecordsQuery = *sqlite.ViewQuery[*Record, RecordSlice]
type recordColumnNames struct {
Key string
Format string
Value string
Created string
Modified string
Expires string
Deleted string
Secret string
Crownjewel string
func buildRecordColumns(alias string) recordColumns {
return recordColumns{
ColumnsExpr: expr.NewColumnsExpr(
"key", "format", "value", "created", "modified", "expires", "deleted", "secret", "crownjewel",
).WithParent("records"),
tableAlias: alias,
Key: sqlite.Quote(alias, "key"),
Format: sqlite.Quote(alias, "format"),
Value: sqlite.Quote(alias, "value"),
Created: sqlite.Quote(alias, "created"),
Modified: sqlite.Quote(alias, "modified"),
Expires: sqlite.Quote(alias, "expires"),
Deleted: sqlite.Quote(alias, "deleted"),
Secret: sqlite.Quote(alias, "secret"),
Crownjewel: sqlite.Quote(alias, "crownjewel"),
}
}
var RecordColumns = buildRecordColumns("records")
type recordColumns struct {
expr.ColumnsExpr
tableAlias string
Key sqlite.Expression
Format sqlite.Expression
@@ -77,57 +82,12 @@ func (recordColumns) AliasedAs(alias string) recordColumns {
return buildRecordColumns(alias)
}
func buildRecordColumns(alias string) recordColumns {
return recordColumns{
tableAlias: alias,
Key: sqlite.Quote(alias, "key"),
Format: sqlite.Quote(alias, "format"),
Value: sqlite.Quote(alias, "value"),
Created: sqlite.Quote(alias, "created"),
Modified: sqlite.Quote(alias, "modified"),
Expires: sqlite.Quote(alias, "expires"),
Deleted: sqlite.Quote(alias, "deleted"),
Secret: sqlite.Quote(alias, "secret"),
Crownjewel: sqlite.Quote(alias, "crownjewel"),
}
}
type recordWhere[Q sqlite.Filterable] struct {
Key sqlite.WhereMod[Q, string]
Format sqlite.WhereNullMod[Q, int16]
Value sqlite.WhereNullMod[Q, []byte]
Created sqlite.WhereMod[Q, int64]
Modified sqlite.WhereMod[Q, int64]
Expires sqlite.WhereMod[Q, int64]
Deleted sqlite.WhereMod[Q, int64]
Secret sqlite.WhereMod[Q, bool]
Crownjewel sqlite.WhereMod[Q, bool]
}
func (recordWhere[Q]) AliasedAs(alias string) recordWhere[Q] {
return buildRecordWhere[Q](buildRecordColumns(alias))
}
func buildRecordWhere[Q sqlite.Filterable](cols recordColumns) recordWhere[Q] {
return recordWhere[Q]{
Key: sqlite.Where[Q, string](cols.Key),
Format: sqlite.WhereNull[Q, int16](cols.Format),
Value: sqlite.WhereNull[Q, []byte](cols.Value),
Created: sqlite.Where[Q, int64](cols.Created),
Modified: sqlite.Where[Q, int64](cols.Modified),
Expires: sqlite.Where[Q, int64](cols.Expires),
Deleted: sqlite.Where[Q, int64](cols.Deleted),
Secret: sqlite.Where[Q, bool](cols.Secret),
Crownjewel: sqlite.Where[Q, bool](cols.Crownjewel),
}
}
// RecordSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type RecordSetter struct {
Key omit.Val[string] `db:"key,pk" `
Format omitnull.Val[int16] `db:"format" `
Format omitnull.Val[int64] `db:"format" `
Value omitnull.Val[[]byte] `db:"value" `
Created omit.Val[int64] `db:"created" `
Modified omit.Val[int64] `db:"modified" `
@@ -139,72 +99,63 @@ type RecordSetter struct {
func (s RecordSetter) SetColumns() []string {
vals := make([]string, 0, 9)
if !s.Key.IsUnset() {
if s.Key.IsValue() {
vals = append(vals, "key")
}
if !s.Format.IsUnset() {
vals = append(vals, "format")
}
if !s.Value.IsUnset() {
vals = append(vals, "value")
}
if !s.Created.IsUnset() {
if s.Created.IsValue() {
vals = append(vals, "created")
}
if !s.Modified.IsUnset() {
if s.Modified.IsValue() {
vals = append(vals, "modified")
}
if !s.Expires.IsUnset() {
if s.Expires.IsValue() {
vals = append(vals, "expires")
}
if !s.Deleted.IsUnset() {
if s.Deleted.IsValue() {
vals = append(vals, "deleted")
}
if !s.Secret.IsUnset() {
if s.Secret.IsValue() {
vals = append(vals, "secret")
}
if !s.Crownjewel.IsUnset() {
if s.Crownjewel.IsValue() {
vals = append(vals, "crownjewel")
}
return vals
}
func (s RecordSetter) Overwrite(t *Record) {
if !s.Key.IsUnset() {
t.Key, _ = s.Key.Get()
if s.Key.IsValue() {
t.Key = s.Key.MustGet()
}
if !s.Format.IsUnset() {
t.Format, _ = s.Format.GetNull()
t.Format = s.Format.MustGetNull()
}
if !s.Value.IsUnset() {
t.Value, _ = s.Value.GetNull()
t.Value = s.Value.MustGetNull()
}
if !s.Created.IsUnset() {
t.Created, _ = s.Created.Get()
if s.Created.IsValue() {
t.Created = s.Created.MustGet()
}
if !s.Modified.IsUnset() {
t.Modified, _ = s.Modified.Get()
if s.Modified.IsValue() {
t.Modified = s.Modified.MustGet()
}
if !s.Expires.IsUnset() {
t.Expires, _ = s.Expires.Get()
if s.Expires.IsValue() {
t.Expires = s.Expires.MustGet()
}
if !s.Deleted.IsUnset() {
t.Deleted, _ = s.Deleted.Get()
if s.Deleted.IsValue() {
t.Deleted = s.Deleted.MustGet()
}
if !s.Secret.IsUnset() {
t.Secret, _ = s.Secret.Get()
if s.Secret.IsValue() {
t.Secret = s.Secret.MustGet()
}
if !s.Crownjewel.IsUnset() {
t.Crownjewel, _ = s.Crownjewel.Get()
if s.Crownjewel.IsValue() {
t.Crownjewel = s.Crownjewel.MustGet()
}
}
@@ -213,46 +164,54 @@ func (s *RecordSetter) Apply(q *dialect.InsertQuery) {
return Records.BeforeInsertHooks.RunHooks(ctx, exec, s)
})
if len(q.Table.Columns) == 0 {
q.Table.Columns = s.SetColumns()
if len(q.TableRef.Columns) == 0 {
q.TableRef.Columns = s.SetColumns()
if len(q.TableRef.Columns) == 0 {
q.TableRef.Columns = []string{"key"}
}
}
q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
vals := make([]bob.Expression, 0, 9)
if !s.Key.IsUnset() {
vals = append(vals, sqlite.Arg(s.Key))
if s.Key.IsValue() {
vals = append(vals, sqlite.Arg(s.Key.MustGet()))
}
if !s.Format.IsUnset() {
vals = append(vals, sqlite.Arg(s.Format))
vals = append(vals, sqlite.Arg(s.Format.MustGetNull()))
}
if !s.Value.IsUnset() {
vals = append(vals, sqlite.Arg(s.Value))
vals = append(vals, sqlite.Arg(s.Value.MustGetNull()))
}
if !s.Created.IsUnset() {
vals = append(vals, sqlite.Arg(s.Created))
if s.Created.IsValue() {
vals = append(vals, sqlite.Arg(s.Created.MustGet()))
}
if !s.Modified.IsUnset() {
vals = append(vals, sqlite.Arg(s.Modified))
if s.Modified.IsValue() {
vals = append(vals, sqlite.Arg(s.Modified.MustGet()))
}
if !s.Expires.IsUnset() {
vals = append(vals, sqlite.Arg(s.Expires))
if s.Expires.IsValue() {
vals = append(vals, sqlite.Arg(s.Expires.MustGet()))
}
if !s.Deleted.IsUnset() {
vals = append(vals, sqlite.Arg(s.Deleted))
if s.Deleted.IsValue() {
vals = append(vals, sqlite.Arg(s.Deleted.MustGet()))
}
if !s.Secret.IsUnset() {
vals = append(vals, sqlite.Arg(s.Secret))
if s.Secret.IsValue() {
vals = append(vals, sqlite.Arg(s.Secret.MustGet()))
}
if !s.Crownjewel.IsUnset() {
vals = append(vals, sqlite.Arg(s.Crownjewel))
if s.Crownjewel.IsValue() {
vals = append(vals, sqlite.Arg(s.Crownjewel.MustGet()))
}
if len(vals) == 0 {
vals = append(vals, sqlite.Arg(nil))
}
return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
@@ -266,7 +225,7 @@ func (s RecordSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
func (s RecordSetter) Expressions(prefix ...string) []bob.Expression {
exprs := make([]bob.Expression, 0, 9)
if !s.Key.IsUnset() {
if s.Key.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
sqlite.Quote(append(prefix, "key")...),
sqlite.Arg(s.Key),
@@ -287,42 +246,42 @@ func (s RecordSetter) Expressions(prefix ...string) []bob.Expression {
}})
}
if !s.Created.IsUnset() {
if s.Created.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
sqlite.Quote(append(prefix, "created")...),
sqlite.Arg(s.Created),
}})
}
if !s.Modified.IsUnset() {
if s.Modified.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
sqlite.Quote(append(prefix, "modified")...),
sqlite.Arg(s.Modified),
}})
}
if !s.Expires.IsUnset() {
if s.Expires.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
sqlite.Quote(append(prefix, "expires")...),
sqlite.Arg(s.Expires),
}})
}
if !s.Deleted.IsUnset() {
if s.Deleted.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
sqlite.Quote(append(prefix, "deleted")...),
sqlite.Arg(s.Deleted),
}})
}
if !s.Secret.IsUnset() {
if s.Secret.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
sqlite.Quote(append(prefix, "secret")...),
sqlite.Arg(s.Secret),
}})
}
if !s.Crownjewel.IsUnset() {
if s.Crownjewel.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
sqlite.Quote(append(prefix, "crownjewel")...),
sqlite.Arg(s.Crownjewel),
@@ -337,20 +296,20 @@ func (s RecordSetter) Expressions(prefix ...string) []bob.Expression {
func FindRecord(ctx context.Context, exec bob.Executor, KeyPK string, cols ...string) (*Record, error) {
if len(cols) == 0 {
return Records.Query(
SelectWhere.Records.Key.EQ(KeyPK),
sm.Where(Records.Columns.Key.EQ(sqlite.Arg(KeyPK))),
).One(ctx, exec)
}
return Records.Query(
SelectWhere.Records.Key.EQ(KeyPK),
sm.Columns(Records.Columns().Only(cols...)),
sm.Where(Records.Columns.Key.EQ(sqlite.Arg(KeyPK))),
sm.Columns(Records.Columns.Only(cols...)),
).One(ctx, exec)
}
// RecordExists checks the presence of a single record by primary key
func RecordExists(ctx context.Context, exec bob.Executor, KeyPK string) (bool, error) {
return Records.Query(
SelectWhere.Records.Key.EQ(KeyPK),
sm.Where(Records.Columns.Key.EQ(sqlite.Arg(KeyPK))),
).Exists(ctx, exec)
}
@@ -372,14 +331,14 @@ func (o *Record) AfterQueryHook(ctx context.Context, exec bob.Executor, queryTyp
return err
}
// PrimaryKeyVals returns the primary key values of the Record
func (o *Record) PrimaryKeyVals() bob.Expression {
// primaryKeyVals returns the primary key values of the Record
func (o *Record) primaryKeyVals() bob.Expression {
return sqlite.Arg(o.Key)
}
func (o *Record) pkEQ() dialect.Expression {
return sqlite.Quote("records", "key").EQ(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
return o.PrimaryKeyVals().WriteSQL(ctx, w, d, start)
return o.primaryKeyVals().WriteSQL(ctx, w, d, start)
}))
}
@@ -404,7 +363,7 @@ func (o *Record) Delete(ctx context.Context, exec bob.Executor) error {
// Reload refreshes the Record using the executor
func (o *Record) Reload(ctx context.Context, exec bob.Executor) error {
o2, err := Records.Query(
SelectWhere.Records.Key.EQ(o.Key),
sm.Where(Records.Columns.Key.EQ(sqlite.Arg(o.Key))),
).One(ctx, exec)
if err != nil {
return err
@@ -441,7 +400,7 @@ func (o RecordSlice) pkIN() dialect.Expression {
return sqlite.Quote("records", "key").In(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
pkPairs := make([]bob.Expression, len(o))
for i, row := range o {
pkPairs[i] = row.PrimaryKeyVals()
pkPairs[i] = row.primaryKeyVals()
}
return bob.ExpressSlice(ctx, w, d, start, pkPairs, "", ", ", "")
}))
@@ -553,3 +512,33 @@ func (o RecordSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
return nil
}
type recordWhere[Q sqlite.Filterable] struct {
Key sqlite.WhereMod[Q, string]
Format sqlite.WhereNullMod[Q, int64]
Value sqlite.WhereNullMod[Q, []byte]
Created sqlite.WhereMod[Q, int64]
Modified sqlite.WhereMod[Q, int64]
Expires sqlite.WhereMod[Q, int64]
Deleted sqlite.WhereMod[Q, int64]
Secret sqlite.WhereMod[Q, bool]
Crownjewel sqlite.WhereMod[Q, bool]
}
func (recordWhere[Q]) AliasedAs(alias string) recordWhere[Q] {
return buildRecordWhere[Q](buildRecordColumns(alias))
}
func buildRecordWhere[Q sqlite.Filterable](cols recordColumns) recordWhere[Q] {
return recordWhere[Q]{
Key: sqlite.Where[Q, string](cols.Key),
Format: sqlite.WhereNull[Q, int64](cols.Format),
Value: sqlite.WhereNull[Q, []byte](cols.Value),
Created: sqlite.Where[Q, int64](cols.Created),
Modified: sqlite.Where[Q, int64](cols.Modified),
Expires: sqlite.Where[Q, int64](cols.Expires),
Deleted: sqlite.Where[Q, int64](cols.Deleted),
Secret: sqlite.Where[Q, bool](cols.Secret),
Crownjewel: sqlite.Where[Q, bool](cols.Crownjewel),
}
}

View File

@@ -65,7 +65,7 @@ func (db *SQLite) putManyWithPreparedStmts(shadowDelete bool) (chan<- record.Rec
}
} else {
// Finalize transcation.
errs <- tx.Commit()
errs <- tx.Commit(db.ctx)
return
}
@@ -75,7 +75,7 @@ func (db *SQLite) putManyWithPreparedStmts(shadowDelete bool) (chan<- record.Rec
}
// Rollback transaction.
errs <- tx.Rollback()
errs <- tx.Rollback(db.ctx)
}()
return batch, errs

View File

@@ -182,7 +182,7 @@ func (db *SQLite) putRecord(r record.Record, tx *bob.Tx) (record.Record, error)
return nil, err
}
// Prepare for setter.
setFormat := omitnull.From(int16(format))
setFormat := omitnull.From(int64(format))
setData := omitnull.From(data)
if len(data) == 0 {
setFormat.Null()
@@ -259,7 +259,7 @@ func (db *SQLite) PutMany(shadowDelete bool) (chan<- record.Record, <-chan error
}
} else {
// Finalize transcation.
errs <- tx.Commit()
errs <- tx.Commit(db.ctx)
return
}
@@ -269,7 +269,7 @@ func (db *SQLite) PutMany(shadowDelete bool) (chan<- record.Record, <-chan error
}
// Rollback transaction.
errs <- tx.Rollback()
errs <- tx.Rollback(db.ctx)
}()
return batch, errs