|
| 1 | +#pragma once |
| 2 | +#include "json_local_rpc.h" |
| 3 | +#include <ydb/core/grpc_services/rpc_calls.h> |
| 4 | +#include <ydb/core/viewer/yaml/yaml.h> |
| 5 | +#include <ydb/public/api/grpc/ydb_query_v1.grpc.pb.h> |
| 6 | +#include <ydb/public/api/grpc/ydb_operation_v1.grpc.pb.h> |
| 7 | + |
| 8 | +namespace NKikimr::NViewer { |
| 9 | + |
| 10 | +using TQueryExecuteScriptRpc = TJsonLocalRpc<Ydb::Query::ExecuteScriptRequest, |
| 11 | + Ydb::Operations::Operation, |
| 12 | + Ydb::Operations::Operation, |
| 13 | + Ydb::Query::V1::QueryService, |
| 14 | + NKikimr::NGRpcService::TGrpcRequestNoOperationCall<Ydb::Query::ExecuteScriptRequest, Ydb::Operations::Operation>>; |
| 15 | + |
| 16 | +class TQueryExecuteScript : public TQueryExecuteScriptRpc { |
| 17 | +public: |
| 18 | + using TBase = TQueryExecuteScriptRpc; |
| 19 | + |
| 20 | + TQueryExecuteScript(IViewer* viewer, NMon::TEvHttpInfo::TPtr& ev) |
| 21 | + : TBase(viewer, ev) |
| 22 | + { |
| 23 | + OperationMode = Ydb::Operations::OperationParams::ASYNC; |
| 24 | + } |
| 25 | + |
| 26 | + void Bootstrap() override { |
| 27 | + if (Event->Get()->Request.GetMethod() != HTTP_METHOD_POST) { |
| 28 | + return ReplyAndPassAway(Viewer->GetHTTPBADREQUEST(Event->Get(), "text/plain", "Only POST method is allowed")); |
| 29 | + } |
| 30 | + |
| 31 | + TStringBuf content = Event->Get()->Request.GetPostContent(); |
| 32 | + static NJson::TJsonReaderConfig JsonConfig; |
| 33 | + NJson::TJsonValue params; |
| 34 | + bool success = NJson::ReadJsonTree(content, &JsonConfig, ¶ms); |
| 35 | + if (!success) { |
| 36 | + return ReplyAndPassAway(Viewer->GetHTTPBADREQUEST(Event->Get(), "text/plain", "Empty content received")); |
| 37 | + } |
| 38 | + if (params.Has("database")) { |
| 39 | + Database = params["database"].GetStringRobust(); |
| 40 | + } else { |
| 41 | + return ReplyAndPassAway(Viewer->GetHTTPBADREQUEST(Event->Get(), "text/plain", "field 'database' is required")); |
| 42 | + } |
| 43 | + Request.mutable_operation_params()->set_operation_mode(Ydb::Operations::OperationParams::ASYNC); |
| 44 | + if (params.Has("query")) { |
| 45 | + auto query = params["query"].GetStringRobust(); |
| 46 | + Request.mutable_script_content()->set_text(query); |
| 47 | + auto syntax = params["syntax"].GetStringRobust(); |
| 48 | + if (syntax == "yql_v1") { |
| 49 | + Request.mutable_script_content()->set_syntax(Ydb::Query::SYNTAX_YQL_V1); |
| 50 | + } else if (syntax == "pg") { |
| 51 | + Request.mutable_script_content()->set_syntax(Ydb::Query::SYNTAX_PG); |
| 52 | + } else { |
| 53 | + Request.mutable_script_content()->set_syntax(Ydb::Query::SYNTAX_UNSPECIFIED); |
| 54 | + } |
| 55 | + } else { |
| 56 | + return ReplyAndPassAway(Viewer->GetHTTPBADREQUEST(Event->Get(), "text/plain", "field 'query' is required")); |
| 57 | + } |
| 58 | + |
| 59 | + if (params.Has("stats")) { |
| 60 | + auto Stats = params["stats"].GetStringRobust(); |
| 61 | + if (Stats == "none"){ |
| 62 | + Request.set_stats_mode(Ydb::Query::STATS_MODE_NONE); |
| 63 | + } else if (Stats == "basic") { |
| 64 | + Request.set_stats_mode(Ydb::Query::STATS_MODE_BASIC); |
| 65 | + } else if (Stats == "profile") { |
| 66 | + Request.set_stats_mode(Ydb::Query::STATS_MODE_FULL); |
| 67 | + } else if (Stats == "full") { |
| 68 | + Request.set_stats_mode(Ydb::Query::STATS_MODE_PROFILE); |
| 69 | + } |
| 70 | + } else { |
| 71 | + Request.set_stats_mode(Ydb::Query::STATS_MODE_NONE); |
| 72 | + } |
| 73 | + |
| 74 | + if (params.Has("exec_mode")) { |
| 75 | + auto execMode = params["exec_mode"].GetStringRobust(); |
| 76 | + if (execMode == "parse") { |
| 77 | + Request.set_exec_mode(Ydb::Query::EXEC_MODE_PARSE); |
| 78 | + } else if (execMode == "validate") { |
| 79 | + Request.set_exec_mode(Ydb::Query::EXEC_MODE_VALIDATE); |
| 80 | + } else if (execMode == "explain") { |
| 81 | + Request.set_exec_mode(Ydb::Query::EXEC_MODE_EXPLAIN); |
| 82 | + } else if (execMode== "execute") { |
| 83 | + Request.set_exec_mode(Ydb::Query::EXEC_MODE_EXECUTE); |
| 84 | + } |
| 85 | + } else { |
| 86 | + Request.set_exec_mode(Ydb::Query::EXEC_MODE_EXECUTE); |
| 87 | + } |
| 88 | + |
| 89 | + TBase::Bootstrap(); |
| 90 | + } |
| 91 | + |
| 92 | + static YAML::Node GetSwagger() { |
| 93 | + YAML::Node node = YAML::Load(R"___( |
| 94 | + get: |
| 95 | + tags: |
| 96 | + - operation |
| 97 | + summary: Execute script |
| 98 | + description: Execute script |
| 99 | + parameters: |
| 100 | + - name: database |
| 101 | + in: query |
| 102 | + description: database name |
| 103 | + required: true |
| 104 | + type: string |
| 105 | + - name: query |
| 106 | + in: query |
| 107 | + description: query |
| 108 | + required: true |
| 109 | + type: string |
| 110 | + - name: syntax |
| 111 | + in: query |
| 112 | + description: syntax |
| 113 | + required: false |
| 114 | + type: string |
| 115 | + - name: stats |
| 116 | + in: query |
| 117 | + description: stats |
| 118 | + required: false |
| 119 | + type: string |
| 120 | + - name: exec_mode |
| 121 | + in: query |
| 122 | + description: exec_mode |
| 123 | + required: false |
| 124 | + type: string |
| 125 | + responses: |
| 126 | + 200: |
| 127 | + description: OK |
| 128 | + content: |
| 129 | + application/json: |
| 130 | + schema: {} |
| 131 | + 400: |
| 132 | + description: Bad Request |
| 133 | + 403: |
| 134 | + description: Forbidden |
| 135 | + 504: |
| 136 | + description: Gateway Timeout |
| 137 | + )___"); |
| 138 | + node["get"]["responses"]["200"]["content"]["application/json"]["schema"] = TProtoToYaml::ProtoToYamlSchema<Ydb::Operations::Operation>(); |
| 139 | + return node; |
| 140 | + } |
| 141 | +}; |
| 142 | + |
| 143 | +} |
0 commit comments