Skip to content

Commit a95d01a

Browse files
Cleanup httpserver_extension.cpp (#19)
1 parent be1356b commit a95d01a

File tree

1 file changed

+44
-80
lines changed

1 file changed

+44
-80
lines changed

src/httpserver_extension.cpp

+44-80
Original file line numberDiff line numberDiff line change
@@ -40,44 +40,44 @@ struct HttpServerState {
4040

4141
static HttpServerState global_state;
4242

43-
std::string GetColumnType(MaterializedQueryResult &result, idx_t column) {
44-
if (result.RowCount() == 0) {
45-
return "String";
46-
}
47-
switch (result.types[column].id()) {
48-
case LogicalTypeId::FLOAT:
49-
return "Float";
50-
case LogicalTypeId::DOUBLE:
51-
return "Double";
52-
case LogicalTypeId::INTEGER:
53-
return "Int32";
54-
case LogicalTypeId::BIGINT:
55-
return "Int64";
56-
case LogicalTypeId::UINTEGER:
57-
return "UInt32";
58-
case LogicalTypeId::UBIGINT:
59-
return "UInt64";
60-
case LogicalTypeId::VARCHAR:
61-
return "String";
62-
case LogicalTypeId::TIME:
63-
return "DateTime";
64-
case LogicalTypeId::DATE:
65-
return "Date";
66-
case LogicalTypeId::TIMESTAMP:
67-
return "DateTime";
68-
case LogicalTypeId::BOOLEAN:
69-
return "Int8";
70-
default:
71-
return "String";
72-
}
73-
return "String";
74-
}
43+
std::string GetColumnType(MaterializedQueryResult &result, idx_t column) {
44+
if (result.RowCount() == 0) {
45+
return "String";
46+
}
47+
switch (result.types[column].id()) {
48+
case LogicalTypeId::FLOAT:
49+
return "Float";
50+
case LogicalTypeId::DOUBLE:
51+
return "Double";
52+
case LogicalTypeId::INTEGER:
53+
return "Int32";
54+
case LogicalTypeId::BIGINT:
55+
return "Int64";
56+
case LogicalTypeId::UINTEGER:
57+
return "UInt32";
58+
case LogicalTypeId::UBIGINT:
59+
return "UInt64";
60+
case LogicalTypeId::VARCHAR:
61+
return "String";
62+
case LogicalTypeId::TIME:
63+
return "DateTime";
64+
case LogicalTypeId::DATE:
65+
return "Date";
66+
case LogicalTypeId::TIMESTAMP:
67+
return "DateTime";
68+
case LogicalTypeId::BOOLEAN:
69+
return "Int8";
70+
default:
71+
return "String";
72+
}
73+
return "String";
74+
}
7575

76-
struct ReqStats {
77-
float elapsed_sec;
78-
int64_t read_bytes;
79-
int64_t read_rows;
80-
};
76+
struct ReqStats {
77+
float elapsed_sec;
78+
int64_t read_bytes;
79+
int64_t read_rows;
80+
};
8181

8282
// Convert the query result to JSON format
8383
static std::string ConvertResultToJSON(MaterializedQueryResult &result, ReqStats &req_stats) {
@@ -176,7 +176,6 @@ bool IsAuthenticated(const duckdb_httplib_openssl::Request& req) {
176176
return false;
177177
}
178178

179-
180179
// Convert the query result to NDJSON (JSONEachRow) format
181180
static std::string ConvertResultToNDJSON(MaterializedQueryResult &result) {
182181
std::string ndjson_output;
@@ -218,40 +217,6 @@ static std::string ConvertResultToNDJSON(MaterializedQueryResult &result) {
218217
return ndjson_output;
219218
}
220219

221-
static void HandleQuery(const string& query, duckdb_httplib_openssl::Response& res) {
222-
try {
223-
if (!global_state.db_instance) {
224-
throw IOException("Database instance not initialized");
225-
}
226-
227-
Connection con(*global_state.db_instance);
228-
const auto& start = std::chrono::system_clock::now();
229-
auto result = con.Query(query);
230-
const auto end = std::chrono::system_clock::now();
231-
232-
const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
233-
ReqStats req_stats{
234-
static_cast<float>(elapsed.count()) / 1000,
235-
0,
236-
0
237-
};
238-
239-
if (result->HasError()) {
240-
res.status = 400;
241-
res.set_content(result->GetError(), "text/plain");
242-
return;
243-
}
244-
245-
// Convert result to JSON
246-
std::string json_output = ConvertResultToJSON(*result, req_stats);
247-
res.set_content(json_output, "application/json");
248-
} catch (const Exception& ex) {
249-
res.status = 400;
250-
res.set_content(ex.what(), "text/plain");
251-
}
252-
}
253-
254-
255220
// Handle both GET and POST requests
256221
void HandleHttpRequest(const duckdb_httplib_openssl::Request& req, duckdb_httplib_openssl::Response& res) {
257222
std::string query;
@@ -342,7 +307,7 @@ void HandleHttpRequest(const duckdb_httplib_openssl::Request& req, duckdb_httpli
342307
std::string json_output = ConvertResultToNDJSON(*result);
343308
res.set_content(json_output, "application/x-ndjson");
344309
}
345-
310+
346311
} catch (const Exception& ex) {
347312
res.status = 500;
348313
std::string error_message = "Code: 59, e.displayText() = DB::Exception: " + std::string(ex.what());
@@ -390,17 +355,17 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t
390355
#ifndef _WIN32
391356
const char* debug_env = std::getenv("DUCKDB_HTTPSERVER_DEBUG");
392357
const char* use_syslog = std::getenv("DUCKDB_HTTPSERVER_SYSLOG");
393-
358+
394359
if (debug_env != nullptr && std::string(debug_env) == "1") {
395360
global_state.server->set_logger([](const duckdb_httplib_openssl::Request& req, const duckdb_httplib_openssl::Response& res) {
396361
time_t now_time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
397362
char timestr[32];
398363
strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", localtime(&now_time));
399364
// Use \r\n for consistent line endings
400-
fprintf(stdout, "[%s] %s %s - %d - from %s:%d\r\n",
365+
fprintf(stdout, "[%s] %s %s - %d - from %s:%d\r\n",
401366
timestr,
402367
req.method.c_str(),
403-
req.path.c_str(),
368+
req.path.c_str(),
404369
res.status,
405370
req.remote_addr.c_str(),
406371
req.remote_port);
@@ -409,9 +374,9 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t
409374
} else if (use_syslog != nullptr && std::string(use_syslog) == "1") {
410375
openlog("duckdb-httpserver", LOG_PID | LOG_NDELAY, LOG_LOCAL0);
411376
global_state.server->set_logger([](const duckdb_httplib_openssl::Request& req, const duckdb_httplib_openssl::Response& res) {
412-
syslog(LOG_INFO, "%s %s - %d - from %s:%d",
377+
syslog(LOG_INFO, "%s %s - %d - from %s:%d",
413378
req.method.c_str(),
414-
req.path.c_str(),
379+
req.path.c_str(),
415380
res.status,
416381
req.remote_addr.c_str(),
417382
req.remote_port);
@@ -436,7 +401,7 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t
436401
}
437402
global_state.is_running = false; // Update the running state
438403
});
439-
404+
440405
// Run the server in the same thread
441406
if (!global_state.server->listen(host_str.c_str(), port)) {
442407
global_state.is_running = false;
@@ -455,7 +420,6 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t
455420
}
456421
});
457422
}
458-
459423
}
460424

461425
void HttpServerStop() {

0 commit comments

Comments
 (0)