Skip to main content

Sql/Postgresql query builder, inspired by the Supabase client SDK functions.

Project description

QueryBuilder for Psycopg2

QueryBuilder is a lightweight Python package designed to simplify building SQL queries when using the Psycopg2 library to interact with PostgreSQL databases. Inspired by Supabase's client SDKs, QueryBuilder aims to provide an intuitive and fluid API for constructing and executing SQL queries.

Features

  • Chainable methods for building SQL queries in a readable and maintainable manner.
  • Support for SELECT statements with conditional filters.
  • Automatic mapping of query results to a list of dictionaries for easier data manipulation.
  • Designed to prevent common SQL query construction errors.

Installation

You can install QueryBuilder using pip:

pip install psycopg2-query-builder

Usage

Here's a quick example of how to use the QueryBuilder in your project:

from internal.database.db import QueryBuilder
from dotenv import dotenv_values
import psycopg2

# Load environment variables
env_config = dotenv_values(".env")

# Establish a database connection
database_connection = psycopg2.connect(
    database=env_config.get("DB_NAME"),
    host=env_config.get("DB_HOST"),
    user=env_config.get("DB_USER"),
    password=env_config.get("DB_PASSWORD"),
    port=env_config.get("DB_PORT"),
)

# Function to get a database cursor
db_curser = database_connection.cursor()

# Create a QueryBuilder instance
query = QueryBuilder(db_curser)

# Build and execute a query
res = (
    query.select("*")
    .equal("id", "uuid")
    .equal("email", "test@gmail.com")
    .execute()
)

# Print the result
print(res)

res = (
    query.select(["id", "email", "hashed_password"])
    .equal("id", "uuid")
    .execute()
)

print(res)

Explanation:

  • Database Connection: A connection to the PostgreSQL database is established using Psycopg2.
  • Cursor Function: The _cursor() function returns a new database cursor, which is passed to the QueryBuilder.
  • Query Construction: The select() method specifies the columns to be retrieved (* means all columns), and the table() method specifies the table to query (users).
  • Query Execution: The execute() method runs the query and returns the results as a list of dictionaries.

License

This project is licensed under the MIT License.

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

psycopg2-query-builder-0.0.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

psycopg2_query_builder-0.0.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file psycopg2-query-builder-0.0.1.tar.gz.

File metadata

  • Download URL: psycopg2-query-builder-0.0.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.6

File hashes

Hashes for psycopg2-query-builder-0.0.1.tar.gz
Algorithm Hash digest
SHA256 c1fe627a27650d657f8cedddb8e026150679b4db858c8ea716e00ff416eb593d
MD5 acaa4a64837344b0b6407c86c01ac801
BLAKE2b-256 471553236cfcf012068bc457a167ba82009696da5f2600652e0433bc7d224996

See more details on using hashes here.

File details

Details for the file psycopg2_query_builder-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for psycopg2_query_builder-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1ffcce59a9e7f3d43534c441c688acfdde77f504066e11226d1a32aa3cf60a40
MD5 d6435433e721173d450dcc11b97e44f3
BLAKE2b-256 ccdd525ba46afdcdbfc705992a445b427ce1caa6ced3eb1878a246b1e53f7f07

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