Skip to content

Commit

Permalink
Initial commit to support MariaDB backedn.
Browse files Browse the repository at this point in the history
  • Loading branch information
mfang90739 committed Dec 9, 2024
1 parent f11cce1 commit a905a89
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def determine_sql_type_for_hex_property(): # noqa: F811
def determine_sql_type_for_timestamp_property(): # noqa: F811
pass

@staticmethod
def determine_sql_type_for_key_as_id(): # noqa: F811
pass

# ------------------------------------------------------------------
# Common SQL types for STIX property classes

Expand Down Expand Up @@ -98,10 +102,6 @@ def determine_sql_type_for_string_property(): # noqa: F811
def determine_sql_type_for_key_as_int(): # noqa: F811
return Integer

@staticmethod
def determine_sql_type_for_key_as_id(): # noqa: F811
return Text

# =========================================================================
# Other methods

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def schema_for_core():
# =========================================================================
# sql type methods (overrides)

@staticmethod
def determine_sql_type_for_key_as_id(): # noqa: F811
return PostgresBackend.determine_sql_type_for_string_property()

@staticmethod
def determine_sql_type_for_binary_property(): # noqa: F811
return PostgresBackend.determine_sql_type_for_string_property()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def set_sqlite_pragma(dbapi_connection, connection_record):
# =========================================================================
# sql type methods (overrides)

@staticmethod
def determine_sql_type_for_key_as_id(): # noqa: F811
return SQLiteBackend.determine_sql_type_for_string_property()

@staticmethod
def determine_sql_type_for_binary_property(): # noqa: F811
return SQLiteBackend.determine_sql_type_for_string_property()
Expand Down
5 changes: 4 additions & 1 deletion stix2/datastore/relational_db/relational_db_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from database_backends.postgres_backend import PostgresBackend
from database_backends.sqlite_backend import SQLiteBackend
from database_backends.mariadb_backend import MariaDBBackend
import pytz
import os

import stix2
from stix2.datastore.relational_db.relational_db import RelationalDBStore
Expand Down Expand Up @@ -289,7 +291,8 @@ def test_dictionary():
def main():
store = RelationalDBStore(
#PostgresBackend("postgresql://localhost/stix-data-sink", force_recreate=True),
SQLiteBackend("sqlite:///stix-data-sink.db", force_recreate=True),
#SQLiteBackend("sqlite:///stix-data-sink.db", force_recreate=True),
MariaDBBackend("mariadb+pymysql://{os.getenv('MARIADB_USER')}:{os.getenv('MARIADB_PASSWORD')}@127.0.0.1:3306/rdb", force_recreate=True),
True,
None,
True,
Expand Down
18 changes: 9 additions & 9 deletions stix2/datastore/relational_db/table_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def create_granular_markings_table(metadata, db_backend, sco_or_sdo):
"marking_ref",
db_backend.determine_sql_type_for_reference_property(),
CheckConstraint(
"marking_ref ~ '^marking-definition--[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$'",
"marking_ref REGEXP '^marking-definition--[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$'",
# noqa: E131
),
),
Expand Down Expand Up @@ -236,7 +236,7 @@ def create_external_references_tables(metadata, db_backend):
db_backend.determine_sql_type_for_key_as_id(),
ForeignKey("common.core_sdo" + ".id", ondelete="CASCADE"),
CheckConstraint(
"id ~ '^[a-z][a-z0-9-]+[a-z0-9]--[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$'", # noqa: E131
"id REGEXP '^[a-z][a-z0-9-]+[a-z0-9]--[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$'", # noqa: E131
),
),
Column("source_name", db_backend.determine_sql_type_for_string_property()),
Expand All @@ -260,7 +260,7 @@ def create_core_table(metadata, db_backend, stix_type_name):
"id",
db_backend.determine_sql_type_for_key_as_id(),
CheckConstraint(
"id ~ '^[a-z][a-z0-9-]+[a-z0-9]--[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$'", # noqa: E131
"id REGEXP '^[a-z][a-z0-9-]+[a-z0-9]--[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$'", # noqa: E131
),
primary_key=True,
),
Expand All @@ -272,7 +272,7 @@ def create_core_table(metadata, db_backend, stix_type_name):
"created_by_ref",
db_backend.determine_sql_type_for_reference_property(),
CheckConstraint(
"created_by_ref ~ '^identity--[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$'", # noqa: E131
"created_by_ref REGEXP '^identity--[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$'", # noqa: E131
),
),
Column("created", db_backend.determine_sql_type_for_timestamp_property()),
Expand Down Expand Up @@ -407,7 +407,7 @@ def generate_table_information(self, name, db_backend, **kwargs): # noqa: F811
self.determine_sql_type(db_backend),
CheckConstraint(
# this regular expression might accept or reject some legal base64 strings
f"{name} ~ " + "'^[-A-Za-z0-9+/]*={0,3}$'",
f"{name} REGEXP " + "'^[-A-Za-z0-9+/]*={0,3}$'",
),
nullable=not self.required,
)
Expand Down Expand Up @@ -535,7 +535,7 @@ def generate_table_information(self, name, db_backend, **kwargs): # noqa: F811
name,
self.determine_sql_type(db_backend),
CheckConstraint(
f"{name} ~ '^{enum_re}$'",
f"{name} REGEXP '^{enum_re}$'",
),
nullable=not self.required,
)
Expand Down Expand Up @@ -630,7 +630,7 @@ def generate_table_information(self, name, db_backend, **kwargs): # noqa: F811
db_backend.determine_sql_type_for_key_as_id(),
ForeignKey(foreign_key_column, ondelete="CASCADE"),
CheckConstraint(
f"{name} ~ '^{table_name}" + "--[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$'",
f"{name} REGEXP '^{table_name}" + "--[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$'",
# noqa: E131
),
primary_key=True,
Expand Down Expand Up @@ -745,13 +745,13 @@ def ref_column(name, specifics, db_backend, auth_type=0):
if auth_type == 0:
constraint = \
CheckConstraint(
f"{name} ~ '^({types})" +
f"{name} REGEXP '^({types})" +
"--[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$'",
)
else:
constraint = \
CheckConstraint(
f"(NOT({name} ~ '^({types})')) AND ({name} ~ " +
f"(NOT({name} REGEXP '^({types})')) AND ({name} REGEXP " +
"'--[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$')",
)
return Column(name, db_backend.determine_sql_type_for_reference_property(), constraint)
Expand Down

0 comments on commit a905a89

Please sign in to comment.