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.3.tar.gz (7.2 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.3-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: query_builder_sql_alchemy-0.1.3.tar.gz
  • Upload date:
  • Size: 7.2 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.3.tar.gz
Algorithm Hash digest
SHA256 a89ec9e9e919a85f344dc6f4c3584403fec4bfbdd422c20f50176dc089cb8034
MD5 f67582092913ecb4f439f0d05fe3d182
BLAKE2b-256 c0a9c2162af677b241f289b45357a0d5dd237f1c9df6cb25958cf4b92cb6ebe0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for query_builder_sql_alchemy-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2116b7f20c5ea1dc876e209559984cb36b17303fb8d6f7b061a86d94b37ae80c
MD5 75d35209d28342f4c3315cc78c112a33
BLAKE2b-256 2e2d411a66a2cc1e064dd905bda0cad998c7cb6cf285278395ff303735ad3b57

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