yql_servlet.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #pragma once
  2. #include "servlet.h"
  3. #include <yql/essentials/ast/yql_errors.h>
  4. #include <yql/essentials/ast/yql_ast.h>
  5. #include <library/cpp/json/json_reader.h>
  6. #include <library/cpp/json/json_writer.h>
  7. #define YQL_ACTION(action) \
  8. class TYqlAction##action: public ::NYql::NHttp::TYqlAction { \
  9. public: \
  10. TYqlAction##action( \
  11. ::NYql::NHttp::TYqlServer& yqlServer, \
  12. ::NJson::TJsonWriter& writer, \
  13. const ::NYql::NHttp::TRequest& req, \
  14. ::NYql::NHttp::TResponse& resp) \
  15. : ::NYql::NHttp::TYqlAction(yqlServer, writer, req, resp) {} \
  16. namespace NYql {
  17. namespace NHttp {
  18. class TYqlServer;
  19. ///////////////////////////////////////////////////////////////////////////////
  20. // TYqlAction
  21. ///////////////////////////////////////////////////////////////////////////////
  22. class TYqlAction: private TNonCopyable
  23. {
  24. public:
  25. enum EOptions {
  26. YqlProgram = 0x01,
  27. SqlProgram = 0x02,
  28. PrintAst = 0x0100,
  29. PrintExpr = 0x0200,
  30. PrintTraceOpt = 0x0400,
  31. WithFinalIssues = 0x0800,
  32. };
  33. public:
  34. TYqlAction(
  35. TYqlServer& yqlServer,
  36. NJson::TJsonWriter& writer,
  37. const TRequest& req,
  38. TResponse& resp)
  39. : YqlServer(yqlServer)
  40. , Writer(writer)
  41. , Req(req)
  42. , Resp(resp)
  43. {
  44. }
  45. protected:
  46. void WriteStatus(bool success, const TIssues& errors) const;
  47. void WriteAstTree(const TAstNode* node);
  48. protected:
  49. TYqlServer& YqlServer;
  50. NJson::TJsonWriter& Writer;
  51. const TRequest& Req;
  52. TResponse& Resp;
  53. };
  54. ///////////////////////////////////////////////////////////////////////////////
  55. // TYqlServlet
  56. ///////////////////////////////////////////////////////////////////////////////
  57. template <typename TAction>
  58. class TYqlServlet: public IServlet
  59. {
  60. public:
  61. TYqlServlet(TYqlServer& yqlServer)
  62. : YqlServer_(yqlServer)
  63. {
  64. }
  65. void DoPost(const TRequest& req, TResponse& resp) const override final {
  66. NJson::TJsonValue value;
  67. TStringBuf body(req.Body.AsCharPtr(), req.Body.Size());
  68. bool parsed = NJson::ReadJsonFastTree(body, &value, true);
  69. Y_ENSURE_EX(parsed, THttpError(HTTP_BAD_REQUEST) << "can't parse json");
  70. const TString& program = value[TStringBuf("program")].GetString();
  71. const TString& input = value[TStringBuf("tableInput")].GetString();
  72. const TString& attr = value[TStringBuf("tableAttr")].GetString();
  73. const TString& lang = value[TStringBuf("lang")].GetString();
  74. const TString& params = value[TStringBuf("parameters")].GetString();
  75. ui32 options = 0;
  76. if (req.RD.CgiParam.Has(TStringBuf("printAst"))) {
  77. options |= TYqlAction::PrintAst;
  78. }
  79. if (req.RD.CgiParam.Has(TStringBuf("printExpr"))) {
  80. options |= TYqlAction::PrintExpr;
  81. }
  82. if (req.RD.CgiParam.Has(TStringBuf("traceOpt"))) {
  83. options |= TYqlAction::PrintTraceOpt;
  84. }
  85. if (lang == TStringBuf("yql")) {
  86. options |= TYqlAction::YqlProgram;
  87. } else if (lang == TStringBuf("sql")) {
  88. options |= TYqlAction::SqlProgram;
  89. }
  90. options |= TYqlAction::WithFinalIssues;
  91. TStringStream output;
  92. NJson::TJsonWriter writer(&output, false);
  93. writer.OpenMap();
  94. TAction action(YqlServer_, writer, req, resp);
  95. action.Perform(program, input, attr, options, params);
  96. writer.CloseMap();
  97. writer.Flush();
  98. resp.Body = TBlob::FromString(output.Str());
  99. resp.ContentType = TStringBuf("application/json");
  100. }
  101. private:
  102. TYqlServer& YqlServer_;
  103. };
  104. } // namspace NNttp
  105. } // namspace NYql