|
| 1 | +import os |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +from sqlalchemy import TIMESTAMP, LargeBinary, Text, VARCHAR |
| 5 | +from sqlalchemy.schema import CreateSchema |
| 6 | + |
| 7 | +from stix2.base import ( |
| 8 | + _DomainObject, _MetaObject, _Observable, _RelationshipObject, |
| 9 | +) |
| 10 | +from stix2.datastore.relational_db.utils import schema_for |
| 11 | + |
| 12 | +from .database_backend_base import DatabaseBackend |
| 13 | + |
| 14 | + |
| 15 | +class MariaDBBackend(DatabaseBackend): |
| 16 | + default_database_connection_url = \ |
| 17 | + f"mariadbsql://{os.getenv('MARIADB_USER')}:" + \ |
| 18 | + f"{os.getenv('MARIADB_PASSWORD')}@" + \ |
| 19 | + f"{os.getenv('MARIADB_IP_ADDRESS')}:" + \ |
| 20 | + f"{os.getenv('MARIADB_PORT', '3306')}/rdb" |
| 21 | + |
| 22 | + def __init__(self, database_connection_url=default_database_connection_url, force_recreate=False, **kwargs: Any): |
| 23 | + super().__init__(database_connection_url, force_recreate=force_recreate, **kwargs) |
| 24 | + |
| 25 | + # ========================================================================= |
| 26 | + # schema methods |
| 27 | + |
| 28 | + def _create_schemas(self): |
| 29 | + with self.database_connection.begin() as trans: |
| 30 | + trans.execute(CreateSchema("common", if_not_exists=True)) |
| 31 | + trans.execute(CreateSchema("sdo", if_not_exists=True)) |
| 32 | + trans.execute(CreateSchema("sco", if_not_exists=True)) |
| 33 | + trans.execute(CreateSchema("sro", if_not_exists=True)) |
| 34 | + |
| 35 | + @staticmethod |
| 36 | + def determine_schema_name(stix_object): |
| 37 | + if isinstance(stix_object, _DomainObject): |
| 38 | + return "sdo" |
| 39 | + elif isinstance(stix_object, _Observable): |
| 40 | + return "sco" |
| 41 | + elif isinstance(stix_object, _RelationshipObject): |
| 42 | + return "sro" |
| 43 | + elif isinstance(stix_object, _MetaObject): |
| 44 | + return "common" |
| 45 | + |
| 46 | + @staticmethod |
| 47 | + def schema_for(stix_class): |
| 48 | + return schema_for(stix_class) |
| 49 | + |
| 50 | + @staticmethod |
| 51 | + def schema_for_core(): |
| 52 | + return "common" |
| 53 | + |
| 54 | + # ========================================================================= |
| 55 | + # sql type methods (overrides) |
| 56 | + |
| 57 | + @staticmethod |
| 58 | + def determine_sql_type_for_key_as_id(): # noqa: F811 |
| 59 | + return VARCHAR(255) |
| 60 | + |
| 61 | + @staticmethod |
| 62 | + def determine_sql_type_for_binary_property(): # noqa: F811 |
| 63 | + return MariaDBBackend.determine_sql_type_for_string_property() |
| 64 | + |
| 65 | + @staticmethod |
| 66 | + def determine_sql_type_for_hex_property(): # noqa: F811 |
| 67 | + # return LargeBinary |
| 68 | + return MariaDBBackend.determine_sql_type_for_string_property() |
| 69 | + |
| 70 | + @staticmethod |
| 71 | + def determine_sql_type_for_timestamp_property(): # noqa: F811 |
| 72 | + return TIMESTAMP(timezone=True) |
| 73 | + |
| 74 | + # ========================================================================= |
| 75 | + # Other methods |
| 76 | + |
| 77 | + @staticmethod |
| 78 | + def array_allowed(): |
| 79 | + return False |
| 80 | + |
| 81 | + @staticmethod |
| 82 | + def create_regex_constraint_expression(column_name, pattern): |
| 83 | + return f"{column_name} REGEXP {pattern}" |
0 commit comments