Exemple #1
0
    def check(my):
        my.search_type = my.kwargs.get("search_type")
        my.values = my.kwargs.get("values")

        my.db_resource = SearchType.get_db_resource_by_search_type(my.search_type)
        my.database = my.db_resource.get_database()
        my.search_type_obj = SearchType.get(my.search_type)
        if my.database != Project.get_project_code() and my.database !='sthpw':
            raise TacticException('You are not allowed to delete the sType [%s] from another project [%s].' %(my.search_type, my.database))
            return False
        
        return True
Exemple #2
0
    def check(self):
        self.search_type = self.kwargs.get("search_type")
        self.values = self.kwargs.get("values")

        self.db_resource = SearchType.get_db_resource_by_search_type(self.search_type)
        self.database = self.db_resource.get_database()
        self.search_type_obj = SearchType.get(self.search_type)
        if self.database != Project.get_project_code() and self.database !='sthpw':
            raise TacticException('You are not allowed to delete the sType [%s] from another project [%s].' %(self.search_type, self.database))
            return False
        
        return True
Exemple #3
0
    def check(my):
        my.search_type = my.kwargs.get("search_type")
        my.values = my.kwargs.get("values")

        my.db_resource = SearchType.get_db_resource_by_search_type(my.search_type)
        my.database = my.db_resource.get_database()
        my.search_type_obj = SearchType.get(my.search_type)
        if my.database != Project.get_project_code() and my.database !='sthpw':
            raise TacticException('You are not allowed to delete the sType [%s] from another project [%s].' %(my.search_type, my.database))
            return False
        
        return True
Exemple #4
0
    def update_dependencies(self):
        '''Function that should be run on insert/update. It's already automatically called during insert.
        On update, the caller needs to call this explicitly. It checks the search type
        this pipeline is associated with and if there is no pipeline code
        column, then update it.  It updates the process table also.'''
        search_type = self.get_value('search_type')
        self.update_process_table(search_type=search_type)

        # don't do anything for task sType
        if search_type == 'sthpw/task':
            return

        if not search_type:
            return

        if ProdSetting.get_value_by_key('autofill_pipeline_code') != 'false':
            try:
                columns = SearchType.get_columns(search_type)
                if not 'pipeline_code' in columns:
                    # add the pipeline code column
                    from pyasm.command import ColumnAddCmd
                    cmd = ColumnAddCmd(search_type, "pipeline_code", "varchar")
                    cmd.execute()
            except SqlException as e:
                print("Error creating column [pipeline_code] for %" %
                      search_type)
                pass

            # go through all of the sobjects and set all the empty ones
            # to the new pipeline
            search = Search(search_type)
            search.add_op("begin")
            search.add_filter("pipeline_code", "NULL", op='is', quoted=False)
            search.add_filter("pipeline_code", "")
            search.add_op("or")
            sobject_ids = search.get_sobject_ids()

            if sobject_ids:
                # this is much faster and memory efficient
                db_resource = SearchType.get_db_resource_by_search_type(
                    search_type)
                sql = DbContainer.get(db_resource)
                tbl = search.get_table()
                sobject_ids = [str(x) for x in sobject_ids]
                pipeline_code = self.get_value("code")
                sql.do_update(
                    '''UPDATE "%s" SET "pipeline_code" = '%s' WHERE id in (%s) '''
                    % (tbl, pipeline_code, ','.join(sobject_ids)))
            """
Exemple #5
0
    def check(self):
        self.search_type = self.kwargs.get("search_type")
        self.values = self.kwargs.get("values")

        self.db_resource = SearchType.get_db_resource_by_search_type(
            self.search_type)
        self.database = self.db_resource.get_database()
        self.search_type_obj = SearchType.get(self.search_type)
        if self.database != Project.get_project_code(
        ) and self.database != 'sthpw':
            raise TacticException(
                'You are not allowed to delete the sType [%s] from another project [%s].'
                % (self.search_type, self.database))
            return False

        return True
Exemple #6
0
    def on_insert(my):
        '''Function that should be run on insert/update. It's already automatically called during insert.
        On update, the caller needs to call this explicitly. It checks the search type
        this pipeline is associated with and if there is no pipeline code
        column, then update it.  It updates the process table also.'''
        search_type = my.get_value('search_type')
        my.update_process_table(search_type=search_type)

        # don't do anything for task sType
        if search_type =='sthpw/task':
            return


        if not search_type:
            return

        if ProdSetting.get_value_by_key('autofill_pipeline_code') != 'false':
            try:
                columns = SearchType.get_columns(search_type)
                if not 'pipeline_code' in columns:
                    # add the pipeline code column
                    from pyasm.command import ColumnAddCmd
                    cmd = ColumnAddCmd(search_type, "pipeline_code", "varchar")
                    cmd.execute()
            except SqlException, e:
                print "Error creating column [pipeline_code] for %" %search_type 
                pass

            # go through all of the sobjects and set all the empty ones
            # to the new pipeline
            search = Search(search_type)
            search.add_op("begin")
            search.add_filter("pipeline_code", "NULL", op='is', quoted=False)
            search.add_filter("pipeline_code", "")
            search.add_op("or")
            sobject_ids = search.get_sobject_ids()
            
            if sobject_ids:
                # this is much faster and memory efficient
                db_resource = SearchType.get_db_resource_by_search_type(search_type)
                sql = DbContainer.get(db_resource)
                tbl = search.get_table()
                sobject_ids = [str(x) for x in sobject_ids]
                pipeline_code =  my.get_value("code")
                sql.do_update('''UPDATE "%s" SET "pipeline_code" = '%s' WHERE id in (%s) ''' %(tbl, pipeline_code, ','.join(sobject_ids)))
            """
