Skip to main content

A lightweight Non-ORM with a builder pattern for querying a postgres database

Project description

Query Builder

A lightweight wrapper around psycopg2 for creating queries using a builder pattern.

Example Query:

Getting Started

Installation

Using pip

pip install git+https://github.com/MeadowlarkEngineering/query-builder.git#egg=query-builder

Using poetry

poetry add git+https://github.com/MeadowlarkEngineering/query-builder.git

Usage

Select

Select everything from a table.

from query_builder import PostgresConfig, Select
config = PostgresConfig(dbname="blog", user="postgres-username", password="postgres-password)
Select("comments").execute(config)
> [{'id': 1, 'text': "A comment"}]

Add joins, multiple where clauses, offsets, and limits.

s = Select(table_name="posts").\
    join(left_table="posts", right_table="authors", left_col="author_id", right_col="id").\
    where(table="authors", column="id", operator="=", value=1).\
    where_and(table="authors", column="created_at", operator=">", value="2024-01-01").\
    where_and(table="authors", column="created_at", operator=">", value="2024-01-01").\
    limit(10).\
    offset(5).\
    order_by("created_at", table="posts")
result = s.execute(config)

Insert data

i = Insert(table_name="posts").values([{"author_id": 1, "body": "this is a post"}]).on_conflict('id', 'update')
result = i.execute(config)

Development

pip3 install --upgrade pip poetry
poetry self add "poetry-dynamic-versioning[plugin]"
poetry install

Unit Testing

Start a postgres database. The tests don't modify the database, but do require an active psycopg2 connection.

docker run -d -e POSTGRES_PASSWORD=postgres -p 5432:5432 postgres

Run the unit tests

poetry run pytest

You may need to set PGUSER, PGDATABASE, and PGPASSWORD environment variables to establish the connection

Copyright 2024 - Meadowlark Engineering

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

sqlark-1.1.1.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

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

sqlark-1.1.1-py3-none-any.whl (22.5 kB view details)

Uploaded Python 3

File details

Details for the file sqlark-1.1.1.tar.gz.

File metadata

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

File hashes

Hashes for sqlark-1.1.1.tar.gz
Algorithm Hash digest
SHA256 6900c7f440d8864a2294eabbbb420b1babfcf0ccbbda260c275fef70f721b989
MD5 be2a8f4421a86750bcab302f646c497e
BLAKE2b-256 9b8109dab68167da3471add5c2b9e4bd94a76f42b949793605ce84e0ef1764ef

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlark-1.1.1.tar.gz:

Publisher: pypi.yaml on MeadowlarkEngineering/sqlark

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

File details

Details for the file sqlark-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: sqlark-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 22.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sqlark-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 21f5fae70b49ddb68a1c6122ee18afbc99ef18290b1f2c8105a68315a6ab39ab
MD5 266ff4f32c9c64ebcc5916bcff9a8d28
BLAKE2b-256 17c298731f02e50e6a3da4cf5beb0f73c114cfefef37121d276ae111a74848ca

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlark-1.1.1-py3-none-any.whl:

Publisher: pypi.yaml on MeadowlarkEngineering/sqlark

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