sqlice

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2021 License: MIT Imports: 6 Imported by: 0

README

sqlice

Go Reference Go Report Card Build Status

sqlice extends the functinailty of squirrel and allows you to your database filtering on slices! This makes it easy to:

  • provide filtering to functions not backed by database storage, while keeping it consistent with your functions that do
  • easily mock out your database interfaces and have your filters work the same

sqlice supports the following filters from the squirrel package:

  • Eq
  • NotEq
  • Lt
  • LtOrEq
  • Gt
  • GtOrEq
  • And
  • Or
  • Like
  • NotLike
  • ILike
  • NotILike

Usage

sqlice will use the name of the struct fields to match with the columns/keys in the filters.

type FooBar struct {
    A int
}

values := []FooBar{{A: 4}, {A: 2}, {A: 1}, {A: 3}}
var filteredValues []FooBar
err := sqlice.Filter(values, &filteredValues, Squirrel.Gt{"A": 2})
if err != nil {
    panic(err)
}
fmt.Println(filteredValues) // {{A: 4}, {A: 3}}

The comparison is case insensitive. The following examples shows comparing against a custom named field

type FooBar struct {
    A int `db:"myColumnName"`
}

values := []FooBar{{A: 4}, {A: 2}, {A: 1}, {A: 3}}
var filteredValues []FooBar
err := sqlice.Filter(values, &filteredValues, Squirrel.Lt{"myColumnName": 3})
if err != nil {
    panic(err)
}
fmt.Println(filteredValues) // {{A: 2}, {A: 1}}

The struct tag used by this package is identical to the ones used by sqlx. This is done intentionally to help ensure that you can use sqlice without needing to do any modifications to your structs

Extending your own filters

To use your own Sqlizers with sqlice, just add the FilterValue(interface{})bool method to your type!

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filter

func Filter(input, output interface{}, filter squirrel.Sqlizer) error

Filter filters the input slice using the filter, storing the result in output. Input must be a slice of filterable elements (a struct) and output must be a pointer to a slice of identical type. If the filter contains fields not present in the struct or values that aren't compatible with corresponding field, an error is returned. If a filter is encountered that is not from the squirrel package, it is only used if it implements ValueFilterer

Example
package main

import (
	"fmt"

	"github.com/Masterminds/squirrel"
	"github.com/pixelrazor/sqlice"
)

func main() {
	type FooBar struct {
		A int
		B string `db:"bar"`
	}
	input := []FooBar{
		{A: 2, B: "b"},
		{A: 4, B: "d"},
		{A: 1, B: "a"},
		{A: 5, B: "e"},
		{A: 3, B: "c"},
	}
	var output []FooBar

	err := sqlice.Filter(input, &output, squirrel.And{
		squirrel.Gt{"A": 1},
		squirrel.Lt{"bar": "e"},
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(output)
}
Output:

[{2 b} {4 d} {3 c}]

Types

type ValueFilterFunc

type ValueFilterFunc func(interface{}) bool

ValueFilterFunc is an adapter that allows a function to be used as a custom filter to Filter. It implements the squirrel.Sqlizer interface to satisfy requirements, but the implementation of ToSql will always return an error

Example
package main

import (
	"fmt"
	"sort"

	"github.com/pixelrazor/sqlice"
)

func main() {
	type FooBar struct {
		Things []int
	}
	filterFunc := func(i interface{}) bool {
		return sort.IntsAreSorted((i.(FooBar).Things))
	}

	input := []FooBar{
		{Things: []int{3, 8, 1}},
		{Things: []int{1, 2, 3}},
		{Things: []int{5, 3, 1}},
		{Things: []int{5, 8, 9}},
	}
	var output []FooBar

	err := sqlice.Filter(input, &output, sqlice.ValueFilterFunc(filterFunc))
	if err != nil {
		panic(err)
	}
	fmt.Println(output)
}
Output:

[{[1 2 3]} {[5 8 9]}]

func (ValueFilterFunc) FilterValue

func (vff ValueFilterFunc) FilterValue(i interface{}) bool

FilterValue calls vff(i)

func (ValueFilterFunc) ToSql

func (vff ValueFilterFunc) ToSql() (string, []interface{}, error)

ToSql always returns an error when called

type ValueFilterer

type ValueFilterer interface {
	FilterValue(interface{}) bool
}

ValueFilterer is the interface that wraps the FilterValue method. FilterValue is given a value from slice of elements, and should return true if it is to be included.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL