def create_syntax(self): k = get_db_key(self.host) if k is not None: cur = connections[k].cursor() cur.execute("SHOW CREATE TABLE `%s`.`%s`" % (self.table_schema, self.table_name)) res = cur.fetchone() return res[1]
def recache_tables(self, collector): host = self.host k = get_db_key(host) if k is not None: tbls = Tables.objects.using(k).all() try: if settings.USE_INCLUDE_NOT_IGNORE: report_include = settings.REPORT_INCLUDE return_tbls = Tables.objects.using(k).none() for tbl in tbls: server_schema_table = '.'.join([host if host is not None else '', tbl.table_schema if tbl.table_schema is not None else '', tbl.table_name if tbl.table_name is not None else '']) do_inc = False if report_include.get(collector.stat) is not None: for reg in report_include.get(collector.stat, []): re_match = re.match(reg, server_schema_table) if re_match is not None: do_inc = True break if not do_inc: if report_include.get('global') is not None: for reg in report_include.get('global', []): re_match = re.match(reg, server_schema_table) if re_match is not None: do_inc = True break if do_inc: return_tbls = return_tbls | tbls.filter(table_schema=tbl.table_schema, table_name=tbl.table_name) else: logger.info("[exclude tables]: %s" % (server_schema_table)) self.cached_tables = return_tbls else: report_ignore = settings.REPORT_IGNORE return_tbls = Tables.objects.using(k).none() for tbl in tbls: server_schema_table = '.'.join([host if host is not None else '', tbl.table_schema if tbl.table_schema is not None else '', tbl.table_name if tbl.table_name is not None else '']) do_rej = False if report_ignore.get(collector.stat) is not None: for reg in report_ignore.get(collector.stat, []): re_match = re.match(reg, server_schema_table) if re_match is not None: do_rej = True break if not do_rej: if report_ignore.get('global') is not None: for reg in report_ignore.get('global', []): re_match = re.match(reg, server_schema_table) if re_match is not None: do_rej = True break if not do_rej: return_tbls = return_tbls | tbls.filter(table_schema=tbl.table_schema, table_name=tbl.table_name) else: logger.info("[exclude tables]: %s" % (server_schema_table)) self.cached_tables = return_tbls except Exception, e: self.cached_tables = tbls
def find_by_schema_and_table(self, host, database_name, table_name): k = get_db_key(host) if k is not None: tables = self.using(k).filter(table_schema=database_name, table_name=table_name) if tables.count() > 0: return tables[0] else: return None
def test_get_db_key(self): sample_dict = { 'localhost_information_schema': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'information_schema', 'USER': '******', 'PASSWORD': '******', 'HOST': 'localhost', 'PORT': '', }, } with self.settings(TABLEIZER_DBS=sample_dict): self.assertEqual('localhost_information_schema', get_db_key('localhost'))
def open_schema(cls, host, schema): k = get_db_key(host) db = settings.TABLEIZER_DBS.get(k) conn_dict = { 'host': host, 'db': schema, 'cursorclass': MySQLdb.cursors.DictCursor, 'user': db.get('USER', ''), 'passwd': db.get('PASSWORD', ''), } port = db.get('PORT', '') if port != '': conn_dict.update({ 'port': int(port) }) connection = MySQLdb.connect(**conn_dict) return connection