@require_context
@_retry_on_deadlock
def conditional_update(context, model, values, expected_values, filters=(), include_deleted='no', project_only=False, order=None):
 _check_is_not_multitable(values, model)
 where_conds = list(filters)
 for (field, condition) in expected_values.items():
  if (not isinstance(condition, db.Condition)):
   condition = db.Condition(condition, field)
  where_conds.append(condition.get_filter(model, field))
 query = model_query(context, model, read_deleted=include_deleted, project_only=project_only).filter(*where_conds)
 order = (list(order) if order else tuple())
 orm_field_list = []
 case_list = []
 unordered_list = []
 for (key, value) in values.items():
  if isinstance(value, db.Case):
   value = case(value.whens, value.value, value.else_)
  if (key in order):
   order[order.index(key)] = (key, value)
   continue
  if isinstance(value, sql.elements.Case):
   value_list = case_list
  elif is_orm_value(value):
   value_list = orm_field_list
  else:
   value_list = unordered_list
  value_list.append((key, value))
 update_args = {'synchronize_session': False}
 if (order or orm_field_list or case_list):
  values = itertools.chain(order, orm_field_list, case_list, unordered_list)
  update_args['update_args'] = {'preserve_parameter_order': True}
 result = query.update(values, **update_args)
 return (0 != result)

@ require_context @ _retry_on_deadlock def conditional_update ( context , model , values , expected_values , filters = ( ) , include_deleted = 'no' , project_only = False , order = None ) : _check_is_not_multitable ( values , model ) where_conds = list ( filters ) for ( field , condition ) in expected_values . items ( ) : if ( not isinstance ( condition , db . Condition ) ) : condition = db . Condition ( condition , field ) where_conds . append ( condition . get_filter ( model , field ) ) query = model_query ( context , model , read_deleted = include_deleted , project_only = project_only ) . filter ( * where_conds ) order = ( list ( order ) if order else tuple ( ) ) orm_field_list = [ ] case_list = [ ] unordered_list = [ ] for ( key , value ) in values . items ( ) : if isinstance ( value , db . Case ) : value = case ( value . whens , value . value , value . else_ ) if ( key in order ) : order [ order . index ( key ) ] = ( key , value ) continue if isinstance ( value , sql . elements . Case ) : value_list = case_list elif is_orm_value ( value ) : value_list = orm_field_list else : value_list = unordered_list value_list . append ( ( key , value ) ) update_args = { 'synchronize_session' : False } if ( order or orm_field_list or case_list ) : values = itertools . chain ( order , orm_field_list , case_list , unordered_list ) update_args [ 'update_args' ] = { 'preserve_parameter_order' : True } result = query . update ( values , ** update_args ) return ( 0 != result )
compare-and-swap conditional update .