Exemplo n.º 1
0
def restriction(alias, column, operator, value, map_null_operator=True):
    if not column:
        raise Exception("Parameter column may not be None!")
    if operator in ["~", "like"] and isinstance(column.type, Integer):
        try:
            int(value)
            # LIKE not allowed on integer columns, change operator to
            # equals
            operator = "="
        except ValueError:
            pass

    if alias:
        alias = "{0}.".format(alias)
    else:
        alias = ""
    lhs = column.name
    if column.table:
        lhs = "{0}{1}".format(alias, column.name)
    if value and isinstance(column.type, Integer) and type(value) is not list:
        try:
            int(value)
        except ValueError:
            # column type is integer, but value is not
            lhs = "cast({0}{1} as char)".format(alias, column.name)
    if operator in ["=", "!="] and (value == "null" or value is None):
        if map_null_operator:
            operator = {"=": "is", "!=": "is not"}.get(operator)
        value = None
    rhs = format_value(column, value)

    return " ".join([lhs, operator, rhs])
Exemplo n.º 2
0
def restriction(alias, column, operator, value, map_null_operator=True):
    if not column:
        raise Exception('Parameter column may not be None!')
    if operator in ['~', 'like'] and isinstance(column.type, Integer):
        try:
            int(value)
            # LIKE not allowed on integer columns, change operator to
            # equals
            operator = '='
        except ValueError:
            pass

    if alias:
        alias = '{0}.'.format(alias)
    else:
        alias = ''
    lhs = column.name
    if column.table:
        lhs = '{0}{1}'.format(alias, column.name)
    if (value
            and isinstance(column.type, Integer)
            and type(value) is not list):
        try:
            int(value)
        except ValueError:
            # column type is integer, but value is not
            lhs = 'cast({0}{1} as text)'.format(alias, column.name)
    if operator in ['=', '!='] and (value == 'null' or value is None):
        if map_null_operator:
            operator = {
                '=': 'is',
                '!=': 'is not'
            }.get(operator)
        value = None
    rhs = format_value(column, value)

    return ' '.join([lhs, operator, rhs])
Exemplo n.º 3
0
    def test_format_value(self):
        """Tests the options.format_value function"""

        DbTestCase.connection.close()
        DbTestCase.connection = MockSource().list()[0]
        DbTestCase.connection.connect()
        con = DbTestCase.connection
        user = con.table("user")
        user2 = con.table("user2")
        article = con.table("article")
        now = datetime.now()

        self.assertEqual(u"null", options.format_value(None, None))
        self.assertEqual("1", options.format_value(None, 1))
        self.assertEqual("'d'", options.format_value(None, "d"))
        self.assertEqual(u"7", options.format_value(user.column("id"), 7))
        self.assertEqual(u"'a'", options.format_value(user.column("id"), "a"))
        self.assertEqual("null", options.format_value(user.column("id"), None))
        self.assertEqual("true", options.format_value(user2.column("deleted"), True))
        self.assertEqual("3.141500", options.format_value(user2.column("score"), 3.1415))
        self.assertEqual("'3.14.15'", options.format_value(user2.column("score"), "3.14.15"))
        self.assertEqual("'{}'".format(str(now)), options.format_value(article.column("created"), now))
        self.assertEqual("('a', 'b')", options.format_value(article.column("id"), ["a", "b"]))
        self.assertEqual(u"'[BLOB]'", options.format_value(article.column("id"), buffer("abc")))