MCPcopy Create free account
hub / github.com/OpenBMB/BMTools / Database

Class Database

bmtools/tools/db_diag/utils/database.py:58–291  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

56
57
58class Database():
59 def __init__(self, args, timeout=-1):
60 self.args = args
61 self.conn = self.resetConn(timeout)
62
63 # self.schema = self.compute_table_schema()
64
65 def resetConn(self, timeout=-1):
66 if self.args.dbtype == 'mysql':
67 conn = pymysql.connect(
68 host=self.args.host,
69 user=self.args.user,
70 passwd=self.args.password,
71 database=self.args.dbname,
72 port=int(self.args.port),
73 charset='utf8',
74 connect_timeout=timeout,
75 read_timeout=timeout,
76 write_timeout=timeout)
77 else:
78 if timeout > 0:
79 conn = psycopg2.connect(database=self.args.dbname,
80 user=self.args.user,
81 password=self.args.password,
82 host=self.args.host,
83 port=self.args.port,
84 options='-c statement_timeout={}s'.format(timeout))
85 else:
86 conn = psycopg2.connect(database=self.args.dbname,
87 user=self.args.user,
88 password=self.args.password,
89 host=self.args.host,
90 port=self.args.port)
91
92 return conn
93 '''
94 def exec_fetch(self, statement, one=True):
95 cur = self.conn.cursor()
96 cur.execute(statement)
97 if one:
98 return cur.fetchone()
99 return cur.fetchall()
100 '''
101
102 def execute_sql(self, sql):
103 fail = 1
104 self.conn = self.resetConn(timeout=2)
105 cur = self.conn.cursor()
106 i = 0
107 cnt = 5 # retry times
108 while fail == 1 and i < cnt:
109 try:
110 fail = 0
111 print("========== start execution time:", time.time())
112 cur.execute(sql)
113 except BaseException:
114 fail = 1
115 time.sleep(1)

Callers 2

build_db_diag_toolFunction · 0.90
memory_diagnosis_agentFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected