#  Copyright (C) 2023
#  ABM, Moscow
#
#  UNPUBLISHED PROPRIETARY MATERIAL.
#  ALL RIGHTS RESERVED.
#
#  Authors: Mike Orlov <m.orlov@abm-jsc.ru>
from typing import Any


class SqlContext:
    def __init__(self, self_table_name: str, key_to_param: dict | None = None):
        self.self_table_name = self_table_name
        self.bind_params = key_to_param is None
        self.key_to_param = key_to_param

    def get_self(self) -> str:
        return self.self_table_name

    def register_parameter(self, value: Any) -> str:
        assert self.key_to_param is not None, f'Attempt to register parameter({value}) with no key_to_param storage'
        if any(
                value == (param_val := (target_item := item)[1]) and type(value) == type(param_val)
                for item in self.key_to_param.items()
        ):
            key = target_item[0]
        else:
            key = f'l{len(self.key_to_param)}'
            self.key_to_param[key] = value
        return ":" + key
