| 56 | |
| 57 | |
| 58 | class 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() |
| 105 | cur = self.conn.cursor() |
| 106 | i = 0 |
| 107 | cnt = 3 # retry times |
| 108 | while fail == 1 and i < cnt: |
| 109 | try: |
| 110 | fail = 0 |
| 111 | cur.execute(sql) |
| 112 | except BaseException: |
| 113 | fail = 1 |
| 114 | res = [] |
| 115 | if fail == 0: |
no outgoing calls
no test coverage detected