Skip to main content

A Python library for dynamically generating SQL queries.

Project description

Pysqlizer

Pysqlizer is a Python library for dynamically generating SQL queries based on tuple length and optimizing query generation efficiency.

Installation

Install via pip:

pip install pysqlizer

Usage

sqlify

Function Signature:

sqlify(keyColumn: str, data: list|tuple|int|str|dict|set|empty_data, raiseException: bool = False, how: str = "IN", condition: str = "OR") -> str

Parameters:

  • keyColumn (str): The column name on which the condition will be applied.
  • data (list): List of values to be used in the query.
  • raiseException (bool, optional): If True, raises an error for an empty data list. Default is False.
  • how (str, optional): SQL condition type (IN, NOT IN, LIKE, NOT LIKE). Default is IN.
  • condition (str, optional): Logical condition between multiple statements (AND, OR). Default is OR.

Examples:

from pysqlizer import sqlify

# Case 1: Basic IN condition
print(sqlify("product_id", [101, 102, 103]))
# Output: "product_id IN (101, 102, 103)"

# Case 2: NOT IN condition
print(sqlify("category_id", [5, 6], how="NOT IN"))
# Output: "category_id NOT IN (5, 6)"

# Case 3: LIKE condition
print(sqlify("product_name", ["phone", "laptop"], how="LIKE", condition="AND"))
# Output: "(product_name LIKE '%phone%' AND product_name LIKE '%laptop%')"

# Case 4: NOT LIKE condition
print(sqlify("product_name", ["tablet", "watch"], how="NOT LIKE", condition="OR"))
# Output: "(product_name NOT LIKE '%tablet%' OR product_name NOT LIKE '%watch%')"

# Case 5: Empty list without exception
print(sqlify("user_id", [], how="IN"))
# Output: "(1=0)" (safe fallback for empty lists)

# Case 6: Empty list with exception
print(sqlify("user_id", [], raiseException=True))
# Raises ValueError: "data list cannot be empty when raiseException=True"

sqlifylike

Function Signature:

sqlifylike(keyColumn: str, data: list|tuple|int|str|dict|set|empty_data, how: str = "LIKE", condition: str = "OR") -> str

Parameters:

  • keyColumn (str): The column name on which the condition will be applied.
  • data (list): List of values to be used in the LIKE conditions.
  • how (str, optional): SQL condition type (LIKE or NOT LIKE). Default is LIKE.
  • condition (str, optional): Logical condition between multiple statements (AND, OR). Default is OR.

Examples:

from pysqlizer import sqlifylike

# Case 1: LIKE condition with OR
print(sqlifylike("product_name", ["phone", "laptop"]))
# Output: "(product_name LIKE '%phone%' OR product_name LIKE '%laptop%')"

# Case 2: LIKE condition with AND
print(sqlifylike("product_name", ["phone", "laptop"], condition="AND"))
# Output: "(product_name LIKE '%phone%' AND product_name LIKE '%laptop%')"

# Case 3: NOT LIKE condition with OR
print(sqlifylike("product_name", ["tablet", "watch"], how="NOT LIKE"))
# Output: "(product_name NOT LIKE '%tablet%' OR product_name NOT LIKE '%watch%')"

# Case 4: NOT LIKE condition with AND
print(sqlifylike("product_name", ["tablet", "watch"], how="NOT LIKE", condition="AND"))
# Output: "(product_name NOT LIKE '%tablet%' AND product_name NOT LIKE '%watch%')"

License

MIT License

Copyright (c) 2025 Akash Kushwaha

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

(Full MIT license text here)

Author

Contributions

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

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

pysqlizer-1.1.0.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

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

pysqlizer-1.1.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file pysqlizer-1.1.0.tar.gz.

File metadata

  • Download URL: pysqlizer-1.1.0.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for pysqlizer-1.1.0.tar.gz
Algorithm Hash digest
SHA256 a99fad477c40b462ae10acdf23c80d7d9935f070635974b9a6a374b8a15ba9ba
MD5 ac8c59417d94bf3724a81db1bc20ae68
BLAKE2b-256 e93156001d3965f06897cbe5df100218311a1a214c1fa0e1ab70e1bcff0f23e8

See more details on using hashes here.

File details

Details for the file pysqlizer-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: pysqlizer-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for pysqlizer-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f703dbdfbfb6ac263dee185ba16e86106869891eafe3d17b1f51973ab7d3adf7
MD5 b00b47fd8266001509cbbce542bfba08
BLAKE2b-256 6b1af09966428b2e5582e3a53f5621ee0a01a120ee48b11de8f732a9122755b7

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