Skip to main content

A flexible SQL query builder for SQLAlchemy ORM models.

Project description

query-builder-sql-alchemy

A flexible SQL query builder for SQLAlchemy ORM models that makes it easy to build complex SQL queries using a fluent interface.

Features

  • 🚀 Fluent interface for building SQL queries
  • 🔍 Support for complex WHERE conditions with AND/OR grouping
  • 🔄 JOIN operations (INNER, LEFT, RIGHT, FULL, CROSS)
  • 📊 ORDER BY, LIMIT, and OFFSET support
  • 🛡️ Safe SQL string generation with proper escaping
  • 🎯 Compatible with SQLAlchemy ORM models
  • 📝 Type hints for better IDE support

Installation

Using pip:

pip install query-builder-sql-alchemy

Using Poetry:

poetry add query-builder-sql-alchemy

Quick Start

from QueryBuilderSqlAlchemy import QueryBuilder, Condition, ConditionGroup, Operator
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String

# Define your SQLAlchemy model
Base = declarative_base()

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    email = Column(String)

# Create a query builder
qb = QueryBuilder()

# Build a simple query
query = (qb
    .select([User.id, User.name])
    .from_table(User)
    .where(Condition(User.name, Operator.LIKE, 'John%'))
    .limit(10)
    .build())

print(query)
# Output: SELECT `users`.`id`, `users`.`name` FROM `users` WHERE `users`.`name` LIKE 'John%' LIMIT 10

Advanced Usage

Complex Conditions

# Using AND/OR conditions
query = (qb
    .select([User.id, User.name, User.email])
    .from_table(User)
    .where(
        ConditionGroup([
            Condition(User.name, Operator.LIKE, 'John%'),
            Condition(User.email, Operator.LIKE, '%@gmail.com')
        ], 'AND')
    )
    .build())

Joins

class Post(Base):
    __tablename__ = 'posts'
    id = Column(Integer, primary_key=True)
    user_id = Column(Integer)
    title = Column(String)

# Join query
query = (qb
    .select([User.name, Post.title])
    .from_table(User)
    .join(User.id, Post.user_id)
    .build())

Aggregations

query = (qb
    .select([(User.id, 'COUNT')])
    .from_table(User)
    .where(Condition(User.name, Operator.LIKE, 'John%'))
    .build())

Available Operators

  • EQ: Equals (=)
  • NE: Not Equals (<>)
  • LT: Less Than (<)
  • LE: Less Than or Equal (<=)
  • GT: Greater Than (>)
  • GE: Greater Than or Equal (>=)
  • LIKE: LIKE operator
  • BETWEEN: Between two values
  • IN: IN operator

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License - see the LICENSE file for details.

Author

Support

If you encounter any issues or have questions, please file an issue on the GitHub repository.

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

query_builder_sql_alchemy-0.1.2.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

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

query_builder_sql_alchemy-0.1.2-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file query_builder_sql_alchemy-0.1.2.tar.gz.

File metadata

  • Download URL: query_builder_sql_alchemy-0.1.2.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.1 Windows/11

File hashes

Hashes for query_builder_sql_alchemy-0.1.2.tar.gz
Algorithm Hash digest
SHA256 c8afaae3c3e491bb0a4dbeb17d7fb370b2115c725606825b6d3e51724e238a53
MD5 5cdd01d50406a607dfa7cdbd59cac8ba
BLAKE2b-256 e02b9c9152bec03c942f331d939e0b00bce877ef5602bd38f1520408ee3a54b9

See more details on using hashes here.

File details

Details for the file query_builder_sql_alchemy-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for query_builder_sql_alchemy-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c100661374932527f00cd04818a441451f9807e415ab629577308a24f37bd3e1
MD5 fad648e293e90acb4e93b1f84f84d5b4
BLAKE2b-256 d08a1b27a1b58c2126458a8e667a179282803720747d8f60b5b6991d327d2d4c

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