Skip to content

Commit

Permalink
fix insert of dictionary values
Browse files Browse the repository at this point in the history
  • Loading branch information
rpiazza committed Sep 17, 2024
1 parent 3f17c0f commit e508f99
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
20 changes: 15 additions & 5 deletions stix2/datastore/relational_db/input_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ def generate_insert_information(self, name, stix_object, **kwargs): # noqa: F81
return {name: stix_object[name]}



def instance_in_valid_types(cls, valid_types):
for v in valid_types:
if isinstance(v, cls):
return True
return False

def is_valid_type(cls, valid_types):
return cls in valid_types or instance_in_valid_types(cls, valid_types)

@add_method(DictionaryProperty)
def generate_insert_information(self, dictionary_name, stix_object, **kwargs): # noqa: F811
bindings = dict()
Expand Down Expand Up @@ -57,15 +67,15 @@ def generate_insert_information(self, dictionary_name, stix_object, **kwargs):
bindings["id"] = foreign_key_value
if not valid_types or len(self.valid_types) == 1:
value_binding = "value"
elif isinstance(value, int) and IntegerProperty in valid_types:
elif isinstance(value, int) and is_valid_type(IntegerProperty, valid_types):
value_binding = "integer_value"
elif isinstance(value, str) and StringProperty in valid_types:
elif isinstance(value, str) and is_valid_type(StringProperty, valid_types):
value_binding = "string_value"
elif isinstance(value, bool) and BooleanProperty in valid_types:
elif isinstance(value, bool) and is_valid_type(BooleanProperty, valid_types):
value_binding = "boolean_value"
elif isinstance(value, float) and FloatProperty in valid_types:
elif isinstance(value, float) and is_valid_type(FloatProperty, valid_types):
value_binding = "float_value"
elif isinstance(value, STIXdatetime) and TimestampProperty in valid_types:
elif isinstance(value, STIXdatetime) and is_valid_type(TimestampProperty, valid_types):
value_binding = "timestamp_value"
else:
value_binding = "string_value"
Expand Down
38 changes: 20 additions & 18 deletions stix2/datastore/relational_db/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def get_all_subclasses(cls):
for subclass in cls.__subclasses__():
# This code might be useful if we decide that some objects just cannot have there tables
# automatically generated

# if hasattr(subclass, "_type") and subclass._type in _IGNORE_OBJECTS:
# print(f'It is currently not possible to create a table for {subclass._type}')
# return []
Expand Down Expand Up @@ -132,39 +132,41 @@ def flat_classes(class_or_classes):
yield class_or_classes


def is_class_or_instance(cls_or_inst, cls):
return cls_or_inst == cls or isinstance(cls_or_inst, cls)


def determine_sql_type_from_class(cls_or_inst): # noqa: F811
if cls_or_inst == BinaryProperty or isinstance(cls_or_inst, BinaryProperty):
if is_class_or_instance(cls_or_inst, BinaryProperty):
return LargeBinary
elif cls_or_inst == BooleanProperty or isinstance(cls_or_inst, BooleanProperty):
elif is_class_or_instance(cls_or_inst, BooleanProperty):
return Boolean
elif cls_or_inst == FloatProperty or isinstance(cls_or_inst, FloatProperty):
elif is_class_or_instance(cls_or_inst, FloatProperty ):
return Float
elif cls_or_inst == HexProperty or isinstance(cls_or_inst, HexProperty):
elif is_class_or_instance(cls_or_inst, HexProperty):
return LargeBinary
elif cls_or_inst == IntegerProperty or isinstance(cls_or_inst, IntegerProperty):
elif is_class_or_instance(cls_or_inst, IntegerProperty):
return Integer
elif (cls_or_inst == StringProperty or cls_or_inst == ReferenceProperty or
isinstance(cls_or_inst, StringProperty) or isinstance(cls_or_inst, ReferenceProperty)):
elif is_class_or_instance(cls_or_inst, StringProperty) or is_class_or_instance(cls_or_inst, ReferenceProperty):
return Text
elif cls_or_inst == TimestampProperty or isinstance(cls_or_inst, TimestampProperty):
elif is_class_or_instance(cls_or_inst, TimestampProperty):
return TIMESTAMP(timezone=True)
elif cls_or_inst == Property or isinstance(cls_or_inst, Property):
elif is_class_or_instance(cls_or_inst, Property):
return Text


def determine_column_name(cls_or_inst): # noqa: F811
if cls_or_inst == BinaryProperty or isinstance(cls_or_inst, BinaryProperty):
if is_class_or_instance(cls_or_inst, BinaryProperty):
return "binary_value"
elif cls_or_inst == BooleanProperty or isinstance(cls_or_inst, BooleanProperty):
elif is_class_or_instance(cls_or_inst, BooleanProperty):
return "boolean_value"
elif cls_or_inst == FloatProperty or isinstance(cls_or_inst, FloatProperty):
elif is_class_or_instance(cls_or_inst, FloatProperty):
return "float_value"
elif cls_or_inst == HexProperty or isinstance(cls_or_inst, HexProperty):
elif is_class_or_instance(cls_or_inst, HexProperty):
return "hex_value"
elif cls_or_inst == IntegerProperty or isinstance(cls_or_inst, IntegerProperty):
elif is_class_or_instance(cls_or_inst, IntegerProperty):
return "integer_value"
elif (cls_or_inst == StringProperty or cls_or_inst == ReferenceProperty or
isinstance(cls_or_inst, StringProperty) or isinstance(cls_or_inst, ReferenceProperty)):
elif is_class_or_instance(cls_or_inst, StringProperty) or is_class_or_instance(cls_or_inst, ReferenceProperty):
return "string_value"
elif cls_or_inst == TimestampProperty or isinstance(cls_or_inst, TimestampProperty):
elif is_class_or_instance(cls_or_inst, TimestampProperty):
return "timestamp_value"

0 comments on commit e508f99

Please sign in to comment.