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.0.tar.gz (17.2 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.0-py3-none-any.whl (22.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sqlark-1.1.0.tar.gz
  • Upload date:
  • Size: 17.2 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.0.tar.gz
Algorithm Hash digest
SHA256 60bfff20a1a23d08afd74bdcbf5b5a3bcbf4f576e8426fedb00959f51a1c289e
MD5 d6e1a08cc47a036a4c7a6ccbe4cb988a
BLAKE2b-256 324687ecb25930e30d383ee299290cec7672f658a4c2d4d1d9c30bb4477f037b

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: sqlark-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 22.1 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d4fc7d5b660e11b4a10c83a0e99f1b3552f23a119fa4e66c5764207636242930
MD5 5313bdefdb6eb03c289ed077ee6b9642
BLAKE2b-256 e8d90154c3a2e545afb6f26d883b8880b69fdf4356ddcfb58cdec1e8fa906789

See more details on using hashes here.

Provenance

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