Skip to content

Commit bb1d527

Browse files
committed
Retry if query result is the number 0.
1 parent a782951 commit bb1d527

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

sqlwrite.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,18 @@ int print_em(void* data, int c_num, char** c_vals, char** c_names) {
129129
return 0;
130130
}
131131

132+
std::string query_result;
132133

133134
int count_em(void* data, int c_num, char** c_vals, char** c_names) {
134-
lines_printed++;
135-
return 0;
135+
for (int i = 0; i < c_num; i++) {
136+
query_result += (c_vals[i] ? c_vals[i] : "");
137+
if ((i < c_num - 1) && (c_num > 1)) {
138+
query_result += "|";
139+
}
140+
}
141+
query_result += "\n";
142+
lines_printed++;
143+
return 0;
136144
}
137145

138146
#include <iostream>
@@ -277,7 +285,7 @@ static bool translateQuery(ai::aistream& ai,
277285

278286
// auto nl_to_sql = fmt::format("Given a database with the following tables, schemas, and indexes, write a SQL query in SQLite's SQL dialect that answers this question or produces the desired report: '{}'. Produce a JSON object with the SQL query as a field \"SQL\". Offer a list of suggestions as SQL commands to create indexes that would improve query performance in a field \"Indexing\". Do so only if those indexes are not already given in 'Existing indexes'. Only produce output that can be parsed as JSON.\n\nSchemas:\n", query);
279287

280-
auto nl_to_sql = fmt::format("Given a database with the following tables, schemas, indexes, and samples for each column, write a valid SQL query in SQLite's SQL dialect that answers this question or produces the desired report: '{}'. Produce a JSON object with the SQL query as a field \"SQL\". The produced query must only reference columns listed in the schemas. Offer a list of suggestions as SQL commands to create indexes that would improve query performance in a field \"Indexing\". Do so only if those indexes are not already given in 'Existing indexes'. Only produce output that can be parsed as JSON.\n", query);
288+
auto nl_to_sql = fmt::format("Given a database with the following tables, schemas, indexes, and samples for each column, write a valid SQL query in SQLite's SQL dialect that answers this question or produces the desired report: '{}'. Produce a JSON object with the SQL query as a field \"SQL\". The produced query must only reference columns listed in the schemas. Offer a list of suggestions as SQL commands to create indexes that would improve query performance in a field \"Indexing\". Do so only if those indexes are not already given in 'Existing indexes'. Refer to the samples to form the query, taking into account format and capitalization. Only produce output that can be parsed as JSON.\n", query);
281289

282290
sqlite3_prepare_v2(db, "SELECT name, sql FROM sqlite_master WHERE type='table' OR type='view'", -1, &stmt, NULL);
283291

@@ -409,9 +417,13 @@ static void real_ask_command(sqlite3_context *ctx, int argc, const char * query)
409417

410418
// Send the query to the database to count the number of lines.
411419
lines_printed = 0;
420+
query_result = "";
412421
auto rc = sqlite3_exec(db, sql_translation.c_str(), count_em, nullptr, nullptr);
413422

414-
if (lines_printed > 0) {
423+
//std::cout << "Translated query: " << sql_translation << std::endl;
424+
//std::cout << "Query result: " << query_result << std::endl;
425+
426+
if ((lines_printed > 0) && (query_result != "0\n")) {
415427
#if RETRY_ON_TOO_MANY_RESULTS
416428
if (lines_printed < LARGE_QUERY_THRESHOLD) {
417429
// We got at least one result and not more than N - exit the retry loop.

0 commit comments

Comments
 (0)