Example #1
0
 def unserialize(self, str, obj_type):
     try:
         root = ElementTree.fromstring(str)
         return self.read_xml_object(obj_type, root)
     except SyntaxError, e:
         msg = "Invalid VisTrails serialized object %s" % str
         raise VistrailsDBException(msg)
         return None
Example #2
0
def translateVistrail(vistrail):
    id_remap = {}
    for action in vistrail.db_get_actions():
        # don't need to change key idx since none of that changes
        new_action_idx = {}
        for annotation in action.db_get_annotations():
            annotation.db_id = vistrail.idScope.getNewId(DBAnnotation.vtType)
            new_action_idx[annotation.db_id] = annotation
        action.db_annotations_id_index = new_action_idx

        for operation in action.db_get_operations():
            # never have annotations as parent objs so
            # don't have to worry about those ids
            if operation.db_what == DBAnnotation.vtType:
                if operation.vtType == 'add':
                    new_id = vistrail.idScope.getNewId(DBAnnotation.vtType)
                    old_id = operation.db_objectId
                    operation.db_objectId = new_id
                    operation.db_data.db_id = new_id
                    id_remap[old_id] = new_id
                elif operation.vtType == 'change':
                    changed_id = operation.db_oldObjId
                    if id_remap.has_key(changed_id):
                        operation.db_oldObjId = id_remap[changed_id]
                    else:
                        raise VistrailsDBException('cannot translate')

                    new_id = vistrail.idScope.getNewId(DBAnnotation.vtType)
                    old_id = operation.db_newObjId
                    operation.db_newObjId = new_id
                    operation.db_data.db_id = new_id
                    id_remap[old_id] = new_id
                elif operation.vtType == 'delete':
                    old_id = operation.db_objectId
                    if id_remap.has_key(old_id):
                        operation.db_objectId = id_remap[old_id]
                    else:
                        raise VistrailsDBException('cannot translate')

    vistrail.db_version = '0.8.1'
    return vistrail
Example #3
0
 def executeSQL(self, db, cmd_tuple, isFetch):
     dbCommand, values = cmd_tuple
     # print 'db: %s' % dbCommand
     # print 'values:', values
     data = None
     cursor = db.cursor()
     try:
         cursor.execute(dbCommand, values)
         if isFetch:
             data = cursor.fetchall()
         else:
             data = cursor.lastrowid
     except Exception, e:
         raise VistrailsDBException('Command "%s" with values "%s" '
                                    'failed: %s' % (dbCommand, values, e))
Example #4
0
 def unserialize(self, str, obj_type):
     def set_dirty(obj):
         for child, _, _ in obj.db_children():
             if child.vtType == DBGroup.vtType:
                 if child.db_workflow:
                     set_dirty(child.db_workflow)
             child.is_dirty = True
             child.is_new = True
     try:
         root = ElementTree.fromstring(str)
         obj = self.read_xml_object(obj_type, root)
         set_dirty(obj)
         return obj
     except SyntaxError, e:
         msg = "Invalid VisTrails serialized object %s" % str
         raise VistrailsDBException(msg)
         return None
Example #5
0
    def delete_from_db(self, db_connection, type, obj_id):
        root_set = set([DBVistrail.vtType, DBWorkflow.vtType, 
                        DBLog.vtType, DBRegistry.vtType])
        if type not in root_set:
            raise VistrailsDBException("Cannot delete entity of type '%s'" \
                                           % type)

        id_str = str(obj_id)
        for (dao_type, dao) in self['sql'].iteritems():
            if dao_type not in root_set:
                db_cmd = \
                    self['sql'][type].createSQLDelete(dao.table,
                                                      {'entity_type': type,
                                                       'entity_id': id_str})
                self['sql'][type].executeSQL(db_connection, db_cmd, False)
        db_cmd = self['sql'][type].createSQLDelete(self['sql'][type].table,
                                                   {'id': id_str})
        self['sql'][type].executeSQL(db_connection, db_cmd, False)
