1515
1616class MariaDB (IDatabase ):
1717 def create_connection (self , url : str , ** kwargs ) -> any :
18- """
19- A method to create a connection with MariaDB database.
20-
21- Parameters:
22- url (str): The URL in the format mariadb://username:password@host:port/database_name
23- **kwargs: Additional keyword arguments for the connection.
24-
25- Returns:
26- any: The connection object.
27- """
2818 url = urlparse (url )
2919 try :
30- # Use official MariaDB connector
3120 connection_params = {
3221 'host' : url .hostname ,
3322 'port' : url .port or int (kwargs .get ('port' , 3306 )),
@@ -37,63 +26,34 @@ def create_connection(self, url: str, **kwargs) -> any:
3726 'autocommit' : True ,
3827 }
3928
40- # Remove None values and add any additional kwargs
4129 connection_params = {k : v for k , v in connection_params .items () if v is not None }
4230 connection_params .update ({k : v for k , v in kwargs .items () if k not in ['port' ]})
4331
4432 conn = mariadb .connect (** connection_params )
45-
4633 log .info (SUCCESSFULLY_CONNECTED_TO_DB_CONSTANT .format ("MariaDB" ))
4734 return conn
4835
4936 except mariadb .Error as e :
50- error_msg = str (e )
51- log .info (ERROR_CONNECTING_TO_DB_CONSTANT .format ("MariaDB" , error_msg ))
37+ log .info (ERROR_CONNECTING_TO_DB_CONSTANT .format ("MariaDB" , str (e )))
5238 return None
5339
5440 def validate_connection (self , connection : any ) -> None :
55- """
56- A function that validates if the provided connection is a MariaDB connection.
57-
58- Parameters:
59- connection: The connection object for accessing the database.
60-
61- Raises:
62- ValueError: If the provided connection is not a MariaDB connection.
63-
64- Returns:
65- None
66- """
6741 if connection is None :
6842 raise ValueError (CONNECTION_ESTABLISH_ERROR_CONSTANT )
69-
70- # MariaDB connection validation (using PyMySQL connection)
7143 if not hasattr (connection , 'cursor' ):
7244 raise ValueError (INVALID_DB_CONNECTION_OBJECT .format ("MariaDB" ))
7345
7446 def execute_sql (self , connection , sql : str ) -> pd .DataFrame :
75- """
76- A method to execute SQL on the database.
77-
78- Parameters:
79- connection (any): The connection object.
80- sql (str): The SQL to be executed.
81-
82- Returns:
83- pd.DataFrame: The result of the SQL query.
84- """
8547 try :
8648 self .validate_connection (connection )
8749 cursor = connection .cursor ()
8850 cursor .execute (sql )
8951
90- # For DDL/DML statements (CREATE, INSERT, UPDATE, DELETE), commit and return empty DataFrame
9152 if sql .strip ().upper ().startswith (('CREATE' , 'INSERT' , 'UPDATE' , 'DELETE' , 'DROP' , 'ALTER' )):
9253 connection .commit ()
9354 cursor .close ()
9455 return pd .DataFrame ()
9556
96- # For SELECT statements, fetch results
9757 results = cursor .fetchall ()
9858 if cursor .description :
9959 column_names = [i [0 ] for i in cursor .description ]
@@ -107,81 +67,33 @@ def execute_sql(self, connection, sql: str) -> pd.DataFrame:
10767 return pd .DataFrame ()
10868
10969 def get_databases (self , connection ) -> List [str ]:
110- """
111- Get a list of databases from the given connection and SQL query.
112-
113- Parameters:
114- connection (object): The connection object for the database.
115-
116- Returns:
117- List[str]: A list of unique database names.
118- """
11970 try :
12071 self .validate_connection (connection )
12172 df_databases = self .execute_sql (connection = connection , sql = MARIADB_SHOW_DATABASE_QUERY )
12273 except Exception as e :
12374 log .info (e )
12475 return []
125-
12676 return df_databases ["Database" ].unique ().tolist ()
12777
12878 def get_table_names (self , connection , database : str ) -> pd .DataFrame :
129- """
130- Retrieves the tables from the information schema for the specified database.
131-
132- Parameters:
133- connection: The database connection object.
134- database (str): The name of the database.
135-
136- Returns:
137- DataFrame: A pandas DataFrame containing the table names from the information schema.
138- """
13979 self .validate_connection (connection )
14080 df_tables = self .execute_sql (connection , MARIADB_DB_TABLES_INFO_SCHEMA_QUERY .format (database ))
14181 return df_tables
14282
14383 def get_all_ddls (self , connection , database : str ) -> pd .DataFrame :
144- """
145- Get all DDLs from the specified database using the provided connection object.
146-
147- Parameters:
148- connection (any): The connection object.
149- database (str): The name of the database.
150-
151- Returns:
152- pd.DataFrame: A pandas DataFrame containing the DDLs for each table in the specified database.
153- """
15484 self .validate_connection (connection )
15585 df_tables = self .get_table_names (connection , database )
15686 df_ddl = pd .DataFrame (columns = ['Table' , 'DDL' ])
15787 for index , row in df_tables .iterrows ():
158- # Handle both uppercase and lowercase column names
15988 table_name = row .get ('TABLE_NAME' ) or row .get ('table_name' )
16089 if table_name :
16190 ddl_df = self .get_ddl (connection , table_name )
16291 df_ddl = df_ddl ._append ({'Table' : table_name , 'DDL' : ddl_df }, ignore_index = True )
16392 return df_ddl
16493
16594 def get_ddl (self , connection : any , table_name : str , ** kwargs ) -> str :
166- """
167- A method to get the DDL for the table.
168-
169- Parameters:
170- connection (any): The connection object.
171- table_name (str): The name of the table.
172- **kwargs: Additional keyword arguments.
173-
174- Returns:
175- str: The DDL for the table.
176- """
17795 ddl_df = self .execute_sql (connection , MARIADB_SHOW_CREATE_TABLE_QUERY .format (table_name ))
17896 return ddl_df ["Create Table" ].iloc [0 ]
17997
18098 def get_dialect (self ) -> str :
181- """
182- A method to get the dialect of the database.
183-
184- Returns:
185- str: The dialect of the database.
186- """
18799 return 'mysql'
0 commit comments