zmysql.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import traceback
  4. from dbutils.pooled_db import PooledDB
  5. import pymysql
  6. from pymysql.cursors import DictCursor
  7. from contextlib import contextmanager
  8. def singleton(class_):
  9. instances = {}
  10. def get_instance(*args, **kwargs) -> BaseDb:
  11. if class_ not in instances:
  12. instances[class_] = class_(*args, **kwargs)
  13. return instances[class_]
  14. return get_instance
  15. class BaseDb:
  16. @staticmethod
  17. def __build_conn(db_key):
  18. db_config = {
  19. 'shanxi': {
  20. 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
  21. 'user': 'cxzx',
  22. 'port': 3306,
  23. 'password': 'sxtvs53$68HD',
  24. 'db': 'shanxi',
  25. 'charset': 'utf8mb4'
  26. },
  27. 'chengyi': {
  28. 'host': '172.16.101.19',
  29. 'user': 'root',
  30. 'port': 3306,
  31. 'password': '*Root123',
  32. 'db': 'ods',
  33. 'charset': 'utf8mb4'
  34. },
  35. 'bigdata': {
  36. 'host': '172.16.101.11',
  37. 'user': 'root',
  38. 'port': 3307,
  39. 'password': 'msstar',
  40. 'db': 'bigdata',
  41. 'charset': 'utf8mb4'
  42. },
  43. 'bigdata-gw': {
  44. 'host': '113.142.79.114',
  45. 'user': 'root',
  46. 'port': 3307,
  47. 'password': 'msstar',
  48. 'db': 'bigdata',
  49. 'charset': 'utf8mb4'
  50. },
  51. 'guns': {
  52. 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
  53. 'user': 'cxzx',
  54. 'port': 3306,
  55. 'password': 'sxtvs53$68HD',
  56. 'db': 'guns',
  57. 'charset': 'utf8mb4'
  58. },
  59. 'dangjian-test': {
  60. 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
  61. 'user': 'cxzx',
  62. 'port': 3306,
  63. 'password': 'sxtvs53$68HD',
  64. 'db': 'dangjian-test',
  65. 'charset': 'utf8mb4'
  66. },
  67. 'kaohe': {
  68. 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
  69. 'user': 'cxzx',
  70. 'port': 3306,
  71. 'password': 'sxtvs53$68HD',
  72. 'db': 'kaohe',
  73. 'charset': 'utf8mb4'
  74. },
  75. 'weibo': {
  76. 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
  77. 'user': 'cxzx',
  78. 'port': 3306,
  79. 'password': 'sxtvs53$68HD',
  80. 'db': 'weibo',
  81. 'charset': 'utf8mb4'
  82. },
  83. 'collect': {
  84. 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
  85. 'user': 'cxzx',
  86. 'port': 3306,
  87. 'password': 'sxtvs53$68HD',
  88. 'db': 'collect',
  89. 'charset': 'utf8mb4'
  90. },
  91. 'open': {
  92. 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
  93. 'user': 'cxzx',
  94. 'port': 3306,
  95. 'password': 'sxtvs53$68HD',
  96. 'db': 'open',
  97. 'charset': 'utf8mb4'
  98. },
  99. 'spider': {
  100. 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
  101. 'user': 'cxzx',
  102. 'port': 3306,
  103. 'password': 'sxtvs53$68HD',
  104. 'db': 'spider',
  105. 'charset': 'utf8mb4'
  106. },
  107. 'operate': {
  108. 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
  109. 'user': 'cxzx',
  110. 'port': 3306,
  111. 'password': 'sxtvs53$68HD',
  112. 'db': 'operate',
  113. 'charset': 'utf8mb4'
  114. },
  115. 'tvad': {
  116. 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
  117. 'user': 'cxzx',
  118. 'port': 3306,
  119. 'password': 'sxtvs53$68HD',
  120. 'db': 'tv_ad',
  121. 'charset': 'utf8mb4'
  122. },
  123. 'fanruan': {
  124. 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
  125. 'user': 'cxzx',
  126. 'port': 3306,
  127. 'password': 'sxtvs53$68HD',
  128. 'db': 'fanruan',
  129. 'charset': 'utf8mb4'
  130. },
  131. 'qingbo': {
  132. 'host': '172.16.101.10',
  133. 'user': 'shanxiguangdian',
  134. 'port': 3306,
  135. 'password': 'swfgVAKWhCfIFVs9',
  136. 'db': 'shanxiguangdian',
  137. 'charset': 'utf8mb4'
  138. },
  139. 'palo': {
  140. 'host': '172.16.101.2',
  141. 'user': 'edap',
  142. 'port': 9021,
  143. 'password': 'Du8Pwv5T',
  144. 'db': 'ods',
  145. 'charset': 'utf8mb4'
  146. },
  147. 'palo-gw': {
  148. 'host': '113.142.79.114',
  149. 'user': 'edap',
  150. 'port': 9021,
  151. 'password': 'Du8Pwv5T',
  152. 'db': 'ods',
  153. 'charset': 'utf8mb4'
  154. },
  155. 'palo_eds': {
  156. 'host': '172.16.101.2',
  157. 'user': 'edap',
  158. 'port': 9021,
  159. 'password': 'Du8Pwv5T',
  160. 'db': 'eds',
  161. 'charset': 'utf8mb4'
  162. },
  163. 'palo_cxzx': {
  164. 'host': '172.16.101.2',
  165. 'user': 'edap',
  166. 'port': 9021,
  167. 'password': 'Du8Pwv5T',
  168. 'db': 'cxzx',
  169. 'charset': 'utf8mb4'
  170. },
  171. 'local': {
  172. 'host': '127.0.0.1',
  173. 'user': 'root',
  174. 'port': 3306,
  175. 'password': '123456',
  176. 'db': 'bak',
  177. 'charset': 'utf8mb4'
  178. },
  179. 'tidb-kuyun': {
  180. 'host': '172.16.101.1',
  181. 'user': 'root',
  182. 'port': 4000,
  183. 'password': 'msstar',
  184. 'db': 'kuyun',
  185. 'charset': 'utf8mb4'
  186. },
  187. 'tidb-qdxw': {
  188. 'host': '172.16.101.1',
  189. 'user': 'root',
  190. 'port': 4000,
  191. 'password': 'msstar',
  192. 'db': 'qdxw',
  193. 'charset': 'utf8mb4'
  194. },
  195. 'tidb-youmei': {
  196. 'host': '172.16.101.1',
  197. 'user': 'root',
  198. 'port': 4000,
  199. 'password': 'msstar',
  200. 'db': 'youmei',
  201. 'charset': 'utf8mb4'
  202. },
  203. 'tidb-bigdata': {
  204. 'host': '172.16.101.1',
  205. 'user': 'root',
  206. 'port': 4000,
  207. 'password': 'msstar',
  208. 'db': 'bigdata',
  209. 'charset': 'utf8mb4'
  210. },
  211. 'mysql_test': {
  212. 'host': '172.16.101.5',
  213. 'user': 'root',
  214. 'port': 33061,
  215. 'password': 'msstar',
  216. 'db': 'zyx',
  217. 'charset': 'utf8mb4'
  218. },
  219. 'rexian-yunos-sync': {
  220. 'host': '172.16.101.5',
  221. 'user': 'root',
  222. 'port': 33060,
  223. 'password': 'msstar',
  224. 'db': 'rexian-yunos',
  225. 'charset': 'utf8mb4'
  226. },
  227. 'report_forms': {
  228. 'host': 'rm-2vc3039h858t9ab7sfo.mysql.cn-chengdu.rds.aliyuncs.com',
  229. 'user': 'cxzx',
  230. 'port': 3306,
  231. 'password': 'sxtvs53$68HD',
  232. 'db': 'report_forms',
  233. 'charset': 'utf8mb4'
  234. },
  235. 'bm': {
  236. 'host': '172.16.16.110',
  237. 'user': 'ssp',
  238. 'port': 3306,
  239. 'password': 'ssp@0518',
  240. 'db': 'rexian-yunos',
  241. 'charset': 'utf8mb4'
  242. },
  243. 'hqy': {
  244. 'host': '10.30.161.60',
  245. 'user': 'hqy-readonly01',
  246. 'port': 3306,
  247. 'password': 'HqyR198kd#sd',
  248. 'db': 'webtv_cms',
  249. 'charset': 'utf8mb4'
  250. },
  251. 'tide': {
  252. 'host': 'rm-2vcyu6uzd1n5m9f756o.mysql.cn-chengdu.rds.aliyuncs.com',
  253. 'user': 'sxsdst',
  254. 'port': 3306,
  255. 'password': 'xfWzxQ@4dfDv',
  256. 'db': 'tidemedia_cms',
  257. 'charset': 'utf8mb4'
  258. },
  259. 'xha-idaas': {
  260. 'host': '10.30.162.15',
  261. 'user': 'root',
  262. 'port': 3306,
  263. 'password': 'AdminStarv2022',
  264. 'db': 'idaas',
  265. 'charset': 'utf8mb4'
  266. },
  267. 'jiaoda': {
  268. 'host': '10.30.135.194',
  269. 'user': 'root',
  270. 'port': 3306,
  271. 'password': 'root',
  272. 'db': 'scas',
  273. 'charset': 'utf8mb4'
  274. },
  275. 'ssp-old':{
  276. 'host': '47.108.249.49',
  277. 'user': 'cxzx',
  278. 'port': 7001,
  279. 'password': '123456',
  280. 'db': 'member',
  281. 'charset': 'utf8mb4'
  282. }
  283. }
  284. return PooledDB(pymysql, 1, cursorclass=DictCursor, **db_config[db_key])
  285. def __init__(self, db_key) -> None:
  286. self.__mysql_pool = self.__build_conn(db_key)
  287. def get_conn(self):
  288. return self.__mysql_pool.connection()
  289. def query(self, sql, args=None):
  290. with self.open_tx() as s:
  291. return s.query(sql, args)
  292. def query_one(self, sql, args=None):
  293. with self.open_tx() as s:
  294. return s.query_one(sql, args)
  295. def update(self, sql, args=None):
  296. with self.open_tx() as s:
  297. s.update(sql, args)
  298. def update_many(self, sql, args=None):
  299. with self.open_tx() as s:
  300. s.update_many(sql, args)
  301. def insert(self, table, columns, args):
  302. with self.open_tx() as s:
  303. s.insert(table, columns, args)
  304. def insert_default(self, table, args):
  305. with self.open_tx() as s:
  306. s.insert_default(table, args)
  307. @contextmanager
  308. def open_tx(self):
  309. """
  310. 开启事务
  311. :return: 开启事务的会话
  312. """
  313. session = Session(self.__mysql_pool.connection())
  314. try:
  315. yield session
  316. session.conn.commit()
  317. except:
  318. session.conn.rollback()
  319. raise Exception("sql error")
  320. finally:
  321. session.conn.close()
  322. @singleton
  323. class MysqlTest(BaseDb):
  324. def __init__(self) -> None:
  325. super().__init__('mysql_test')
  326. @singleton
  327. class MysqlRexian(BaseDb):
  328. def __init__(self) -> None:
  329. super().__init__('rexian-yunos-sync')
  330. @singleton
  331. class QingBo(BaseDb):
  332. def __init__(self) -> None:
  333. super().__init__('qingbo')
  334. @singleton
  335. class FanRuan(BaseDb):
  336. def __init__(self) -> None:
  337. super().__init__('fanruan')
  338. @singleton
  339. class KaoHe(BaseDb):
  340. def __init__(self) -> None:
  341. super().__init__('kaohe')
  342. @singleton
  343. class ReportFormsDb(BaseDb):
  344. def __init__(self) -> None:
  345. super().__init__('report_forms')
  346. @singleton
  347. class BigDataDb(BaseDb):
  348. def __init__(self) -> None:
  349. super().__init__('bigdata')
  350. @singleton
  351. class BigDataGwDb(BaseDb):
  352. def __init__(self) -> None:
  353. super().__init__('bigdata-gw')
  354. @singleton
  355. class ShanxiDb(BaseDb):
  356. def __init__(self) -> None:
  357. super().__init__('shanxi')
  358. @singleton
  359. class PaloDb(BaseDb):
  360. def __init__(self) -> None:
  361. super().__init__('palo')
  362. @singleton
  363. class PaloGwDb(BaseDb):
  364. def __init__(self) -> None:
  365. super().__init__('palo-gw')
  366. @singleton
  367. class PaloEdsDb(BaseDb):
  368. def __init__(self) -> None:
  369. super().__init__('palo_eds')
  370. @singleton
  371. class PaloCxzxDb(BaseDb):
  372. def __init__(self) -> None:
  373. super().__init__('palo_cxzx')
  374. @singleton
  375. class GunsDb(BaseDb):
  376. def __init__(self) -> None:
  377. super().__init__('guns')
  378. @singleton
  379. class ChengyiDb(BaseDb):
  380. def __init__(self) -> None:
  381. super().__init__('chengyi')
  382. @singleton
  383. class DangjianTest(BaseDb):
  384. def __init__(self) -> None:
  385. super().__init__('dangjian-test')
  386. @singleton
  387. class CollectDb(BaseDb):
  388. def __init__(self) -> None:
  389. super().__init__('collect')
  390. @singleton
  391. class OpenDb(BaseDb):
  392. def __init__(self) -> None:
  393. super().__init__('open')
  394. @singleton
  395. class OperateDb(BaseDb):
  396. def __init__(self) -> None:
  397. super().__init__('operate')
  398. @singleton
  399. class SpiderDb(BaseDb):
  400. def __init__(self) -> None:
  401. super().__init__('spider')
  402. @singleton
  403. class TvAdDb(BaseDb):
  404. def __init__(self) -> None:
  405. super().__init__('tvad')
  406. @singleton
  407. class TiKuyunDb(BaseDb):
  408. def __init__(self) -> None:
  409. super().__init__('tidb-kuyun')
  410. @singleton
  411. class TiYoumeiDb(BaseDb):
  412. def __init__(self) -> None:
  413. super().__init__('tidb-youmei')
  414. @singleton
  415. class TiKuyunDb(BaseDb):
  416. def __init__(self) -> None:
  417. super().__init__('tidb-kuyun')
  418. @singleton
  419. class TiQdxw(BaseDb):
  420. def __init__(self) -> None:
  421. super().__init__('tidb-qdxw')
  422. @singleton
  423. class TiBigDataDb(BaseDb):
  424. def __init__(self) -> None:
  425. super().__init__('tidb-bigdata')
  426. @singleton
  427. class FanruanDb(BaseDb):
  428. def __init__(self) -> None:
  429. super().__init__('fanruan')
  430. @singleton
  431. class WeiboDb(BaseDb):
  432. def __init__(self) -> None:
  433. super().__init__('weibo')
  434. @singleton
  435. class LocalDb(BaseDb):
  436. def __init__(self) -> None:
  437. super().__init__('local')
  438. @singleton
  439. class BMDb(BaseDb):
  440. def __init__(self) -> None:
  441. super().__init__('bm')
  442. @singleton
  443. class HqyDb(BaseDb):
  444. def __init__(self) -> None:
  445. super().__init__('hqy')
  446. @singleton
  447. class TideDb(BaseDb):
  448. def __init__(self) -> None:
  449. super().__init__('tide')
  450. @singleton
  451. class XhaIdaasDb(BaseDb):
  452. def __init__(self) -> None:
  453. super().__init__('xha-idaas')
  454. @singleton
  455. class JiaodaDb(BaseDb):
  456. def __init__(self) -> None:
  457. super().__init__('jiaoda')
  458. @singleton
  459. class SspOldDb(BaseDb):
  460. def __init__(self) -> None:
  461. super().__init__('ssp-old')
  462. class Session:
  463. def __init__(self, conn) -> None:
  464. self.conn = conn
  465. def update(self, sql, args=None):
  466. cursor = self.conn.cursor()
  467. cursor.execute(sql, args)
  468. cursor.close()
  469. def update_many(self, sql, args=None):
  470. cursor = self.conn.cursor()
  471. cursor.executemany(sql, args)
  472. cursor.close()
  473. def query(self, sql, args=None):
  474. # print(sql)
  475. cursor = self.conn.cursor()
  476. cursor.execute(sql, args)
  477. data = cursor.fetchall()
  478. cursor.close()
  479. return data
  480. def query_one(self, sql, args=None):
  481. cursor = self.conn.cursor()
  482. cursor.execute(sql, args)
  483. data = cursor.fetchone()
  484. cursor.close()
  485. return data
  486. def insert(self, table, columns, args):
  487. cursor = self.conn.cursor()
  488. size = len(columns)
  489. if size < 1:
  490. raise Exception('列不能为空')
  491. column_sql = ','.join([f'`{x}`' for x in columns])
  492. values_sql = ','.join(['%s' for i in range(size)])
  493. sql = f'insert into {table} ({column_sql}) values ({values_sql})'
  494. cursor.executemany(sql, args)
  495. cursor.close()
  496. def insert_default(self, table, args):
  497. cursor = self.conn.cursor()
  498. if len(args) < 1:
  499. return
  500. size = len(args[0])
  501. values_sql = ','.join(['%s' for i in range(size)])
  502. sql = f'insert into {table} values ({values_sql})'
  503. cursor.executemany(sql, args)
  504. cursor.close()