filter

package
v0.25.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MemoFilterCELAttributes = []cel.EnvOption{
	cel.Variable("content", cel.StringType),
	cel.Variable("creator_id", cel.IntType),
	cel.Variable("created_ts", cel.IntType),
	cel.Variable("updated_ts", cel.IntType),
	cel.Variable("pinned", cel.BoolType),
	cel.Variable("tag", cel.StringType),
	cel.Variable("tags", cel.ListType(cel.StringType)),
	cel.Variable("visibility", cel.StringType),
	cel.Variable("has_task_list", cel.BoolType),

	cel.Function("now",
		cel.Overload("now",
			[]*cel.Type{},
			cel.IntType,
			cel.FunctionBinding(func(_ ...ref.Val) ref.Val {
				return types.Int(time.Now().Unix())
			}),
		),
	),
}

MemoFilterCELAttributes are the CEL attributes for memo.

View Source
var SQLTemplates = map[string]SQLTemplate{
	"json_extract": {
		SQLite:     "JSON_EXTRACT(`memo`.`payload`, '%s')",
		MySQL:      "JSON_EXTRACT(`memo`.`payload`, '%s')",
		PostgreSQL: "memo.payload%s",
	},
	"json_array_length": {
		SQLite:     "JSON_ARRAY_LENGTH(COALESCE(JSON_EXTRACT(`memo`.`payload`, '$.tags'), JSON_ARRAY()))",
		MySQL:      "JSON_LENGTH(COALESCE(JSON_EXTRACT(`memo`.`payload`, '$.tags'), JSON_ARRAY()))",
		PostgreSQL: "jsonb_array_length(COALESCE(memo.payload->'tags', '[]'::jsonb))",
	},
	"json_contains_element": {
		SQLite:     "JSON_EXTRACT(`memo`.`payload`, '$.tags') LIKE ?",
		MySQL:      "JSON_CONTAINS(JSON_EXTRACT(`memo`.`payload`, '$.tags'), ?)",
		PostgreSQL: "memo.payload->'tags' @> jsonb_build_array(?)",
	},
	"json_contains_tag": {
		SQLite:     "JSON_EXTRACT(`memo`.`payload`, '$.tags') LIKE ?",
		MySQL:      "JSON_CONTAINS(JSON_EXTRACT(`memo`.`payload`, '$.tags'), ?)",
		PostgreSQL: "memo.payload->'tags' @> jsonb_build_array(?)",
	},
	"boolean_true": {
		SQLite:     "JSON_EXTRACT(`memo`.`payload`, '$.property.hasTaskList') = 1",
		MySQL:      "JSON_EXTRACT(`memo`.`payload`, '$.property.hasTaskList') = CAST('true' AS JSON)",
		PostgreSQL: "(memo.payload->'property'->>'hasTaskList')::boolean = true",
	},
	"boolean_false": {
		SQLite:     "JSON_EXTRACT(`memo`.`payload`, '$.property.hasTaskList') = 0",
		MySQL:      "JSON_EXTRACT(`memo`.`payload`, '$.property.hasTaskList') = CAST('false' AS JSON)",
		PostgreSQL: "(memo.payload->'property'->>'hasTaskList')::boolean = false",
	},
	"boolean_not_true": {
		SQLite:     "JSON_EXTRACT(`memo`.`payload`, '$.property.hasTaskList') != 1",
		MySQL:      "JSON_EXTRACT(`memo`.`payload`, '$.property.hasTaskList') != CAST('true' AS JSON)",
		PostgreSQL: "(memo.payload->'property'->>'hasTaskList')::boolean != true",
	},
	"boolean_not_false": {
		SQLite:     "JSON_EXTRACT(`memo`.`payload`, '$.property.hasTaskList') != 0",
		MySQL:      "JSON_EXTRACT(`memo`.`payload`, '$.property.hasTaskList') != CAST('false' AS JSON)",
		PostgreSQL: "(memo.payload->'property'->>'hasTaskList')::boolean != false",
	},
	"boolean_compare": {
		SQLite:     "JSON_EXTRACT(`memo`.`payload`, '$.property.hasTaskList') %s ?",
		MySQL:      "JSON_EXTRACT(`memo`.`payload`, '$.property.hasTaskList') %s CAST(? AS JSON)",
		PostgreSQL: "(memo.payload->'property'->>'hasTaskList')::boolean %s ?",
	},
	"boolean_check": {
		SQLite:     "JSON_EXTRACT(`memo`.`payload`, '$.property.hasTaskList') IS TRUE",
		MySQL:      "JSON_EXTRACT(`memo`.`payload`, '$.property.hasTaskList') = CAST('true' AS JSON)",
		PostgreSQL: "(memo.payload->'property'->>'hasTaskList')::boolean IS TRUE",
	},
	"table_prefix": {
		SQLite:     "`memo`",
		MySQL:      "`memo`",
		PostgreSQL: "memo",
	},
	"timestamp_field": {
		SQLite:     "`memo`.`%s`",
		MySQL:      "UNIX_TIMESTAMP(`memo`.`%s`)",
		PostgreSQL: "EXTRACT(EPOCH FROM memo.%s)",
	},
	"content_like": {
		SQLite:     "`memo`.`content` LIKE ?",
		MySQL:      "`memo`.`content` LIKE ?",
		PostgreSQL: "memo.content ILIKE ?",
	},
	"visibility_in": {
		SQLite:     "`memo`.`visibility` IN (%s)",
		MySQL:      "`memo`.`visibility` IN (%s)",
		PostgreSQL: "memo.visibility IN (%s)",
	},
}

