Exemple #1
0
    def test_orWith(self):

        tests = (
            (expression.isExpression("A", "1", True),
             expression.isExpression("B", "2", True),
             "(is(A, 1, True) OR is(B, 2, True))"),
            (expression.isExpression("A", "1", True),
             expression.andExpression((expression.isExpression("B", "2",
                                                               True), )),
             "(is(A, 1, True) OR is(B, 2, True))"),
            (expression.isExpression("A", "1", True),
             expression.andExpression((
                 expression.isExpression("B", "2", True),
                 expression.isExpression("C", "3", True),
             )), "(is(A, 1, True) OR (is(B, 2, True) AND is(C, 3, True)))"),
            (expression.isExpression("A", "1", True),
             expression.orExpression((expression.isExpression("B", "2",
                                                              True), )),
             "(is(A, 1, True) OR is(B, 2, True))"),
            (expression.isExpression("A", "1", True),
             expression.orExpression((
                 expression.isExpression("B", "2", True),
                 expression.isExpression("C", "3", True),
             )), "(is(A, 1, True) OR is(B, 2, True) OR is(C, 3, True))"),
            (expression.andExpression((expression.isExpression("A", "1",
                                                               True), )),
             expression.isExpression("B", "2", True),
             "(is(A, 1, True) OR is(B, 2, True))"),
            (expression.andExpression((
                expression.isExpression("A", "1", True),
                expression.isExpression("B", "2", True),
            )), expression.isExpression("C", "3", True),
             "((is(A, 1, True) AND is(B, 2, True)) OR is(C, 3, True))"),
            (expression.orExpression((expression.isExpression("A", "1",
                                                              True), )),
             expression.isExpression("B", "2", True),
             "(is(A, 1, True) OR is(B, 2, True))"),
            (expression.orExpression((
                expression.isExpression("A", "1", True),
                expression.isExpression("B", "2", True),
            )), expression.isExpression("C", "3", True),
             "(is(A, 1, True) OR is(B, 2, True) OR is(C, 3, True))"),
        )

        for expr1, expr2, result in tests:
            self.assertEqual(str(expr1.orWith(expr2)),
                             result,
                             msg="Failed on %s" % (result, ))
Exemple #2
0
def propfilterListExpression(propfilters, fields):
    """
    Create an expression for a list of prop-filter elements.

    @param propfilters: the C{list} of L{ComponentFilter} elements.
    @return: a L{baseExpression} for the expression tree.
    """

    if len(propfilters) == 1:
        return propfilterExpression(propfilters[0], fields)
    else:
        return expression.orExpression([propfilterExpression(c, fields) for c in propfilters])
Exemple #3
0
def propfilterListExpression(propfilters, fields):
    """
    Create an expression for a list of prop-filter elements.

    @param propfilters: the C{list} of L{ComponentFilter} elements.
    @return: a L{baseExpression} for the expression tree.
    """

    if len(propfilters) == 1:
        return propfilterExpression(propfilters[0], fields)
    else:
        return expression.orExpression([propfilterExpression(c, fields) for c in propfilters])
    def test_or_query(self):

        resource = self.FakeHomeChild()
        obj = resource._objectSchema
        expr = expression.orExpression((
            expression.isExpression(obj.UID, 5678, False),
            expression.isnotExpression(obj.RESOURCE_NAME, "foobar.ics", False),
        ))
        select, args = SQLQueryGenerator(expr, resource,
                                         resource.id()).generate()
        self.assertEqual(
            select.toSQL(),
            SQLFragment(
                "select distinct RESOURCE_NAME, ICALENDAR_UID from CALENDAR_OBJECT where CALENDAR_RESOURCE_ID = ? and (ICALENDAR_UID = ? or RESOURCE_NAME != ?)",
                [1234, 5678, "foobar.ics"]))
        self.assertEqual(args, {})
    def test_or_query(self):

        resource = self.FakeHomeChild()
        obj = resource._objectSchema
        expr = expression.orExpression((
            expression.isExpression(obj.UID, 5678, False),
            expression.isnotExpression(obj.RESOURCE_NAME, "foobar.ics", False),
        ))
        select, args = SQLQueryGenerator(expr, resource, resource.id()).generate()
        self.assertEqual(
            select.toSQL(),
            SQLFragment(
                "select distinct RESOURCE_NAME, ICALENDAR_UID from CALENDAR_OBJECT where CALENDAR_RESOURCE_ID = ? and (ICALENDAR_UID = ? or RESOURCE_NAME != ?)",
                [1234, 5678, "foobar.ics"]
            )
        )
        self.assertEqual(args, {})
