Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions database_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,37 @@ class ExtractCounter(Base):
name = Column(String(36), primary_key=True, unique=True)
counter_value = Column(BigInteger)

class ExtractRelations(Base):
__tablename__ = "extract_relation_dict"
name = Column(String(36), primary_key=True, unique=True)
value_table = Column(JSON)

def get_saved_constrains_from_name(name:str):
session, connection, engine = connect_to_db()
result=session.query(ExtractRelations).filter(ExtractRelations.name==name).all()
session.close()
connection.close()
return result

def update_relations_by_name(name:str,upadet_dict:dict):
session, connection, engine = connect_to_db()
session.query(ExtractRelations).filter(ExtractRelations.name==name).update(
{ExtractRelations.value_table: upadet_dict}, synchronize_session=False
)
session.commit()
session.close()
connection.close()

def insert_relation_constraints(new_entity:ExtractRelations):
session, connection, engine = connect_to_db()
try:
session.add(new_entity)
session.commit()
session.close()
connection.close()
except KeyboardInterrupt:
session.close()
connection.close()

def select_counter_from_name(name: str):
sql_str: str = f"SELECT counter_value FROM extract_counter WHERE name ='{name}';"
Expand Down Expand Up @@ -97,7 +128,10 @@ def delete_all_rows_from_pql_entity():

def delete_all_rows_from_counter_saving():
fetch_data_from_query("DELETE FROM extract_counter WHERE TRUE;")
delete_all_rows_from_saved_constraints()

def delete_all_rows_from_saved_constraints():
fetch_data_from_query("DELETE FROM extract_relation_dict WHERE TRUE;")

def insert_entity(insert_entity: PqlEntity):
session, connection, engine = connect_to_db()
Expand Down
35 changes: 32 additions & 3 deletions extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
select_counter_from_name,
update_counter_by_name,
insert_save_extract,
ExtractCounter,
ExtractCounter, get_saved_constrains_from_name, update_relations_by_name, insert_relation_constraints,
ExtractRelations,
)

faker1: Faker = Faker()
Expand Down Expand Up @@ -136,7 +137,14 @@ def apply(self, parent_entity: str, relation_key: str, end_result: dict):
The State is the actual array from the Stream
End_Result is the sorted dict
and key ist the Name of the Relation from the Config"""

if not self.constraints:
saved_constraints=get_saved_constrains_from_name(self.config_entity)
if saved_constraints:
self.constraints = saved_constraints[0].value_table.get("constraints")
# self.constraints = saved_constraints
else:
self.constraints = []
insert_relation_constraints(ExtractRelations(name=self.config_entity,value_table={}))
parent_entity_id = end_result[parent_entity][-1].get(self.own_id)
foreignkey_id = end_result[self.config_entity][-1].get(self.relation_id)

Expand All @@ -157,6 +165,7 @@ def apply(self, parent_entity: str, relation_key: str, end_result: dict):
)
return
self.constraints.append(contstraint_entry)
update_relations_by_name(self.config_entity,{"constraints":self.constraints})
return_dict: dict = {f"{relation_key}": foreignkey_id}
return return_dict

Expand Down Expand Up @@ -199,6 +208,16 @@ def apply(self, parent_entity: str, key, end_result: dict):
The State is the actual array from the Stream
End_Result is the sorted dict
and key ist the Name of the Relation from the Config"""
if not self.constraints:
saved_constraints=get_saved_constrains_from_name(self.config_entity)
if saved_constraints:
self.constraints = saved_constraints[0].value_table.get("constraints")
# self.constraints = saved_constraints
else:
self.constraints = []
insert_relation_constraints(ExtractRelations(name=self.config_entity,value_table={}))


parent_entity_id = end_result[parent_entity][-1].get(self.own_id)
foreignkey_id = end_result[self.config_entity][-1].get(self.relation_id)

Expand All @@ -216,6 +235,7 @@ def apply(self, parent_entity: str, key, end_result: dict):
)

self.constraints.append(contstraint_entry)
update_relations_by_name(self.config_entity,{"constraints":self.constraints})
return_dict: dict = {f"{key}": foreignkey_id}
return return_dict

Expand All @@ -240,7 +260,7 @@ def __init__(self, config_entity: str, entity_id: str) -> None:
self.config_entity = config_entity
self.relation_id = entity_id
self.change_index = 0
self.constraints = []
self.constraints:[] = None
self.own_id = None

def is_generated(self) -> bool:
Expand All @@ -255,6 +275,14 @@ def apply(self, parent_entity: str, key: str, end_result: dict):
The State is the actual array from the Stream
End_Result is the sorted dict
and key ist the Name of the Relation from the Config"""
if not self.constraints:
saved_constraints=get_saved_constrains_from_name(self.config_entity)
if saved_constraints:
self.constraints = saved_constraints[0].value_table.get("constraints")
else:
self.constraints = []
insert_relation_constraints(ExtractRelations(name=self.config_entity,value_table={}))

parent_entity_id = end_result[parent_entity][-1].get(self.own_id)
foreignkey_id = end_result[self.config_entity][-1].get(self.relation_id)

Expand All @@ -271,6 +299,7 @@ def apply(self, parent_entity: str, key: str, end_result: dict):
element_dict: dict = elements
if element_dict.get(parent_entity) == parent_entity_id:
fk_array.append(element_dict.get(self.config_entity))
update_relations_by_name(self.config_entity,{"constraints":self.constraints})
return_dict: dict = {f"{key}": fk_array}
return return_dict

Expand Down
Loading