Skip to content

Commit 2fe9a04

Browse files
committed
feat: Add support for executing AI-generated SQL commands
fix: Improve regex pattern matching for JSON responses patch: Update interactive mode to handle command shortcuts feat: Add option to automatically execute AI-generated SQL commands
1 parent 12203a5 commit 2fe9a04

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

manager.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def execute_sql_command(
7878
finally:
7979
cursor.close()
8080

81-
def tables(self, tables_only: bool = False):
81+
def tables(self):
8282
"""List tables available"""
8383
return self.execute_sql_command("PRAGMA table_list;")
8484

@@ -179,7 +179,7 @@ def process_response(self, response: str) -> list[str]:
179179
Args:
180180
response (str): ai response
181181
"""
182-
if response.startswith("{") and not response.endswith("}"):
182+
if response.startswith("{") and not response.strip().endswith("}"):
183183
response += "}"
184184

185185
sql_statements = re.findall(self.sql_pattern, response)
@@ -227,6 +227,7 @@ def __init__(
227227
disable_suggestions,
228228
new_history_thread,
229229
json,
230+
yes,
230231
color,
231232
ai,
232233
):
@@ -236,6 +237,7 @@ def __init__(
236237
self.db_manager = Sqlite3Manager(db_path, auto_commit)
237238
self.disable_coloring = disable_coloring
238239
self.json = json
240+
self.yes = yes
239241
self.color = color
240242
history_file_path = Path.home() / ".sqlite3-cli-manager-history.txt"
241243
if new_history_thread and history_file_path.exists():
@@ -416,21 +418,44 @@ def do_columns(self, line):
416418
click.secho("Table name is required.", fg="yellow")
417419

418420
@cli_error_handler
419-
def default(self, line):
421+
def default(self, line: str, prompt_confirmation: bool = False, ai_generated=False):
420422
"""Run sql statemnt against database"""
423+
421424
if line.startswith("./"):
422425
self.do_sys(line[2:])
423426
return
427+
428+
elif line.startswith("!"):
429+
# Let's try to mimic the unix' previous command(s) execution shortcut
430+
history = self.completer_session.history.get_strings()
431+
command_number = line.count("!")
432+
if len(history) >= command_number:
433+
line = [history[-command_number - 1]]
434+
prompt_confirmation = True
435+
else:
436+
click.secho("Index out of range!", fg="yellow")
437+
return
438+
424439
elif line.startswith("/sql"):
425440
line = [line[4:]]
426441
elif line.startswith("/ai"):
427442
line = TextToSql(self.db_manager).generate(line[3:])
443+
ai_generated = prompt_confirmation = True
428444
elif self.ai:
429445
line = self.text_to_sql.generate(line)
446+
ai_generated = prompt_confirmation = True
430447
else:
431448
line = [line]
432449
self.__start_time = time.time()
433450
for sql_statement in line:
451+
if (
452+
not self.yes
453+
and prompt_confirmation
454+
and not click.confirm("[Exc] - " + sql_statement)
455+
):
456+
continue
457+
if ai_generated:
458+
self.completer_session.history.append_string(sql_statement)
434459
success, response = self.db_manager.execute_sql_command(sql_statement)
435460
Commands.stdout_data(success, response, json=self.json, color=self.color)
436461
self.__end_time = time.time()
@@ -541,6 +566,12 @@ def execute(database, sql, ai, json, quiet):
541566
default="cyan",
542567
)
543568
@click.option("-j", "--json", help="Stdout results in json format", is_flag=True)
569+
@click.option(
570+
"-y",
571+
"--yes",
572+
help="Okay to execution of AI generated sql statements",
573+
is_flag=True,
574+
)
544575
@click.option("-a", "--auto-commit", is_flag=True, help="Enable auto-commit")
545576
@click.option(
546577
"-i", "--ai", is_flag=True, help="Generate sql statements from prompt by AI"
@@ -564,6 +595,7 @@ def interactive(
564595
database,
565596
color,
566597
json,
598+
yes,
567599
auto_commit,
568600
ai,
569601
disable_coloring,
@@ -578,6 +610,7 @@ def interactive(
578610
disable_suggestions=disable_suggestions,
579611
new_history_thread=new_history_thread,
580612
json=json,
613+
yes=yes,
581614
color=color,
582615
ai=ai,
583616
)

0 commit comments

Comments
 (0)