expr

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package expr builds dialect-aware SQL expressions for JSON map columns. Results are clause.Expr values that pass directly into GORM calls.

Dialect is read once per call from DATABASE env var. Falls back to SQLite if unset or unrecognised.

DATABASE=sqlite   → json_extract / json_set / json_remove / json_patch
DATABASE=postgres → ->> / jsonb_set / #- / ||

Index

Constants

This section is empty.

Variables

View Source
var ByteaIPFormatter = func(quoted string) string {
	if CurrentDialect() == DialectPostgres {
		return fmt.Sprintf(
			"(get_byte(%s, 12)::text || '.' || get_byte(%s, 13)::text || '.' || get_byte(%s, 14)::text || '.' || get_byte(%s, 15)::text)",
			quoted, quoted, quoted, quoted,
		)
	}
	return fmt.Sprintf("hex(%s)", quoted)
}

Functions

func Merge

func Merge(col string, patch map[string]interface{}) clause.Expr

Merge shallow-merges patch into col, adding new keys and overwriting existing ones.

db.Model(&u).UpdateColumn("meta", expr.Merge("meta", map[string]any{"theme": "dark", "lang": "en"}))

func Remove

func Remove(col string, keys ...string) clause.Expr

Remove deletes one or more keys from col in a single expression.

db.Model(&u).UpdateColumn("meta", expr.Remove("meta", "theme"))
db.Model(&u).UpdateColumn("meta", expr.Remove("meta", "theme", "lang", "score"))

func RemoveByValue

func RemoveByValue(col, targetValue string) clause.Expr

RemoveByValue returns a SET expression that rebuilds col without any entries whose value equals targetValue.

db.Model(&u).UpdateColumn("meta", expr.RemoveByValue("meta", "dark"))
db.Model(&u).UpdateColumn("meta", expr.RemoveByValue("meta", "english"))

func Set

func Set(col, key string, value interface{}) clause.Expr

Set writes value at key inside col.

db.Model(&u).UpdateColumn("meta", expr.Set("meta", "theme", "dark"))

func Where

func Where(col, key string, op Op, value interface{}) clause.Expr

Where compares the value at key in col using op. Numeric ops (Gt, Lt, Gte, Lte) cast the extracted value to a number first, so comparisons work correctly instead of falling back to string ordering.

db.Where(expr.Where("meta", "theme",  expr.Eq,  "dark")).Find(&rows)
db.Where(expr.Where("meta", "score",  expr.Gt,  100)).Find(&rows)
db.Where(expr.Where("meta", "rating", expr.Lte, 4.5)).Find(&rows)

func WhereHasValue

func WhereHasValue(col, value string) clause.Expr

WhereHasValue matches rows where any entry in col has the given value.

db.Where(expr.WhereHasValue("meta", "dark")).Find(&rows)

func WhereNotNull

func WhereNotNull(col, key string) clause.Expr

WhereNotNull matches rows where key exists and is not null.

db.Where(expr.WhereNotNull("meta", "verified")).Find(&rows)

func WhereNull

func WhereNull(col, key string) clause.Expr

WhereNull matches rows where key is absent or null.

db.Where(expr.WhereNull("meta", "deleted_at")).Find(&rows)

Types

type Dialect

type Dialect string

Dialect is the underlying database engine.

const (
	DialectSQLite   Dialect = "sqlite"
	DialectPostgres Dialect = "postgres"
)

func CurrentDialect

func CurrentDialect() Dialect

CurrentDialect reads the DATABASE env var to determine the active dialect.

type Op

type Op string

Op is a SQL comparison operator.

const (
	Eq  Op = "="
	Neq Op = "!="
	Gt  Op = ">"
	Lt  Op = "<"
	Gte Op = ">="
	Lte Op = "<="
)

type SearchField

type SearchField struct {
	Field     string
	Formatter func(quoted string) string
}

func ByteaField

func ByteaField(field string) SearchField

Jump to

Keyboard shortcuts

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