@@ -40,44 +40,44 @@ struct HttpServerState {
40
40
41
41
static HttpServerState global_state;
42
42
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
+ }
75
75
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
+ };
81
81
82
82
// Convert the query result to JSON format
83
83
static std::string ConvertResultToJSON (MaterializedQueryResult &result, ReqStats &req_stats) {
@@ -176,7 +176,6 @@ bool IsAuthenticated(const duckdb_httplib_openssl::Request& req) {
176
176
return false ;
177
177
}
178
178
179
-
180
179
// Convert the query result to NDJSON (JSONEachRow) format
181
180
static std::string ConvertResultToNDJSON (MaterializedQueryResult &result) {
182
181
std::string ndjson_output;
@@ -218,40 +217,6 @@ static std::string ConvertResultToNDJSON(MaterializedQueryResult &result) {
218
217
return ndjson_output;
219
218
}
220
219
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
-
255
220
// Handle both GET and POST requests
256
221
void HandleHttpRequest (const duckdb_httplib_openssl::Request& req, duckdb_httplib_openssl::Response& res) {
257
222
std::string query;
@@ -342,7 +307,7 @@ void HandleHttpRequest(const duckdb_httplib_openssl::Request& req, duckdb_httpli
342
307
std::string json_output = ConvertResultToNDJSON (*result);
343
308
res.set_content (json_output, " application/x-ndjson" );
344
309
}
345
-
310
+
346
311
} catch (const Exception& ex) {
347
312
res.status = 500 ;
348
313
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
390
355
#ifndef _WIN32
391
356
const char * debug_env = std::getenv (" DUCKDB_HTTPSERVER_DEBUG" );
392
357
const char * use_syslog = std::getenv (" DUCKDB_HTTPSERVER_SYSLOG" );
393
-
358
+
394
359
if (debug_env != nullptr && std::string (debug_env) == " 1" ) {
395
360
global_state.server ->set_logger ([](const duckdb_httplib_openssl::Request& req, const duckdb_httplib_openssl::Response& res) {
396
361
time_t now_time = std::chrono::system_clock::to_time_t (std::chrono::system_clock::now ());
397
362
char timestr[32 ];
398
363
strftime (timestr, sizeof (timestr), " %Y-%m-%d %H:%M:%S" , localtime (&now_time));
399
364
// 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 " ,
401
366
timestr,
402
367
req.method .c_str (),
403
- req.path .c_str (),
368
+ req.path .c_str (),
404
369
res.status ,
405
370
req.remote_addr .c_str (),
406
371
req.remote_port );
@@ -409,9 +374,9 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t
409
374
} else if (use_syslog != nullptr && std::string (use_syslog) == " 1" ) {
410
375
openlog (" duckdb-httpserver" , LOG_PID | LOG_NDELAY, LOG_LOCAL0);
411
376
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" ,
413
378
req.method .c_str (),
414
- req.path .c_str (),
379
+ req.path .c_str (),
415
380
res.status ,
416
381
req.remote_addr .c_str (),
417
382
req.remote_port );
@@ -436,7 +401,7 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t
436
401
}
437
402
global_state.is_running = false ; // Update the running state
438
403
});
439
-
404
+
440
405
// Run the server in the same thread
441
406
if (!global_state.server ->listen (host_str.c_str (), port)) {
442
407
global_state.is_running = false ;
@@ -455,7 +420,6 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t
455
420
}
456
421
});
457
422
}
458
-
459
423
}
460
424
461
425
void HttpServerStop () {
0 commit comments