Skip to main content

A small package that allows you to break your query in manageable chunks (normally fields), allowing you to focus on building each variable / field independently, with notes / documentation and at the end cam be compiled to a single gigantic query string that otherwise would be impossible to manage or maintain

Project description

Query Doc Package

A small package that allows you to break your query in manageable chunks (normally fields), allowing you to focus on building each variable / field independently, with notes / documentation and at the end cam be compiled to a single gigantic query string that otherwise would be impossible to manage or maintain

from query_doc import QueryDoc

Instantiate

qd = QueryDoc({})

ADD FIELDS

FIELD A

_field = qd.field()
_field.name = 'FieldA'
_field.desc = 'FieldA description'
_select = f"""SELECT "TABLE_A"."FILED_A" AS "{_field.name}"\n"""
_field.select = _select
_field.from_ = f"""FROM "TABLE_A"\n"""
_where = f"""WHERE "TABLE_A"."COND_FIELD_A" IS NOT NULL 
    AND "TABLE_A"."DATE_FIELD" = '{{YYYY-MM-DD}}'\n"""
_field.where = _where
qd.add_field(_field)

FIELD B

_field = qd.field()
_field.name = 'FieldB'
_field.desc = 'FieldB description'
_select = f"""    , "TABLE_B"."FILED_B" AS "{_field.name}"\n"""
_join = f"""LEFT OUTER JOIN (
    SELECT *
    FROM "TABLE_B"
) AS "TABLE_B" ON (
    "TABLE_B"."FK_FROM_A" = "TABLE_A"."KEY_FIELD"
    AND "TABLE_B"."DATE_FIELD" = "TABLE_A"."DATE_FIELD"
)\n"""
_field.select = _select
_field.join = _join
qd.add_field(_field)

CONCAT FIELD A & B

_field = qd.field()
_field.name = 'FieldA+FieldB'
_field.desc = 'FieldA Concat FieldB description'
_select = f"""    , (COALESCE(@FieldA, '') || COALESCE(@FieldB, '')) AS "{_field.name}"\n"""
_field.select = _select
qd.add_field(_field)

GET QUERY PARTS DICT

query_parts = qd.get_query_parts()
print(query_parts)
    {'FieldA': {'name': 'FieldA', 'desc': 'FieldA description', 'select': 'SELECT "TABLE_A"."FILED_A" AS "FieldA"\n', 'from_': 'FROM "TABLE_A"\n', 'join': None, 'where': 'WHERE "TABLE_A"."COND_FIELD_A" IS NOT NULL \n    AND "TABLE_A"."DATE_FIELD" = \'{YYYY-MM-DD}\'\n', 'group_by': None, 'order_by': None, 'having': None, 'window': None, 'extras': None, 'active': True}, 'FieldB': {'name': 'FieldB', 'desc': 'FieldB description', 'select': '    , "TABLE_B"."FILED_B" AS "FieldB"\n', 'from_': None, 'join': 'LEFT OUTER JOIN (\n    SELECT *\n    FROM "TABLE_B"\n) AS "TABLE_B" ON (\n    "TABLE_B"."FK_FROM_A" = "TABLE_A"."KEY_FIELD"\n    AND "TABLE_B"."DATE_FIELD" = "TABLE_A"."DATE_FIELD"\n)\n', 'where': None, 'group_by': None, 'order_by': None, 'having': None, 'window': None, 'extras': None, 'active': True}, 'FieldA+FieldB': {'name': 'FieldA+FieldB', 'desc': 'FieldA Concat FieldB description', 'select': '    , (@FieldA || @FieldB) AS "FieldA+FieldB"\n', 'from_': None, 'join': None, 'where': None, 'group_by': None, 'order_by': None, 'having': None, 'window': None, 'extras': None, 'active': True}}

GET QUERY STRING (SQL)

sql = qd.get_query_sql(None)
print(sql)
{'name': None, 'desc': None, 'select': 'SELECT "TABLE_A"."FILED_A" AS "FieldA"\n    , "TABLE_B"."FILED_B" AS "FieldB"\n    , (("TABLE_A"."FILED_A") || ( "TABLE_B"."FILED_B")) AS "FieldA+FieldB"\n', 'from_': 'FROM "TABLE_A"\n', 'join': 'LEFT OUTER JOIN (\n    SELECT *\n    FROM "TABLE_B"\n) AS "TABLE_B" ON (\n    "TABLE_B"."FK_FROM_A" = "TABLE_A"."KEY_FIELD"\n    AND "TABLE_B"."DATE_FIELD" = "TABLE_A"."DATE_FIELD"\n)\n', 'where': 'WHERE "TABLE_A"."COND_FIELD_A" IS NOT NULL \n    AND "TABLE_A"."DATE_FIELD" = \'{YYYY-MM-DD}\'\n', 'group_by': '', 'order_by': '', 'having': '', 'window': '', 'extras': None, 'active': True}
SELECT "TABLE_A"."FILED_A" AS "FieldA"
    , "TABLE_B"."FILED_B" AS "FieldB"
    , (("TABLE_A"."FILED_A") || ( "TABLE_B"."FILED_B")) AS "FieldA+FieldB"
FROM "TABLE_A"
LEFT OUTER JOIN (
    SELECT *
    FROM "TABLE_B"
) AS "TABLE_B" ON (
    "TABLE_B"."FK_FROM_A" = "TABLE_A"."KEY_FIELD"
    AND "TABLE_B"."DATE_FIELD" = "TABLE_A"."DATE_FIELD"
)
WHERE "TABLE_A"."COND_FIELD_A" IS NOT NULL 
    AND "TABLE_A"."DATE_FIELD" = '{YYYY-MM-DD}'

SET DATE

import datetime
dates = [datetime.date(2023, 1, 31)]
sql = qd.set_date(sql, dates)
print(sql)
    SELECT "TABLE_A"."FILED_A" AS "FieldA"
        , "TABLE_B"."FILED_B" AS "FieldB"
        , (("TABLE_A"."FILED_A") || ( "TABLE_B"."FILED_B")) AS "FieldA+FieldB"
    FROM "TABLE_A"
    LEFT OUTER JOIN (
        SELECT *
        FROM "TABLE_B"
    ) AS "TABLE_B" ON (
        "TABLE_B"."FK_FROM_A" = "TABLE_A"."KEY_FIELD"
        AND "TABLE_B"."DATE_FIELD" = "TABLE_A"."DATE_FIELD"
    )
    WHERE "TABLE_A"."COND_FIELD_A" IS NOT NULL 
        AND "TABLE_A"."DATE_FIELD" IN ('2023-01-31')


          

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

query_doc-1.0.4.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

query_doc-1.0.4-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file query_doc-1.0.4.tar.gz.

File metadata

  • Download URL: query_doc-1.0.4.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for query_doc-1.0.4.tar.gz
Algorithm Hash digest
SHA256 599f12e4938e734ac2161044ddb006275c3b35570a35cf10d0c2505549d5c1c9
MD5 4441bb8d11d8d3830964ca00a1278fdd
BLAKE2b-256 e0696f8f814aab0fc05bb6d30105e5104e3ddd93fc7077857339173061eea77c

See more details on using hashes here.

File details

Details for the file query_doc-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: query_doc-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for query_doc-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e44ec336a43cb39011394614ce6b66714aa359e343140502f2f9280188cb279f
MD5 bb434f7ae6ef1d5abe5418bee01756e4
BLAKE2b-256 38611dd0d3c3fc36449c0f6f38b2858eb70e8d75d1318390ece52b4f35e13589

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