Skip to main content

Query building for the postgresql prepared statements and asyncpg.

Project description

BuildStatus Coverage pypi

Query building for the postgresql prepared statements and asyncpg.

Lots of more powerful features, including full clause construction, multiple values, logic functions, query pretty-printing and different variable substitution - below is just a very quick summary. Please check the code and tests for examples.

Building Queries

Simple variable substitution:

from buildpg import render

render('select * from mytable where x=:foo and y=:bar', foo=123, bar='whatever')
>> 'select * from mytable where x=$1 and y=$2', [123, 'whatever']

Use of V to substitute constants:

from buildpg import V, render

render('select * from mytable where :col=:foo', col=V('x'), foo=456)
>> 'select * from mytable where x=$1', [456]

Complex logic:

from buildpg import V, funcs, render

where_logic = V('foo.bar') == 123
if spam_value:
    where_logic &= V('foo.spam') <= spam_value

if exclude_cake:
    where_logic &= funcs.not_(V('foo.cake').in_([1, 2, 3]))

render('select * from foo :where', where=where_logic)
>> 'select * from foo foo.bar = $1 AND foo.spam <= $2 AND not(foo.cake in $3)', [123, 123, ['x', 'y']]

Values usage:

from buildpg import Values, render

render('insert into the_table (:values__names) values :values', values=Values(a=123, b=456, c='hello'))
>> 'insert into the_table (a, b, c) values ($1, $2, $3)', [123, 456, 'hello']

With asyncpg

As a wrapper around asyncpg:

import asyncio
from buildpg import asyncpg

async def main():
    async with asyncpg.create_pool_b('postgres://postgres@localhost:5432/db') as pool:
        await pool.fetchval_b('select spam from mytable where x=:foo and y=:bar', foo=123, bar='whatever')
        >> 42

asyncio.get_event_loop().run_until_complete(main())

Both the pool and connections have *_b variants of all common query methods:

  • execute_b

  • executemany_b

  • fetch_b

  • fetchval_b

  • fetchrow_b

  • cursor_b

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

buildpg-0.1.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

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

buildpg-0.1-py36.py37-none-any.whl (10.5 kB view details)

Uploaded Python 3.6Python 3.7

File details

Details for the file buildpg-0.1.tar.gz.

File metadata

  • Download URL: buildpg-0.1.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3

File hashes

Hashes for buildpg-0.1.tar.gz
Algorithm Hash digest
SHA256 8d322f9397f9c2f26ef122182b0ced2cb7539f5482474417c5f10de3fba62924
MD5 d5bc97c2647d6252ee5f32ab49ef2c36
BLAKE2b-256 bc8b60d1f6764c4e0e278cf1dbdc9711c1bf137e1b5974366281dc06612b5831

See more details on using hashes here.

File details

Details for the file buildpg-0.1-py36.py37-none-any.whl.

File metadata

  • Download URL: buildpg-0.1-py36.py37-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3.6, Python 3.7
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3

File hashes

Hashes for buildpg-0.1-py36.py37-none-any.whl
Algorithm Hash digest
SHA256 951d37966adfa6ef622b01abb57ae5fdf759fd95c66be0472180faa06a3bc36b
MD5 5920811569d01edfdf3b6a16670df226
BLAKE2b-256 721a37cae9cbd03eda62ab45bacb603ce1fce10a8cdaeda5a8d0c49f796a85ba

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