6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/glancer/workflow/executor.rb', line 6
def self.execute(sql, original_question: nil, attempt: 1, message_id: nil)
unless sql.strip.match?(/\A\s*(select|with)\b/i)
Glancer::Utils::Logger.error("Workflow::Executor", "Blocked attempt to run non-SELECT SQL.")
raise Glancer::Error, "Only SELECT queries are allowed for execution."
end
Glancer::Utils::Logger.info("Workflow::Executor", "Executing SQL (Attempt ##{attempt})...")
run_id = SecureRandom.uuid
= "#{sql.strip} /*glancer,run_id:#{run_id}*/"
begin
result = nil
Glancer::Utils::Transaction.make do |connection|
apply_statement_timeout(connection)
result = connection.exec_query().to_a
raise ActiveRecord::Rollback
end
Glancer::Audit.create!(
question: original_question,
code: ,
code_type: "sql",
adapter: Glancer.configuration.resolved_adapter,
run_id: run_id,
executed_at: Time.current,
message_id: message_id
)
result
rescue StandardError => e
if attempt >= 3
Glancer::Utils::Logger.error("Workflow::Executor", "Final failure after #{attempt} attempts: #{e.message}")
return { error: true, message: e.message, last_code: sql }
end
Glancer::Utils::Logger.warn("Workflow::Executor",
"SQL Error (Attempt ##{attempt}): #{e.message}. Requesting correction...")
fixed_sql = Glancer::Workflow::Builder.fix_sql(sql, e.message)
execute(fixed_sql, original_question: original_question, attempt: attempt + 1, message_id: message_id)
end
end
|