# Path: django_migration_linter/sql_analyser/mysql.py
# class MySqlAnalyser(BaseAnalyser):
#     migration_tests = [
#         {
#             "code": "ALTER_COLUMN",
#             "fn": lambda sql, **kw: re.search(
#                 "ALTER TABLE .* MODIFY .* (?!NULL);?$", sql
#             ),
#             "mode": "one_liner",
#             "type": "error",
#         }
#     ]
# 
#     @staticmethod
#     def detect_column(sql):
#         if isinstance(sql, str):
#             regex_result = re.search("COLUMN [`\"'](.*?)[`\"']", sql, re.IGNORECASE)
#             if regex_result:
#                 return regex_result.group(1)
#             regex_result = re.search("MODIFY [`\"'](.*?)[`\"']", sql, re.IGNORECASE)
#             if regex_result:
#                 return regex_result.group(1)
# 
# Path: django_migration_linter/sql_analyser/postgresql.py
# class PostgresqlAnalyser(BaseAnalyser):
#     migration_tests = [
#         {
#             "code": "CREATE_INDEX",
#             "fn": has_create_index,
#             "msg": "CREATE INDEX locks table",
#             "mode": "transaction",
#             "type": "warning",
#         },
#         {
#             "code": "DROP_INDEX",
#             "fn": lambda sql, **kw: re.search("DROP INDEX", sql)
#             and not re.search("INDEX CONCURRENTLY", sql),
#             "msg": "DROP INDEX locks table",
#             "mode": "one_liner",
#             "type": "warning",
#         },
#         {
#             "code": "REINDEX",
#             "fn": lambda sql, **kw: sql.startswith("REINDEX"),
#             "msg": "REINDEX locks table",
#             "mode": "one_liner",
#             "type": "warning",
#         },
#     ]
# 
# Path: django_migration_linter/sql_analyser/sqlite.py
# class SqliteAnalyser(BaseAnalyser):
#     migration_tests = [
#         {
#             "code": "RENAME_TABLE",
#             "fn": lambda sql, **kw: re.search("ALTER TABLE .* RENAME TO", sql)
#             and "__old" not in sql
#             and "new__" not in sql,
#             "type": "error",
#         },
#         {
#             "code": "DROP_TABLE",
#             # TODO: improve to detect that the table names overlap
#             "fn": lambda sql_statements, **kw: any(
#                 sql.startswith("DROP TABLE") for sql in sql_statements
#             )
#             and not any(sql.startswith("CREATE TABLE") for sql in sql_statements),
#             "msg": "DROPPING table",
#             "mode": "transaction",
#             "type": "error",
#         },
#         {
#             "code": "NOT_NULL",
#             "fn": lambda sql_statements, **kw: any(
#                 re.search("NOT NULL(?! PRIMARY)(?! DEFAULT)", sql)
#                 for sql in sql_statements
#             )
#             and any(
#                 re.search("ALTER TABLE .* RENAME TO", sql)
#                 and ("__old" in sql or "new__" in sql)
#                 for sql in sql_statements
#             ),
#             "mode": "transaction",
#             "type": "error",
#         },
#     ]
# 
#     @staticmethod
#     def detect_table(sql):
#         if isinstance(sql, str):
#             regex_result = re.search("TABLE [`\"'](.*?)[`\"']", sql, re.IGNORECASE)
#             if regex_result:
#                 return regex_result.group(1)
#             regex_result = re.search("ON [`\"'](.*?)[`\"']", sql, re.IGNORECASE)
#             if regex_result:
#                 return regex_result.group(1)


import logging
from django_migration_linter.sql_analyser import (
    MySqlAnalyser,
    PostgresqlAnalyser,
    SqliteAnalyser,
)


logger = logging.getLogger("django_migration_linter")

ANALYSER_STRING_MAPPING = {
    "sqlite": SqliteAnalyser,
    "mysql": MySqlAnalyser,
    "postgresql": PostgresqlAnalyser,
}


def get_sql_analyser_class(database_vendor, analyser_string=None):
    if analyser_string:
        return get_sql_analyser_from_string(analyser_string)
    return get_sql_analyser_class_from_db_vendor(database_vendor)


def get_sql_analyser_from_string(analyser_string):
    if analyser_string not in ANALYSER_STRING_MAPPING:
        raise ValueError(
            "Unknown SQL analyser. Known values: {}".format(analyser_string.keys())
        )
    return ANALYSER_STRING_MAPPING[analyser_string]


def get_sql_analyser_class_from_db_vendor(database_vendor):
    if "mysql" in database_vendor:
        sql_analyser_class = MySqlAnalyser
    elif "postgre" in database_vendor:

