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
SELECTstatements 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 theQueryBuilder. - Query Construction: The
select()method specifies the columns to be retrieved (*means all columns), and thetable()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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c1fe627a27650d657f8cedddb8e026150679b4db858c8ea716e00ff416eb593d
|
|
| MD5 |
acaa4a64837344b0b6407c86c01ac801
|
|
| BLAKE2b-256 |
471553236cfcf012068bc457a167ba82009696da5f2600652e0433bc7d224996
|
File details
Details for the file psycopg2_query_builder-0.0.1-py3-none-any.whl.
File metadata
- Download URL: psycopg2_query_builder-0.0.1-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1ffcce59a9e7f3d43534c441c688acfdde77f504066e11226d1a32aa3cf60a40
|
|
| MD5 |
d6435433e721173d450dcc11b97e44f3
|
|
| BLAKE2b-256 |
ccdd525ba46afdcdbfc705992a445b427ce1caa6ced3eb1878a246b1e53f7f07
|