Skip to content

Commit cf9bcd0

Browse files
author
imash
committed
refactor: remove verbose comments from code
1 parent 070d1b5 commit cf9bcd0

File tree

5 files changed

+28
-406
lines changed

5 files changed

+28
-406
lines changed

mindsql/core/mindsql_core.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def create_database_query(self, question: str, connection, tables: list, **kwarg
4949
question_sql_list = self.vectorstore.retrieve_relevant_question_sql(question, **kwargs)
5050
prompt = self.build_sql_prompt(question=question, connection=connection, question_sql_list=question_sql_list,
5151
tables=tables, **kwargs)
52-
# log.info(prompt) # Don't show full prompt to users
52+
# log.info(prompt)
5353
llm_response = self.llm.invoke(prompt, **kwargs)
5454
return _helper.helper.extract_sql(llm_response)
5555

@@ -176,18 +176,15 @@ def __get_ddl_statements(self, connection: any, tables: list[str], question: str
176176
Returns:
177177
list[str]: The list of DDL statements.
178178
"""
179-
# Try vector store first (semantic search - best for finding relevant tables)
180179
vector_ddls = []
181180
try:
182181
vector_ddls = self.vectorstore.retrieve_relevant_ddl(question, **kwargs)
183182
except Exception as e:
184183
log.info(f"Vector store retrieval failed: {e}")
185184

186-
# If vector store returns good results, use them
187185
if vector_ddls and len(vector_ddls) > 0:
188186
return vector_ddls
189187

190-
# Fallback: get all DDLs from database if vector store fails
191188
if tables and connection:
192189
ddl_statements = []
193190
for table_name in tables:

mindsql/databases/mariadb.py

Lines changed: 1 addition & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,8 @@
1515

1616
class 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'

mindsql/llms/googlegenai.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ def __init__(self, config=None):
2323
api_key = config.pop('api_key')
2424
genai.configure(api_key=api_key)
2525

26-
# Get model name from config, default to gemini-1.5-flash
2726
model_name = config.pop('model', 'gemini-1.5-flash')
28-
# Store temperature for later use if provided
2927
self.default_temperature = config.pop('temperature', 0.1)
3028
self.model = genai.GenerativeModel(model_name, **config)
3129

0 commit comments

Comments
 (0)