Skip to main content

Python bindings for libpg_query — parse, deparse, and walk PostgreSQL SQL statements

Project description

pg-query-python

Python bindings for libpg_query — parse, deparse, and walk PostgreSQL SQL statements.

Ships pre-built wheels with the libpg_query shared library for Linux (x86_64, aarch64) and macOS (x86_64, arm64). No compiler required at install time.

Install

pip install pg-query-python

Usage

Parse SQL to a protobuf AST

from pg_query import parse

tree = parse("SELECT id, name FROM users WHERE active = true")
print(tree)

Deparse an AST back to SQL

from pg_query import parse, deparse

tree = parse("SELECT 1")
sql = deparse(tree)
assert sql == "SELECT 1"

Walk the AST

from pg_query import parse, walk

tree = parse("SELECT id FROM users JOIN orders ON users.id = orders.user_id")
for field_name, node in walk(tree):
    if type(node).DESCRIPTOR.name == "RangeVar":
        print(f"Table: {node.relname}")

Visitor pattern

from pg_query import parse, Visitor

class TableFinder(Visitor):
    def __init__(self):
        self.tables = []

    def visit_RangeVar(self, node):
        self.tables.append(node.relname)

tree = parse("SELECT * FROM users, orders")
finder = TableFinder()
finder.visit(tree)
print(finder.tables)  # ['users', 'orders']

Building from source

If no pre-built wheel is available for your platform, pip will build from source. You'll need git, make, and a C compiler:

pip install pg-query-python --no-binary pg-query-python

To target a specific libpg_query version:

LIBPG_QUERY_TAG=16-latest pip install pg-query-python --no-binary pg-query-python

License

BSD-3-Clause (this package) and BSD-3-Clause + PostgreSQL License (libpg_query).

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

pg_query_python-0.1.3.tar.gz (88.2 kB view details)

Uploaded Source

Built Distributions

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

pg_query_python-0.1.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pg_query_python-0.1.3-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

