Skip to main content

Transform column and table name to desired one

Project description

SQL Refactor

This library contains a class, Refactor, for refactoring a SQL with a given knowledge of tables and column names mapping.

Structur of the knowledge

The knowledge is a dictionary containing the mapping of old_table-new_table and corresponding old_column-new_column.

The structure of the knowledge:

{
    "old_table_name":
    {
        "new_table": "new_table_name",
        "column_knowledge":
        {
            "column_1" : "new_column_1",
            "column_2" : "new_column_2",

        }
        "preserved" : boolean
    },
    .
    .
    .
}

How to use

Code sample:

from sql_refactor import Refactor

# refer to Structure of Knowledge
knowledge = KNOWLEDGE

refactor = Refactor(knowledge)
refactor.refactor('''SELECT column_1, column_2 FROM old_table_name''')

new_query = refactor.result()

Result:

SELECT
  new_column_1 AS column_1,
  new_column_2 AS column_2
FROM
  `new_table_name`

Limitation

This repo is under development which contains some limitations.

Limitations of the sql_parser:

  1. MERGE statement is not supported yet

  2. CREATE [TEMPORARY] FUNCTION is not supported yet

  3. Function call without () is not supported.

    For instance: TIMESTAMP '2021-01-01' can not be parsed, expected TIMESTAMP('2021-01-01')

  4. DECLARE statement is not supported yet

Limitations of the sql_refactor:

  1. Has been tested on non-scripting SQL, and it works fine.
  2. For the scripting SQL, only works if the query only contains CREATE TEMP TABLE + main select

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

sql-refactor-0.0.3.tar.gz (41.2 kB view hashes)

Uploaded Source

Built Distribution

sql_refactor-0.0.3-py3-none-any.whl (58.9 kB view hashes)

Uploaded Python 3

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