123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- import traceback
- from dbutils.pooled_db import PooledDB
- import pymysql
- from pymysql.cursors import DictCursor
- from contextlib import contextmanager
- def singleton(class_):
- instances = {}
- def get_instance(*args, **kwargs) -> BaseDb:
- if class_ not in instances:
- instances[class_] = class_(*args, **kwargs)
- return instances[class_]
- return get_instance
- class BaseDb:
- @staticmethod
- def __build_conn(db_key):
- db_config = {
- 'shanxi': {
- 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
- 'user': 'cxzx',
- 'port': 3306,
- 'password': 'sxtvs53$68HD',
- 'db': 'shanxi',
- 'charset': 'utf8mb4'
- },
- 'chengyi': {
- 'host': '172.16.101.19',
- 'user': 'root',
- 'port': 3306,
- 'password': '*Root123',
- 'db': 'ods',
- 'charset': 'utf8mb4'
- },
- 'bigdata': {
- 'host': '172.16.101.11',
- 'user': 'root',
- 'port': 3307,
- 'password': 'msstar',
- 'db': 'bigdata',
- 'charset': 'utf8mb4'
- },
- 'bigdata-gw': {
- 'host': '113.142.79.114',
- 'user': 'root',
- 'port': 3307,
- 'password': 'msstar',
- 'db': 'bigdata',
- 'charset': 'utf8mb4'
- },
- 'guns': {
- 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
- 'user': 'cxzx',
- 'port': 3306,
- 'password': 'sxtvs53$68HD',
- 'db': 'guns',
- 'charset': 'utf8mb4'
- },
- 'dangjian-test': {
- 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
- 'user': 'cxzx',
- 'port': 3306,
- 'password': 'sxtvs53$68HD',
- 'db': 'dangjian-test',
- 'charset': 'utf8mb4'
- },
- 'kaohe': {
- 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
- 'user': 'cxzx',
- 'port': 3306,
- 'password': 'sxtvs53$68HD',
- 'db': 'kaohe',
- 'charset': 'utf8mb4'
- },
- 'weibo': {
- 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
- 'user': 'cxzx',
- 'port': 3306,
- 'password': 'sxtvs53$68HD',
- 'db': 'weibo',
- 'charset': 'utf8mb4'
- },
- 'collect': {
- 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
- 'user': 'cxzx',
- 'port': 3306,
- 'password': 'sxtvs53$68HD',
- 'db': 'collect',
- 'charset': 'utf8mb4'
- },
- 'open': {
- 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
- 'user': 'cxzx',
- 'port': 3306,
- 'password': 'sxtvs53$68HD',
- 'db': 'open',
- 'charset': 'utf8mb4'
- },
- 'spider': {
- 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
- 'user': 'cxzx',
- 'port': 3306,
- 'password': 'sxtvs53$68HD',
- 'db': 'spider',
- 'charset': 'utf8mb4'
- },
- 'operate': {
- 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
- 'user': 'cxzx',
- 'port': 3306,
- 'password': 'sxtvs53$68HD',
- 'db': 'operate',
- 'charset': 'utf8mb4'
- },
- 'tvad': {
- 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
- 'user': 'cxzx',
- 'port': 3306,
- 'password': 'sxtvs53$68HD',
- 'db': 'tv_ad',
- 'charset': 'utf8mb4'
- },
- 'fanruan': {
- 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
- 'user': 'cxzx',
- 'port': 3306,
- 'password': 'sxtvs53$68HD',
- 'db': 'fanruan',
- 'charset': 'utf8mb4'
- },
- 'qingbo': {
- 'host': '172.16.101.10',
- 'user': 'shanxiguangdian',
- 'port': 3306,
- 'password': 'swfgVAKWhCfIFVs9',
- 'db': 'shanxiguangdian',
- 'charset': 'utf8mb4'
- },
- 'palo': {
- 'host': '172.16.101.2',
- 'user': 'edap',
- 'port': 9021,
- 'password': 'Du8Pwv5T',
- 'db': 'ods',
- 'charset': 'utf8mb4'
- },
- 'palo-gw': {
- 'host': '113.142.79.114',
- 'user': 'edap',
- 'port': 9021,
- 'password': 'Du8Pwv5T',
- 'db': 'ods',
- 'charset': 'utf8mb4'
- },
- 'palo_eds': {
- 'host': '172.16.101.2',
- 'user': 'edap',
- 'port': 9021,
- 'password': 'Du8Pwv5T',
- 'db': 'eds',
- 'charset': 'utf8mb4'
- },
- 'palo_cxzx': {
- 'host': '172.16.101.2',
- 'user': 'edap',
- 'port': 9021,
- 'password': 'Du8Pwv5T',
- 'db': 'cxzx',
- 'charset': 'utf8mb4'
- },
- 'local': {
- 'host': '127.0.0.1',
- 'user': 'root',
- 'port': 3306,
- 'password': '123456',
- 'db': 'bak',
- 'charset': 'utf8mb4'
- },
- 'tidb-kuyun': {
- 'host': '172.16.101.1',
- 'user': 'root',
- 'port': 4000,
- 'password': 'msstar',
- 'db': 'kuyun',
- 'charset': 'utf8mb4'
- },
- 'tidb-qdxw': {
- 'host': '172.16.101.1',
- 'user': 'root',
- 'port': 4000,
- 'password': 'msstar',
- 'db': 'qdxw',
- 'charset': 'utf8mb4'
- },
- 'tidb-youmei': {
- 'host': '172.16.101.1',
- 'user': 'root',
- 'port': 4000,
- 'password': 'msstar',
- 'db': 'youmei',
- 'charset': 'utf8mb4'
- },
- 'tidb-bigdata': {
- 'host': '172.16.101.1',
- 'user': 'root',
- 'port': 4000,
- 'password': 'msstar',
- 'db': 'bigdata',
- 'charset': 'utf8mb4'
- },
- 'mysql_test': {
- 'host': '172.16.101.5',
- 'user': 'root',
- 'port': 33061,
- 'password': 'msstar',
- 'db': 'zyx',
- 'charset': 'utf8mb4'
- },
- 'rexian-yunos-sync': {
- 'host': '172.16.101.5',
- 'user': 'root',
- 'port': 33060,
- 'password': 'msstar',
- 'db': 'rexian-yunos',
- 'charset': 'utf8mb4'
- },
- 'report_forms': {
- 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
- 'user': 'cxzx',
- 'port': 3306,
- 'password': 'sxtvs53$68HD',
- 'db': 'report_forms',
- 'charset': 'utf8mb4'
- },
- 'bm': {
- 'host': '172.16.16.110',
- 'user': 'ssp',
- 'port': 3306,
- 'password': 'ssp@0518',
- 'db': 'rexian-yunos',
- 'charset': 'utf8mb4'
- },
- 'hqy': {
- 'host': '10.30.161.60',
- 'user': 'hqy-readonly01',
- 'port': 3306,
- 'password': 'HqyR198kd#sd',
- 'db': 'webtv_cms',
- 'charset': 'utf8mb4'
- },
- 'tide': {
- 'host': 'rm-2vcyu6uzd1n5m9f756o.mysql.cn-chengdu.rds.aliyuncs.com',
- 'user': 'sxsdst',
- 'port': 3306,
- 'password': 'xfWzxQ@4dfDv',
- 'db': 'tidemedia_cms',
- 'charset': 'utf8mb4'
- },
- 'xha-idaas': {
- 'host': '10.30.162.15',
- 'user': 'root',
- 'port': 3306,
- 'password': 'AdminStarv2022',
- 'db': 'idaas',
- 'charset': 'utf8mb4'
- },
- 'jiaoda': {
- 'host': '10.30.135.194',
- 'user': 'root',
- 'port': 3306,
- 'password': 'root',
- 'db': 'scas',
- 'charset': 'utf8mb4'
- },
- 'ssp-old':{
- 'host': '47.108.249.49',
- 'user': 'cxzx',
- 'port': 7001,
- 'password': '123456',
- 'db': 'member',
- 'charset': 'utf8mb4'
- }
- }
- return PooledDB(pymysql, 1, cursorclass=DictCursor, **db_config[db_key])
- def __init__(self, db_key) -> None:
- self.__mysql_pool = self.__build_conn(db_key)
- def get_conn(self):
- return self.__mysql_pool.connection()
- def query(self, sql, args=None):
- with self.open_tx() as s:
- return s.query(sql, args)
- def query_one(self, sql, args=None):
- with self.open_tx() as s:
- return s.query_one(sql, args)
- def update(self, sql, args=None):
- with self.open_tx() as s:
- s.update(sql, args)
- def update_many(self, sql, args=None):
- with self.open_tx() as s:
- s.update_many(sql, args)
- def insert(self, table, columns, args):
- with self.open_tx() as s:
- s.insert(table, columns, args)
- def insert_default(self, table, args):
- with self.open_tx() as s:
- s.insert_default(table, args)
- @contextmanager
- def open_tx(self):
- """
- 开启事务
- :return: 开启事务的会话
- """
- session = Session(self.__mysql_pool.connection())
- try:
- yield session
- session.conn.commit()
- except:
- session.conn.rollback()
- raise Exception("sql error")
- finally:
- session.conn.close()
- @singleton
- class MysqlTest(BaseDb):
- def __init__(self) -> None:
- super().__init__('mysql_test')
- @singleton
- class MysqlRexian(BaseDb):
- def __init__(self) -> None:
- super().__init__('rexian-yunos-sync')
- @singleton
- class QingBo(BaseDb):
- def __init__(self) -> None:
- super().__init__('qingbo')
- @singleton
- class FanRuan(BaseDb):
- def __init__(self) -> None:
- super().__init__('fanruan')
- @singleton
- class KaoHe(BaseDb):
- def __init__(self) -> None:
- super().__init__('kaohe')
- @singleton
- class ReportFormsDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('report_forms')
- @singleton
- class BigDataDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('bigdata')
- @singleton
- class BigDataGwDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('bigdata-gw')
- @singleton
- class ShanxiDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('shanxi')
- @singleton
- class PaloDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('palo')
- @singleton
- class PaloGwDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('palo-gw')
- @singleton
- class PaloEdsDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('palo_eds')
- @singleton
- class PaloCxzxDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('palo_cxzx')
- @singleton
- class GunsDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('guns')
- @singleton
- class ChengyiDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('chengyi')
- @singleton
- class DangjianTest(BaseDb):
- def __init__(self) -> None:
- super().__init__('dangjian-test')
- @singleton
- class CollectDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('collect')
- @singleton
- class OpenDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('open')
- @singleton
- class OperateDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('operate')
- @singleton
- class SpiderDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('spider')
- @singleton
- class TvAdDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('tvad')
- @singleton
- class TiKuyunDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('tidb-kuyun')
- @singleton
- class TiYoumeiDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('tidb-youmei')
- @singleton
- class TiKuyunDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('tidb-kuyun')
- @singleton
- class TiQdxw(BaseDb):
- def __init__(self) -> None:
- super().__init__('tidb-qdxw')
- @singleton
- class TiBigDataDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('tidb-bigdata')
- @singleton
- class FanruanDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('fanruan')
- @singleton
- class WeiboDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('weibo')
- @singleton
- class LocalDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('local')
- @singleton
- class BMDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('bm')
- @singleton
- class HqyDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('hqy')
- @singleton
- class TideDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('tide')
- @singleton
- class XhaIdaasDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('xha-idaas')
- @singleton
- class JiaodaDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('jiaoda')
- @singleton
- class SspOldDb(BaseDb):
- def __init__(self) -> None:
- super().__init__('ssp-old')
- class Session:
- def __init__(self, conn) -> None:
- self.conn = conn
- def update(self, sql, args=None):
- cursor = self.conn.cursor()
- cursor.execute(sql, args)
- cursor.close()
- def update_many(self, sql, args=None):
- cursor = self.conn.cursor()
- cursor.executemany(sql, args)
- cursor.close()
- def query(self, sql, args=None):
- # print(sql)
- cursor = self.conn.cursor()
- cursor.execute(sql, args)
- data = cursor.fetchall()
- cursor.close()
- return data
- def query_one(self, sql, args=None):
- cursor = self.conn.cursor()
- cursor.execute(sql, args)
- data = cursor.fetchone()
- cursor.close()
- return data
- def insert(self, table, columns, args):
- cursor = self.conn.cursor()
- size = len(columns)
- if size < 1:
- raise Exception('列不能为空')
- column_sql = ','.join([f'`{x}`' for x in columns])
- values_sql = ','.join(['%s' for i in range(size)])
- sql = f'insert into {table} ({column_sql}) values ({values_sql})'
- cursor.executemany(sql, args)
- cursor.close()
- def insert_default(self, table, args):
- cursor = self.conn.cursor()
- if len(args) < 1:
- return
- size = len(args[0])
- values_sql = ','.join(['%s' for i in range(size)])
- sql = f'insert into {table} values ({values_sql})'
- cursor.executemany(sql, args)
- cursor.close()
|