Example #1
0
    def update_by_model(self,
                        model: object,
                        table_name,
                        not_null=False,
                        **kwargs):
        """更新的方法

        :param model: 带更新对象
        :param table_name: 对应数据库表名
        :param not_null: model中值为空时是否进行更新,默认更新所有值
        :param kwargs: 标识字段,用于生成where子句
        :return: 0或1
        """
        if commonUtil.isBlank(kwargs):
            # 不允许全表进行更新,必须设置条件
            raise SqlConditionMissingError("更新语句缺少限制字段")
        model_dict = model.__dict__
        sql = "UPDATE " + table_name + " SET "
        effective_sql = False
        for key, value in model_dict.items():
            if not not_null or value:
                effective_sql = True
                if value is None:
                    value = ""
                if type(value) == int or type(value) == float:
                    sql += key + "=" + str(value) + ","
                else:
                    sql += key + "='" + str(value) + "',"
        if effective_sql:
            sql = sql.strip(",")
            sql += combine_condition(**kwargs)
            return self.execute(sql)
Example #2
0
    def update(self, table_name, cols_values: Dict[str, Any], condition):
        """更新的方法

        :param table_name: 表名
        :param cols_values: 要更新的列与值
        :param condition: 更新条件
        :return:
        """

        cols = cols_values.keys()
        values = cols_values.values()
        if commonUtil.isBlank(condition):
            raise SqlConditionMissingError
        if not cols_values:
            raise ArgumentMissingError(method="update",
                                       missing_args="cols_values")
        sql = f"update {table_name} set "

        for key, value in cols_values.items():
            if value is None:
                value = ""
            elif type(value) == int or type(value) == float:
                sql += key + "=" + str(value) + ","
            else:
                sql += key + "='" + str(value) + "',"
        sql = sql.strip(",")
        sql += " " + condition
        return self.execute(sql)
Example #3
0
    def delete(self, table_name, condition):
        """删除的方法

        :param table_name: 表名
        :param condition: 条件
        :return:
        """
        if commonUtil.isBlank(condition):
            raise SqlConditionMissingError("删除操作必须传入条件限制!")
        sql = f"delete from {table_name}{condition}"
        return self.execute(sql)
Example #4
0
def chrome_header_parse(str):
    """ 由于chrome浏览器F12看到的header不是标准的json或字典形式,将其转换为字典返回

    :param str: 输入字符传
    :return: dict
    """
    result = {}

    key_values = str.split("\n")
    for key_value in key_values:
        if commonUtil.isBlank(key_value):
            continue
        key_value = key_value.strip().split(":")
        result[key_value[0].strip()] = key_value[1].strip()
    return result
Example #5
0
    def update_many(self, models: list, table_name, keys, not_null=False):
        if commonUtil.isBlank(models):
            return 1

        if not keys:
            raise SqlConditionMissingError("更新方法key值缺失")

        model = models[0]

        for key in keys:
            if not hasattr(model, key):
                raise SqlConditionNotExistError("key值非表中字段")
        for model in models:
            where_condition = {}
            for key in keys:
                where_condition[key] = getattr(model, key)
            self.update_by_model(model,
                                 table_name=table_name,
                                 not_null=not_null,
                                 **where_condition)
 def test_isBlank(self):
     self.assertEqual(
         commonUtil.isBlank("") and commonUtil.isBlank("   ")
         and commonUtil.isBlank({}) and commonUtil.isBlank([])
         and commonUtil.isBlank(None), True)