json

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetDefaultDialect

func SetDefaultDialect(d JSONDialect)

SetDefaultDialect sets the default JSON dialect for operations.

Types

type JSONDialect

type JSONDialect interface {
	// Name returns the dialect name (e.g., "mysql", "postgres", "sqlite")
	Name() string

	// ExtractPath generates SQL for extracting a value at a JSON path.
	// Returns the SQL fragment (e.g., "JSON_EXTRACT(col, ?)" for MySQL)
	ExtractPath(column, path string) (sql string, vars []any)

	// PathEq generates SQL for checking if a JSON path equals a value.
	// Returns the SQL and variables for parameterized queries.
	PathEq(column, path string, value any) (sql string, vars []any)

	// PathNeq generates SQL for checking if a JSON path does not equal a value.
	PathNeq(column, path string, value any) (sql string, vars []any)

	// PathGt generates SQL for checking if a JSON path is greater than a value.
	PathGt(column, path string, value any) (sql string, vars []any)

	// PathGte generates SQL for checking if a JSON path is greater than or equal to a value.
	PathGte(column, path string, value any) (sql string, vars []any)

	// PathLt generates SQL for checking if a JSON path is less than a value.
	PathLt(column, path string, value any) (sql string, vars []any)

	// PathLte generates SQL for checking if a JSON path is less than or equal to a value.
	PathLte(column, path string, value any) (sql string, vars []any)

	// Contains generates SQL for checking if JSON contains a value.
	Contains(column string, value any, path string) (sql string, vars []any)

	// SetPath generates SQL for setting a value at a JSON path.
	// Returns the SQL expression for use in UPDATE statements.
	SetPath(column, path string, value any) clause.Expr

	// SetMultiplePaths generates SQL for setting multiple path-value pairs.
	// This is used by the SetBuilder for batch updates.
	SetMultiplePaths(column string, paths []string, values []any) clause.Expr

	// RemovePath generates SQL for removing a JSON path.
	RemovePath(column, path string) clause.Expr

	// RemoveMultiplePaths generates SQL for removing multiple JSON paths.
	RemoveMultiplePaths(column string, paths []string) clause.Expr

	// MergePatch generates SQL for RFC 7396 Merge Patch.
	// (MySQL: JSON_MERGE_PATCH, Postgres: ||, SQLite: json_patch)
	MergePatch(column string, value any) clause.Expr

	// MergePreserve generates SQL for merging with array preservation (Legacy/Concat).
	// (MySQL: JSON_MERGE_PRESERVE, Postgres: ||)
	MergePreserve(column string, value any) clause.Expr
}

JSONDialect defines the interface for database-specific JSON operations. Each database (MySQL, PostgreSQL, SQLite) implements this interface to generate the correct SQL syntax for JSON operations.

var (
	MySQL    JSONDialect = &mysqlDialect{}
	Postgres JSONDialect = &postgresDialect{}
	SQLite   JSONDialect = &sqliteDialect{}
)

Dialect instances

func DefaultDialect

func DefaultDialect() JSONDialect

DefaultDialect returns the current default JSON dialect.

func DialectByName

func DialectByName(name string) JSONDialect

DialectByName returns a JSONDialect by its name.

type JSONPath

type JSONPath struct {
	Column string // Database column name
	Path   string // JSON path expression (e.g. "$.name")
}

JSONPath represents a path within a JSON column. It holds both the column name and the JSON path expression.

func (JSONPath) Arg

func (p JSONPath) Arg(value any) PathValue

Arg creates a PathValue pair for use with bulk update methods.

func (JSONPath) Contains

func (p JSONPath) Contains(value any) clause.Expression

Contains creates a contains expression using the default dialect.

func (JSONPath) Eq

func (p JSONPath) Eq(value any) clause.Expression

Eq creates an equality expression using the default dialect.

func (JSONPath) Gt

func (p JSONPath) Gt(value any) clause.Expression

Gt creates a greater-than expression using the default dialect.

func (JSONPath) Gte

func (p JSONPath) Gte(value any) clause.Expression

Gte creates a greater-than-or-equal expression using the default dialect.

func (JSONPath) Lt

func (p JSONPath) Lt(value any) clause.Expression

Lt creates a less-than expression using the default dialect.

func (JSONPath) Lte

