如何比较SQLAlchemy中的版本号? (给定一个功能)

时间:2011-08-23 21:10:12

标签: python sqlalchemy comparator

我有这个功能来比较版本号:

def version_gt(left, right):
    """
    Compare two version numbers telling which is greater    

    Returns True if left is greater, False if right is greater.
    """
    left = map(int, filter(None, str(left).split('.')))
    right = map(int, filter(None, str(right).split('.')))
    for i in range(max([len(left), len(right)])):
        if i > len(left)-1:
            # left ran out of version numbers, therefore right is greater
            return False
        elif i > len(right)-1:
            # right ran out of version numbers therefore left is greater
            return True
        else:
            # compare versions only if the version number is different
            if left[i] != right[i]:
                return left[i] > right[i]
    return False # if all is equal, return False since left is not greater

如何在SQLAlchemy中使用它来比较select语句中的版本号?

我无法弄清楚在线文档。谢谢你的帮助。 -Sunjay03

[编辑]以下是我如何定义列:

Table("updates", metadata,
    Column("u_id", Integer, primary_key=True),
    Column("appname", String, nullable=False),
    Column("version", String, nullable=False),
    Column("uploaded", DateTime, default=func.now()),
)

答案与此有关: http://www.sqlalchemy.org/docs/orm/mapper_config.html#custom-comparators

我无法理解,也许你可以。

1 个答案:

答案 0 :(得分:1)

sqlite数据库引擎在单个列上进行那种比较的表现力还不够,即使在原始SQL中也是如此。最好的选择是将version列拆分成几个,或者简单地接受一个简单的字符串比较(对updates1.version > updates2.version的影响。如果这些都不是一个选项,你需要使用您需要的功能扩展sqlite引擎(通过sqlite_create_function())。