@@ -3095,6 +3095,146 @@ class SetGlobalVariableHandler final : public HttpRequestHandler {
3095
3095
}
3096
3096
};
3097
3097
3098
+ class ShowSessionVariablesHandler final : public HttpRequestHandler {
3099
+ public:
3100
+ SharedPtr<OutgoingResponse> handle (const SharedPtr<IncomingRequest> &request) final {
3101
+ auto infinity = Infinity::RemoteConnect ();
3102
+ DeferFn defer_fn ([&]() { infinity->RemoteDisconnect (); });
3103
+
3104
+ auto result = infinity->ShowVariables (SetScope::kSession );
3105
+
3106
+ nlohmann::json json_response;
3107
+ HTTPStatus http_status;
3108
+
3109
+ if (result.IsOk ()) {
3110
+ json_response[" error_code" ] = 0 ;
3111
+ DataBlock *data_block = result.result_table_ ->GetDataBlockById (0 ).get (); // Assume the variables output data only included in one data block
3112
+ auto row_count = data_block->row_count ();
3113
+ for (int row = 0 ; row < row_count; ++row) {
3114
+ // variable name
3115
+ Value name_value = data_block->GetValue (0 , row);
3116
+ const String& config_name = name_value.ToString ();
3117
+ // variable value
3118
+ Value value = data_block->GetValue (1 , row);
3119
+ const String &config_value = value.ToString ();
3120
+ json_response[config_name] = config_value;
3121
+ }
3122
+ http_status = HTTPStatus::CODE_200;
3123
+ } else {
3124
+ json_response[" error_code" ] = result.ErrorCode ();
3125
+ json_response[" error_message" ] = result.ErrorMsg ();
3126
+ http_status = HTTPStatus::CODE_500;
3127
+ }
3128
+ return ResponseFactory::createResponse (http_status, json_response.dump ());
3129
+ }
3130
+ };
3131
+
3132
+ class ShowSessionVariableHandler final : public HttpRequestHandler {
3133
+ public:
3134
+ SharedPtr<OutgoingResponse> handle (const SharedPtr<IncomingRequest> &request) final {
3135
+ auto infinity = Infinity::RemoteConnect ();
3136
+ DeferFn defer_fn ([&]() { infinity->RemoteDisconnect (); });
3137
+
3138
+ auto variable_name = request->getPathVariable (" variable_name" );
3139
+ auto result = infinity->ShowVariable (variable_name, SetScope::kSession );
3140
+
3141
+ nlohmann::json json_response;
3142
+ HTTPStatus http_status;
3143
+
3144
+ if (result.IsOk ()) {
3145
+ json_response[" error_code" ] = 0 ;
3146
+ DataBlock *data_block = result.result_table_ ->GetDataBlockById (0 ).get ();
3147
+ Value value = data_block->GetValue (0 , 0 );
3148
+ const String &variable_value = value.ToString ();
3149
+ json_response[variable_name] = variable_value;
3150
+
3151
+ http_status = HTTPStatus::CODE_200;
3152
+ } else {
3153
+ json_response[" error_code" ] = result.ErrorCode ();
3154
+ json_response[" error_message" ] = result.ErrorMsg ();
3155
+ http_status = HTTPStatus::CODE_500;
3156
+ }
3157
+ return ResponseFactory::createResponse (http_status, json_response.dump ());
3158
+ }
3159
+ };
3160
+
3161
+ class SetSessionVariableHandler final : public HttpRequestHandler {
3162
+ public:
3163
+ SharedPtr<OutgoingResponse> handle (const SharedPtr<IncomingRequest> &request) final {
3164
+ auto infinity = Infinity::RemoteConnect ();
3165
+ DeferFn defer_fn ([&]() { infinity->RemoteDisconnect (); });
3166
+
3167
+ nlohmann::json json_response;
3168
+ HTTPStatus http_status;
3169
+ QueryResult result;
3170
+
3171
+ String data_body = request->readBodyToString ();
3172
+ try {
3173
+
3174
+ nlohmann::json http_body_json = nlohmann::json::parse (data_body);
3175
+ if (http_body_json.size () != 1 ) {
3176
+ json_response[" error_code" ] = 3076 ;
3177
+ json_response[" error_message" ] = " No variable will be set" ;
3178
+ http_status = HTTPStatus::CODE_500;
3179
+ return ResponseFactory::createResponse (http_status, json_response.dump ());
3180
+ }
3181
+
3182
+ for (const auto &set_variable : http_body_json.items ()) {
3183
+ String var_name = set_variable.key ();
3184
+ const auto &var_value = set_variable.value ();
3185
+ switch (var_value.type ()) {
3186
+ case nlohmann::json::value_t ::boolean: {
3187
+ bool bool_value = var_value.template get <bool >();
3188
+ result = infinity->SetVariableOrConfig (var_name, bool_value, SetScope::kSession );
3189
+ break ;
3190
+ }
3191
+ case nlohmann::json::value_t ::number_integer: {
3192
+ i64 integer_value = var_value.template get <i64>();
3193
+ result = infinity->SetVariableOrConfig (var_name, integer_value, SetScope::kSession );
3194
+ break ;
3195
+ }
3196
+ case nlohmann::json::value_t ::number_unsigned: {
3197
+ i64 integer_value = var_value.template get <u64>();
3198
+ result = infinity->SetVariableOrConfig (var_name, integer_value, SetScope::kSession );
3199
+ break ;
3200
+ }
3201
+ case nlohmann::json::value_t ::number_float: {
3202
+ f64 double_value = var_value.template get <f64>();
3203
+ result = infinity->SetVariableOrConfig (var_name, double_value, SetScope::kSession );
3204
+ break ;
3205
+ }
3206
+ case nlohmann::json::value_t ::string: {
3207
+ String str_value = var_value.template get <std::string>();
3208
+ result = infinity->SetVariableOrConfig (var_name, str_value, SetScope::kSession );
3209
+ break ;
3210
+ }
3211
+ case nlohmann::json::value_t ::array:
3212
+ case nlohmann::json::value_t ::object:
3213
+ case nlohmann::json::value_t ::binary:
3214
+ case nlohmann::json::value_t ::null:
3215
+ case nlohmann::json::value_t ::discarded: {
3216
+ json_response[" error_code" ] = ErrorCode::kInvalidExpression ;
3217
+ json_response[" error_message" ] = fmt::format (" Invalid set variable expression" );
3218
+ return ResponseFactory::createResponse (http_status, json_response.dump ());
3219
+ }
3220
+ }
3221
+ }
3222
+ } catch (nlohmann::json::exception &e) {
3223
+ json_response[" error_code" ] = ErrorCode::kInvalidJsonFormat ;
3224
+ json_response[" error_message" ] = e.what ();
3225
+ }
3226
+ if (result.IsOk ()) {
3227
+ json_response[" error_code" ] = 0 ;
3228
+ http_status = HTTPStatus::CODE_200;
3229
+ } else {
3230
+ json_response[" error_code" ] = result.ErrorCode ();
3231
+ json_response[" error_message" ] = result.ErrorMsg ();
3232
+ http_status = HTTPStatus::CODE_500;
3233
+ }
3234
+ return ResponseFactory::createResponse (http_status, json_response.dump ());
3235
+ }
3236
+ };
3237
+
3098
3238
class SetConfigHandler final : public HttpRequestHandler {
3099
3239
public:
3100
3240
SharedPtr<OutgoingResponse> handle (const SharedPtr<IncomingRequest> &request) final {
@@ -3220,7 +3360,7 @@ void HTTPServer::Start(const String& ip_address, u16 port) {
3220
3360
router->route (" PUT" , " /databases/{database_name}/tables/{table_name}/indexes/{index_name}" , MakeShared<OptimizeIndexHandler>());
3221
3361
3222
3362
// alter
3223
- router->route (" PUT " , " /databases/{database_name}/tables/{table_name}/columns" , MakeShared<AddColumnsHandler>());
3363
+ router->route (" POST " , " /databases/{database_name}/tables/{table_name}/columns" , MakeShared<AddColumnsHandler>());
3224
3364
router->route (" DELETE" , " /databases/{database_name}/tables/{table_name}/columns" , MakeShared<DropColumnsHandler>());
3225
3365
3226
3366
// segment
@@ -3243,10 +3383,12 @@ void HTTPServer::Start(const String& ip_address, u16 port) {
3243
3383
router->route (" POST" , " /configs" , MakeShared<SetConfigHandler>());
3244
3384
3245
3385
// variable
3246
- router->route (" GET" , " /variables" , MakeShared<ShowGlobalVariablesHandler>());
3247
- router->route (" GET" , " /variables/{variable_name}" , MakeShared<ShowGlobalVariableHandler>());
3248
-
3249
- router->route (" POST" , " /variables" , MakeShared<SetGlobalVariableHandler>());
3386
+ router->route (" GET" , " /variables/global" , MakeShared<ShowGlobalVariablesHandler>());
3387
+ router->route (" GET" , " /variables/global/{variable_name}" , MakeShared<ShowGlobalVariableHandler>());
3388
+ router->route (" SET" , " /variables/global" , MakeShared<SetGlobalVariableHandler>());
3389
+ router->route (" GET" , " /variables/session" , MakeShared<ShowSessionVariablesHandler>());
3390
+ router->route (" GET" , " /variables/session/{variable_name}" , MakeShared<ShowSessionVariableHandler>());
3391
+ router->route (" SET" , " /variables/session" , MakeShared<SetSessionVariableHandler>());
3250
3392
3251
3393
SharedPtr<HttpConnectionProvider> connection_provider = HttpConnectionProvider::createShared ({ip_address, port, WebAddress::IP_4});
3252
3394
SharedPtr<HttpConnectionHandler> connection_handler = HttpConnectionHandler::createShared (router);
0 commit comments