func (p JSONPath) Lte(value any) clause.Expression

Lte creates a less-than-or-equal expression using the default dialect.

func (JSONPath) Neq

func (p JSONPath) Neq(value any) clause.Expression

Neq creates a not-equal expression using the default dialect.

func (JSONPath) Remove

func (p JSONPath) Remove() clause.Assignment

Remove creates an assignment expression for removing this JSON path using the default dialect.

func (JSONPath) Set

func (p JSONPath) Set(value any) clause.Assignment

Set creates an assignment expression for setting this JSON path using the default dialect.

func (JSONPath) With

func (p JSONPath) With(dialect JSONDialect) JSONPathOps

With returns a JSONPathOps that can be used for query operations with the specified dialect.

type JSONPathOps

type JSONPathOps struct {
	// contains filtered or unexported fields
}

JSONPathOps provides JSON path operations with a specific dialect.

func NewPathOps

func NewPathOps(column clause.Column, path string, dialect JSONDialect) JSONPathOps

NewPathOps creates a JSONPathOps for the given column, path, and dialect.

func (JSONPathOps) Contains

func (p JSONPathOps) Contains(value any) clause.Expression

Contains creates a contains expression for this JSON path.

func (JSONPathOps) Eq

func (p JSONPathOps) Eq(value any) clause.Expression

Eq creates an equality expression for this JSON path.

func (JSONPathOps) Gt

func (p JSONPathOps) Gt(value any) clause.Expression

Gt creates a greater-than expression for this JSON path.

func (JSONPathOps) Gte

func (p JSONPathOps) Gte(value any) clause.Expression

Gte creates a greater-than-or-equal expression for this JSON path.

func (JSONPathOps) Lt

func (p JSONPathOps) Lt(value any) clause.Expression

Lt creates a less-than expression for this JSON path.

func (JSONPathOps) Lte

func (p JSONPathOps) Lte(value any) clause.Expression

Lte creates a less-than-or-equal expression for this JSON path.

func (JSONPathOps) Neq

func (p JSONPathOps) Neq(value any) clause.Expression

Neq creates a not-equal expression for this JSON path.

func (JSONPathOps) Remove

func (p JSONPathOps) Remove() clause.Assignment

Remove creates an assignment expression for removing this JSON path.

func (JSONPathOps) Set

func (p JSONPathOps) Set(value any) clause.Assignment

Set creates an assignment expression for setting this JSON path.

type JSONRemoveBuilder

type JSONRemoveBuilder struct {
	// contains filtered or unexported fields
}

JSONRemoveBuilder builds JSON REMOVE expressions with multiple paths.

func NewRemoveBuilder

func NewRemoveBuilder(column string, dialect JSONDialect) *JSONRemoveBuilder

NewRemoveBuilder creates a new JSONRemoveBuilder for the given column and dialect.

func (*JSONRemoveBuilder) Assignment

func (b *JSONRemoveBuilder) Assignment(col clause.Column) clause.Assignment

Assignment returns the clause.Assignment for use with UpdateColumns.

func (*JSONRemoveBuilder) Build

func (b *JSONRemoveBuilder) Build() clause.Expr

Build generates the SQL expression using the configured dialect.

func (*JSONRemoveBuilder) Path

Path adds a path to remove.

type JSONSetBuilder

type JSONSetBuilder struct {
	// contains filtered or unexported fields
}

JSONSetBuilder builds JSON SET expressions with multiple path-value pairs.

func NewSetBuilder

func NewSetBuilder(column string, dialect JSONDialect) *JSONSetBuilder

NewSetBuilder creates a new JSONSetBuilder for the given column and dialect.

func (*JSONSetBuilder) Assignment

func (b *JSONSetBuilder) Assignment(col clause.Column) clause.Assignment

Assignment returns the clause.Assignment for use with UpdateColumns.

func (*JSONSetBuilder) Build

func (b *JSONSetBuilder) Build() clause.Expr

Build generates the SQL expression using the configured dialect.

func (*JSONSetBuilder) Path

func (b *JSONSetBuilder) Path(path string, value any) *JSONSetBuilder

Path adds a path-value pair to the builder.

type PathValue

type PathValue struct {
	Path  string
	Value any
}

PathValue represents a path-value pair for bulk updates.

Jump to

Keyboard shortcuts

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