Skip to main content

Python library for parsing the sql statements into easy to use metadata about sql operation, columns, filtering conditions and more. Visit the project at https://github.com/lako55/py-sqlstatement for more details.

Project description

About sqlstatement python library

Python library sqlparse is a great start when it comes to processing SQL statements programatically. However it is more complex to leverage the output since it is a tree form of every word in SQL statement called tokens. The intention of this project is to wrap the sqlparse output with the conscise structure of the entities describing the specific sql statement in a form of a table with specific action such as CREATE or SELECT FROM while providing all the meta data (column names, types, values to be inserted) related to that sql action.

Deliverable of this project is a python library called SQLStatement.

Getting Started

Follow these steps to try out sqlstatement library in a safe environment:

  1. clone this repo
  2. open terminal and change directory to the repository folder
  3. type following commands in terminal
$ python -m venv env
$ source env/bin/activate
$ pip install sqlparse
$ python
  1. run following statements in python shell
>>> from sqlstatement.sql import SQLEntityFactory

>>> sql = "SELECT firstname, age FROM persons WHERE lastname = 'Doe';"
>>> sqlentity = SQLEntityFactory.create_entity(sql)

>>> print(sqlentity)
  1. Formatted output in bash. Every entity is a namedtuple such as SQLTable or SQLColumn. See sqlentities python module for more details about all the entities.
SQLTable(
    name='persons', 
    action=<SQLDMLAction.SELECT: 'SELECT'>, 
    columns=[
        SQLColumn(
            name='firstname', 
            action=<SQLDMLAction.SELECT: 'SELECT'>, 
            type=None, 
            size=None, 
            constraints=[], 
            value=None), 
        SQLColumn(
            name='age', 
            action=<SQLDMLAction.SELECT: 'SELECT'>, 
            type=None, 
            size=None, 
            constraints=[], 
            value=None)
    ], 
    where=[
        SQLAnd(
            filter=[
                SQLColumn(
                    name='lastname', 
                    action=<SQLDMLAction.WHEREEQUAL: 'WHEREEQUAL'>, 
                    type=None, 
                    size=None, 
                    constraints=None, 
                    value='Doe')
            ])
    ])

Install from pypi.org

Library can be installed also as a python package. Open terminal windeos and type

$ pip install sqlstatement

Supported SQL Statements

Please refer to test_sql python module in this repository for the list of all the sql statements which are supported and passed the test.

Future Features

  1. support of all the different SQL flavours => TBD
  2. parsing the foreign key in CREATE/ALTER TABLE
  3. enriching parsing of WHERE clause with different operators. Currently only equal operator is supported

License

BSD 3-Clause License

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

sqlstatement-0.0.1.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

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

sqlstatement-0.0.1-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file sqlstatement-0.0.1.tar.gz.

File metadata

  • Download URL: sqlstatement-0.0.1.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for sqlstatement-0.0.1.tar.gz
Algorithm Hash digest
SHA256 94d646d1db8658d150eee26b6228834b64f710cbf563929c6eb9555b7624d933
MD5 04b8e9da6a1f7cb43c32c316987d0df3
BLAKE2b-256 cf1f401b4a8c5b21f49b12148719c59ce10470fbe0fd9749c223fbecdfe2e461

See more details on using hashes here.

File details

Details for the file sqlstatement-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: sqlstatement-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for sqlstatement-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d383780254e079ce4f0d7a3939d7ebe4deb4d2c68e2e76625d22d5e294d47f87
MD5 23621880b9a2d0ae9f4b496a6175d495
BLAKE2b-256 53ef3880066566976525a875902f7b33e99e93b965d8f63abff20bbfd3b17c8f

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