Metadata-Version: 2.4
Name: schema-query-utils
Version: 1.0.0
Summary: Reusable MSSQL utility package
Author: Moulidharan
Requires-Python: >=3.8
Description-Content-Type: text/markdown

# 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

```bash
pip install schema-query-utils
```

---

# Functions

## 1. `query_builder()`

Generates dynamic SQL queries.

### Supported Operations

* INSERT
* UPDATE
* SELECT

---

### Example

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

print(query)
```

### Output

```sql
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

```python
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:

```python
'YYYY-MM-DD'
```

Example:

```python
datetime(2026, 5, 22)
```

becomes:

```sql
'2026-05-22'
```

---

# Default Date Handling

If a value contains:

```python
1900-01-01
```

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

```sql
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.
