ws_sqlService.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2014 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef _ESPWIZ_WS_SQL_HPP__
  14. #define _ESPWIZ_WS_SQL_HPP__
  15. #include <build-config.h>
  16. #include "ws_sql.hpp"
  17. #include "ws_sql_esp.ipp"
  18. #include "ws_ecl_wuinfo.hpp"
  19. #include "ws_workunitsHelpers.hpp"
  20. #include "ws_dfuService.hpp"
  21. #include "fileview.hpp"
  22. #include "environment.hpp"
  23. #include "TpWrapper.hpp"
  24. #include "HPCCFileCache.hpp"
  25. #include "HPCCFile.hpp"
  26. #include "ECLEngine.hpp"
  27. #include "SQLTable.hpp"
  28. #include "HPCCSQLLexer.h"
  29. #include "HPCCSQLParser.h"
  30. #include "HPCCSQLTreeWalker.hpp"
  31. #include "dautils.hpp"
  32. #define EMBEDDEDSQLQUERYCOMMENT "\n\n/****************************************************\nOriginal SQL: \"%s\"\nNormalized SQL: \"%s\"\n****************************************************/\n"
  33. static const char* WSSQLACCESS = "WsSqlAccess";
  34. static const char* WSSQLRESULT = "WsSQLResult";
  35. static const char* WSSQLCOUNT = "WsSQLCount";
  36. static const char* WSSQLRESULTSCHEMA = "WsSQLResultSchema";
  37. static StringBuffer g_wssqlBuildVersion;
  38. class CwssqlSoapBindingEx : public CwssqlSoapBinding
  39. {
  40. public:
  41. CwssqlSoapBindingEx(IPropertyTree *cfg, const char *name, const char *process, http_soap_log_level llevel=hsl_none) : CwssqlSoapBinding(cfg, name, process, llevel)
  42. {
  43. }
  44. };
  45. class CwssqlEx : public Cwssql
  46. {
  47. private:
  48. BoolHash validClusters;
  49. CriticalSection crit;
  50. IPropertyTree *cfg;
  51. std::map<std::string,std::string> cachedSQLQueries;
  52. static const unsigned int ExpireSeconds = 60 * 60;
  53. CriticalSection critCache;
  54. bool isQueryCached(const char * sqlQuery);
  55. bool getCachedQuery(const char * sqlQuery, StringBuffer & wuid);
  56. bool addQueryToCache(const char * sqlQuery, const char * wuid);
  57. void removeQueryFromCache(const char * sqlQuery);
  58. time_t cacheFlushTime;
  59. bool isCacheExpired()
  60. {
  61. time_t timeNow;
  62. time(&timeNow);
  63. return difftime(timeNow, cacheFlushTime) > ExpireSeconds;
  64. }
  65. void setNewCacheFlushTime()
  66. {
  67. time(&cacheFlushTime);
  68. }
  69. void setWsSqlBuildVersion(const char* buildVersion)
  70. {
  71. g_wssqlBuildVersion.clear();
  72. if(buildVersion&&*buildVersion)
  73. g_wssqlBuildVersion.set(buildVersion);
  74. g_wssqlBuildVersion.trim();
  75. }
  76. public:
  77. IMPLEMENT_IINTERFACE;
  78. virtual void init(IPropertyTree *_cfg, const char *_process, const char *_service);
  79. bool onEcho(IEspContext &context, IEspEchoRequest &req, IEspEchoResponse &resp);
  80. bool onPrepareSQL(IEspContext &context, IEspPrepareSQLRequest &req, IEspPrepareSQLResponse &resp);
  81. bool onExecuteSQL(IEspContext &context, IEspExecuteSQLRequest &req, IEspExecuteSQLResponse &resp);
  82. bool getWUResult(IEspContext &context, const char * wuid, StringBuffer &resp, unsigned start, unsigned count, int sequence, const char * dsname, const char * schemaname);
  83. bool onExecutePreparedSQL(IEspContext &context, IEspExecutePreparedSQLRequest &req, IEspExecutePreparedSQLResponse &resp);
  84. bool onGetDBSystemInfo(IEspContext &context, IEspGetDBSystemInfoRequest &req, IEspGetDBSystemInfoResponse &resp);
  85. bool onGetDBMetaData(IEspContext &context, IEspGetDBMetaDataRequest &req, IEspGetDBMetaDataResponse &resp);
  86. bool onGetResults(IEspContext &context, IEspGetResultsRequest &req, IEspGetResultsResponse &resp);
  87. bool onGetRelatedIndexes(IEspContext &context, IEspGetRelatedIndexesRequest &req, IEspGetRelatedIndexesResponse &resp);
  88. bool onSetRelatedIndexes(IEspContext &context, IEspSetRelatedIndexesRequest &req, IEspSetRelatedIndexesResponse &resp);
  89. bool onCreateTableAndLoad(IEspContext &context, IEspCreateTableAndLoadRequest &req, IEspCreateTableAndLoadResponse &resp);
  90. void refreshValidClusters();
  91. bool isValidCluster(const char *cluster);
  92. void processMultipleClusterOption(StringArray & clusters, const char * targetcluster, StringBuffer & hashoptions);
  93. void fetchRequiredHpccFiles(IArrayOf<SQLTable> * sqltables);
  94. static void fetchRequiredHpccFiles(IArrayOf<SQLTable> * sqltables, HpccFiles * hpccfilecache);
  95. HPCCSQLTreeWalker * parseSQL(IEspContext &context, StringBuffer & sqltext, bool attemptParameterization = true);
  96. bool executePublishedQueryByName(IEspContext &context, const char * queryset, const char * queryname, StringBuffer &clonedwuid, const char *paramXml, IArrayOf<IConstNamedValue> *variables, const char * targetcluster, int start, int count);
  97. bool executePublishedQueryByWuId(IEspContext &context, const char * targetwuid, StringBuffer &clonedwuid, const char *paramXml, IArrayOf<IConstNamedValue> *variables, const char * targetcluster, int start, int count);
  98. bool executePublishedQuery(IEspContext &context, const char * queryset, const char * queryname, StringBuffer &resp, int start, int count, int waittime);
  99. bool executePublishedQuery(IEspContext &context, const char * wuid, StringBuffer &resp, int start, int count, int waittime);
  100. bool cloneAndExecuteWU(IEspContext &context, const char * originalwuid, StringBuffer &clonedwuid, const char *paramXml, IArrayOf<IConstNamedValue> *variables, IArrayOf<IConstNamedValue> *debugs, const char * targetcluster);
  101. bool publishWorkunit(IEspContext &context, const char * queryname, const char * wuid, const char * targetcluster);
  102. static void createWUXMLParams(StringBuffer & xmlparams, HPCCSQLTreeWalker* parsedSQL, IArrayOf<IConstNamedValue> *variables, IConstWorkUnit * cw);
  103. static void createWUXMLParams(StringBuffer & xmlparams, const IArrayOf <ISQLExpression> * parameterlist);
  104. const char* getWsSqlBuildVersion()
  105. {
  106. return g_wssqlBuildVersion.str();
  107. }
  108. };
  109. #endif //_ESPWIZ_WS_SQL_HPP__