Skip to main content

Parses queries from an sql file, and turns them into callable f-strings. Also a file cache decorator for slow queries and multi-session permanence, supports async functions.

Project description

PyQuickSQL

For a more thorough explanation see example.md
To install: pip install qsql

How to use the query loader:

import quicksql as qq
queries = qq.LoadSQL('path/to/queries.sql')

Printing the queries should give you something like this:

LoadSQL('path/to/queries.sql')
Query Name: examplequery_1, Params: order_avg, num_orders
Query Name: examplequery_2, Params: sdate, edate, product_id

These are callable objects that return the string given the non-optional **params.
They are equivalent to an f-string + a lambda but loaded from an sql file and stored in the LoadSQL object.
print(str(queries)) will give you the raw SQL string.

How to use them:

print(queries.examplequery_2(
    product_id=10,
    sdate='1-10-2022',
    edate=qq.NoStr("DATE'4-11-2023'"),
    something_not_a_param='test'))
Unused variables: something_not_a_param in query examplequery_2

Above will be printed as a warning with invalid inclusions, no non-verbose option yet.

SELECT
  c.CustomerName,
  o.OrderDate,
  o.Status,
  (SELECT SUM(od.Quantity * od.UnitPrice) FROM OrderDetails od WHERE od.OrderID = o.OrderID) AS TotalValue
FROM
  Customers c
INNER JOIN Orders o ON c.CustomerID = o.CustomerID
WHERE
  o.OrderDate BETWEEN '1-10-2022' AND DATE'4-11-2023'
  AND EXISTS (SELECT 1 FROM OrderDetails od WHERE od.OrderID = o.OrderID AND od.ProductID = 10)
ORDER BY
  TotalValue DESC;

How to use the file cache:

This is very similar to functool's cache, with the main difference being that @qq.file_cache caches the asset to memory and to your system's default temporary directory. If the memory cache ever fails (eg a restarted kernel) it will load the asset from it's pickled file.

from random import randint
import quicksql as qq
@qq.file_cache()
def test_mem(size:int):
    return [randint(0,10) for _ in range(size)]

print(test_mem(8))

[10, 3, 4, 9, 2, 2, 4, 2]

print(test_mem(8))

[10, 3, 4, 9, 2, 2, 4, 2]

To clear the cache: qq.clear_cache()

For more examples and how to configure see example.ipynb

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

qsql-1.0.2.1.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

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

qsql-1.0.2.1-py2.py3-none-any.whl (6.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file qsql-1.0.2.1.tar.gz.

File metadata

  • Download URL: qsql-1.0.2.1.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for qsql-1.0.2.1.tar.gz
Algorithm Hash digest
SHA256 7a6f76c499e08fcefbaadeac514fa7dc70e83d338fc21fc17ace811665d4fe27
MD5 3c6b86e321af22a1a7949e0a7a003917
BLAKE2b-256 34a95e0a43ff8a73c05249b89c08cb4b412783cef3403492b420a238ad3040cb

See more details on using hashes here.

File details

Details for the file qsql-1.0.2.1-py2.py3-none-any.whl.

File metadata

  • Download URL: qsql-1.0.2.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for qsql-1.0.2.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 518196662d2aa3cd2db58fd2f141c3712b867633fc3cda274b4c6d8df20c072f
MD5 0bf89b244bc0898c03751b4fb0f263f8
BLAKE2b-256 f096a322157b1a50fde9dc57cd7574d1e29d7d13c2cc041d48fa2a6fe748a436

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