Skip to content

Commit

Permalink
fix schema name for subtables, composite primary keys, RelationalRBSt…
Browse files Browse the repository at this point in the history
…ore stub
  • Loading branch information
rpiazza committed Mar 27, 2024
1 parent ee49963 commit 9a1663f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
49 changes: 48 additions & 1 deletion stix2/datastore/relational_db/relational_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from sqlalchemy.schema import CreateSchema, CreateTable

from stix2.base import _STIXBase
from stix2.datastore import DataSink
from stix2.datastore import DataSink, DataSource, DataStoreMixin
from stix2.datastore.relational_db.input_creation import (
generate_insert_for_object,
)
Expand Down Expand Up @@ -62,6 +62,42 @@ def _add(store, stix_data, allow_custom=True, version="2.1"):
store.insert_object(stix_obj)


class RelationalDBStore(DataStoreMixin):
"""Interface to a file directory of STIX objects.
FileSystemStore is a wrapper around a paired FileSystemSink
and FileSystemSource.
Args:
stix_dir (str): path to directory of STIX objects
allow_custom (bool): whether to allow custom STIX content to be
pushed/retrieved. Defaults to True for FileSystemSource side
(retrieving data) and False for FileSystemSink
side(pushing data). However, when parameter is supplied, it
will be applied to both FileSystemSource and FileSystemSink.
bundlify (bool): whether to wrap objects in bundles when saving
them. Default: False.
encoding (str): The encoding to use when reading a file from the
filesystem.
Attributes:
source (FileSystemSource): FileSystemSource
sink (FileSystemSink): FileSystemSink
"""
def __init__(self, database_connection_url, allow_custom=None, encoding='utf-8'):
if allow_custom is None:
allow_custom_source = True
allow_custom_sink = False
else:
allow_custom_sink = allow_custom_source = allow_custom

super(RelationalDBStore, self).__init__(
source=RelationalDBSource(database_connection_url, allow_custom=allow_custom_source, encoding=encoding),
sink=RelationalDBSink(database_connection_url, allow_custom=allow_custom_sink),
)


class RelationalDBSink(DataSink):
"""Interface for adding/pushing STIX objects to an in-memory dictionary.
Expand Down Expand Up @@ -150,3 +186,14 @@ def insert_object(self, stix_object):
print("executing: ", stmt)
trans.execute(stmt)
trans.commit()


class RelationalDBSource(DataSource):
def get(self, stix_id, version=None, _composite_filters=None):
pass

def all_versions(self, stix_id, version=None, _composite_filters=None):
pass

def query(self, query=None):
pass
5 changes: 2 additions & 3 deletions stix2/datastore/relational_db/table_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ def generate_table_information(self, name, metadata, schema_name, table_name, **
self.contained.specifics,
canonicalize_table_name(table_name + "_" + name),
canonicalize_table_name(table_name, schema_name) + ".id",
"common",
schema_name,
),
]
elif isinstance(self.contained, EmbeddedObjectProperty):
Expand All @@ -502,8 +502,7 @@ def generate_table_information(self, name, metadata, schema_name, table_name, **
ForeignKey(
canonicalize_table_name(table_name, schema_name) + ".id",
ondelete="CASCADE",
),
primary_key=True,
)
),
)
columns.append(
Expand Down

0 comments on commit 9a1663f

Please sign in to comment.