Example #6
0
def translate_object(obj, method_name, version=None, target_version=None):
    if version is None:
        version = obj.version
    if target_version is None:
        target_version = currentVersion

    version_map = {
        '0.3.0': '0.3.1',
        '0.3.1': '0.6.0',
        '0.5.0': '0.6.0',
        '0.6.0': '0.7.0',
        '0.7.0': '0.8.0',
        '0.8.0': '0.8.1',
        '0.8.1': '0.9.0',
        '0.9.0': '0.9.1',
        '0.9.1': '0.9.3',
        '0.9.2': '0.9.3',
        '0.9.3': '0.9.4',
        '0.9.4': '0.9.5',
        '0.9.5': '1.0.0',
        '1.0.0': '1.0.1',
        '1.0.1': '1.0.2',
        }

    rev_version_map = {
        '1.0.2': '1.0.1',
        '1.0.1': '1.0.0',
        '1.0.0': '0.9.5',
        '0.9.5': '0.9.4',
        '0.9.4': '0.9.3',
        }

    def get_translate_module(map, start_version, end_version):
        translate_dir = 'db.versions.' + get_version_name(end_version) + \
            '.translate.' + get_version_name(start_version)
        return __import__(translate_dir, {}, {}, [''])

    path = []
    old_tuple = version.split('.')
    new_tuple = target_version.split('.')
    map = version_map
    for i, j in izip(old_tuple, new_tuple):
        if i < j:
            # forward
            break
        elif i > j:
            # reverse
            map = rev_version_map
            break

    # don't get stuck in an infinite loop
    count = 0
    while version != target_version:
        if count > len(map):
            break
        next_version = map[version]
        translate_module = get_translate_module(map, version, next_version)

        if not hasattr(translate_module, method_name):
            raise VistrailsDBException("Cannot translate version: "
                                       "version %s missing method '%s'" % \
                                           (version, method_name))
        obj = getattr(translate_module, method_name)(obj)
        version = next_version
        count += 1

    if version != target_version:
        msg = "An error occurred when translating,"
        msg += "only able to translate to version '%s'" % version
        raise VistrailsDBException(msg)

    return obj
Example #7
0
    def open_many_from_db(self, db_connection, vtType, ids, lock=False):
        """ Loads multiple objects. They need to be loaded as one single
            multiple select statement command for performance reasons.
        """

        log_dao = self['sql'][vtType]
        # loop through ids and build SELECT statements
        selects = [
            log_dao.get_sql_select(db_connection, {'id': id}, lock)
            for id in ids
        ]
        # Execute all SELECT statements for main objects
        results = log_dao.executeSQLGroup(db_connection, selects, True)

        # list of final objects
        objects = []
        # list of selects
        selects = []
        # list of children id:all_objects_dict
        all_objects_dict = {}
        # process each result and extract child SELECTS
        # daoList should contain (id, dao_type, dao, result) values
        daoList = []
        # selects should contain dbCommand values
        selects = []
        global_props = {}
        for id, data in zip(ids, results):
            res_objects = log_dao.process_sql_columns(data, global_props)
            if len(res_objects) > 1:
                raise VistrailsDBException("More than object of type '%s' and "
                                           "id '%s' exist in the database" % \
                                               (vtType, id))
            elif len(res_objects) <= 0:
                raise VistrailsDBException("No objects of type '%s' and "
                                           "id '%s' exist in the database" % \
                                               (vtType, id))
            all_objects = {}
            all_objects_dict[id] = all_objects
            all_objects.update(res_objects)
            objects.append(res_objects.values()[0])
            # collect all commands so that they can be executed together

            # generate SELECT statements for children
            for dao_type, dao in self['sql'].iteritems():
                if (dao_type == DBVistrail.vtType
                        or dao_type == DBWorkflow.vtType
                        or dao_type == DBLog.vtType
                        or dao_type == DBRegistry.vtType):
                    continue

                daoList.append([id, dao_type, dao, None])
                dbCommand = dao.get_sql_select(db_connection, global_props,
                                               lock)
                selects.append(dbCommand)

        # Execute all child select statements
        results = self['sql'][vtType].executeSQLGroup(db_connection, selects,
                                                      True)
        for i in xrange(len(daoList)):
            daoList[i][3] = results[i]

        # process results
        for id, dao_type, dao, data in daoList:
            all_objects = all_objects_dict[id]
            current_objs = dao.process_sql_columns(data, global_props)
            all_objects.update(current_objs)

            if dao_type == DBGroup.vtType:
                for key, obj in current_objs.iteritems():
                    new_props = {
                        'parent_id': key[1],
                        'entity_id': global_props['entity_id'],
                        'entity_type': global_props['entity_type']
                    }
                    res_obj = self.open_from_db(db_connection,
                                                DBWorkflow.vtType, None, lock,
                                                new_props)
                    res_dict = {}
                    res_dict[(res_obj.vtType, res_obj.db_id)] = res_obj
                    all_objects.update(res_dict)

        for id, all_objects in all_objects_dict.iteritems():
            for key, obj in all_objects.iteritems():
                if key[0] == vtType and key[1] == id:
                    continue
                self['sql'][obj.vtType].from_sql_fast(obj, all_objects)
        for id, dao_type, dao, data in daoList:
            all_objects = all_objects_dict[id]
            for obj in all_objects.itervalues():
                obj.is_dirty = False
                obj.is_new = False

        return objects
