Compare commits
5 Commits
entgo/v1.1
...
entgo/v1.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f73f016ec5 | ||
|
|
d549d305ae | ||
|
|
23dcad60a3 | ||
|
|
fa7ae4f876 | ||
|
|
9f6a4eba80 |
@@ -565,12 +565,12 @@ func filterDatePartField(s *sql.Selector, datePart, field string) string {
|
||||
}
|
||||
|
||||
// filterJsonb 提取JSONB字段
|
||||
// Postgresql: WHERE ("app_profile"."preferences" -> daily_email) = 'true'
|
||||
// Postgresql: WHERE ("app_profile"."preferences" ->> 'daily_email') = 'true'
|
||||
func filterJsonb(s *sql.Selector, p *sql.Predicate, jsonbField, field string) *sql.Predicate {
|
||||
p.Append(func(b *sql.Builder) {
|
||||
switch s.Builder.Dialect() {
|
||||
case dialect.Postgres:
|
||||
b.Ident(s.C(field)).WriteString(" -> ").WriteString(jsonbField)
|
||||
b.Ident(s.C(field)).WriteString(" ->> ").WriteString("'" + jsonbField + "'")
|
||||
//b.Arg(strings.ToLower(value))
|
||||
break
|
||||
|
||||
@@ -588,7 +588,7 @@ func filterJsonbField(s *sql.Selector, jsonbField, field string) string {
|
||||
p := sql.P()
|
||||
switch s.Builder.Dialect() {
|
||||
case dialect.Postgres:
|
||||
p.Ident(s.C(field)).WriteString(" -> ").WriteString(jsonbField)
|
||||
p.Ident(s.C(field)).WriteString(" ->> ").WriteString("'" + jsonbField + "'")
|
||||
//b.Arg(strings.ToLower(value))
|
||||
break
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"entgo.io/ent/dialect"
|
||||
"entgo.io/ent/dialect/sql"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
@@ -681,7 +682,7 @@ func TestFilter(t *testing.T) {
|
||||
s.Where(p)
|
||||
|
||||
query, args := s.Query()
|
||||
require.Equal(t, "SELECT * FROM \"app_profile\" WHERE \"app_profile\".\"preferences\" -> daily_email = $1", query)
|
||||
require.Equal(t, "SELECT * FROM \"app_profile\" WHERE \"app_profile\".\"preferences\" ->> 'daily_email' = $1", query)
|
||||
require.NotEmpty(t, args)
|
||||
require.Equal(t, args[0], "true")
|
||||
})
|
||||
@@ -691,4 +692,5 @@ func TestFilterJsonbField(t *testing.T) {
|
||||
s := sql.Dialect(dialect.Postgres).Select("*").From(sql.Table("app_profile"))
|
||||
str := filterJsonbField(s, "daily_email", "preferences")
|
||||
fmt.Println(str)
|
||||
assert.Equal(t, str, "\"app_profile\".\"preferences\" ->> 'daily_email'")
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ func BuildSetNullUpdater(fields []string) func(u *sql.UpdateBuilder) {
|
||||
}
|
||||
|
||||
// ExtractJsonFieldKeyValues 提取json字段的键值对
|
||||
func ExtractJsonFieldKeyValues(msg proto.Message, paths []string) []string {
|
||||
func ExtractJsonFieldKeyValues(msg proto.Message, paths []string, needToSnakeCase bool) []string {
|
||||
var keyValues []string
|
||||
rft := msg.ProtoReflect()
|
||||
for _, path := range paths {
|
||||
@@ -46,7 +46,14 @@ func ExtractJsonFieldKeyValues(msg proto.Message, paths []string) []string {
|
||||
continue
|
||||
}
|
||||
|
||||
keyValues = append(keyValues, fmt.Sprintf("'%s'", stringcase.ToSnakeCase(path)))
|
||||
var k string
|
||||
if needToSnakeCase {
|
||||
k = stringcase.ToSnakeCase(path)
|
||||
} else {
|
||||
k = path
|
||||
}
|
||||
|
||||
keyValues = append(keyValues, fmt.Sprintf("'%s'", k))
|
||||
|
||||
v := rft.Get(fd)
|
||||
switch v.Interface().(type) {
|
||||
@@ -77,8 +84,8 @@ func SetJsonNullFieldUpdateBuilder(fieldName string, msg proto.Message, paths []
|
||||
}
|
||||
|
||||
// SetJsonFieldValueUpdateBuilder 设置json字段的值
|
||||
func SetJsonFieldValueUpdateBuilder(fieldName string, msg proto.Message, paths []string) func(u *sql.UpdateBuilder) {
|
||||
keyValues := ExtractJsonFieldKeyValues(msg, paths)
|
||||
func SetJsonFieldValueUpdateBuilder(fieldName string, msg proto.Message, paths []string, needToSnakeCase bool) func(u *sql.UpdateBuilder) {
|
||||
keyValues := ExtractJsonFieldKeyValues(msg, paths, needToSnakeCase)
|
||||
if len(keyValues) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
2
tag.bat
2
tag.bat
@@ -3,7 +3,7 @@ git tag v1.1.13
|
||||
git tag bank_card/v1.1.3
|
||||
git tag geoip/v1.1.3
|
||||
|
||||
git tag entgo/v1.1.17
|
||||
git tag entgo/v1.1.20
|
||||
git tag gorm/v1.1.3
|
||||
|
||||
git push origin --tags
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package util
|
||||
package timeutil
|
||||
|
||||
import "time"
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package util
|
||||
package timeutil
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package util
|
||||
package timeutil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package util
|
||||
package timeutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package util
|
||||
package timeutil
|
||||
|
||||
import "time"
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package util
|
||||
package timeutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package util
|
||||
package timeutil
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"google.golang.org/protobuf/types/known/durationpb"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
"github.com/tx7do/go-utils/trans"
|
||||
@@ -132,3 +133,19 @@ func TimeToTimestamppb(tm *time.Time) *timestamppb.Timestamp {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func FloatToDurationpb(duration *float64, timePrecision time.Duration) *durationpb.Duration {
|
||||
if duration == nil {
|
||||
return nil
|
||||
}
|
||||
return durationpb.New(time.Duration(*duration) * timePrecision)
|
||||
}
|
||||
|
||||
func DurationpbToFloat(duration *durationpb.Duration, timePrecision time.Duration) *float64 {
|
||||
if duration == nil {
|
||||
return nil
|
||||
}
|
||||
seconds := duration.AsDuration().Seconds()
|
||||
secondsWithPrecision := seconds / timePrecision.Seconds()
|
||||
return &secondsWithPrecision
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package util
|
||||
package timeutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
Reference in New Issue
Block a user