# Path: pyelt/mappings/transformations.py
# class FieldTransformation():
#     def __init__(self, name: str = '', sql: str = '') -> None:
#         self.name = name #type: str
#         self.field_name = 'id' #type: str
#         self.table = ''
#         self.descr = '' #type: str
#         self.filter = '' #type: str
#         # self.type = FieldTransformationType.INLINE #type: str
#         self.steps = {} #type: Dict[int, FieldTransformStep]
#         if sql:
#             # self.parse_sql(sql)
#             step = self.new_step(sql)
# 
#     def get_table(self):
#         return self.table
# 
#     def parse_sql(self, sql: str):
#         pos_start = sql.find('(')
#         pos_end = sql.rfind(')')
#         func_name = sql[:pos_start]
#         func_inner = sql[pos_start + 1:pos_end]
#         step = self.new_step(func_inner)
#         self.steps[step.sort_order] = step
# 
#     def new_step(self, sql: str) -> 'FieldTransformStep':
#         step = FieldTransformStep(sql=sql)
#         step.sort_order = len(self.steps) + 1
#         self.steps[step.sort_order] = step
#         return step
# 
#     def get_sql(self, alias: str='')->str:
#         sql = ''
#         index = 0
#         steps = sorted(self.steps.values(), key = lambda x: x.sort_order)
#         for step in steps:
#             # step_sql = step.sql
#             step_sql = step.get_sql(alias)
#             step_sql = step_sql.replace(self.field_name, "{fld}")
#             if (index > 0):
#                 if '{fld}' in step_sql:
#                     sql = step_sql.replace("{fld}", sql)
#                 else:
#                     sql = step_sql.replace("{step" + str(index) + "}", sql)
#             else:
#                  sql = step_sql
#                  sql = sql.replace("{fld}", self.field_name)
#             index += 1
#         return sql
# 
#     def __repr__(self):
#         return self.get_sql('')


from pyelt.datalayers.database import *
from pyelt.datalayers.dv import *
from pyelt.mappings.transformations import FieldTransformation
        # dv_schema = entity.cls_get_schema(dwh).name
        # params['schema'] = dv_schema

        params['fields'] = self.__get_fields()
        params['from'] = self.__get_from()
        params['filter'] = self.__get_filter()

        sql = "SELECT {fields} \nFROM {from}\nWHERE {filter}".format(**params)
        return sql


    def __get_fields_new(self):
        fields = ""
        sats_in_query = self.__get_sats()
        for map in self.field_mappings:
            if not map.source:
                fields += "NULL AS {}, \n".format(map.target.name)
                continue
            source_table = map.source.get_table()
            source_field = map.source  # type: Column
            # if type(source_table) is Hub:
            if 'hub' in source_table.__dbname__:
                fields += "hub.{} AS {}, \n".format(source_field.name, map.target.name)
            else:
                sat_name = map.source.get_table().name
                key = sat_name
                if map.source_type:
                    # voor hybride sats, type toevoegen aan sat naam
                    key += map.source_type
                sat_alias = sats_in_query[key]['sat_alias']

# Path: pyelt/mappings/transformations.py
# class FieldTransformation():
#     def __init__(self, name: str = '', sql: str = '') -> None:
#         self.name = name #type: str
#         self.field_name = 'id' #type: str
#         self.table = ''
#         self.descr = '' #type: str
#         self.filter = '' #type: str
#         # self.type = FieldTransformationType.INLINE #type: str
#         self.steps = {} #type: Dict[int, FieldTransformStep]
#         if sql:
#             # self.parse_sql(sql)
#             step = self.new_step(sql)
# 
#     def get_table(self):
#         return self.table
# 
#     def parse_sql(self, sql: str):
#         pos_start = sql.find('(')
#         pos_end = sql.rfind(')')
#         func_name = sql[:pos_start]
#         func_inner = sql[pos_start + 1:pos_end]
#         step = self.new_step(func_inner)
#         self.steps[step.sort_order] = step
# 
#     def new_step(self, sql: str) -> 'FieldTransformStep':
#         step = FieldTransformStep(sql=sql)
#         step.sort_order = len(self.steps) + 1
#         self.steps[step.sort_order] = step
#         return step
# 
#     def get_sql(self, alias: str='')->str:
#         sql = ''
#         index = 0
#         steps = sorted(self.steps.values(), key = lambda x: x.sort_order)
#         for step in steps:
#             # step_sql = step.sql
#             step_sql = step.get_sql(alias)
#             step_sql = step_sql.replace(self.field_name, "{fld}")
#             if (index > 0):
#                 if '{fld}' in step_sql:
#                     sql = step_sql.replace("{fld}", sql)
#                 else:
#                     sql = step_sql.replace("{step" + str(index) + "}", sql)
#             else:
#                  sql = step_sql
#                  sql = sql.replace("{fld}", self.field_name)
#             index += 1
#         return sql
# 
#     def __repr__(self):
#         return self.get_sql('')


# Path: pyelt/helpers/QueryMaker_old.py
from pyelt.datalayers.database import *
from pyelt.datalayers.dv import *
from pyelt.mappings.transformations import FieldTransformation

        # dv_schema = entity.cls_get_schema(dwh).name
        # params['schema'] = dv_schema

        params['fields'] = self.__get_fields()
        params['from'] = self.__get_from()
        params['filter'] = self.__get_filter()

        sql = "SELECT {fields} \nFROM {from}\nWHERE {filter}".format(**params)
        return sql


    def __get_fields_new(self):
        fields = ""
        sats_in_query = self.__get_sats()
        for map in self.field_mappings:
            if not map.source:
                fields += "NULL AS {}, \n".format(map.target.name)
                continue
            source_table = map.source.get_table()
            source_field = map.source  # type: Column
            # if type(source_table) is Hub:
            if 'hub' in source_table.__dbname__:
                fields += "hub.{} AS {}, \n".format(source_field.name, map.target.name)
            else:
                sat_name = map.source.get_table().name
                key = sat_name
                if map.source_type:
                    # voor hybride sats, type toevoegen aan sat naam
                    key += map.source_type
                sat_alias = sats_in_query[key]['sat_alias']

                if isinstance(map.source, FieldTransformation):