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, 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.0.2.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.0.2-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pysqlizer-1.0.2.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.0.2.tar.gz
Algorithm Hash digest
SHA256 8578b53500d14f2a6bd2901671ae6d1b4cfd7d3e47e549f7ba17eaf2c412e49d
MD5 0335dec8e6c13a39acdcc2b073247343
BLAKE2b-256 b90cc62b3c811d1296e0324327fb1f03a31707532eec0c69d90826f950bab0a6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pysqlizer-1.0.2-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.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6966e68a58d5f46a8d0f38b5cd3eeeb9884838a9c0b4254abb7dbbb433310420
MD5 60f4743f06d289582feaa65bddd5e51b
BLAKE2b-256 1d50c8b06be856c9ae1334cc7af6e6aa01f57667780155e445f27932e2a61e87

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