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 ¶
- Variables
- func Merge(col string, patch map[string]interface{}) clause.Expr
- func Remove(col string, keys ...string) clause.Expr
- func RemoveByValue(col, targetValue string) clause.Expr
- func Set(col, key string, value interface{}) clause.Expr
- func Where(col, key string, op Op, value interface{}) clause.Expr
- func WhereHasValue(col, value string) clause.Expr
- func WhereNotNull(col, key string) clause.Expr
- func WhereNull(col, key string) clause.Expr
- type Dialect
- type Op
- type SearchField
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
Set writes value at key inside col.
db.Model(&u).UpdateColumn("meta", expr.Set("meta", "theme", "dark"))
func Where ¶
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 ¶
WhereHasValue matches rows where any entry in col has the given value.
db.Where(expr.WhereHasValue("meta", "dark")).Find(&rows)
func WhereNotNull ¶
WhereNotNull matches rows where key exists and is not null.
db.Where(expr.WhereNotNull("meta", "verified")).Find(&rows)
Types ¶
type Dialect ¶
type Dialect string
Dialect is the underlying database engine.
func CurrentDialect ¶
func CurrentDialect() Dialect
CurrentDialect reads the DATABASE env var to determine the active dialect.
type SearchField ¶
func ByteaField ¶
func ByteaField(field string) SearchField