From 1bc90c169e4e7ea464d4b33247000a919fa1a388 Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Wed, 20 Jul 2022 11:34:01 -0600 Subject: [PATCH] Adding database FilterOptions struct --- database/options.go | 38 ++++++++++++++++++++++++++++++++++++++ go.mod | 3 +++ go.sum | 6 ++++++ 3 files changed, 47 insertions(+) create mode 100644 database/options.go diff --git a/database/options.go b/database/options.go new file mode 100644 index 0000000..79a38cf --- /dev/null +++ b/database/options.go @@ -0,0 +1,38 @@ +package waxbar + +import sq "github.com/Masterminds/squirrel" + +// FilterOptions is a simple helper for sql filtering +type FilterOptions struct { + Filter sq.Sqlizer + Limit int + Offset int + OrderBy string +} + +// GetBuilder returns a squirrel SelectBuilder based on options +func (f *FilterOptions) GetBuilder(filter sq.Sqlizer) sq.SelectBuilder { + query := sq.Select() + if f.Filter != nil { + if filter != nil { + query = query.Where(sq.And{filter, f.Filter}) + } else { + query = query.Where(f.Filter) + } + } else { + if filter != nil { + query = query.Where(filter) + } + } + if f.Limit > 0 { + query = query.Limit(uint64(f.Limit)) + } + + if f.Offset > 0 { + query = query.Offset(uint64(f.Offset)) + } + if f.OrderBy != "" { + query = query.OrderBy(f.OrderBy) + } + return query +} diff --git a/go.mod b/go.mod index 407f875..60e6033 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( ) require ( + github.com/Masterminds/squirrel v1.5.3 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.1.1 // indirect github.com/dustin/go-humanize v1.0.0 // indirect @@ -35,6 +36,8 @@ require ( github.com/klauspost/compress v1.13.5 // indirect github.com/klauspost/cpuid v1.3.1 // indirect github.com/labstack/gommon v0.3.1 // indirect + github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect + github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect github.com/leodido/go-urn v1.2.1 // indirect github.com/mattn/go-colorable v0.1.11 // indirect github.com/mattn/go-isatty v0.0.14 // indirect diff --git a/go.sum b/go.sum index ae5d0eb..b45058c 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ git.sr.ht/~sircmpwn/dowork v0.0.0-20210820133136-d3970e97def3 h1:9WCv5cK67s2SiY/R4DWT/OchEsFnfYDz3lbevKxZ4QI= git.sr.ht/~sircmpwn/dowork v0.0.0-20210820133136-d3970e97def3/go.mod h1:8neHEO3503w/rNtttnR0JFpQgM/GFhaafVwvkPsFIDw= +github.com/Masterminds/squirrel v1.5.3 h1:YPpoceAcxuzIljlr5iWpNKaql7hLeG1KLSrhvdHpkZc= +github.com/Masterminds/squirrel v1.5.3/go.mod h1:NNaOrjSoIDfDA40n7sr2tPNZRfjzjA400rg+riTZj10= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= @@ -89,6 +91,10 @@ github.com/labstack/echo/v4 v4.7.2/go.mod h1:xkCDAdFCIf8jsFQ5NnbK7oqaF/yU1A1X20L github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k= github.com/labstack/gommon v0.3.1 h1:OomWaJXm7xR6L1HmEtGyQf26TEn7V6X88mktX9kee9o= github.com/labstack/gommon v0.3.1/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM= +github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw= +github.com/lann/builder v0.0.0-20180802200727-47ae307949d0/go.mod h1:dXGbAdH5GtBTC4WfIxhKZfyBF/HBFgRZSWwZ9g/He9o= +github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhRWSsG5rVo6hYhAB/ADZrk= +github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0/go.mod h1:vmVJ0l/dxyfGW6FmdpVm2joNMFikkuWg0EoCKLGUMNw= github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w= github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= github.com/lib/pq v1.4.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -- 2.45.2