pg_query_python-0.1.3-cp313-cp313-macosx_11_0_arm64.whl (779.7 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pg_query_python-0.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

pg_query_python-0.1.3-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

pg_query_python-0.1.3-cp312-cp312-macosx_11_0_arm64.whl (779.7 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

pg_query_python-0.1.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

pg_query_python-0.1.3-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.2 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64

pg_query_python-0.1.3-cp311-cp311-macosx_11_0_arm64.whl (779.7 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

File details

Details for the file pg_query_python-0.1.3.tar.gz.

File metadata

  • Download URL: pg_query_python-0.1.3.tar.gz
  • Upload date:
  • Size: 88.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pg_query_python-0.1.3.tar.gz
Algorithm Hash digest
SHA256 6c540159954e20208a0be1a00d992d23d71d64aca5d65a215f814b6191ab3a54
MD5 349b92760e075f69b6a455d6562df2bf
BLAKE2b-256 5d0533c37ed4162fc4150e221e83c1d063ae6102fba4c0c291551da0b1286ffa

See more details on using hashes here.

Provenance

The following attestation bundles were made for pg_query_python-0.1.3.tar.gz:

Publisher: build.yml on trail-ml/pg-query-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pg_query_python-0.1.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pg_query_python-0.1.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 204ed67cd9582b091601ee2f874261f406b0838c2032edb1a1955ee0bce1824c
MD5 1fa57c3cd7c25a6a71708d222dc3930c
BLAKE2b-256 8ed44a18cd436369d446298abbeb6af52783dfb4511465a8197e0fb93ac978de

See more details on using hashes here.

Provenance

The following attestation bundles were made for pg_query_python-0.1.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build.yml on trail-ml/pg-query-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pg_query_python-0.1.3-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pg_query_python-0.1.3-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 23b6b6c89d2ab9416d9c14ba3a1934d1481e0ed6f20df41543b5e68f75b4d586
MD5 b5b443a60bd3f92deec199612a1d0c0a
BLAKE2b-256 6ef870b7a6712efb2d32105792a13381651df6f74e7bb36dce38c6330f0ed734

See more details on using hashes here.

Provenance

The following attestation bundles were made for pg_query_python-0.1.3-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: build.yml on trail-ml/pg-query-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pg_query_python-0.1.3-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pg_query_python-0.1.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0b25270edf2911cea6c2970dbd518cd927d82012334cf8931b08642d1fecbc05
MD5 b5e6d4a90efb025a62eff69be50cc613
BLAKE2b-256 4eebf8fddf81ccc77dd63dcf8a9063dc13d10fa35e9cbe1b8ac620df52ef6fd1

See more details on using hashes here.

Provenance

The following attestation bundles were made for pg_query_python-0.1.3-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: build.yml on trail-ml/pg-query-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pg_query_python-0.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pg_query_python-0.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a6499508c3fc86770d01d25f33453813a7030b856d7194fde58f762d04bc9ac3
MD5 2635e2265b801c07d7de4c574f623346
BLAKE2b-256 ae953d28099ea6b0648626bfe5ff8cff990e8ac83c60cf10e5a22ba4cd0c7cfc

See more details on using hashes here.

Provenance

The following attestation bundles were made for pg_query_python-0.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build.yml on trail-ml/pg-query-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pg_query_python-0.1.3-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pg_query_python-0.1.3-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 36356df6cfbf199d31cfc46a055fac96c81ed3139ad6f4f1cb7cbd31c45613f2
MD5 794da38f3982a498ca2b7cec3a310ba4
BLAKE2b-256 02ed7316d9c670abd99fe98f8c16cbe0fa93995f6eb0a2303c62ce9c06165303

See more details on using hashes here.

Provenance

The following attestation bundles were made for pg_query_python-0.1.3-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: build.yml on trail-ml/pg-query-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pg_query_python-0.1.3-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pg_query_python-0.1.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d16e46d328b1829537eb86ea54a138e2b2651a938f397390899b9eae62b5058b
MD5 35f32096c10f9d79f65d8202acae12c7
BLAKE2b-256 5459d4986f2ed323b1c1b83b3602b6178963812f1714bee418239847e45ca8e2

See more details on using hashes here.

Provenance

The following attestation bundles were made for pg_query_python-0.1.3-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: build.yml on trail-ml/pg-query-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pg_query_python-0.1.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pg_query_python-0.1.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 84f376ed80f5500cc423ffc5a4009175ad7de59dbd6eb64d77da7c981a694bb0
MD5 d5a846b42dcebf4a7b1bbd38b4bdb1bf
BLAKE2b-256 a4502977c1067e5dc7e290db119239bdc4a618c7282ec1107b9b6aa9050c6177

See more details on using hashes here.

Provenance

The following attestation bundles were made for pg_query_python-0.1.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build.yml on trail-ml/pg-query-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pg_query_python-0.1.3-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pg_query_python-0.1.3-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 6f9243d2c1fdc70b2e67d09530cda7db840b95c656c81798287ab1269e555745
MD5 b381e38fc385ee68f8e6b9e587a99383
BLAKE2b-256 8a6152805763218b05d889426a9d6fb791eba2c978a105d13f7caf26c462e46b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pg_query_python-0.1.3-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: build.yml on trail-ml/pg-query-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pg_query_python-0.1.3-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pg_query_python-0.1.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9dc4511ae6004408677bf81456343cf4f7db283d00b7ae6c3134913753622045
MD5 98cd26c1f31eab00c58fc827c1c29e74
BLAKE2b-256 bdb167d7773871ddbeb69790a2e678022c1157da2adce276bc54b8b084bd9498

See more details on using hashes here.

Provenance

The following attestation bundles were made for pg_query_python-0.1.3-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: build.yml on trail-ml/pg-query-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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