Skip to content

Commit 8b9c10d

Browse files
committed
fix-sqlite-core-properties
1 parent afb737f commit 8b9c10d

File tree

5 files changed

+20
-17
lines changed

5 files changed

+20
-17
lines changed

stix2/datastore/relational_db/demo.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import datetime as dt
22

33
from database_backends.postgres_backend import PostgresBackend
4+
from database_backends.sqlite_backend import SQLiteBackend
45
import sys
56
import json
67

@@ -19,6 +20,7 @@ def main():
1920
bundle = stix2.parse(json.load(f), allow_custom=True)
2021
store = RelationalDBStore(
2122
PostgresBackend("postgresql://localhost/stix-data-sink", force_recreate=True),
23+
# SQLiteBackend("sqlite:///stix-data-sink.db", force_recreate=True),
2224
True,
2325
None,
2426
True,

stix2/datastore/relational_db/input_creation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def generate_insert_information( # noqa: F811
310310
]
311311
for elem in stix_object[name]:
312312
bindings = {
313-
"id": stix_object["id"],
313+
"id": foreign_key_value,
314314
name: db_backend.process_value_for_insert(self.contained, elem),
315315
}
316316
insert_statements.append(insert(table).values(bindings))

stix2/datastore/relational_db/relational_db_testing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,9 @@ def test_dictionary():
290290

291291
def main():
292292
store = RelationalDBStore(
293-
# MariaDBBackend(f"mariadb+pymysql://admin:[email protected]:3306/rdb", force_recreate=True),
293+
MariaDBBackend(f"mariadb+pymysql://admin:[email protected]:3306/rdb", force_recreate=True),
294294
# PostgresBackend("postgresql://localhost/stix-data-sink", force_recreate=True),
295-
SQLiteBackend("sqlite:///stix-data-sink.db", force_recreate=True),
295+
# SQLiteBackend("sqlite:///stix-data-sink.db", force_recreate=True),
296296

297297
True,
298298
None,

stix2/datastore/relational_db/table_creation.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55

66
from stix2.datastore.relational_db.add_method import add_method
77
from stix2.datastore.relational_db.utils import (
8-
SCO_COMMON_PROPERTIES, SDO_COMMON_PROPERTIES, canonicalize_table_name,
9-
determine_column_name, determine_sql_type_from_stix, flat_classes,
10-
get_stix_object_classes, shorten_extension_definition_id
8+
canonicalize_table_name, determine_column_name, determine_core_properties, determine_sql_type_from_stix,
9+
flat_classes, get_stix_object_classes, shorten_extension_definition_id
1110
)
1211
from stix2.properties import (
1312
BinaryProperty, BooleanProperty, DictionaryProperty,
@@ -16,7 +15,8 @@
1615
ObjectReferenceProperty, Property, ReferenceProperty, StringProperty,
1716
TimestampProperty, TypeProperty,
1817
)
19-
from stix2.v21.base import _Extension, _Observable
18+
19+
from stix2.v21.base import (_Extension, _Observable)
2020
from stix2.v21.common import KillChainPhase
2121

2222

@@ -809,14 +809,7 @@ def generate_object_table(
809809
table_name = shorten_extension_definition_id(table_name)
810810
if parent_table_name:
811811
table_name = parent_table_name + "_" + table_name
812-
if is_embedded_object:
813-
core_properties = list()
814-
elif schema_name in ["sdo", "sro", "common"]:
815-
core_properties = SDO_COMMON_PROPERTIES
816-
elif schema_name == "sco":
817-
core_properties = SCO_COMMON_PROPERTIES
818-
else:
819-
core_properties = list()
812+
core_properties = determine_core_properties(stix_object_class, is_embedded_object)
820813
columns = list()
821814
tables = list()
822815
if issubclass(stix_object_class, _Observable):

stix2/datastore/relational_db/utils.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@
4141
}
4242

4343

44+
def determine_core_properties(stix_object_class, is_embedded_object):
45+
if is_embedded_object or issubclass(stix_object_class, (_MetaObject, _Extension)):
46+
return list()
47+
elif issubclass(stix_object_class, (_RelationshipObject, _DomainObject)):
48+
return SDO_COMMON_PROPERTIES
49+
elif issubclass(stix_object_class, _Observable):
50+
return SCO_COMMON_PROPERTIES
51+
else:
52+
raise ValueError(f"{stix_object_class} not a STIX object")
53+
4454
def canonicalize_table_name(table_name, schema_name=None):
4555
if schema_name:
4656
full_name = schema_name + "." + table_name
@@ -103,7 +113,6 @@ def get_stix_object_classes():
103113
)
104114

105115
def schema_for(stix_class):
106-
107116
if issubclass(stix_class, _DomainObject):
108117
schema_name = "sdo"
109118
elif issubclass(stix_class, _RelationshipObject):
@@ -116,7 +125,6 @@ def schema_for(stix_class):
116125
schema_name = getattr(stix_class, "_applies_to", "sco")
117126
else:
118127
schema_name = None
119-
120128
return schema_name
121129

122130

0 commit comments

Comments
 (0)