SQLTemplates contains common SQL patterns for different databases.

Functions

func FormatPlaceholders added in v0.25.0

func FormatPlaceholders(dbType TemplateDBType, count int, startIndex int) []string

FormatPlaceholders formats a list of placeholders for the given database type.

func GetConstValue

func GetConstValue(expr *exprv1.Expr) (any, error)

GetConstValue returns the constant value of the expression.

func GetExprValue added in v0.24.4

func GetExprValue(expr *exprv1.Expr) (any, error)

GetExprValue attempts to get a value from an expression, trying constants first, then functions.

func GetFunctionValue added in v0.24.4

func GetFunctionValue(expr *exprv1.Expr) (any, error)

GetFunctionValue evaluates CEL function calls and returns their value. This is specifically for time functions like now().

func GetIdentExprName

func GetIdentExprName(expr *exprv1.Expr) (string, error)

GetIdentExprName returns the name of the identifier expression.

func GetParameterPlaceholder added in v0.25.0

func GetParameterPlaceholder(dbType TemplateDBType, index int) string

GetParameterPlaceholder returns the appropriate parameter placeholder for the database.

func GetParameterValue added in v0.25.0

func GetParameterValue(dbType TemplateDBType, templateName string, value interface{}) interface{}

GetParameterValue returns the appropriate parameter value for the database.

func GetSQL added in v0.25.0

func GetSQL(templateName string, dbType TemplateDBType) string

GetSQL returns the appropriate SQL for the given template and database type.

func Parse

func Parse(filter string, opts ...cel.EnvOption) (expr *exprv1.ParsedExpr, err error)

Parse parses the filter string and returns the parsed expression. The filter string should be a CEL expression.

Types

type CommonSQLConverter added in v0.25.0

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

CommonSQLConverter handles the common CEL to SQL conversion logic.

func NewCommonSQLConverter added in v0.25.0

func NewCommonSQLConverter(dialect SQLDialect) *CommonSQLConverter

NewCommonSQLConverter creates a new converter with the specified dialect.

func (*CommonSQLConverter) ConvertExprToSQL added in v0.25.0

func (c *CommonSQLConverter) ConvertExprToSQL(ctx *ConvertContext, expr *exprv1.Expr) error

ConvertExprToSQL converts a CEL expression to SQL using the configured dialect.

type ConvertContext

type ConvertContext struct {
	Buffer strings.Builder
	Args   []any
	// The offset of the next argument in the condition string.
	// Mainly using for PostgreSQL.
	ArgsOffset int
}

func NewConvertContext

func NewConvertContext() *ConvertContext

type DatabaseType added in v0.25.0

type DatabaseType string

DatabaseType represents the type of database.

const (
	SQLite     DatabaseType = "sqlite"
	MySQL      DatabaseType = "mysql"
	PostgreSQL DatabaseType = "postgres"
)

type MySQLDialect added in v0.25.0

type MySQLDialect struct{}

MySQLDialect implements SQLDialect for MySQL.

func (*MySQLDialect) GetBooleanCheck added in v0.25.0

func (d *MySQLDialect) GetBooleanCheck(path string) string

func (*MySQLDialect) GetBooleanComparison added in v0.25.0

func (d *MySQLDialect) GetBooleanComparison(path string, value bool) string

func (*MySQLDialect) GetBooleanValue added in v0.25.0

func (*MySQLDialect) GetBooleanValue(value bool) interface{}

func (*MySQLDialect) GetCurrentTimestamp added in v0.25.0

func (*MySQLDialect) GetCurrentTimestamp() string

func (*MySQLDialect) GetJSONArrayLength added in v0.25.0

func (d *MySQLDialect) GetJSONArrayLength(path string) string

func (*MySQLDialect) GetJSONContains added in v0.25.0

func (d *MySQLDialect) GetJSONContains(path, _ string) string

func (*MySQLDialect) GetJSONExtract added in v0.25.0

func (d *MySQLDialect) GetJSONExtract(path string) string

func (*MySQLDialect) GetJSONLike added in v0.25.0

func (d *MySQLDialect) GetJSONLike(path, _ string) string

func (*MySQLDialect) GetParameterPlaceholder added in v0.25.0

func (*MySQLDialect) GetParameterPlaceholder(_ int) string

func (*MySQLDialect) GetTablePrefix added in v0.25.0

func (*MySQLDialect) GetTablePrefix() string

func (*MySQLDialect) GetTimestampComparison added in v0.25.0