Exemple #7
0
    def dump_to_tactic(my, path=None, mode='sql'):

        from pyasm.search import SearchType, Sql, DbContainer

        search_type_obj = SearchType.get(my.search_type)
        database = search_type_obj.get_database()
        table = search_type_obj.get_table()
        db_resource = SearchType.get_db_resource_by_search_type(my.search_type)
        sql = DbContainer.get(db_resource)
        exists = sql.table_exists(table)   
        if not exists:
            return

        info = sql.get_column_info(table)
        impl = sql.get_database_impl()


        columns = info.keys()
        columns.sort()

        # if the table does not exist, there are no columns
        if not columns:
            return

        if path:
            import os
            f = codecs.getwriter('utf8')(open(path, 'ab'))
        else:
            import sys
            f = sys.stdout

        if not my.delimiter:
            my.delimiter = "--"
            my.end_delimiter = my.delimiter


        f.write( "%s\n" % my.delimiter )

        if mode == 'sobject':
            f.write("table = CreateTable('%s')\n" % my.search_type)
        else:
            f.write("table = CreateTable()\n")
            f.write("table.set_table('%s')\n" % table)

        # Hard code this - all search types have id as the primary key at the
        # moment
        primary_key_col = 'id'

        for column in columns:

            if column in my.ignore_columns:
                continue

            col_info = info[column]
            #print col_info
            space = " "*(25-len(column)) 
            data_type = col_info['data_type']
            is_nullable = col_info['nullable']

            if column == "id":
                # A dump will output a database independent serial
                #data_type = impl.get_serial()   <--- Database depenedent
                data_type = 'serial'
                f.write("table.add('%s',%s'%s', primary_key=True)\n" % (column, space, data_type) )
                continue

            elif data_type in ['varchar','char','nvarchar']:
                size = col_info.get('size')
                if size == -1:
                    size = 'max'
                if not size:
                    size = 256
                if is_nullable:
                    f.write("table.add('%s',%s'%s', length='%s' )\n" % (column, space, data_type, size))
                else:
                    f.write("table.add('%s',%s'%s', length='%s', not_null=True )\n" % (column, space, data_type, size))
                continue


            if is_nullable:
                f.write("table.add('%s',%s'%s' )\n" % (column, space, data_type))
            else:
                f.write("table.add('%s',%s'%s', not_null=True )\n" % (column, space, data_type))


        # add the constraints
        constraints = impl.get_constraints(db_resource, table)
        for constraint in constraints:
            name = constraint.get("name")
            columns = constraint.get("columns")
            mode = constraint.get("mode")
            if not name:
                name = "%s_%s_idx" % (name, "_".join(columns))
            f.write('''table.add_constraint(%s, mode="%s")\n''' % (columns, mode))
            #def add_constraint(my, columns, mode="UNIQUE"):


        #f.write("table.set_primary_key('id')\n")

        # Disable commit for now
        #if mode == 'sobject':
        #    f.write("table.commit()\n")

        f.write( "%s\n" % my.end_delimiter )
        f.write("\n")
Exemple #8
0
    def resolve_relationship_attrs(self, attrs, search_type, search_type2):

        if attrs.get("relationship") not in ("search_type","search_code","search_id"):
            return attrs


        search_type_obj = SearchType.get(search_type)
        search_type_obj2 = SearchType.get(search_type2)

        my_is_from = attrs['from'] == search_type_obj.get_base_key()

        db_resource = SearchType.get_db_resource_by_search_type(search_type)
        db_resource2 = SearchType.get_db_resource_by_search_type(search_type2)
        db_impl = db_resource.get_database_impl()
        db_impl2 = db_resource2.get_database_impl()

        # <connect from="sthpw/note" to="*"
        #    type='hierarchy' relationship='search_type'/>

        prefix = attrs.get("prefix")
        if prefix:
            prefix = "%s_" % prefix
        else:
            prefix = ""

        if my_is_from:

            if db_impl2.get_database_type() == "MongoDb":
                attrs['from_col'] = '%ssearch_code' % prefix
                attrs['to_col'] = db_impl2.get_id_col(db_resource2,search_type2)
                attrs['relationship'] = 'search_code'
            else:
                code_column = "%ssearch_code" % prefix
                has_code = SearchType.column_exists(search_type, code_column)
                if has_code:
                    attrs['from_col'] = '%ssearch_code' % prefix
                    attrs['to_col'] = 'code'
                    attrs['relationship'] = 'search_code'
                else:
                    attrs['from_col'] = '%ssearch_id' % prefix
                    attrs['to_col'] = db_impl2.get_id_col(db_resource2,search_type2)
                    attrs['relationship'] = 'search_id'

        else:

            if db_impl.get_database_type() == "MongoDb":
                attrs['to_col'] = '%ssearch_code' % prefix
                attrs['from_col'] = db_impl.get_id_col(db_resource,search_type)
                attrs['relationship'] = 'search_code'
            else:
                code_column = "%ssearch_code" % prefix
                has_code = SearchType.column_exists(search_type2, code_column)
                if has_code:
                    attrs['from_col'] = 'code'
                    attrs['to_col'] = '%ssearch_code' % prefix
                    attrs['relationship'] = 'search_code'
                else:
                    attrs['from_col'] = db_impl.get_id_col(db_resource,search_type)
                    attrs['to_col'] = '%ssearch_id' % prefix
                    attrs['relationship'] = 'search_id'

        return attrs