Skip to content

Commit a905a89

Browse files
committed
Initial commit to support MariaDB backedn.
1 parent f11cce1 commit a905a89

File tree

5 files changed

+25
-14
lines changed

5 files changed

+25
-14
lines changed

stix2/datastore/relational_db/database_backends/database_backend_base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ def determine_sql_type_for_hex_property(): # noqa: F811
6767
def determine_sql_type_for_timestamp_property(): # noqa: F811
6868
pass
6969

70+
@staticmethod
71+
def determine_sql_type_for_key_as_id(): # noqa: F811
72+
pass
73+
7074
# ------------------------------------------------------------------
7175
# Common SQL types for STIX property classes
7276

@@ -98,10 +102,6 @@ def determine_sql_type_for_string_property(): # noqa: F811
98102
def determine_sql_type_for_key_as_int(): # noqa: F811
99103
return Integer
100104

101-
@staticmethod
102-
def determine_sql_type_for_key_as_id(): # noqa: F811
103-
return Text
104-
105105
# =========================================================================
106106
# Other methods
107107

stix2/datastore/relational_db/database_backends/postgres_backend.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ def schema_for_core():
5454
# =========================================================================
5555
# sql type methods (overrides)
5656

57+
@staticmethod
58+
def determine_sql_type_for_key_as_id(): # noqa: F811
59+
return PostgresBackend.determine_sql_type_for_string_property()
60+
5761
@staticmethod
5862
def determine_sql_type_for_binary_property(): # noqa: F811
5963
return PostgresBackend.determine_sql_type_for_string_property()

stix2/datastore/relational_db/database_backends/sqlite_backend.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ def set_sqlite_pragma(dbapi_connection, connection_record):
3030
# =========================================================================
3131
# sql type methods (overrides)
3232

33+
@staticmethod
34+
def determine_sql_type_for_key_as_id(): # noqa: F811
35+
return SQLiteBackend.determine_sql_type_for_string_property()
36+
3337
@staticmethod
3438
def determine_sql_type_for_binary_property(): # noqa: F811
3539
return SQLiteBackend.determine_sql_type_for_string_property()

stix2/datastore/relational_db/relational_db_testing.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
from database_backends.postgres_backend import PostgresBackend
44
from database_backends.sqlite_backend import SQLiteBackend
5+
from database_backends.mariadb_backend import MariaDBBackend
56
import pytz
7+
import os
68

79
import stix2
810
from stix2.datastore.relational_db.relational_db import RelationalDBStore
@@ -289,7 +291,8 @@ def test_dictionary():
289291
def main():
290292
store = RelationalDBStore(
291293
#PostgresBackend("postgresql://localhost/stix-data-sink", force_recreate=True),
292-
SQLiteBackend("sqlite:///stix-data-sink.db", force_recreate=True),
294+
#SQLiteBackend("sqlite:///stix-data-sink.db", force_recreate=True),
295+
MariaDBBackend("mariadb+pymysql://{os.getenv('MARIADB_USER')}:{os.getenv('MARIADB_PASSWORD')}@127.0.0.1:3306/rdb", force_recreate=True),
293296
True,
294297
None,
295298
True,

stix2/datastore/relational_db/table_creation.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def create_granular_markings_table(metadata, db_backend, sco_or_sdo):
174174
"marking_ref",
175175
db_backend.determine_sql_type_for_reference_property(),
176176
CheckConstraint(
177-
"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}$'",
177+
"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}$'",
178178
# noqa: E131
179179
),
180180
),
@@ -236,7 +236,7 @@ def create_external_references_tables(metadata, db_backend):
236236
db_backend.determine_sql_type_for_key_as_id(),
237237
ForeignKey("common.core_sdo" + ".id", ondelete="CASCADE"),
238238
CheckConstraint(
239-
"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
239+
"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
240240
),
241241
),
242242
Column("source_name", db_backend.determine_sql_type_for_string_property()),
@@ -260,7 +260,7 @@ def create_core_table(metadata, db_backend, stix_type_name):
260260
"id",
261261
db_backend.determine_sql_type_for_key_as_id(),
262262
CheckConstraint(
263-
"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
263+
"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
264264
),
265265
primary_key=True,
266266
),
@@ -272,7 +272,7 @@ def create_core_table(metadata, db_backend, stix_type_name):
272272
"created_by_ref",
273273
db_backend.determine_sql_type_for_reference_property(),
274274
CheckConstraint(
275-
"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
275+
"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
276276
),
277277
),
278278
Column("created", db_backend.determine_sql_type_for_timestamp_property()),
@@ -407,7 +407,7 @@ def generate_table_information(self, name, db_backend, **kwargs): # noqa: F811
407407
self.determine_sql_type(db_backend),
408408
CheckConstraint(
409409
# this regular expression might accept or reject some legal base64 strings
410-
f"{name} ~ " + "'^[-A-Za-z0-9+/]*={0,3}$'",
410+
f"{name} REGEXP " + "'^[-A-Za-z0-9+/]*={0,3}$'",
411411
),
412412
nullable=not self.required,
413413
)
@@ -535,7 +535,7 @@ def generate_table_information(self, name, db_backend, **kwargs): # noqa: F811
535535
name,
536536
self.determine_sql_type(db_backend),
537537
CheckConstraint(
538-
f"{name} ~ '^{enum_re}$'",
538+
f"{name} REGEXP '^{enum_re}$'",
539539
),
540540
nullable=not self.required,
541541
)
@@ -630,7 +630,7 @@ def generate_table_information(self, name, db_backend, **kwargs): # noqa: F811
630630
db_backend.determine_sql_type_for_key_as_id(),
631631
ForeignKey(foreign_key_column, ondelete="CASCADE"),
632632
CheckConstraint(
633-
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}$'",
633+
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}$'",
634634
# noqa: E131
635635
),
636636
primary_key=True,
@@ -745,13 +745,13 @@ def ref_column(name, specifics, db_backend, auth_type=0):
745745
if auth_type == 0:
746746
constraint = \
747747
CheckConstraint(
748-
f"{name} ~ '^({types})" +
748+
f"{name} REGEXP '^({types})" +
749749
"--[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}$'",
750750
)
751751
else:
752752
constraint = \
753753
CheckConstraint(
754-
f"(NOT({name} ~ '^({types})')) AND ({name} ~ " +
754+
f"(NOT({name} REGEXP '^({types})')) AND ({name} REGEXP " +
755755
"'--[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}$')",
756756
)
757757
return Column(name, db_backend.determine_sql_type_for_reference_property(), constraint)

0 commit comments

Comments
 (0)