diff --git a/pythonProject/.idea/.gitignore b/pythonProject/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/pythonProject/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/pythonProject/main.py b/pythonProject/main.py new file mode 100644 index 0000000..c8e0db3 --- /dev/null +++ b/pythonProject/main.py @@ -0,0 +1,17 @@ +# This is a sample Python script. +from sqlparse import SQLParser + + +# Press Umschalt+F10 to execute it or replace it with your code. +# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. + +def main(): + sql_tables = SQLParser.parse_sql_file("schema.sql") + + + +# Press the green button in the gutter to run the script. +if __name__ == '__main__': + main() + +# See PyCharm help at https://www.jetbrains.com/help/pycharm/ diff --git a/pythonProject/schema.sql b/pythonProject/schema.sql new file mode 100644 index 0000000..6113eee --- /dev/null +++ b/pythonProject/schema.sql @@ -0,0 +1,12 @@ +CREATE TABLE `test` ( + `id` bigint(20) NOT NULL, + `test_value` int(11) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci + +CREATE TABLE `test2` ( + `id` bigint(20) NOT NULL, + `test_value` int(11) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci + diff --git a/pythonProject/sqlparse/SQLColumn.py b/pythonProject/sqlparse/SQLColumn.py new file mode 100644 index 0000000..db5bb2d --- /dev/null +++ b/pythonProject/sqlparse/SQLColumn.py @@ -0,0 +1,15 @@ +from enum import Enum + + +class SQLColumnType(Enum): + BIGINT = 'bigint(20)' + INT = 'int(11)' + + +class SQLColumn: + + def __init__(self, column_name: str, column_type: SQLColumnType, nullable: bool): + self.columnName = column_name + self.columnType = column_type + self.nullable = nullable + diff --git a/pythonProject/sqlparse/SQLParser.py b/pythonProject/sqlparse/SQLParser.py new file mode 100644 index 0000000..5975f97 --- /dev/null +++ b/pythonProject/sqlparse/SQLParser.py @@ -0,0 +1,98 @@ +import re + +from sqlparse import SQLColumn +from sqlparse.SQLTable import SQLTable + +tableNamePattern = r"CREATE TABLE `(\w+)`" + + +def read_sql_file(sql_path: str): + with open(sql_path, 'r') as file: + return file.read() + + +def split_table_definition(sql_string: str): + splitted_sql_string = sql_string.split("CREATE TABLE") + splitted_sql_string.pop(0) + for index, sql_table_string in enumerate(splitted_sql_string): + splitted_sql_string[index] = "CREATE TABLE" + sql_table_string + return splitted_sql_string + + +def parse_sql_file(sql_path: str): + sql_string = read_sql_file(sql_path) + sql_tables = split_table_definition(sql_string) + processed_tables = [SQLTable] + for sql_table in sql_tables: + table = parse_table_sql(sql_table) + processed_tables.append(table) + return processed_tables + + +def parse_table_sql(table_sql: str): + table_name = get_table_name(table_sql) + table_content = get_table_content(table_sql) + table = process_table_content(table_content, table_name) + return table + + +def process_table_content(table_content: str, table_name: str): + splitted_table_content = table_content.split(",") + primary_key = "" + columns = [] + for column_definition in splitted_table_content: + if column_definition.startswith(" PRIMARY KEY"): + primary_key = get_primary_key(column_definition) + else: + column = parse_column_definition(column_definition) + columns.append(column) + return SQLTable(table_name, columns, primary_key) + + +def parse_column_definition(column_definition: str): + column_name = get_column_name(column_definition) + column_type = get_column_type(column_definition) + nullable = get_nullable(column_definition) + return SQLColumn.SQLColumn(column_name, column_type, nullable) + + +def get_nullable(column_definition: str): + return "NOT NULL" in column_definition + + +def get_column_type(column_definition: str): + splitted_column_definition = column_definition.split(" ") + for column_definition_characteristic in splitted_column_definition: + if column_definition_characteristic == 'bigint(20)': + return SQLColumn.SQLColumnType.BIGINT + elif column_definition_characteristic == 'int(11)': + return SQLColumn.SQLColumnType.INT + + +def get_column_name(column_definition: str): + match = re.search("`(\w+)`", column_definition) + if match: + column_name = match.group(1) + return column_name + + +def get_primary_key(primary_key_sql: str): + match = re.search("PRIMARY KEY \(`(\w+)`\)", primary_key_sql) + if match: + return match.group(1) + + +def get_table_name(sql_string: str): + match = re.search(tableNamePattern, sql_string) + if match: + table_name = match.group(1) + return table_name + + +def get_table_content(sql_string: str): + last_ceiling_index = sql_string.rfind(")") + first_ceiling_index = sql_string.index("(") + 1 + + table_content = sql_string[first_ceiling_index:last_ceiling_index] + table_content = re.compile(r"\s+").sub(" ", table_content).strip() + return table_content diff --git a/pythonProject/sqlparse/SQLTable.py b/pythonProject/sqlparse/SQLTable.py new file mode 100644 index 0000000..e6ea0d0 --- /dev/null +++ b/pythonProject/sqlparse/SQLTable.py @@ -0,0 +1,9 @@ +from sqlparse.SQLColumn import SQLColumn + + +class SQLTable: + def __init__(self, table_name, columns: [SQLColumn], primary_key: str): + self.table_name = table_name + self.columns = columns + self.primary_key = primary_key +