Wraps string building of MS SQL queries
Project description
SqlQueryBuilder
Wraps string building of MS SQL queries. It provides first level validation.
Current features
- Provides a string building facility for:
- Table
- Temporary Table
- Field
- Function on field
- String
- Using:
- Select
- Update
- From
- InnerJoin
- Where
- OrderBy
- Handling automatically:
- Table aliases
- Field aliases
Not implemented (yet)
- Insert
- Compound (sub-)queries
- Functions
- Proper Conditions
What it does not do
Connect to a database server and run the queries by itself.
It gives you the query as a string, it's then up to yopu to pass it to a SQL engine.
Example
- Define the elements of your query:
# Defines a Table for customers
customers_table = Table("Customers").add("id", "customerId", "mainPhone")
# Defines a Table for accounts
accounts_table = Table("Accounts", "acc").add("id", "customerId", "accountId")
# Selects the fields
select = Select(customers_table, accounts_table.fields.accountId)
# Sets an alias for an existing field
customers_table.fields.customerId.set_alias("cid")
# Specifies the table to takes as a FROM
from_ = From(customers_table)
# Specifies a temporary table for the select
atemptable = select.to_temptable("a_temp_table")
# Defines the InnerJoin
innerjoin = InnerJoin(accounts_table, accounts_table.fields.customerId, customers_table)
# Defines an Order By a defined field
orderby = OrderBy(accounts_table.fields.accountId)
# Adds a Where filter
where = Where(accounts_table.fields.accountId, "gt 20000 AND", customers_table.fields.mainPhone, "LIKE 001%")
- Inspect what those elements are made of:
# See what the object Select looks like
print(select)
# > Select(fields=[Field(name='id', alias='None', table='Customers'),
# ... Field(name='customerId', alias='cid', table='Customers'),
# ... Field(name='mainPhone', alias='None', table='Customers'),
# ... Field(name='accountId', alias='None', table='Accounts')])
# Initializes the QueryBuilder
query_builder = QueryBuilder(select, where, orderby, innerjoin, from_)
- Build your query:
# Builds the query
query_str: str = query_builder.build()
print(query_str)
# > SELECT Customers.id, Customers.customerId, Customers.mainPhone, acc.accountId
# > INTO #a_temp_table
# > FROM Customers
# > INNER JOIN Accounts acc on acc.customerId = cid
# > WHERE acc.accountId gt 20000 AND cid LIKE 'A1C%'
# > ORDER BY cid DESC
- Manipulate temporary tables which components are inferred:
# Is able to infer fields from temporary tables and use it as with a regular table.
temp_select = Select(atemptable)
print(temp_select)
# > Select(fields=[Field(name='id', alias='None', table='#a_temp_table'),
# ... Field(name='customerId', alias='None', table='#a_temp_table'),
# ... Field(name='mainPhone', alias='None', table='#a_temp_table'),
# ... Field(name='accountId', alias='None', table='#a_temp_table')])
# Builds the query from the temporary table
print(QueryBuilder(
temp_select,
From(atemptable)
).build())
# > SELECT #a_temp_table.id, #a_temp_table.customerId, #a_temp_table.mainPhone, #a_temp_table.accountId
# > FROM #a_temp_table
(Find it directly in the module example.py)
Compatible versions
Python 3.7 and above.
Dependencies
(None)
Authors
Wael GRIBAA Contact: g.wael@outlook.fr Made in September 2024
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sql_querystring_builder-0.0.3.post1.tar.gz.
File metadata
- Download URL: sql_querystring_builder-0.0.3.post1.tar.gz
- Upload date:
- Size: 47.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9b5c46ebd0aafc866ddd84a66727781742d12ad4af3e1952472bf6cf531e4606
|
|
| MD5 |
662d3dad33895436db9192cd9f5f96fe
|
|
| BLAKE2b-256 |
0a4d2c0ab953ceedc7ac527adc65f54d00190b3354d0ede796e293fda648b03e
|
File details
Details for the file sql_querystring_builder-0.0.3.post1-py3-none-any.whl.
File metadata
- Download URL: sql_querystring_builder-0.0.3.post1-py3-none-any.whl
- Upload date:
- Size: 32.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d63b3650a95236d6c6d212b76116f3382192e40c6b58065cd3c8d7cc9c4e3e6
|
|
| MD5 |
b0d0341f0e46483f5fe58417d38878a1
|
|
| BLAKE2b-256 |
8740f1c8bd419f2a01f9aac3bb854f2dddf1b168225ecf2288156055aadfc51c
|