https://github.com/marcinn/sqltemplate
SQL templating library
https://github.com/marcinn/sqltemplate
library python python3 sql template
Last synced: 19 days ago
JSON representation
SQL templating library
- Host: GitHub
- URL: https://github.com/marcinn/sqltemplate
- Owner: marcinn
- License: bsd-2-clause
- Created: 2017-02-10T09:34:25.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2023-04-21T20:25:35.000Z (almost 3 years ago)
- Last Synced: 2025-04-06T10:10:02.514Z (12 months ago)
- Topics: library, python, python3, sql, template
- Language: Python
- Size: 14.6 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sqltemplate
Database query tool based on SQL templates
## Development status
* Alpha
* API may be radically changed until first Beta release
* Not tested automatically yet
### Roadmap
* 0.6 - drop django-sqltemplate compatibility layer
* 0.7 - automated tests, first beta release
* 0.8 - stable API
* 0.9,<1.0 - minor improvements without API changes, bugfixes
* 1.0 - first stable release
## Introduction
SQL is a great and poweruful DSL, which is easeier in maintenance
when you're working on complext queries (i.e. reporting queries).
But the main problem of raw SQL is a commonly used "spaghetti" anti-pattern,
when you're embedding/building SQLs directly in your code.
The solution comes from templating SQLs idea and `sqltemplate`
is a simple implementation of it.
## Template system / database agnostic
The `sqltemplate` provides an abstraction layer for templating SQL
queries and working with query results. It requires an adapter to work.
For example - if you want to use `sqltemplate` together with Django
templates and database management, use [django-sqltemplate](https://github.com/marcinn/django-sqltemplate) adapter
directly.
## Backward compatibilty with `django-template`
The 0.5.x branch contains a compatibility layer with `django-template`.
The layer provides Django adapter and necessary imports.
## Building an adapter
Adapter must implement few callables required by `sqltemplate` core.
The adapter may be as a class instance or pure Python module providing
such functions. These functions are defined as:
`load_template(template_name)`
Must load template by name and return some kind of template object,
which will be passed to `render_template()` as an argument.
`create_template_from_string(string)`
Creates template object from provided string.
`render_template(template, context)`
Must render the template object using provided context (a dict).
`do_query(sql, query_params, using=None)`
Must execute a `sql` query with `query_params` applied.
Optional `using` argument specifies database connection identifier.
This may be any object dependent on how your adapter recognizes
particular connections. For Django adapter, for example, this is a
string with a connection name.
## Dependencies
* sqlparse
* flatdict
## License
BSD