Skip to content

Commit

Permalink
Reintroduce except_ function on MdxHierarchySet
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusWirtz committed Dec 6, 2022
1 parent 8ee6c25 commit c88a41a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
24 changes: 24 additions & 0 deletions mdxpy/mdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,10 @@ def range(start_member: Union[str, Member], end_member: Union[str, Member]) -> '
end_member = Member.of(end_member)
return RangeHierarchySet(start_member, end_member)

@staticmethod
def unions(sets: List['MdxHierarchySet'], allow_duplicates: bool = False) -> 'MdxHierarchySet':
return MultiUnionHierarchySet(sets, allow_duplicates)

def filter_by_attribute(self, attribute_name: str, attribute_values: List,
operator: Optional[str] = '=') -> 'MdxHierarchySet':
return FilterByAttributeHierarchySet(self, attribute_name, attribute_values, operator)
Expand Down Expand Up @@ -1020,6 +1024,26 @@ def to_mdx(self) -> str:
f"{{STRTOMEMBER('[{self.dimension}].[{self.hierarchy}].[' + [{self.underlying_hierarchy_set.dimension}].[{self.underlying_hierarchy_set.hierarchy}].CURRENTMEMBER.PROPERTIES(\"{self.attribute}\") + ']')}})}}"


class MultiUnionHierarchySet(MdxHierarchySet):

def __init__(self, sets: List[MdxHierarchySet], allow_duplicates: bool = False):
if not sets:
raise RuntimeError('sets must not be empty')

super(MultiUnionHierarchySet, self).__init__(
sets[0].dimension,
sets[0].hierarchy)

self.sets = sets
self.allow_duplicates = allow_duplicates

def to_mdx(self) -> str:
if self.allow_duplicates:
return f"{{{','.join(set_.to_mdx() for set_ in self.sets)}}}"
else:
return f"{{{' + '.join(set_.to_mdx() for set_ in self.sets)}}}"


class MdxAxis:
def __init__(self):
self.tuples: List[MdxTuple] = list()
Expand Down
15 changes: 15 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,9 @@ def test_mdx_hierarchy_descendants_with_flag_and_member_level(self):
def test_mdx_hierarchy_set_range(self):
member1 = Member.of('Dimension', 'Hierarchy', 'Member1')
member2 = Member.of('Dimension', 'Hierarchy', 'Member2')

hierarchy_set = MdxHierarchySet.range(member1, member2)

self.assertEqual("{[dimension].[hierarchy].[member1]:[dimension].[hierarchy].[member2]}",
hierarchy_set.to_mdx())

Expand Down Expand Up @@ -615,6 +617,19 @@ def test_mdx_hierarchy_set_generate_attribute_to_member(self):
"{STRTOMEMBER('[manager].[manager].[' + [store].[store].CURRENTMEMBER.PROPERTIES(\"Manager\") + ']')})}",
hierarchy_set.to_mdx())

def test_mdx_hierarchy_set_unions_allow_duplicates(self):
hierarchy_set = MdxSet.unions([
MdxHierarchySet.children(Member.of("Dimension", "element1")),
MdxHierarchySet.member(Member.of("Dimension", "element2")),
MdxHierarchySet.member(Member.of("Dimension", "element3"))
], True)

self.assertEqual(
"{{[dimension].[dimension].[element1].CHILDREN},"
"{[dimension].[dimension].[element2]},"
"{[dimension].[dimension].[element3]}}",
hierarchy_set.to_mdx())

def test_mdx_builder_simple(self):
mdx = MdxBuilder.from_cube("cube") \
.rows_non_empty() \
Expand Down

0 comments on commit c88a41a

Please sign in to comment.