labpack/databases/sql.py
def update(self, updated, original=None):

        ''' a method to update changes to a record in the table

        :param updated: dictionary with updated record fields
        :param original: [optional] dictionary with original record fields 
        :return: list of dictionaries with updated field details

        NOTE:   if original is empty, method will poll database for the
                most recent version of the record with which to compare the
                new details for changes
        '''

        title = '%s.update' % self.__class__.__name__

        # validate inputs
        input_fields = {
            'updated': updated,
            'original': original
        }
        for key, value in input_fields.items():
            if value:
                object_title = '%s(%s=%s)' % (title, key, str(value))
                self.fields.validate(value, '.%s' % key, object_title)
        if not 'id' in updated.keys():
            raise ValueError('%s(updated={...}) must have id field.' % title)
        if original:
            if not 'id' in original.keys():
                raise ValueError('%s(original={...}) must have id field.' % title)
            elif updated['id'] != original['id']:
                raise ValueError('%s original["id"] value must match updated["id"]' % title)

        # extract primary key
        record_id = updated['id']

        # # handle missing id
        #     if not '.id' in self.model.keyMap.keys():
        #         del updated['id']
        #         if original:
        #             del original['id']

        # validate new details against record model
        new_details = self.model.validate(updated)

        # retrieve old record if not specified
        if not original:
            try:
                original = self.read(record_id)
            except:
                raise ValueError('%s updated["id"] does not exist.' % title)

        # determine record differences
        from labpack.parsing.comparison import compare_records
        update_list = compare_records(new_details, original)

        # construct update keywords
        update_kwargs = {}
        for update in update_list:
            if update['action'] not in ('DELETE', 'REMOVE'):
                current_details = new_details
                save_path = ''
                for segment in update['path']:
                    if save_path:
                        save_path += '.'
                    save_path += segment
                    key = '.%s' % save_path
                    if isinstance(current_details[segment], dict) and not key in self.empty_maps:
                        current_details = current_details[segment]
                        continue
                    elif isinstance(current_details[segment], dict):
                        update_kwargs[save_path] = pickle.dumps(current_details[segment])
                        break
                    elif isinstance(current_details[segment], list):
                        update_kwargs[save_path] = pickle.dumps(current_details[segment])
                        break
                    else:
                        update_kwargs[save_path] = current_details[segment]
            else:
                current_details = original
                save_path = ''
                for i in range(len(update['path'])):
                    segment = update['path'][i]
                    if save_path:
                        save_path += '.'
                    save_path += segment
                    key = '.%s' % save_path
                    if update['action'] == 'DELETE' and i + 1 == len(update['path']):
                        update_kwargs[save_path] = None
                    elif isinstance(current_details[segment], dict) and not key in self.empty_maps:
                        current_details = current_details[segment]
                        continue
                    elif isinstance(current_details[segment], dict):
                        update_kwargs[save_path] = pickle.dumps(new_details[segment])
                        break
                    elif isinstance(current_details[segment], list):
                        update_kwargs[save_path] = pickle.dumps(new_details[segment])
                        break
                    else:
                        update_kwargs[save_path] = None

        # send update command
        if update_kwargs:
            update_statement = self.table.update(self.table.c.id == record_id).values(**update_kwargs)
            self.session.execute(update_statement)

        return update_list
 a method to update changes to a record in the table

        :param updated: dictionary with updated record fields
        :param original: [optional] dictionary with original record fields 
        :return: list of dictionaries with updated field details

        NOTE:   if original is empty, method will poll database for the
                most recent version of the record with which to compare the
                new details for changes
        
a method to update changes to a record in the table
a method to update changes to a record in the table
def update(self, updated, original=None):
        title = '%s.update' % self.__class__.__name__
        input_fields = {
            'updated': updated,
            'original': original
        }
        for key, value in input_fields.items():
            if value:
                object_title = '%s(%s=%s)' % (title, key, str(value))
                self.fields.validate(value, '.%s' % key, object_title)
        if not 'id' in updated.keys():
            raise ValueError('%s(updated={...}) must have id field.' % title)
        if original:
            if not 'id' in original.keys():
                raise ValueError('%s(original={...}) must have id field.' % title)
            elif updated['id'] != original['id']:
                raise ValueError('%s original["id"] value must match updated["id"]' % title)
        record_id = updated['id']
        new_details = self.model.validate(updated)
        if not original:
            try:
                original = self.read(record_id)
            except:
                raise ValueError('%s updated["id"] does not exist.' % title)
        from labpack.parsing.comparison import compare_records
        update_list = compare_records(new_details, original)
        update_kwargs = {}
        for update in update_list:
            if update['action'] not in ('DELETE', 'REMOVE'):
                current_details = new_details
                save_path = ''
                for segment in update['path']:
                    if save_path:
                        save_path += '.'
                    save_path += segment
                    key = '.%s' % save_path
                    if isinstance(current_details[segment], dict) and not key in self.empty_maps:
                        current_details = current_details[segment]
                        continue
                    elif isinstance(current_details[segment], dict):
                        update_kwargs[save_path] = pickle.dumps(current_details[segment])
                        break
                    elif isinstance(current_details[segment], list):
                        update_kwargs[save_path] = pickle.dumps(current_details[segment])
                        break
                    else:
                        update_kwargs[save_path] = current_details[segment]
            else:
                current_details = original
                save_path = ''
                for i in range(len(update['path'])):
                    segment = update['path'][i]
                    if save_path:
                        save_path += '.'
                    save_path += segment
                    key = '.%s' % save_path
                    if update['action'] == 'DELETE' and i + 1 == len(update['path']):
                        update_kwargs[save_path] = None
                    elif isinstance(current_details[segment], dict) and not key in self.empty_maps:
                        current_details = current_details[segment]
                        continue
                    elif isinstance(current_details[segment], dict):
                        update_kwargs[save_path] = pickle.dumps(new_details[segment])
                        break
                    elif isinstance(current_details[segment], list):
                        update_kwargs[save_path] = pickle.dumps(new_details[segment])
                        break
                    else:
                        update_kwargs[save_path] = None
        if update_kwargs:
            update_statement = self.table.update(self.table.c.id == record_id).values(**update_kwargs)
            self.session.execute(update_statement)
        return update_list
