def remove_schema(self, db_query, moc):
     """
     Method: create_schema
     Description: 删除表结构
     Parameter:
         db_query: DB查询对象
     """
     moc_name = moc.get_moc_name()
     table_name = self.__moc_defs[moc_name]['TABLE_NAME']
     if not DBOperator.check_table_exists(db_query, table_name):
         return
     if DBOperator.check_table_exists(db_query, table_name):
         sql = 'DROP TABLE user_sync.%s' % (table_name)
         debug.info(sql)
         db_query.execute(sql)
    def create_schema(self):
        """
        Method: create_schema
        Description: 创建表结构
        Parameter: 无
        Return: 
        Others: 
        """
        db_query = self.__connection.get_active_query()
        if DBOperator.check_table_exists(db_query, self.__TABLE_NAME):
            return
        fields = []
        for attr in self.__FIELDS:
            field = DBOperator.get_field_def(attr)
            fields.append(field)
        #创建递增序列
        sql = 'CREATE SEQUENCE user_sync.sync_id_seq START WITH 1 INCREMENT BY 1 MINVALUE 1'
        debug.info(sql)
        db_query.execute(sql)
        
        #创建同步表结构
        sql = 'CREATE TABLE user_sync.%s (%s)' % (self.__TABLE_NAME, ','.join(fields))
        debug.info(sql)
        db_query.execute(sql)

        # 创建主键
        sql = 'ALTER TABLE user_sync.%s ADD CONSTRAINT %s_PK PRIMARY KEY("id")' % (self.__TABLE_NAME, self.__TABLE_NAME)
        debug.info(sql)
        db_query.execute(sql)

        #授权给user_acp
        sql = 'GRANT ALL ON user_sync.%s TO user_acp' % self.__TABLE_NAME
        debug.info(sql)
        db_query.execute(sql)

        sql = 'GRANT ALL ON user_sync.sync_id_seq TO user_acp'
        debug.info(sql)
        db_query.execute(sql)
    def remove_schema(self):
        """
        Method: remove_schema
        Description: 删除表结构
        Parameter: 
        Return: 
        Others: 
        """
        db_query = self.__connection.get_active_query()
        if not DBOperator.check_table_exists(db_query, self.__TABLE_NAME):
            return

        sql = 'DROP SEQUENCE user_sync.sync_id_seq'
        debug.info(sql)
        db_query.execute(sql)

        sql = 'DROP TABLE user_sync.%s' % (self.__TABLE_NAME)
        debug.info(sql)
        db_query.execute(sql)
示例#4
0
 def start_monitor_system():
     """
     Start the whole monitor system. Actually all the parts of the monitor system can be run via python file alone
     """
     # start config monitor
     print("Starting ConfigMonitor")
     ConfigMonitor().start()
     # start coordinator
     print("Starting Coordinator")
     MonitorCoordinator().start()
     # start analyzer
     print("Starting DataAnalyzer")
     DataAnalyzer().start()
     # start db operator
     print("Starting DBOperator")
     DBOperator().start()
     # start alarm operator
     print("Starting AlarmOperator")
     AlarmOperator().start()
     # start application operator
     print("Starting AppOperator")
     AppOperator().start()
     print("Done.")
    def create_schema(self, db_query, moc, sync_object):
        """
        Method: create_schema
        Description: 创建表结构
        Parameter:
            db_query: DB查询对象
            moc: MOC对象
            sync_object: 是否是同步对象
        Return: 
        Others: 
        """
        moc_name = moc.get_moc_name()
        if moc_name not in self.__moc_defs.keys():
            self.__moc_defs[moc_name] = {}
            self.__moc_defs[moc_name]['TABLE_NAME'] = '%s%s' % (TABLE_PREFIX, moc.get_moc_name())
            self.__moc_defs[moc_name]['FIELDS'] = []
            self.__moc_defs[moc_name]['FIELDS'].append(MocAttrDef(name = 'moid'
                                                              , is_key = True
                                                              , attr_type = type_def.TYPE_STRING
                                                              , max_len = 128))
            for attr in moc.__ATTR_DEF__:
                self.__moc_defs[moc_name]['FIELDS'].append(attr)

            if (sync_object is True):
                # 追加字段
                self.__moc_defs[moc_name]['FIELDS'].append(MocAttrDef(name = '_SYNC_SOURCE'
                                                              , is_key = False
                                                              , attr_type = type_def.TYPE_INT32
                                                              , max_len = 0))

        table_name = self.__moc_defs[moc_name]['TABLE_NAME']
        #self.remove_schema(db_query, moc)        
        if not DBOperator.check_table_exists(db_query, table_name):
            fields = []
            sql_indexes = []
            index_names = {}
            for attr in self.__moc_defs[moc_name]['FIELDS']:
                field = DBOperator.get_field_def(attr)
                fields.append(field)
                if attr.is_key is True:
                    index_name = 'idx_%s_%s' % (moc.get_moc_name(), attr.name)
                    index_name = index_name[0:20]
                    if index_name in index_names.keys():
                        index_names[index_name] = index_names[index_name] + 1
                    else:
                        index_names[index_name] = 1
                    index_name = '%s_%d' % (index_name, index_names[index_name])
                    sql_indexes.append('CREATE INDEX %s ON user_sync.%s("%s")' % (index_name
                                                                   , table_name
                                                                   , attr.name))
            sql = 'CREATE TABLE user_sync.%s (%s)' % (table_name, ','.join(fields))
            debug.info(sql)
            db_query.execute(sql)

            if sync_object is True:
                # 创建主键
                sql = 'ALTER TABLE user_sync.%s ADD CONSTRAINT %s_PK PRIMARY KEY("moid", "_SYNC_SOURCE")' % (table_name, table_name)
                debug.info(sql)
                db_query.execute(sql)
        
            for sql_index in sql_indexes:
                debug.info(sql_index)
                db_query.execute(sql_index)
            sql = 'GRANT ALL ON user_sync.%s TO user_acp' % table_name
            debug.info(sql)
            db_query.execute(sql)
示例#6
0
def main():
    db = DBOperator('localhost', 3306, 'root', '123456', 'dict')
    server = DictServer(db, HOST, PORT)
    server.server_forever()
    db.close()