func (d *MySQLDialect) GetTimestampComparison(field string) string

type PostgreSQLDialect added in v0.25.0

type PostgreSQLDialect struct{}

PostgreSQLDialect implements SQLDialect for PostgreSQL.

func (*PostgreSQLDialect) GetBooleanCheck added in v0.25.0

func (d *PostgreSQLDialect) GetBooleanCheck(path string) string

func (*PostgreSQLDialect) GetBooleanComparison added in v0.25.0

func (d *PostgreSQLDialect) GetBooleanComparison(path string, _ bool) string

func (*PostgreSQLDialect) GetBooleanValue added in v0.25.0

func (*PostgreSQLDialect) GetBooleanValue(value bool) interface{}

func (*PostgreSQLDialect) GetCurrentTimestamp added in v0.25.0

func (*PostgreSQLDialect) GetCurrentTimestamp() string

func (*PostgreSQLDialect) GetJSONArrayLength added in v0.25.0

func (d *PostgreSQLDialect) GetJSONArrayLength(path string) string

func (*PostgreSQLDialect) GetJSONContains added in v0.25.0

func (d *PostgreSQLDialect) GetJSONContains(path, _ string) string

func (*PostgreSQLDialect) GetJSONExtract added in v0.25.0

func (d *PostgreSQLDialect) GetJSONExtract(path string) string

func (*PostgreSQLDialect) GetJSONLike added in v0.25.0

func (d *PostgreSQLDialect) GetJSONLike(path, _ string) string

func (*PostgreSQLDialect) GetParameterPlaceholder added in v0.25.0

func (*PostgreSQLDialect) GetParameterPlaceholder(index int) string

func (*PostgreSQLDialect) GetTablePrefix added in v0.25.0

func (*PostgreSQLDialect) GetTablePrefix() string

func (*PostgreSQLDialect) GetTimestampComparison added in v0.25.0

func (d *PostgreSQLDialect) GetTimestampComparison(field string) string

type SQLDialect added in v0.25.0

type SQLDialect interface {
	// Basic field access
	GetTablePrefix() string
	GetParameterPlaceholder(index int) string

	// JSON operations
	GetJSONExtract(path string) string
	GetJSONArrayLength(path string) string
	GetJSONContains(path, element string) string
	GetJSONLike(path, pattern string) string

	// Boolean operations
	GetBooleanValue(value bool) interface{}
	GetBooleanComparison(path string, value bool) string
	GetBooleanCheck(path string) string

	// Timestamp operations
	GetTimestampComparison(field string) string
	GetCurrentTimestamp() string
}

SQLDialect defines database-specific SQL generation methods.

func GetDialect added in v0.25.0

func GetDialect(dbType DatabaseType) SQLDialect

GetDialect returns the appropriate dialect for the database type.

type SQLTemplate added in v0.25.0

type SQLTemplate struct {
	SQLite     string
	MySQL      string
	PostgreSQL string
}

SQLTemplate holds database-specific SQL fragments.

type SQLiteDialect added in v0.25.0

type SQLiteDialect struct{}

SQLiteDialect implements SQLDialect for SQLite.

func (*SQLiteDialect) GetBooleanCheck added in v0.25.0

func (d *SQLiteDialect) GetBooleanCheck(path string) string

func (*SQLiteDialect) GetBooleanComparison added in v0.25.0

func (d *SQLiteDialect) GetBooleanComparison(path string, value bool) string

func (*SQLiteDialect) GetBooleanValue added in v0.25.0

func (*SQLiteDialect) GetBooleanValue(value bool) interface{}

func (*SQLiteDialect) GetCurrentTimestamp added in v0.25.0

func (*SQLiteDialect) GetCurrentTimestamp() string

func (*SQLiteDialect) GetJSONArrayLength added in v0.25.0

func (d *SQLiteDialect) GetJSONArrayLength(path string) string

func (*SQLiteDialect) GetJSONContains added in v0.25.0

func (d *SQLiteDialect) GetJSONContains(path, _ string) string

func (*SQLiteDialect) GetJSONExtract added in v0.25.0

func (d *SQLiteDialect) GetJSONExtract(path string) string

func (*SQLiteDialect) GetJSONLike added in v0.25.0

func (d *SQLiteDialect) GetJSONLike(path, _ string) string

func (*SQLiteDialect) GetParameterPlaceholder added in v0.25.0

func (*SQLiteDialect) GetParameterPlaceholder(_ int) string

func (*SQLiteDialect) GetTablePrefix added in v0.25.0

func (*SQLiteDialect) GetTablePrefix() string

func (*SQLiteDialect) GetTimestampComparison added in v0.25.0

func (d *SQLiteDialect) GetTimestampComparison(field string) string

type TemplateDBType added in v0.25.0

type TemplateDBType string

TemplateDBType represents the database type for templates.

const (
	SQLiteTemplate     TemplateDBType = "sqlite"
	MySQLTemplate      TemplateDBType = "mysql"
	PostgreSQLTemplate TemplateDBType = "postgres"
)

Jump to

Keyboard shortcuts

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