Skip to content

Commit 2b55b4d

Browse files
committed
handle and expression check contraints
1 parent fab7199 commit 2b55b4d

File tree

5 files changed

+21
-18
lines changed

5 files changed

+21
-18
lines changed

stix2/datastore/relational_db/database_backends/database_backend_base.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Any
22

3-
from sqlalchemy import Boolean, Float, Integer, String, Text, create_engine
3+
from sqlalchemy import Boolean, CheckConstraint, Float, Integer, String, Text, create_engine
44
from sqlalchemy_utils import create_database, database_exists, drop_database
55

66
from stix2.base import (
@@ -122,9 +122,12 @@ def determine_stix_type(stix_object):
122122
def array_allowed():
123123
return False
124124

125-
@staticmethod
126-
def create_regex_constraint_expression(column, pattern):
127-
pass
125+
def create_regex_constraint_expression(self, column_name, pattern):
126+
return CheckConstraint(self.create_regex_constraint_clause(column_name, pattern))
127+
128+
def create_regex_constraint_and_expression(self, clause1, clause2):
129+
return (CheckConstraint("((" + self.create_regex_constraint_clause(clause1[0], clause1[1]) + ") AND (" +
130+
self.create_regex_constraint_clause(clause2[0], clause2[1]) + "))"))
128131

129132
def process_value_for_insert(self, stix_type, value):
130133
sql_type = stix_type.determine_sql_type(self)

stix2/datastore/relational_db/database_backends/mariadb_backend.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ def determine_sql_type_for_timestamp_property(): # noqa: F811
7878
def array_allowed():
7979
return False
8080

81-
@staticmethod
82-
def create_regex_constraint_expression(column_name, pattern):
83-
return CheckConstraint(f"{column_name} REGEXP {pattern}")
81+
def create_regex_constraint_clause(self, column_name, pattern):
82+
return f"{column_name} REGEXP {pattern}"
8483

8584

stix2/datastore/relational_db/database_backends/postgres_backend.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,5 @@ def determine_sql_type_for_timestamp_property(): # noqa: F811
8686
def array_allowed():
8787
return True
8888

89-
@staticmethod
90-
def create_regex_constraint_expression(column_name, pattern):
91-
return CheckConstraint(f"{column_name} ~ {pattern}")
89+
def create_regex_constraint_clause(self, column_name, pattern):
90+
return f"{column_name} ~ {pattern}"

stix2/datastore/relational_db/database_backends/sqlite_backend.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ def determine_sql_type_for_timestamp_property(): # noqa: F811
6464
def array_allowed():
6565
return False
6666

67-
@staticmethod
68-
def create_regex_constraint_expression(column_name, pattern):
67+
def create_regex_constraint_expression(self, column_name, pattern):
68+
return None
69+
70+
def create_regex_constraint_and_expression(self, clause1, clause2):
6971
return None
7072

7173
@staticmethod

stix2/datastore/relational_db/table_creation.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def create_ref_table(metadata, db_backend, specifics, table_name, foreign_key_na
9191
def create_hashes_table(name, metadata, db_backend, schema_name, table_name, key_type=Text, level=1):
9292
columns = list()
9393
# special case, perhaps because its a single embedded object with hashes, and not a list of embedded object
94-
# making the parent table's primary key does seem to worl
94+
# making the parent table's primary key does seem to work
9595

9696
columns.append(
9797
Column(
@@ -720,13 +720,13 @@ def ref_column(name, specifics, db_backend, auth_type=0):
720720
types = "|".join(specifics)
721721
if auth_type == 0:
722722
reg_ex = f"'^({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}$'" # noqa: F811
723-
constraint = db_backend.create_regex_constraint_expression(name, reg_ex)
723+
constraint = db_backend.create_regex_constraint_expression(name, reg_ex)
724724
else:
725725
reg_ex = "'--[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}$')"
726-
# constraint = \
727-
# CheckConstraint(db_backend.create_regex_constraint_expression(f"NOT({name}", f"'^({types})'") + " AND " +
728-
# db_backend.create_regex_constraint_expression(name, reg_ex))
729-
return Column(name, db_backend.determine_sql_type_for_reference_property()) # , constraint)
726+
constraint = \
727+
db_backend.create_regex_constraint_and_expression((f"NOT({name}", f"'^({types})'"),
728+
(name, reg_ex))
729+
return Column(name, db_backend.determine_sql_type_for_reference_property(), constraint) # , constraint)
730730
else:
731731
return Column(
732732
name,

0 commit comments

Comments
 (0)