| 98 | pass |
| 99 | |
| 100 | def execute(self, sql: str, database: str = None, truncate: bool = True, verbose: bool = True, |
| 101 | no_except: bool = False) -> Optional[str]: |
| 102 | if verbose: |
| 103 | # print("== EXECUTING ==") |
| 104 | if len(sql) < 300: |
| 105 | pass |
| 106 | # print(sql) |
| 107 | self.conn.reconnect() |
| 108 | try: |
| 109 | with self.conn.cursor() as cursor: |
| 110 | if database: |
| 111 | cursor.execute(f"use `{database}`;") |
| 112 | cursor.fetchall() |
| 113 | cursor.execute(sql, multi=True) |
| 114 | result = cursor.fetchall() |
| 115 | result = str(result) |
| 116 | self.conn.commit() |
| 117 | except Exception as e: |
| 118 | if no_except: |
| 119 | raise |
| 120 | result = str(e) |
| 121 | if verbose: |
| 122 | if len(result) < 200: |
| 123 | pass |
| 124 | # print(result) |
| 125 | else: |
| 126 | pass |
| 127 | # print("len result:", len(result)) |
| 128 | if len(result) > 800 and truncate: |
| 129 | result = result[:800] + "[TRUNCATED]" |
| 130 | if not sql.lower().startswith("select"): |
| 131 | pass # IMPORTANT: if `execute` is called in a high rate, here must wait for the transaction |
| 132 | # time.sleep(0.5) # insure transaction is done |
| 133 | return result |
| 134 | |
| 135 | def is_port_open(self, port) -> bool: |
| 136 | try: |