sqljson

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2024 License: Apache-2.0 Imports: 8 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append[T any](u *sql.UpdateBuilder, column string, elems []T, opts ...Option)

Append writes to the given SQL builder the SQL command for appending JSON values into the array, optionally defined as a key. Note, the generated SQL will use the Go semantics, the JSON column/key will be set to the given Array in case it is `null` or NULL. For example:

Append(u, column, []string{"a", "b"})
UPDATE "t" SET "c" = CASE
	WHEN ("c" IS NULL OR "c" = 'null'::jsonb)
	THEN $1 ELSE "c" || $2 END

Append(u, column, []any{"a", 1}, sqljson.Path("a"))
UPDATE "t" SET "c" = CASE
	WHEN (("c"->'a')::jsonb IS NULL OR ("c"->'a')::jsonb = 'null'::jsonb)
	THEN jsonb_set("c", '{a}', $1, true) ELSE jsonb_set("c", '{a}', "c"->'a' || $2, true) END

func HasKey

func HasKey(column string, opts ...Option) *sql.Predicate

HasKey return a predicate for checking that a JSON key exists and not NULL.

sqljson.HasKey("column", sql.DotPath("a.b[2].c"))

func LenEQ

func LenEQ(column string, size int, opts ...Option) *sql.Predicate

LenEQ return a predicate for checking that an array length of a JSON (returned by the path) is equal to the given argument.

sqljson.LenEQ("a", 1, sqljson.Path("b"))

func LenGT

func LenGT(column string, size int, opts ...Option) *sql.Predicate

LenGT return a predicate for checking that an array length of a JSON (returned by the path) is greater than the given argument.

sqljson.LenGT("a", 1, sqljson.Path("b"))

func LenGTE

func LenGTE(column string, size int, opts ...Option) *sql.Predicate

LenGTE return a predicate for checking that an array length of a JSON (returned by the path) is greater than or equal to the given argument.

sqljson.LenGTE("a", 1, sqljson.Path("b"))

func LenLT

func LenLT(column string, size int, opts ...Option) *sql.Predicate

LenLT return a predicate for checking that an array length of a JSON (returned by the path) is less than the given argument.

sqljson.LenLT("a", 1, sqljson.Path("b"))

func LenLTE

func LenLTE(column string, size int, opts ...Option) *sql.Predicate

LenLTE return a predicate for checking that an array length of a JSON (returned by the path) is less than or equal to the given argument.

sqljson.LenLTE("a", 1, sqljson.Path("b"))

func LenNEQ

func LenNEQ(column string, size int, opts ...Option) *sql.Predicate

LenNEQ return a predicate for checking that an array length of a JSON (returned by the path) is not equal to the given argument.

sqljson.LenEQ("a", 1, sqljson.Path("b"))

func LenPath

func LenPath(column string, opts ...Option) sql.Querier

LenPath returns an SQL expression for getting the length of a JSON value (returned by the path).

func OrderLen

func OrderLen(column string, opts ...Option) func(*sql.Selector)

OrderLen returns a custom predicate function (as defined in the doc), that sets the result order by the length of the given JSON value.

func OrderLenDesc

func OrderLenDesc(column string, opts ...Option) func(*sql.Selector)

OrderLenDesc returns a custom predicate function (as defined in the doc), that sets the result order by the length of the given JSON value, but in descending order.

func OrderValue

func OrderValue(column string, opts ...Option) func(*sql.Selector)

OrderValue returns a custom predicate function (as defined in the doc), that sets the result order by the given JSON value.

func OrderValueDesc

func OrderValueDesc(column string, opts ...Option) func(*sql.Selector)

OrderValueDesc returns a custom predicate function (as defined in the doc), that sets the result order by the given JSON value, but in descending order.

func ParsePath

func ParsePath(dotpath string) ([]string, error)

ParsePath parses the "dotpath" for the DotPath option.

"a.b"		=> ["a", "b"]
"a[1][2]"	=> ["a", "[1]", "[2]"]
"a.\"b.c\"	=> ["a", "\"b.c\""]

func StringContains

func StringContains(column string, sub string, opts ...Option) *sql.Predicate

StringContains return a predicate for checking that a JSON string value (returned by the path) contains the given substring

func StringHasPrefix

func StringHasPrefix(column string, prefix string, opts ...Option) *sql.Predicate

StringHasPrefix return a predicate for checking that a JSON string value (returned by the path) has the given substring as prefix

func StringHasSuffix

func StringHasSuffix(column string, suffix string, opts ...Option) *sql.Predicate

StringHasSuffix return a predicate for checking that a JSON string value (returned by the path) has the given substring as suffix