Example #8
0
    def open_from_db(self,
                     db_connection,
                     vtType,
                     id=None,
                     lock=False,
                     global_props=None):
        all_objects = {}
        if global_props is None:
            global_props = {}
        if id is not None:
            global_props['id'] = id
        # print global_props
        res_objects = self['sql'][vtType].get_sql_columns(
            db_connection, global_props, lock)
        if len(res_objects) > 1:
            raise VistrailsDBException("More than object of type '%s' and "
                                       "id '%s' exist in the database" % \
                                           (vtType, id))
        elif len(res_objects) <= 0:
            raise VistrailsDBException("No objects of type '%s' and "
                                       "id '%s' exist in the database" % \
                                           (vtType, id))

        all_objects.update(res_objects)
        res = res_objects.values()[0]
        global_props = {'entity_id': res.db_id, 'entity_type': res.vtType}

        # collect all commands so that they can be executed together
        # daoList should contain (dao_type, dao, dbCommand) values
        daoList = []
        # dbCommandList should contain dbCommand values
        dbCommandList = []

        # generate SELECT statements
        for dao_type, dao in self['sql'].iteritems():
            if (dao_type == DBVistrail.vtType or dao_type == DBWorkflow.vtType
                    or dao_type == DBLog.vtType
                    or dao_type == DBRegistry.vtType):
                continue

            daoList.append([dao_type, dao, None])
            dbCommand = dao.get_sql_select(db_connection, global_props, lock)
            dbCommandList.append(dbCommand)

        # Exacute all select statements
        results = self['sql'][vtType].executeSQLGroup(db_connection,
                                                      dbCommandList, True)

        # add result to correct dao
        for i in xrange(len(daoList)):
            daoList[i][2] = results[i]

        # process results
        for dao_type, dao, data in daoList:
            current_objs = dao.process_sql_columns(data, global_props)
            all_objects.update(current_objs)

            if dao_type == DBGroup.vtType:
                for key, obj in current_objs.iteritems():
                    new_props = {
                        'parent_id': key[1],
                        'entity_id': global_props['entity_id'],
                        'entity_type': global_props['entity_type']
                    }
                    res_obj = self.open_from_db(db_connection,
                                                DBWorkflow.vtType, None, lock,
                                                new_props)
                    res_dict = {}
                    res_dict[(res_obj.vtType, res_obj.db_id)] = res_obj
                    all_objects.update(res_dict)

        for key, obj in all_objects.iteritems():
            if key[0] == vtType and key[1] == id:
                continue
            self['sql'][obj.vtType].from_sql_fast(obj, all_objects)
        for obj in all_objects.itervalues():
            obj.is_dirty = False
            obj.is_new = False

        return res