# Path: django_migration_linter/sql_analyser/mysql.py
# class MySqlAnalyser(BaseAnalyser):
#     migration_tests = [
#         {
#             "code": "ALTER_COLUMN",
#             "fn": lambda sql, **kw: re.search(
#                 "ALTER TABLE .* MODIFY .* (?!NULL);?$", sql
#             ),
#             "mode": "one_liner",
#             "type": "error",
#         }
#     ]
# 
#     @staticmethod
#     def detect_column(sql):
#         if isinstance(sql, str):
#             regex_result = re.search("COLUMN [`\"'](.*?)[`\"']", sql, re.IGNORECASE)
#             if regex_result:
#                 return regex_result.group(1)
#             regex_result = re.search("MODIFY [`\"'](.*?)[`\"']", sql, re.IGNORECASE)
#             if regex_result:
#                 return regex_result.group(1)
# 
# Path: django_migration_linter/sql_analyser/postgresql.py
# class PostgresqlAnalyser(BaseAnalyser):
#     migration_tests = [
#         {
#             "code": "CREATE_INDEX",
#             "fn": has_create_index,
#             "msg": "CREATE INDEX locks table",
#             "mode": "transaction",
#             "type": "warning",
#         },
#         {
#             "code": "DROP_INDEX",
#             "fn": lambda sql, **kw: re.search("DROP INDEX", sql)
#             and not re.search("INDEX CONCURRENTLY", sql),
#             "msg": "DROP INDEX locks table",
#             "mode": "one_liner",
#             "type": "warning",
#         },
#         {
#             "code": "REINDEX",
#             "fn": lambda sql, **kw: sql.startswith("REINDEX"),
#             "msg": "REINDEX locks table",
#             "mode": "one_liner",
#             "type": "warning",
#         },
#     ]
# 
# Path: django_migration_linter/sql_analyser/sqlite.py
# class SqliteAnalyser(BaseAnalyser):
#     migration_tests = [
#         {
#             "code": "RENAME_TABLE",
#             "fn": lambda sql, **kw: re.search("ALTER TABLE .* RENAME TO", sql)
#             and "__old" not in sql
#             and "new__" not in sql,
#             "type": "error",
#         },
#         {
#             "code": "DROP_TABLE",
#             # TODO: improve to detect that the table names overlap
#             "fn": lambda sql_statements, **kw: any(
#                 sql.startswith("DROP TABLE") for sql in sql_statements
#             )
#             and not any(sql.startswith("CREATE TABLE") for sql in sql_statements),
#             "msg": "DROPPING table",
#             "mode": "transaction",
#             "type": "error",
#         },
#         {
#             "code": "NOT_NULL",
#             "fn": lambda sql_statements, **kw: any(
#                 re.search("NOT NULL(?! PRIMARY)(?! DEFAULT)", sql)
#                 for sql in sql_statements
#             )
#             and any(
#                 re.search("ALTER TABLE .* RENAME TO", sql)
#                 and ("__old" in sql or "new__" in sql)
#                 for sql in sql_statements
#             ),
#             "mode": "transaction",
#             "type": "error",
#         },
#     ]
# 
#     @staticmethod
#     def detect_table(sql):
#         if isinstance(sql, str):
#             regex_result = re.search("TABLE [`\"'](.*?)[`\"']", sql, re.IGNORECASE)
#             if regex_result:
#                 return regex_result.group(1)
#             regex_result = re.search("ON [`\"'](.*?)[`\"']", sql, re.IGNORECASE)
#             if regex_result:
#                 return regex_result.group(1)


# Path: django_migration_linter/sql_analyser/analyser.py
import logging
from django_migration_linter.sql_analyser import (
    MySqlAnalyser,
    PostgresqlAnalyser,
    SqliteAnalyser,
)



logger = logging.getLogger("django_migration_linter")

ANALYSER_STRING_MAPPING = {
    "sqlite": SqliteAnalyser,
    "mysql": MySqlAnalyser,
    "postgresql": PostgresqlAnalyser,
}


def get_sql_analyser_class(database_vendor, analyser_string=None):
    if analyser_string:
        return get_sql_analyser_from_string(analyser_string)
    return get_sql_analyser_class_from_db_vendor(database_vendor)


def get_sql_analyser_from_string(analyser_string):
    if analyser_string not in ANALYSER_STRING_MAPPING:
        raise ValueError(
            "Unknown SQL analyser. Known values: {}".format(analyser_string.keys())
        )
    return ANALYSER_STRING_MAPPING[analyser_string]


def get_sql_analyser_class_from_db_vendor(database_vendor):
    if "mysql" in database_vendor:
        sql_analyser_class = MySqlAnalyser
    elif "postgre" in database_vendor:

        sql_analyser_class = PostgresqlAnalyser