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.0.0.tar.gz (17.1 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.0.0-py3-none-any.whl (21.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for sqlark-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5429f660a5e333017ad4dfac742ae0fe09b7c354773101f21f727c8c275c7264
MD5 a13361dd7ce092a20f7ecbefc0bd8e27
BLAKE2b-256 c66550bfa88cc02d3cdede2d4e1f5703f8eb95d0d814e84f89c601e7fe0caa51

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlark-1.0.0.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.0.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for sqlark-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 62f58db3e5c7a38ed7383101373ac0ffec29dc6063ab01669fbb60d1f0ec9b3e
MD5 e2131177842777789d18bf75120ed591
BLAKE2b-256 0aeabc122bf518a9b9f3e39dc96c9b413e9568f1c355281e47887fc6dc2740cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlark-1.0.0-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