database.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:toshi-services-lib 作者: toshiapp 项目源码 文件源码
def update(self, tablename, update_args, query_args=None):
        """Very simple "generic" update helper.
        will generate the update statement, converting the `update_args`
        dict into "key = value, key = value" statements, and converting
        the `query_args` dict into "key = value AND key = value"
        statements. string values will be wrapped in 'quotes', while
        other types will be left as their python representation.
        """

        if not self.transaction:
            raise DatabaseError("No transaction in progress")

        query = "UPDATE {} SET ".format(tablename)
        arglist = []
        qnum = 1
        if isinstance(update_args, dict):
            update_args = update_args.items()
        if isinstance(update_args, (list, tuple, ItemsView)):
            setstmts = []
            for k, v in update_args:
                setstmts.append("{} = ${}".format(k, qnum))
                qnum += 1
                arglist.append(v)
            query += ', '.join(setstmts)
        else:
            raise DatabaseError("expected dict or list for update_args")
        if isinstance(query_args, dict):
            query_args = query_args.items()
        if isinstance(query_args, (list, tuple, ItemsView)):
            query += " WHERE "
            wherestmts = []
            # TODO: support OR somehow?
            for k, v in query_args:
                wherestmts.append("{} = ${}".format(k, qnum))
                qnum += 1
                arglist.append(v)
            query += ' AND '.join(wherestmts)
        elif query_args is not None:
            raise DatabaseError("expected dict or list or None for query_args")

        resp = await self.connection.execute(query, *arglist)

        if resp and resp[0].startswith("ERROR:"):
            raise DatabaseError(resp)
        return resp
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号