func ValueContains

func ValueContains(column string, arg any, opts ...Option) *sql.Predicate

ValueContains return a predicate for checking that a JSON value (returned by the path) contains the given argument.

sqljson.ValueContains("a", 1, sqljson.Path("b"))

func ValueEQ

func ValueEQ(column string, arg any, opts ...Option) *sql.Predicate

ValueEQ return a predicate for checking that a JSON value (returned by the path) is equal to the given argument.

sqljson.ValueEQ("a", 1, sqljson.Path("b"))

func ValueGT

func ValueGT(column string, arg any, opts ...Option) *sql.Predicate

ValueGT return a predicate for checking that a JSON value (returned by the path) is greater than the given argument.

sqljson.ValueGT("a", 1, sqljson.Path("b"))

func ValueGTE

func ValueGTE(column string, arg any, opts ...Option) *sql.Predicate

ValueGTE return a predicate for checking that a JSON value (returned by the path) is greater than or equal to the given argument.

sqljson.ValueGTE("a", 1, sqljson.Path("b"))

func ValueIn

func ValueIn(column string, args []any, opts ...Option) *sql.Predicate

ValueIn return a predicate for checking that a JSON value (returned by the path) is IN the given arguments.

sqljson.ValueIn("a", []any{1, 2, 3}, sqljson.Path("b"))

func ValueIsNotNull

func ValueIsNotNull(column string, opts ...Option) *sql.Predicate

ValueIsNotNull return a predicate for checking that a JSON value (returned by the path) is not null literal (JSON "null").

sqljson.ValueIsNotNull("a", sqljson.Path("b"))

func ValueIsNull

func ValueIsNull(column string, opts ...Option) *sql.Predicate

ValueIsNull return a predicate for checking that a JSON value (returned by the path) is a null literal (JSON "null").

In order to check if the column is NULL (database NULL), or if the JSON key exists, use sql.IsNull or sqljson.HasKey.

sqljson.ValueIsNull("a", sqljson.Path("b"))

func ValueLT

func ValueLT(column string, arg any, opts ...Option) *sql.Predicate

ValueLT return a predicate for checking that a JSON value (returned by the path) is less than the given argument.

sqljson.ValueLT("a", 1, sqljson.Path("b"))

func ValueLTE

func ValueLTE(column string, arg any, opts ...Option) *sql.Predicate

ValueLTE return a predicate for checking that a JSON value (returned by the path) is less than or equal to the given argument.

sqljson.ValueLTE("a", 1, sqljson.Path("b"))

func ValueNEQ

func ValueNEQ(column string, arg any, opts ...Option) *sql.Predicate

ValueNEQ return a predicate for checking that a JSON value (returned by the path) is not equal to the given argument.

sqljson.ValueNEQ("a", 1, sqljson.Path("b"))

func ValueNotIn

func ValueNotIn(column string, args []any, opts ...Option) *sql.Predicate

ValueNotIn return a predicate for checking that a JSON value (returned by the path) is NOT IN the given arguments.

sqljson.ValueNotIn("a", []any{1, 2, 3}, sqljson.Path("b"))

func ValuePath

func ValuePath(column string, opts ...Option) sql.Querier

ValuePath returns an SQL expression for getting the JSON value of a column with an optional path and cast options.

sqljson.ValueEQ(
	column,
	sqljson.ValuePath(column, Path("a"), Cast("int")),
	sqljson.Path("a"),
)

Types

type Option

type Option func(*PathOptions)

Option allows for calling database JSON paths with functional options.

func Cast

func Cast(typ string) Option

Cast indicates that the result value should be cast to the given type.

ValuePath(b, "column", Path("a", "b", "[1]", "c"), Cast("int"))

func DotPath

func DotPath(dotpath string) Option

DotPath is similar to Path, but accepts string with dot format.

ValuePath(b, "column", DotPath("a.b.c"))
ValuePath(b, "column", DotPath("a.b[2].c"))

Note that DotPath is ignored if the input is invalid.

func Path

func Path(path ...string) Option

Path sets the path to the JSON value of a column.

ValuePath(b, "column", Path("a", "b", "[1]", "c"))

func Unquote

func Unquote(unquote bool) Option

Unquote indicates that the result value should be unquoted.

ValuePath(b, "column", Path("a", "b", "[1]", "c"), Unquote(true))

type PathOptions

type PathOptions struct {
	Ident   string
	Path    []string
	Cast    string
	Unquote bool
}

PathOptions holds the options for accessing a JSON value from an identifier.

func (*PathOptions) Query

func (p *PathOptions) Query() (string, []any)

Jump to

Keyboard shortcuts

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