Skip to content

Commit 6145a1b

Browse files
committed
add support for pivot
1 parent 277b48c commit 6145a1b

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

src/http_server.cpp

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,62 @@ void HttpServer::DoHandleRun(const httplib::Request &req,
372372
});
373373
}
374374

375+
unique_ptr<SQLStatement> last_statement;
376+
377+
auto statements = connection->ExtractStatements(content);
378+
auto statement_count = statements.size();
379+
380+
if (statement_count == 0) {
381+
SetResponseErrorResult(res, "No statements");
382+
return;
383+
}
384+
385+
// If there's more than one statement, run all but the last.
386+
if (statement_count > 1) {
387+
for (auto i = 0; i < statement_count - 1; ++i) {
388+
auto &statement = statements[i];
389+
auto pending = connection->PendingQuery(std::move(statement));
390+
// Return any error found before execution.
391+
if (pending->HasError()) {
392+
SetResponseErrorResult(res, pending->GetError());
393+
return;
394+
}
395+
// Execute tasks until result is ready (or there's an error).
396+
auto exec_result = PendingExecutionResult::RESULT_NOT_READY;
397+
while (!PendingQueryResult::IsResultReady(exec_result)) {
398+
exec_result = pending->ExecuteTask();
399+
if (exec_result == PendingExecutionResult::BLOCKED ||
400+
exec_result == PendingExecutionResult::NO_TASKS_AVAILABLE) {
401+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
402+
}
403+
}
404+
// Return any error found during execution.
405+
switch (exec_result) {
406+
case PendingExecutionResult::EXECUTION_ERROR:
407+
SetResponseErrorResult(res, pending->GetError());
408+
return;
409+
case PendingExecutionResult::EXECUTION_FINISHED:
410+
case PendingExecutionResult::RESULT_READY:
411+
// ignore the result
412+
pending->Execute();
413+
break;
414+
default:
415+
SetResponseErrorResult(res, "Unexpected PendingExecutionResult");
416+
return;
417+
}
418+
}
419+
}
420+
421+
// Get the last statement.
422+
auto &statement_to_run = statements[statement_count - 1];
423+
375424
// We use a pending query so we can execute tasks and fetch chunks
376425
// incrementally. This enables cancellation.
377426
unique_ptr<PendingQueryResult> pending;
378427

379428
// Create pending query, with request content as SQL.
380429
if (parameter_values.size() > 0) {
381-
auto prepared = connection->Prepare(content);
430+
auto prepared = connection->Prepare(std::move(statement_to_run));
382431
if (prepared->HasError()) {
383432
SetResponseErrorResult(res, prepared->GetError());
384433
return;
@@ -391,7 +440,7 @@ void HttpServer::DoHandleRun(const httplib::Request &req,
391440
}
392441
pending = prepared->PendingQuery(values, true);
393442
} else {
394-
pending = connection->PendingQuery(content, true);
443+
pending = connection->PendingQuery(std::move(statement_to_run), true);
395444
}
396445

397446
if (pending->HasError()) {

0 commit comments

Comments
 (0)