Exemple #6
0
def propfilterExpression(propfilter, fields):
    """
    Create an expression for a single prop-filter element.

    @param propfilter: the L{PropertyFilter} element.
    @return: a L{baseExpression} for the expression tree.
    """

    # Only handle UID right now
    if propfilter.filter_name != "UID":
        raise ValueError

    # Handle is-not-defined case
    if not propfilter.defined:
        # Test for <<field>> != "*"
        return expression.isExpression(fields["UID"], "", True)

    # Handle embedded parameters/text-match
    params = []
    for filter in propfilter.filters:
        if isinstance(filter, TextMatch):
            if filter.match_type == "equals":
                tm = expression.isnotExpression if filter.negate else expression.isExpression
            elif filter.match_type == "contains":
                tm = expression.notcontainsExpression if filter.negate else expression.containsExpression
            elif filter.match_type == "starts-with":
                tm = expression.notstartswithExpression if filter.negate else expression.startswithExpression
            elif filter.match_type == "ends-with":
                tm = expression.notendswithExpression if filter.negate else expression.endswithExpression
            params.append(
                tm(fields[propfilter.filter_name], str(filter.text), True))
        else:
            # No embedded parameters - not right now as our Index does not handle them
            raise ValueError

    # Now build return expression
    if len(params) > 1:
        if propfilter.propfilter_test == "anyof":
            return expression.orExpression(params)
        else:
            return expression.andExpression(params)
    elif len(params) == 1:
        return params[0]
    else:
        return None
Exemple #7
0
def propfilterExpression(propfilter, fields):
    """
    Create an expression for a single prop-filter element.

    @param propfilter: the L{PropertyFilter} element.
    @return: a L{baseExpression} for the expression tree.
    """

    # Only handle UID right now
    if propfilter.filter_name != "UID":
        raise ValueError

    # Handle is-not-defined case
    if not propfilter.defined:
        # Test for <<field>> != "*"
        return expression.isExpression(fields["UID"], "", True)

    # Handle embedded parameters/text-match
    params = []
    for filter in propfilter.filters:
        if isinstance(filter, TextMatch):
            if filter.match_type == "equals":
                tm = expression.isnotExpression if filter.negate else expression.isExpression
            elif filter.match_type == "contains":
                tm = expression.notcontainsExpression if filter.negate else expression.containsExpression
            elif filter.match_type == "starts-with":
                tm = expression.notstartswithExpression if filter.negate else expression.startswithExpression
            elif filter.match_type == "ends-with":
                tm = expression.notendswithExpression if filter.negate else expression.endswithExpression
            params.append(tm(fields[propfilter.filter_name], str(filter.text), True))
        else:
            # No embedded parameters - not right now as our Index does not handle them
            raise ValueError

    # Now build return expression
    if len(params) > 1:
        if propfilter.propfilter_test == "anyof":
            return expression.orExpression(params)
        else:
            return expression.andExpression(params)
    elif len(params) == 1:
        return params[0]
    else:
        return None
    def test_orWith(self):

        tests = (
            (
                expression.isExpression("A", "1", True),
                expression.isExpression("B", "2", True),
                "(is(A, 1, True) OR is(B, 2, True))"
            ),
            (
                expression.isExpression("A", "1", True),
                expression.andExpression((
                    expression.isExpression("B", "2", True),
                )),
                "(is(A, 1, True) OR is(B, 2, True))"
            ),
            (
                expression.isExpression("A", "1", True),
                expression.andExpression((
                    expression.isExpression("B", "2", True),
                    expression.isExpression("C", "3", True),
                )),
                "(is(A, 1, True) OR (is(B, 2, True) AND is(C, 3, True)))"
            ),
            (
                expression.isExpression("A", "1", True),
                expression.orExpression((
                    expression.isExpression("B", "2", True),
                )),
                "(is(A, 1, True) OR is(B, 2, True))"
            ),
            (
                expression.isExpression("A", "1", True),
                expression.orExpression((
                    expression.isExpression("B", "2", True),
                    expression.isExpression("C", "3", True),
                )),
                "(is(A, 1, True) OR is(B, 2, True) OR is(C, 3, True))"
            ),
            (
                expression.andExpression((
                    expression.isExpression("A", "1", True),
                )),
                expression.isExpression("B", "2", True),
                "(is(A, 1, True) OR is(B, 2, True))"
            ),
            (
                expression.andExpression((
                    expression.isExpression("A", "1", True),
                    expression.isExpression("B", "2", True),
                )),
                expression.isExpression("C", "3", True),
                "((is(A, 1, True) AND is(B, 2, True)) OR is(C, 3, True))"
            ),
            (
                expression.orExpression((
                    expression.isExpression("A", "1", True),
                )),
                expression.isExpression("B", "2", True),
                "(is(A, 1, True) OR is(B, 2, True))"
            ),
            (
                expression.orExpression((
                    expression.isExpression("A", "1", True),
                    expression.isExpression("B", "2", True),
                )),
                expression.isExpression("C", "3", True),
                "(is(A, 1, True) OR is(B, 2, True) OR is(C, 3, True))"
            ),
        )

        for expr1, expr2, result in tests:
            self.assertEqual(str(expr1.orWith(expr2)), result, msg="Failed on %s" % (result,))