A class to represent a JDBC connection handler Attributes ---------- driver : str jdbc driver url : str database url username : str database username password : str database password
| 9 | |
| 10 | |
| 11 | class JDBCHandler: |
| 12 | """ |
| 13 | A class to represent a JDBC connection handler |
| 14 | |
| 15 | Attributes |
| 16 | ---------- |
| 17 | driver : str |
| 18 | jdbc driver |
| 19 | url : str |
| 20 | database url |
| 21 | username : str |
| 22 | database username |
| 23 | password : str |
| 24 | database password |
| 25 | """ |
| 26 | |
| 27 | JAR_PATH = "benchbase.jar" |
| 28 | |
| 29 | def __init__(self, driver: str, url: str, username: str, password: str): |
| 30 | self.driver = driver |
| 31 | self.url = url |
| 32 | self.username = username |
| 33 | self.password = password |
| 34 | |
| 35 | def start_jvm(self): |
| 36 | """Function that starts the Java Virtual Machine based on the JAR file created by BenchBase""" |
| 37 | jpype.startJVM(classpath=[self.JAR_PATH]) |
| 38 | |
| 39 | def get_connection(self): |
| 40 | """Function that returns a database connection based on the class attributes |
| 41 | |
| 42 | Returns: |
| 43 | jaydebeapi.Connection: A connection to the database service |
| 44 | """ |
| 45 | return jaydebeapi.connect(self.driver, self.url, [self.username, self.password]) |
| 46 | |
| 47 | def data_from_table(self, conn: jaydebeapi.Connection, table: str): |
| 48 | """Function that pulls data from a specific table of the database |
| 49 | |
| 50 | Args: |
| 51 | conn (jaydebeapi.Connection): Connection to the database |
| 52 | table (str): Name of the table |
| 53 | |
| 54 | Returns: |
| 55 | (pd.DataFrame,list[int]): The table as a DataFrame and a list of indexes for all time-related columns |
| 56 | """ |
| 57 | curs = conn.cursor() |
| 58 | curs.execute(f"SELECT * FROM {table}") |
| 59 | |
| 60 | res = curs.fetchall() |
| 61 | meta = curs.description |
| 62 | curs.close() |
| 63 | |
| 64 | cols = [] |
| 65 | col_types = [] |
| 66 | for entry in meta: |
| 67 | cols.append(str(entry[0])) |
| 68 | col_types.append(entry[1]) |