Skip to main content

Reusable MSSQL utility package

Project description

MSSQL Dynamic Query Builder & Schema Validator

A lightweight Python utility package for dynamically generating SQL queries and validating table data using Microsoft SQL Server schema metadata.


Features

  • Dynamic INSERT query generation
  • Dynamic UPDATE query generation
  • Dynamic SELECT query generation
  • Automatic NULL handling
  • SQL string escaping
  • Datetime formatting (YYYY-MM-DD)
  • Schema-based datatype validation
  • Default datatype value handling
  • MSSQL compatible

Installation

pip install schema-query-utils

Functions

1. query_builder()

Generates dynamic SQL queries.

Supported Operations

  • INSERT
  • UPDATE
  • SELECT

Example

query = query_builder(
    "insert",
    "database.schema.Employee",
    {
        "name": "John",
        "salary": 5000
    }
)

print(query)

Output

INSERT INTO database.schema.Employee
(name, salary)
VALUES ('John', 5000)

2. get_table_schema()

Validates and formats input data based on SQL table schema.

Features

  • Converts datetime objects to string format
  • Converts empty/default values to NULL
  • Applies datatype-based defaults
  • Dynamically validates data using INFORMATION_SCHEMA

Example

data = {
    "name": "John",
    "created_date": datetime.now(),
    "salary": None
}

cleaned_data = get_table_schema(
    conn,
    "database_name",
    "dbo",
    "Employee",
    data
)

print(cleaned_data)

NULL Handling

The package automatically converts:

Input Value SQL Value
None NULL
'None' NULL
'null' NULL
'' NULL

Datetime Handling

Python datetime objects are automatically converted to:

'YYYY-MM-DD'

Example:

datetime(2026, 5, 22)

becomes:

'2026-05-22'

Default Date Handling

If a value contains:

1900-01-01

and the column allows NULL, the value will automatically become:

NULL

Supported Datatypes

SQL Datatype Default Value
int 0
bigint 0
smallint 0
tinyint 0
decimal 0.0
numeric 0.0
float 0.0
varchar ''
date '1900-01-01'
datetime '1900-01-01'

Requirements

  • Python 3.8+
  • Microsoft SQL Server
  • pyodbc / pymssql compatible

Security Note

This package generates raw SQL strings dynamically.

It is recommended to use:

  • trusted input sources
  • internal systems
  • controlled environments

Avoid directly passing untrusted user input without validation.


License

MIT License

Maintainer

Developed and maintained by Moulidharan.

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

schema_query_utils-1.0.0.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

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

schema_query_utils-1.0.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file schema_query_utils-1.0.0.tar.gz.

File metadata

  • Download URL: schema_query_utils-1.0.0.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for schema_query_utils-1.0.0.tar.gz
Algorithm Hash digest
SHA256 fd104dcb709ec05f64870867eb4def4d11d8dfad01df1d511b4254f54e6b431f
MD5 52f5e1d8a37d57bc7c9398f78a56bcee
BLAKE2b-256 c45de229e6604c705c18cbefe636e6715deb2c409ec5089af62caccf2c9ee916

See more details on using hashes here.

File details

Details for the file schema_query_utils-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for schema_query_utils-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 037102d98b3ae1280ebfea53b8bcb0c689b3007d64ca9bbc81b21999665a284c
MD5 651acb75ecab8ae64df8da0a8b96a11c
BLAKE2b-256 4f9d0974fdb07324b7a41647c2e4b0b4f0d36b2278bb99dfbc6f1cfab50159e0

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