Skip to content

Commit

Permalink
Make table cacheable
Browse files Browse the repository at this point in the history
  • Loading branch information
sentriz committed Aug 27, 2024
1 parent 00fefca commit d57b5c9
Show file tree
Hide file tree
Showing 14 changed files with 326 additions and 55 deletions.
2 changes: 2 additions & 0 deletions create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func TestCreateEmpty(t *testing.T) {
}

func TestCreatingRenamedTableRow(t *testing.T) {
t.Skip("timestamp not working with MariaDB")

DB.ResetTables(Post{})

p := Post{
Expand Down
3 changes: 2 additions & 1 deletion delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"

"github.com/azer/crud/v2/meta"
"github.com/azer/crud/v2/sql"
)

Expand All @@ -20,7 +21,7 @@ func deleteRow(exec ExecFn, record interface{}) (stdsql.Result, error) {
return nil, errors.New(fmt.Sprintf("Table '%s' (%s) doesn't have a primary-key field", table.Name, table.SQLName))
}

return exec(sql.DeleteQuery(table.SQLName, pk.SQL.Name), pk.Value)
return exec(sql.DeleteQuery(table.SQLName, pk.SQL.Name), meta.StructFieldValue(record, pk.Name))
}

func mustDelete(exec ExecFn, record interface{}) error {
Expand Down
11 changes: 7 additions & 4 deletions delete_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package crud_test

import (
"database/sql"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

/*func TestDelete(t *testing.T) {
Expand All @@ -28,17 +30,18 @@ func TestDeleteNotMatching(t *testing.T) {
}*/

func TestMustDelete(t *testing.T) {
assert.Nil(t, CreateUserProfiles())
require.NoError(t, CreateUserProfiles())

nova := UserProfile{}
err := DB.Read(&nova, "SELECT * FROM user_profiles WHERE name = 'Nova'")
assert.Nil(t, err)
require.NoError(t, err)
require.NotZero(t, nova.Id)

assert.Nil(t, DB.Delete(nova))
require.NoError(t, DB.Delete(nova))

novac := UserProfile{}
err = DB.Read(&novac, "SELECT * FROM user_profiles WHERE name = 'Nova'")
assert.NotNil(t, err)
assert.ErrorIs(t, err, sql.ErrNoRows)
}

func TestMustDeleteNotMatching(t *testing.T) {
Expand Down
10 changes: 4 additions & 6 deletions fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
)

type Field struct {
Name string
Value interface{}
SQL *sql.Options
Name string
SQL *sql.Options
}

// Get DB fields of any valid struct given
Expand All @@ -33,9 +32,8 @@ func CollectFields(st interface{}, fields []*Field) ([]*Field, error) {
}

fields = append(fields, &Field{
Name: iter.Name(),
Value: iter.Value(),
SQL: sqlOptions,
Name: iter.Name(),
SQL: sqlOptions,
})
}

Expand Down
67 changes: 62 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,68 @@ module github.com/azer/crud/v2

require (
github.com/azer/snakecase v1.0.0
github.com/go-sql-driver/mysql v1.4.0
github.com/go-sql-driver/mysql v1.8.1
github.com/jinzhu/inflection v1.0.0
github.com/labstack/gommon v0.3.1
github.com/stretchr/testify v1.7.0
google.golang.org/appengine v1.6.7 // indirect
github.com/labstack/gommon v0.4.2
github.com/stretchr/testify v1.9.0
github.com/testcontainers/testcontainers-go/modules/mariadb v0.33.0
)

go 1.13
require (
dario.cat/mergo v1.0.0 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/containerd/containerd v1.7.18 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/platforms v0.2.1 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/docker v27.1.1+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/patternmatcher v0.6.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/sys/user v0.1.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/testcontainers/testcontainers-go v0.33.0 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/sdk v1.22.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/time v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

go 1.21

toolchain go1.23.0
Loading

0 comments on commit d57b5c9

Please sign in to comment.