Skip to content

Commit

Permalink
fix multiple schema names
Browse files Browse the repository at this point in the history
  • Loading branch information
rpiazza committed Apr 9, 2024
1 parent f604a76 commit dce689a
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions stix2/datastore/relational_db/input_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def generate_insert_information(self, name, stix_object, data_sink=None, table_n
"id": stix_object["id"],
"ext_table_name": canonicalize_table_name(ex_name, schema_name),
}
ex_table = data_sink.tables_dictionary[table_name + "_" + "extensions"]
ex_table = data_sink.tables_dictionary[canonicalize_table_name(table_name + "_" + "extensions", schema_name)]
input_statements.append(insert(ex_table).values(bindings))
input_statements.extend(
generate_insert_for_sub_object(
Expand Down Expand Up @@ -158,13 +158,14 @@ def generate_insert_information(self, name, stix_object, **kwargs): # noqa: F81


@add_method(ListProperty)
def generate_insert_information(self, name, stix_object, level=0, is_extension=False, foreign_key_value=None, **kwargs): # noqa: F811
def generate_insert_information(self, name, stix_object, level=0, is_extension=False,
foreign_key_value=None, schema_name=None, **kwargs): # noqa: F811
data_sink = kwargs.get("data_sink")
table_name = kwargs.get("table_name")
if isinstance(self.contained, ReferenceProperty):
insert_statements = list()

table = data_sink.tables_dictionary[canonicalize_table_name(table_name + "_" + name)]
table = data_sink.tables_dictionary[canonicalize_table_name(table_name + "_" + name, schema_name)]
for idx, item in enumerate(stix_object[name]):
bindings = {
"id": stix_object["id"] if id in stix_object else foreign_key_value,
Expand All @@ -177,7 +178,7 @@ def generate_insert_information(self, name, stix_object, level=0, is_extension=F
for value in stix_object[name]:
with data_sink.database_connection.begin() as trans:
next_id = trans.execute(data_sink.sequence)
table = data_sink.tables_dictionary[canonicalize_table_name(table_name + "_" + name)]
table = data_sink.tables_dictionary[canonicalize_table_name(table_name + "_" + name, schema_name)]
bindings = {
"id": foreign_key_value,
"ref_id": next_id,
Expand All @@ -188,7 +189,7 @@ def generate_insert_information(self, name, stix_object, level=0, is_extension=F
data_sink,
value,
table_name + "_" + name + "_" + self.contained.type.__name__,
None,
schema_name,
next_id,
level,
True,
Expand Down Expand Up @@ -355,23 +356,21 @@ def generate_insert_for_sub_object(
if type_name.startswith("extension-definition"):
type_name = type_name[0:30]
type_name = type_name.replace("extension-definition-", "ext_def")
table_name = canonicalize_table_name(type_name, schema_name)
object_table = data_sink.tables_dictionary[table_name]
sub_insert_statements = list()
for name, prop in stix_object._properties.items():
if name in stix_object:
result = prop.generate_insert_information(
name,
stix_object,
data_sink=data_sink,
table_name=table_name if isinstance(prop, (DictionaryProperty, ListProperty)) else parent_table_name,
schema_name=None,
table_name=type_name if isinstance(prop, (DictionaryProperty, ListProperty)) else parent_table_name,
schema_name=schema_name,
foreign_key_value=foreign_key_value,
is_embedded_object=is_embedded_object,
is_list=is_list,
level=level+1,
is_extension=is_extension,
parent_table_name=table_name,
parent_table_name=type_name,
)
if isinstance(result, dict):
bindings.update(result)
Expand All @@ -381,6 +380,7 @@ def generate_insert_for_sub_object(
raise ValueError("wrong type" + result)
if foreign_key_value:
bindings["id"] = foreign_key_value
object_table = data_sink.tables_dictionary[canonicalize_table_name(type_name, schema_name)]
insert_statements.append(insert(object_table).values(bindings))
insert_statements.extend(sub_insert_statements)
return insert_statements
Expand All @@ -394,8 +394,6 @@ def generate_insert_for_object(data_sink, stix_object, schema_name, level=0):
else:
core_properties = SDO_COMMON_PROPERTIES
type_name = stix_object["type"]
table_name = canonicalize_table_name(type_name, schema_name)
object_table = data_sink.tables_dictionary[table_name]
insert_statements.extend(generate_insert_for_core(data_sink, stix_object, core_properties, schema_name))
if "id" in stix_object:
foreign_key_value = stix_object["id"]
Expand All @@ -407,7 +405,7 @@ def generate_insert_for_object(data_sink, stix_object, schema_name, level=0):
result = prop.generate_insert_information(
name, stix_object,
data_sink=data_sink,
table_name=table_name,
table_name=type_name,
schema_name=schema_name,
parent_table_name=type_name,
level=level,
Expand All @@ -420,6 +418,7 @@ def generate_insert_for_object(data_sink, stix_object, schema_name, level=0):
else:
raise ValueError("wrong type" + result)

object_table = data_sink.tables_dictionary[canonicalize_table_name(type_name, schema_name)]
insert_statements.append(insert(object_table).values(bindings))
insert_statements.extend(sub_insert_statements)

Expand Down

0 comments on commit dce689a

Please sign in to comment.