Skip to main content

License

pysqltemplate

The pysqltemplate package (sqltemplate module) is a Python library that provides an object-oriented abstraction for SQL SELECT queries. The objects are composable, extendable, and flexible. The first production use of pysqltemplate was in maintaining a library of complex queries that shared significant pieces but differed in their intent, and pysqltemplate allowed the shared pieces to maintained centrally for easy modification as criteria changed.

pysqltemplate is not an Object-Relational Mapping (ORM) of a database model. Instead, it would be better described as an object-oriented SQL builder and templating system. There are core concepts of the Query, a Clause (which refers to each of the parts of a SQL query), and a Phrase (one part of a clause). However, as with SQL statements, some of these are interoperable at times (though not in every case):

  • A Query can become a Prhase with the FROM (Table) Clause
  • A Query can often be transformed into an EXISTS phrase programmatically
  • The GROUP BY clause can often be autodetermined from the SELECT clause

templates

pysqltemplate itself comes with a rather unsophisticated templating system for reusing SQL phrases with some alteration. Simply, any pysqltemplate object has a template_values property, which can map a set of alphanumeric keys to values to apply whenever SQL is requested. The templates are defined in the phrasal text by surrounded it in '@', and possibly providing additional information. For example:

persontbl = sqltemplate.TableClause('person', join_condition='@connector@.personid = person.personid')

Q1 = sqltemplate.Query(
    select='*',
    tables=[
        'action', 
        persontbl.extend_template(connector='action')
    ]
)

Q2 = sqltemplate.Query(
    select='*',
    tables=[
        'characteristic', 
        persontbl.extend_template(connector='characteristic')
    ]
)

template syntax

There are some additional syntactic options for templates besides the direct repace @word@:

Default Templates

Indicator: Pipe Separation Template Example: @templatekey|defaultvalue@ Definition: Specifies a default value if the sqltemplate object has no value defined for the template key. Usage Example:

persontbl = sqltemplate.TableClause(
    'person', 
    join_condition='@connector|identity@.personid = person.personid',
)

Function Syntax

Indicator: Colon Separation Template Example: @function:templatekey@ Definition: Specifies a function to call with the current sql (and the value for the templatekey). The function must be defined as template_function_{function}. Some functional templates are already defined (and listed below). Usage Example:

class ExpandedTableClause(sqltemplate.TableClause):

    @classmethod
    def template_function_between(cls, sql, clause, value):
        sql = sql.replace(clause, value + " BETWEEN startdate and enddate")

enrltemplate = ExpandedTableClause(
    'enrollment', 
    join_condition='@between:date@',
)

currentenrl = enrltemplate.extend_template(date='sysdate')
jan1enrl = enrltemplate.extend_template(date="to_date('2021-01-01', 'YYYY-MM-DD')")

Defined Template Functions

The following are pre-defined functions that can be used in templates.

Generic Equals (eq)

Function: eq Definition: Depending on whether the value is a list of multiple values or just a single value, will write a phrase in either equal format or inlist format Usage Example:

tmpl = sqltemplate.Table(
    'person', 
    join_condition='@eq:id@',
)

thisperson = tmpl.extend_template(id=55)
thosepersons = tmpl.extend_template(id=[100,200,300])

Generic Not-Equals (ne)

Function: ne Definition: Depending on whether the value is a list of multiple values or just a single value, will write a phrase in either not-equal format or not inlist format Usage Example:

tmpl = sqltemplate.Table(
    'person', 
    join_condition='@ne:id@',
)

thisperson = tmpl.extend_template(id=55)
thosepersons = tmpl.extend_template(id=[100,200,300])

Distribution

Project Status

Currently, sqltemplate has been used in practice but is shallowly vetted condition and should be considered somewhere between alpha and beta software.

If you wish to rely on features of this package, I am likely more than willing to accommodate and to incorporate sensible design improvements or, in some cases, changes.

Installation

Use the package manager pip to install sqltemplate.

pip install sqltemplate

Usage

Many examples of usage are available in the main test files included in the t/ subdirectory.

import sqltemplate

standard_select = sqltemplate.SelectClause('person.name', 'person.id', 'person.birthdate', 'address.city')

enrollment = sqltemplate.TableClause(
    'enrollment', 
    join_condition = 'enrollment.personid = person.personid',
    exists_condition = 'enrollment.personid = person.personid',
)

basequery = sqltemplate.Query(
    select=standard_select,
    tables=[
        'person',
        enrollment,
        sqltemplate.TableClause('address', join_condition=[ 'address.personid = person.personid', 'address.active=1'] )
    ]
)

active_people = basequery.copy(extend_where="enrollment.status = 'active'")

inactive_people = basequery.copy(extend_where="enrollment.status = 'inactive'")

people_with_no_enrollments = sqltemplate.Query(
    select=standard_select,
    tables=['person'],
    where=[ enrollment.as_exists(negate=True)],
)

Contributing

Contributions are collaboration is welcome. For major changes, please contact me in advance to discuss.

Please make sure to update tests for any contribution, as appropriate.

Author

Kevin Crouse. Copyright, 2021.

License

Apache 2.0

Metadata

Release files for pysqltemplate 0.0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pysqltemplate 0.0.1.0
File Size Uploaded Provenance Actions
pysqltemplate-0.0.1.0.tar.gz 17.7 kB Without provenance Details for pysqltemplate-0.0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pysqltemplate 0.0.1.0
File Interpreter ABI Platform Actions
pysqltemplate-0.0.1.0-py3-none-any.whl Python 3 none any Details for pysqltemplate-0.0.1.0-py3-none-any.whl

Total release size: 41.5 kB

Release files / pysqltemplate-0.0.1.0.tar.gz

Download URL pysqltemplate-0.0.1.0.tar.gz
Size 17.7 kB
Tags Source
SHA-256 checksum
How to use checksums
2e0ff943ef45671f2ae0034c42f6a205c0bc41c04ad06b35b88655b8cf1893ee
BLAKE2b-256 checksum
How to use checksums
31f17fdb96f80055f91cbfb77680cf0d015b46f4337cfd7a685d066566c61a07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.4

Release files / pysqltemplate-0.0.1.0-py3-none-any.whl

Download URL pysqltemplate-0.0.1.0-py3-none-any.whl
Size 23.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
586bcd508e638ab549bed20e5fb3f7c8ed20cbb92cdd6788cb40835395420811
BLAKE2b-256 checksum
How to use checksums
604422987498f500cd085d1dc7cdd651dddd5eee559b6f74478c35087dfea31b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.4

Security for pysqltemplate 0.0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page