Skip to main content

PostgreSQL dialect plugin for SQLRules (regex, JSONB, ARRAY, range).

Project description

sqlrules-postgresql

PostgreSQL dialect plugin for SQLRules.

Install

pip install sqlrules-postgresql

Usage

import re
from typing import Annotated, Any

from pydantic import BaseModel, Field
from sqlalchemy import Column, MetaData, Table
from sqlalchemy.dialects.postgresql import ARRAY, INT4RANGE, JSONB, TEXT

from sqlrules import ArrayContains, Compiler, JsonContains, RangeContains
from sqlrules_postgresql import PostgresPlugin

class RowFilter(BaseModel):
    name: Annotated[str, Field(pattern=re.compile(r"^a", re.I))]
    meta: Annotated[dict[str, Any], JsonContains({"active": True})]
    tags: Annotated[list[str], ArrayContains(["admin"])]
    span: Annotated[int, RangeContains(5)]

table = Table(
    "rows",
    MetaData(),
    Column("name", TEXT),
    Column("meta", JSONB),
    Column("tags", ARRAY(TEXT)),
    Column("span", INT4RANGE),
)

compiler = Compiler(plugins=[PostgresPlugin()], dialect="postgresql")
rules = compiler.compile(RowFilter, table)

Operators

IR operator SQLAlchemy / PostgreSQL
pattern ~ or ~* (when PatternSpec.ignore_case)
type_check Shape/type predicates from TypeSpec (see matrix below)
json_contains JSONB contains / @>
json_has_key JSONB has_key / ?
array_contains array contains
array_overlap array overlap / &&
range_contains range @>
range_overlap range &&

type_check matrix (approximate)

Enable with Compiler(..., emit_type_checks=True). Not full Pydantic parity — inexpressible pairs raise.

Python type Lax Strict
int Integer column; String ~ digit pattern; numeric whole-number Integer column only
bool unsupported (raise) Boolean IN (true, false)
str String/Text IS NOT NULL same
float / Decimal numeric column; String float-ish ~ numeric column
date / datetime / time / UUID typed column or String format ~ typed column

Optional[T](column IS NULL) OR <predicate>.

Security note

pattern becomes a PostgreSQL regex (~ / ~*). Untrusted pattern strings can cause expensive engine-side evaluation (ReDoS-class cost). Prefer static/allowlisted patterns. See SECURITY.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlrules_postgresql-1.0.1.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlrules_postgresql-1.0.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file sqlrules_postgresql-1.0.1.tar.gz.

File metadata

  • Download URL: sqlrules_postgresql-1.0.1.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sqlrules_postgresql-1.0.1.tar.gz
Algorithm Hash digest
SHA256 f1d7f6c97fa21304104844cc358ba0f97fd18cf4a04d7d428da818749ee7718b
MD5 5881ebc50b8054b4503bc9becf4b356f
BLAKE2b-256 26b429a5e20537c4f98c09f6db5fe69ddd896d5d08e6dc3fb2f99487c67216ad

See more details on using hashes here.

File details

Details for the file sqlrules_postgresql-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlrules_postgresql-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c4493afa5e74a9a22e8ebd40728f4677040edc8dc12bdb9be465cf7db4d8bc8f
MD5 2267807771d8782cfc729006c88e839b
BLAKE2b-256 8269cac8b9acfba92459ab3ccab3e6b9417081563fc7abeaefe06e7ac55a6dc6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page