Skip to content

Commit

Permalink
Add fix for unix date time filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardLindhout committed Dec 4, 2021
1 parent e281f83 commit 2e6e352
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func main() {
backend, // directory where sqlboiler files are put
frontend, // directory where gqlgen models live
gbgen.ConvertPluginConfig{
// UseReflectWorkaroundForSubModelFilteringInPostgresIssue25: true, // see issue #25 on GitHub
DatabaseDriver: gbgen.MySQL, // or gbgen.PostgreSQL,
},
)),
api.AddPlugin(gbgen.NewResolverPlugin(
Expand Down
16 changes: 15 additions & 1 deletion convert_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,18 @@ type Config struct {
PackageName string
}

// DatabaseDriver defines which data syntax to use for some of the converts
type DatabaseDriver string

const (
// MySQL is the default
MySQL DatabaseDriver = "mysql"
// PostgreSQL is the default
PostgreSQL DatabaseDriver = "postgres"
)

type ConvertPluginConfig struct {
UseReflectWorkaroundForSubModelFilteringInPostgresIssue25 bool
DatabaseDriver DatabaseDriver
}

var _ plugin.ConfigMutator = &ConvertPlugin{}
Expand Down Expand Up @@ -244,6 +254,10 @@ func (m *ConvertPlugin) MutateConfig(originalCfg *config.Config) error {
if err := os.MkdirAll(m.Output.Directory, os.ModePerm); err != nil {
log.Error().Err(err).Str("directory", m.Output.Directory).Msg("could not create directories")
}

if m.PluginConfig.DatabaseDriver == "" {
fmt.Println("Please specify database driver, see README on github")
}
// log.Debug().Msg("[customization] looking for *_customized files")

log.Debug().Msg("[convert] get boiler models")
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ require (
github.com/rs/zerolog v1.20.0
github.com/vektah/gqlparser/v2 v2.0.1
github.com/volatiletech/strmangle v0.0.1
golang.org/x/mod v0.3.0
golang.org/x/tools v0.0.0-20200507205054-480da3ebd79c
golang.org/x/mod v0.4.2
golang.org/x/tools v0.1.5
)
27 changes: 16 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,36 +64,41 @@ github.com/volatiletech/inflect v0.0.1 h1:2a6FcMQyhmPZcLa+uet3VJ8gLn/9svWhJxJYwv
github.com/volatiletech/inflect v0.0.1/go.mod h1:IBti31tG6phkHitLlr5j7shC5SOo//x0AjDzaJU1PLA=
github.com/volatiletech/strmangle v0.0.1 h1:UKQoHmY6be/R3tSvD2nQYrH41k43OJkidwEiC74KIzk=
github.com/volatiletech/strmangle v0.0.1/go.mod h1:F6RA6IkB5vq0yTG4GQ0UsbbRcl3ni9P76i+JrTBKFFg=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190125232054-d66bd3c5d5a6/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190515012406-7d7faa4812bd/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200114235610-7ae403b6b589/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200507205054-480da3ebd79c h1:TDspWmUQsjdWzrHnd5imfaJSfhR4AO/R7kG++T2cONw=
golang.org/x/tools v0.0.0-20200507205054-480da3ebd79c/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.1.5 h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
24 changes: 23 additions & 1 deletion sqlboiler_graphql_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func SchemaGet(
relationName := getRelationName(field)
w.tl(relationName + ": " + field.BoilerField.Relationship.Name + "Where" + directives)
} else {
w.tl(field.Name + ": " + field.Type + "Filter" + directives)
w.tl(field.Name + ": " + getFilterType(field) + "Filter" + directives)
}
}
w.tl("or: " + model.Name + "Where")
Expand Down Expand Up @@ -530,6 +530,14 @@ func SchemaGet(
return w.s.String()
}

func getFilterType(field *SchemaField) string {
boilerType := field.BoilerField.Type
if boilerType == "null.Time" || boilerType == "time.Time" {
return "TimeUnix"
}
return field.Type
}

func enhanceFields(config SchemaConfig, model *SchemaModel, fields []*SchemaField, parentType ParentType) []*SchemaField {
if config.HookChangeFields != nil {
return config.HookChangeFields(model, fields, parentType)
Expand Down Expand Up @@ -723,6 +731,7 @@ func toGraphQLType(boilerField *BoilerField) string {

// TODO: make this a scalar or something configurable?
// I like to use unix here
// make sure TimeUnixFilter keeps working
if strings.Contains(lowerBoilerType, "time") {
return "Int"
}
Expand Down Expand Up @@ -927,6 +936,19 @@ input IntFilter {
notIn: [Int!]
}
input TimeUnixFilter {
isNullOrZero: Boolean
isNull: Boolean
notNullOrZero: Boolean
notNull: Boolean
equalTo: Int
notEqualTo: Int
lessThan: Int
lessThanOrEqualTo: Int
moreThan: Int
moreThanOrEqualTo: Int
}
input FloatFilter {
isNullOrZero: Boolean
isNull: Boolean
Expand Down
50 changes: 49 additions & 1 deletion template_files/generated_filter.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func isNotNullOr(column string, v string) qm.QueryMod {

func appendSubQuery(queryMods []qm.QueryMod, q *queries.Query) []qm.QueryMod {
// TODO: integrate with subquery in sqlboiler if it will be released in the future
{{- if $.PluginConfig.UseReflectWorkaroundForSubModelFilteringInPostgresIssue25 }}
{{- if eq $.PluginConfig.DatabaseDriver "postgres" }}
// https://github.com/web-ridge/gqlgen-sqlboiler/issues/25 we need this for postgres
member := reflect.ValueOf(q).Elem().FieldByName("dialect")
dialectPtr := (**drivers.Dialect)(unsafe.Pointer(member.UnsafeAddr()))
Expand All @@ -71,6 +71,7 @@ func appendSubQuery(queryMods []qm.QueryMod, q *queries.Query) []qm.QueryMod {
return append(queryMods, qm.Where(fmt.Sprintf("EXISTS(%v)", qsClean), args...))
}


func BooleanFilterToMods(m *{{ $.Frontend.PackageName }}.BooleanFilter, column string) []qm.QueryMod {
if m == nil {
return nil
Expand Down Expand Up @@ -267,6 +268,53 @@ func IntFilterToMods(m *{{ $.Frontend.PackageName }}.IntFilter, column string) [
}
return queryMods
}


func TimeUnixFilterToMods(m *{{ $.Frontend.PackageName }}.TimeUnixFilter, c string) []qm.QueryMod {
if m == nil {
return nil
}

{{- if eq $.PluginConfig.DatabaseDriver "postgres" }}
column := "extract(epoch from (" + c + ")"
{{- else if eq $.PluginConfig.DatabaseDriver "mysql" }}
column := "UNIX_TIMESTAMP(" + c + ")"
{{- end }}

var queryMods []qm.QueryMod
if m.IsNullOrZero != nil {
queryMods = append(queryMods, isNullOr(column, isZero))
}
if m.IsNull != nil {
queryMods = append(queryMods, qmhelper.WhereIsNull(column))
}
if m.NotNullOrZero != nil {
queryMods = append(queryMods, isNotNullOr(column, isZero))
}
if m.NotNull != nil {
queryMods = append(queryMods, qmhelper.WhereIsNotNull(column))
}
if m.EqualTo != nil {
queryMods = append(queryMods, qmhelper.Where(column, qmhelper.EQ, *m.EqualTo))
}
if m.NotEqualTo != nil {
queryMods = append(queryMods, qmhelper.Where(column, qmhelper.NEQ, *m.NotEqualTo))
}
if m.LessThan != nil {
queryMods = append(queryMods, qmhelper.Where(column, qmhelper.LT, *m.LessThan))
}
if m.MoreThan != nil {
queryMods = append(queryMods, qmhelper.Where(column, qmhelper.GT, *m.MoreThan))
}
if m.LessThanOrEqualTo != nil {
queryMods = append(queryMods, qmhelper.Where(column, qmhelper.LTE, *m.LessThanOrEqualTo))
}
if m.MoreThanOrEqualTo != nil {
queryMods = append(queryMods, qmhelper.Where(column, qmhelper.GTE, *m.MoreThanOrEqualTo))
}
return queryMods
}

{{ range $enum := .Enums }}
func {{ .Name }}FilterToMods(m *{{ $.Frontend.PackageName }}.{{ .Name }}Filter, column string) []qm.QueryMod {
if m == nil {
Expand Down

0 comments on commit 2e6e352

Please sign in to comment.