Skip to main content

A Django module for execute custom sql query in a .sql file

Project description

# django-query-execfile
**Django module for execute sql query in a file**

Full documentation for the project is available at []

---

**Note**:


---

# Overview

django-query-execfile is a simple library for execute .sql query file.
Especially


# Requirements

* Python (2.7.x )
* Django (1.5+ )

# Installation

Install using `pip`...

```shell
pip install django-query-execfile
```


# Usage & Example

This is content of a .sql file

```sql
#sum_customerPayment_amount_rate_exchanged_group_by_day
SELECT
DATE(customerPayment.created) AS created,
SUM(transaction.amount * transaction.exchange_rate) AS amount_exchanged
FROM shop_module_customerpayment customerPayment
LEFT JOIN shop_module_transaction transaction ON transaction.id = customerPayment.transaction_id
WHERE (customerPayment.created BETWEEN %(from_date)s AND %(to_date)s)
GROUP BY DATE(customerPayment.created);
```


Usage example

```python
from models import Order
from query_execfile import sql_execfile, raw_queryfile

result = sql_execfile('../datagrip/payment_transaction_stats.sql'),
params={'from_date': .., 'to_date': ..},
mapResultToDict=True,
includeDescription=True
)
print result

rawQuerySet = raw_queryfile(Order, '../datagrip/order_rawquery.sql')
for order in rawQuerySet:
doSomeThingWith(order)
```

**mapResultToDict=True** will map results to a dict with key = first comment above the query.
So you can write multiple query in a single file and execute these query at once.

**includeDescription=True** will add a extra row at first as column header using description from the query.
In this case is _AS created_ and _AS amount_exchanged_


**Result return of that example:**

```json
{"sum_customerPayment_amount_rate_exchanged_group_by_day": [
[
"created",
"amount_exchanged"
],
[
"2016-03-01",
4933000.0
],
[
"2016-03-02",
7144000.0
],
...
...
...
[
"2016-03-10",
2110000.0
]
],}
```


# Documentation & Support

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

django-query-execfile-0.4.tar.gz (4.3 kB view details)

Uploaded Source

File details

Details for the file django-query-execfile-0.4.tar.gz.

File metadata

File hashes

Hashes for django-query-execfile-0.4.tar.gz
Algorithm Hash digest
SHA256 b10c4a115e5a0c6c4327ce1f483626337170070c5c0229c621cdf65bec3620db
MD5 6fd359d9488721e0b9db9eaef6f940bf
BLAKE2b-256 c4356822e0458da2b6fde8d72bd485e630b0eeb269db127df0e32c1b4d04d16d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page