Skip to main content

Parses PostgreSQL/MySQL and translates ADQL to PostgreSQL/MySQL.

Project description

queryparser

Tool for parsing and processing of MySQL/PostgreSQL and translation of ADQL SELECT-like queries

Designed to be used in conjunction with django-daiquri as a query processing backend but it can be easily used as a stand-alone tool or integrated into another project.

pytest Workflow Status Coverage Status License Latest Version

Installation

The easiest way to install the package is by using the pip tool:

    pip install queryparser-python3

Alternatively, you can clone the repository and install it from there. However, this step also requires generating the parser which is a slightly more elaborate process (see below).

Generating the parser from the git repository

To generate the parsers you need python3 , java above version 7, and antlr4 (antlr-4.*-complete.jar has to be installed inside the /usr/local/lib/ or /usr/local/bin/ directories).

After cloning the project run

    make

and a lib directory will be created. After that, run

    python setup.py install

to install the generated parser in your virtual environment.

Parsing MySQL and PostgreSQL

Parsing and processing of MySQL queries can be done by creating an instance of the MySQLQueryProcessor class

    from queryparser.mysql import MySQLQueryProcessor
    qp = MySQLQueryProcessor()

feeding it a MySQL query

    sql = "SELECT a FROM db.tab;"
    qp.set_query(sql)

and running it with

    qp.process_query()

After the processing is completed, the processor object qp will include tables, columns, functions, and keywords used in the query or will raise a QuerySyntaxError if there are any syntax errors in the query.

Alternatively, passing the query at initialization automatically processes it.

PostgreSQL parsing is very similar to MySQL, except it requires importing the PostgreSQLProcessor class:

    from queryparser.postgresql import PostgreSQLQueryProcessor
    qp = PostgreSQLQueryProcessor()

The rest of the functionality remains the same.

Translating ADQL

Translation of ADQL queries is done similarly by first creating an instance of the ADQLQueryTranslator class

    from queryparser.adql import ADQLQueryTranslator
    adql = "SELECT TOP 100 POINT('ICRS', ra, de) FROM db.tab;"
    adt = ADQLQueryTranslator(adql)

and calling

    adt.to_mysql()

which returns a translated string representing a valid MySQL query if the ADQL query had no errors. The MySQL query can then be parsed with the MySQLQueryProcessor in the same way as shown above.

Testing

First, install pytest

    pip install pytest

then run the test suite for a version of python you would like to test with

    pytest lib/

More elaborate testing procedures can be found in the development notes.

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

queryparser-python3-0.6.1.tar.gz (262.6 kB view details)

Uploaded Source

Built Distribution

queryparser_python3-0.6.1-py3-none-any.whl (268.0 kB view details)

Uploaded Python 3

File details

Details for the file queryparser-python3-0.6.1.tar.gz.

File metadata

  • Download URL: queryparser-python3-0.6.1.tar.gz
  • Upload date:
  • Size: 262.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.2

File hashes

Hashes for queryparser-python3-0.6.1.tar.gz
Algorithm Hash digest
SHA256 38882e341cbb69536cfb1a5e51ad92d15079c13e662833a2bcd044f4539c1901
MD5 2a052fffbb40994803aa7cb320eb15ee
BLAKE2b-256 1efe247cbac00320e06218e5f399c63b37d9467ec8f6b9e43c12f32d46c9188e

See more details on using hashes here.

File details

Details for the file queryparser_python3-0.6.1-py3-none-any.whl.

File metadata

File hashes

Hashes for queryparser_python3-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 20edd1d7c117f6d4536db8e0fa265ede89906055c956a7a141293e507dc39ad6
MD5 501d18624cf60cf600f7d2774f4a7498
BLAKE2b-256 7bff18bf75739b2a6c4e41a9aa128d673fbe272910381e2794565ba3efe8e6e6

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