def update ( self , updated , original = None ) : title = '%s.update' % self . __class__ . __name__ input_fields = { 'updated' : updated , 'original' : original } for key , value in input_fields . items ( ) : if value : object_title = '%s(%s=%s)' % ( title , key , str ( value ) ) self . fields . validate ( value , '.%s' % key , object_title ) if not 'id' in updated . keys ( ) : raise ValueError ( '%s(updated={...}) must have id field.' % title ) if original : if not 'id' in original . keys ( ) : raise ValueError ( '%s(original={...}) must have id field.' % title ) elif updated [ 'id' ] != original [ 'id' ] : raise ValueError ( '%s original["id"] value must match updated["id"]' % title ) record_id = updated [ 'id' ] new_details = self . model . validate ( updated ) if not original : try : original = self . read ( record_id ) except : raise ValueError ( '%s updated["id"] does not exist.' % title ) from labpack . parsing . comparison import compare_records update_list = compare_records ( new_details , original ) update_kwargs = { } for update in update_list : if update [ 'action' ] not in ( 'DELETE' , 'REMOVE' ) : current_details = new_details save_path = '' for segment in update [ 'path' ] : if save_path : save_path += '.' save_path += segment key = '.%s' % save_path if isinstance ( current_details [ segment ] , dict ) and not key in self . empty_maps : current_details = current_details [ segment ] continue elif isinstance ( current_details [ segment ] , dict ) : update_kwargs [ save_path ] = pickle . dumps ( current_details [ segment ] ) break elif isinstance ( current_details [ segment ] , list ) : update_kwargs [ save_path ] = pickle . dumps ( current_details [ segment ] ) break else : update_kwargs [ save_path ] = current_details [ segment ] else : current_details = original save_path = '' for i in range ( len ( update [ 'path' ] ) ) : segment = update [ 'path' ] [ i ] if save_path : save_path += '.' save_path += segment key = '.%s' % save_path if update [ 'action' ] == 'DELETE' and i + 1 == len ( update [ 'path' ] ) : update_kwargs [ save_path ] = None elif isinstance ( current_details [ segment ] , dict ) and not key in self . empty_maps : current_details = current_details [ segment ] continue elif isinstance ( current_details [ segment ] , dict ) : update_kwargs [ save_path ] = pickle . dumps ( new_details [ segment ] ) break elif isinstance ( current_details [ segment ] , list ) : update_kwargs [ save_path ] = pickle . dumps ( new_details [ segment ] ) break else : update_kwargs [ save_path ] = None if update_kwargs : update_statement = self . table . update ( self . table . c . id == record_id ) . values ( ** update_kwargs ) self . session . execute ( update_statement ) return update_list
a method to update changes to a record in the table
a method to update changes to a record in the table
''' a method to update changes to a record in the table

        :param updated: dictionary with updated record fields
        :param original: [optional] dictionary with original record fields 
        :return: list of dictionaries with updated field details

        NOTE:   if original is empty, method will poll database for the
                most recent version of the record with which to compare the
                new details for changes
        ''' # validate inputs # extract primary key # # handle missing id #     if not '.id' in self.model.keyMap.keys(): #         del updated['id'] #         if original: #             del original['id'] # validate new details against record model # retrieve old record if not specified # determine record differences # construct update keywords # send update command
list of dictionaries with updated field details
NOTE:   if original is empty, method will poll database for the
most recent version of the record with which to compare the
new details for changes
list of dictionaries with updated field details NOTE : if original is empty method will poll database for the most recent version of the record with which to compare the new details for changes
self
updated
dictionary with updated record fields
dictionary with updated record fields
original
[optional] dictionary with original record fields
[ optional ] dictionary with original record fields