psycopg2-query-builder


Namepsycopg2-query-builder JSON
Version 0.0.1 PyPI version JSON
download
home_pageNone
SummarySql/Postgresql query builder, inspired by the Supabase client SDK functions.
upload_time2024-08-16 15:12:32
maintainerNone
docs_urlNone
authorMiso Menze
requires_pythonNone
licenseNone
keywords python sql postgresql query builder database
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# 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](https://supabase.com/), 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:

```bash
pip install psycopg2-query-builder
```

## Usage

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

```python
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.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "psycopg2-query-builder",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "python, sql, postgresql, query, builder, database",
    "author": "Miso Menze",
    "author_email": "<misomenze6@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/47/15/53236cfcf012068bc457a167ba82009696da5f2600652e0433bc7d224996/psycopg2-query-builder-0.0.1.tar.gz",
    "platform": null,
    "description": "\n# QueryBuilder for Psycopg2\n\nQueryBuilder is a lightweight Python package designed to simplify building SQL queries when using the Psycopg2 library\nto interact with PostgreSQL databases. Inspired by [Supabase's client SDKs](https://supabase.com/), QueryBuilder aims to\nprovide an intuitive and fluid API for constructing and executing SQL queries.\n\n## Features\n\n- Chainable methods for building SQL queries in a readable and maintainable manner.\n- Support for `SELECT` statements with conditional filters.\n- Automatic mapping of query results to a list of dictionaries for easier data manipulation.\n- Designed to prevent common SQL query construction errors.\n\n## Installation\n\nYou can install QueryBuilder using pip:\n\n```bash\npip install psycopg2-query-builder\n```\n\n## Usage\n\nHere's a quick example of how to use the QueryBuilder in your project:\n\n```python\nfrom internal.database.db import QueryBuilder\nfrom dotenv import dotenv_values\nimport psycopg2\n\n# Load environment variables\nenv_config = dotenv_values(\".env\")\n\n# Establish a database connection\ndatabase_connection = psycopg2.connect(\n    database=env_config.get(\"DB_NAME\"),\n    host=env_config.get(\"DB_HOST\"),\n    user=env_config.get(\"DB_USER\"),\n    password=env_config.get(\"DB_PASSWORD\"),\n    port=env_config.get(\"DB_PORT\"),\n)\n\n# Function to get a database cursor\ndb_curser = database_connection.cursor()\n\n# Create a QueryBuilder instance\nquery = QueryBuilder(db_curser)\n\n# Build and execute a query\nres = (\n    query.select(\"*\")\n    .equal(\"id\", \"uuid\")\n    .equal(\"email\", \"test@gmail.com\")\n    .execute()\n)\n\n# Print the result\nprint(res)\n\nres = (\n    query.select([\"id\", \"email\", \"hashed_password\"])\n    .equal(\"id\", \"uuid\")\n    .execute()\n)\n\nprint(res)\n```\n\n### Explanation:\n\n- **Database Connection:** A connection to the PostgreSQL database is established using Psycopg2.\n- **Cursor Function:** The `_cursor()` function returns a new database cursor, which is passed to the `QueryBuilder`.\n- **Query Construction:** The `select()` method specifies the columns to be retrieved (`*` means all columns), and\n  the `table()` method specifies the table to query (`users`).\n- **Query Execution:** The `execute()` method runs the query and returns the results as a list of dictionaries.\n\n## License\n\nThis project is licensed under the MIT License.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Sql/Postgresql query builder, inspired by the Supabase client SDK functions.",
    "version": "0.0.1",
    "project_urls": null,
    "split_keywords": [
        "python",
        " sql",
        " postgresql",
        " query",
        " builder",
        " database"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ccdd525ba46afdcdbfc705992a445b427ce1caa6ced3eb1878a246b1e53f7f07",
                "md5": "d6435433e721173d450dcc11b97e44f3",
                "sha256": "1ffcce59a9e7f3d43534c441c688acfdde77f504066e11226d1a32aa3cf60a40"
            },
            "downloads": -1,
            "filename": "psycopg2_query_builder-0.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d6435433e721173d450dcc11b97e44f3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 6024,
            "upload_time": "2024-08-16T15:12:30",
            "upload_time_iso_8601": "2024-08-16T15:12:30.930610Z",
            "url": "https://files.pythonhosted.org/packages/cc/dd/525ba46afdcdbfc705992a445b427ce1caa6ced3eb1878a246b1e53f7f07/psycopg2_query_builder-0.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "471553236cfcf012068bc457a167ba82009696da5f2600652e0433bc7d224996",
                "md5": "acaa4a64837344b0b6407c86c01ac801",
                "sha256": "c1fe627a27650d657f8cedddb8e026150679b4db858c8ea716e00ff416eb593d"
            },
            "downloads": -1,
            "filename": "psycopg2-query-builder-0.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "acaa4a64837344b0b6407c86c01ac801",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 5259,
            "upload_time": "2024-08-16T15:12:32",
            "upload_time_iso_8601": "2024-08-16T15:12:32.295822Z",
            "url": "https://files.pythonhosted.org/packages/47/15/53236cfcf012068bc457a167ba82009696da5f2600652e0433bc7d224996/psycopg2-query-builder-0.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-16 15:12:32",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "psycopg2-query-builder"
}
        
Elapsed time: 0.33171s