@@ -403,6 +403,7 @@ def execute_sql(self, result_type=MULTI):
403403 ret .append (result )
404404 return ret
405405
406+
406407class SQLInsertCompiler (NonrelInsertCompiler , SQLCompiler ):
407408 @safe_call
408409 def insert (self , data , return_id = False ):
@@ -412,39 +413,21 @@ def insert(self, data, return_id=False):
412413 pass
413414 return self ._save (data , return_id )
414415
416+
415417# TODO: Define a common nonrel API for updates and add it to the nonrel
416418# backend base classes and port this code to that API
417419class SQLUpdateCompiler (NonrelUpdateCompiler , SQLCompiler ):
418420 query_class = MongoQuery
419421
420- @safe_call
421- def execute_raw (self , update_spec , multi = True , ** kwargs ):
422- collection = self .get_collection ()
423- criteria = self .build_query ()._mongo_query
424- options = self .connection .operation_flags .get ('update' , {})
425- options = dict (options , ** kwargs )
426- info = collection .update (criteria , update_spec , multi = multi , ** options )
427- if info is not None :
428- return info .get ('n' )
429-
430- def execute_sql (self , result_type ):
431- return self .execute_raw (* self ._get_update_spec ())
432-
433- def _get_update_spec (self ):
422+ def update (self , values ):
434423 multi = True
435424 spec = {}
436- for field , _ , value in self . query . values :
425+ for field , value in values :
437426 if getattr (field , 'forbids_updates' , False ):
438427 raise DatabaseError ("Updates on %ss are not allowed" %
439428 field .__class__ .__name__ )
440429 if field .unique :
441430 multi = False
442- if hasattr (value , 'prepare_database_save' ):
443- value = value .prepare_database_save (field )
444- else :
445- value = field .get_db_prep_save (value , connection = self .connection )
446-
447- value = self .convert_value_for_db (field .db_type (connection = self .connection ), value )
448431 if hasattr (value , "evaluate" ):
449432 assert value .connector in (value .ADD , value .SUB )
450433 assert not value .negated
@@ -465,7 +448,18 @@ def _get_update_spec(self):
465448 raise DatabaseError ("Can not modify _id" )
466449 spec .setdefault (action , {})[column ] = value
467450
468- return spec , multi
451+ return self .execute_update (spec , multi )
452+
453+ @safe_call
454+ def execute_update (self , update_spec , multi = True , ** kwargs ):
455+ collection = self .get_collection ()
456+ criteria = self .build_query ()._mongo_query
457+ options = self .connection .operation_flags .get ('update' , {})
458+ options = dict (options , ** kwargs )
459+ info = collection .update (criteria , update_spec , multi = multi , ** options )
460+ if info is not None :
461+ return info .get ('n' )
462+
469463
470464class SQLDeleteCompiler (NonrelDeleteCompiler , SQLCompiler ):
471465 pass
0 commit comments