From 5e2ac40608708a1c7b4e66ee48281fb2bf25eb2d Mon Sep 17 00:00:00 2001 From: Jan Steemann Date: Thu, 24 Jul 2014 11:18:20 +0200 Subject: [PATCH] copied query error --- arangod/Aql/error.cpp | 162 ++++++++++++++++++++++++++++++++ arangod/Aql/error.h | 92 ++++++++++++++++++ arangod/Makefile.files | 1 + arangod/V8Server/v8-vocbase.cpp | 49 ++++++++++ 4 files changed, 304 insertions(+) create mode 100644 arangod/Aql/error.cpp create mode 100644 arangod/Aql/error.h diff --git a/arangod/Aql/error.cpp b/arangod/Aql/error.cpp new file mode 100644 index 0000000000..c6c2947bd8 --- /dev/null +++ b/arangod/Aql/error.cpp @@ -0,0 +1,162 @@ +//////////////////////////////////////////////////////////////////////////////// +/// @brief Aql, error handling +/// +/// @file +/// +/// DISCLAIMER +/// +/// Copyright 2014 ArangoDB GmbH, Cologne, Germany +/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany +/// +/// Licensed under the Apache License, Version 2.0 (the "License"); +/// you may not use this file except in compliance with the License. +/// You may obtain a copy of the License at +/// +/// http://www.apache.org/licenses/LICENSE-2.0 +/// +/// Unless required by applicable law or agreed to in writing, software +/// distributed under the License is distributed on an "AS IS" BASIS, +/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +/// See the License for the specific language governing permissions and +/// limitations under the License. +/// +/// Copyright holder is ArangoDB GmbH, Cologne, Germany +/// +/// @author Jan Steemann +/// @author Copyright 2014, ArangoDB GmbH, Cologne, Germany +/// @author Copyright 2012-2013, triAGENS GmbH, Cologne, Germany +//////////////////////////////////////////////////////////////////////////////// + +#include "Aql/error.h" + +#include "BasicsC/tri-strings.h" + +using namespace triagens::aql; + +// ----------------------------------------------------------------------------- +// --SECTION-- defines +// ----------------------------------------------------------------------------- + +//////////////////////////////////////////////////////////////////////////////// +/// @brief error snippet length +//////////////////////////////////////////////////////////////////////////////// + +#define SNIPPET_LENGTH 32 + +//////////////////////////////////////////////////////////////////////////////// +/// @brief error snippet suffix +//////////////////////////////////////////////////////////////////////////////// + +#define SNIPPET_SUFFIX "..." + +// ----------------------------------------------------------------------------- +// --SECTION-- public functions +// ----------------------------------------------------------------------------- + +//////////////////////////////////////////////////////////////////////////////// +/// @brief get the error string registered last +//////////////////////////////////////////////////////////////////////////////// + +char* query_error_t::getMessage () const { + if (getCode() == TRI_ERROR_NO_ERROR) { + return nullptr; + } + + if (_message == nullptr) { + return nullptr; + } + + if (_data && (nullptr != strstr(_message, "%s"))) { + char buffer[256]; + + int written = snprintf(buffer, sizeof(buffer), _message, _data); + + return TRI_DuplicateString2Z(TRI_UNKNOWN_MEM_ZONE, (const char*) &buffer, (size_t) written); + } + + return TRI_DuplicateStringZ(TRI_UNKNOWN_MEM_ZONE, _message); +} + +//////////////////////////////////////////////////////////////////////////////// +/// @brief get a formatted query error message +//////////////////////////////////////////////////////////////////////////////// + +char* query_error_t::getContextError (char const* query, + size_t queryLength, + size_t line, + size_t column) const { + const char* p; + char* q; + char* result; + char c; + // note: line numbers reported by bison/flex start at 1, columns start at 0 + size_t offset; + size_t currentLine = 1; + size_t currentColumn = 0; + + TRI_ASSERT(query); + + p = query; + while ((c = *p)) { + if (currentLine > line || (currentLine >= line && currentColumn >= column)) { + break; + } + + if (c == '\n') { + ++p; + ++currentLine; + currentColumn = 0; + } + else if (c == '\r') { + ++p; + ++currentLine; + currentColumn = 0; + + if (*p == '\n') { + ++p; + } + } + else { + ++currentColumn; + ++p; + } + } + + // p is pointing at the position in the query the parse error occurred at + TRI_ASSERT(p >= query); + + offset = (size_t) (p - query); + + if (queryLength < offset + SNIPPET_LENGTH) { + return TRI_DuplicateString2Z(TRI_UNKNOWN_MEM_ZONE, query + offset, queryLength - offset); + } + + q = result = static_cast(TRI_Allocate(TRI_UNKNOWN_MEM_ZONE, + SNIPPET_LENGTH + strlen(SNIPPET_SUFFIX) + 1, false)); + + if (result == nullptr) { + // out of memory + return nullptr; + } + + // copy query part + memcpy(q, query + offset, SNIPPET_LENGTH); + q += SNIPPET_LENGTH; + + // copy ... + memcpy(q, SNIPPET_SUFFIX, strlen(SNIPPET_SUFFIX)); + q += strlen(SNIPPET_SUFFIX); + + *q = '\0'; + + return result; +} + +// ----------------------------------------------------------------------------- +// --SECTION-- END-OF-FILE +// ----------------------------------------------------------------------------- + +// Local Variables: +// mode: outline-minor +// outline-regexp: "/// @brief\\|/// {@inheritDoc}\\|/// @page\\|// --SECTION--\\|/// @\\}" +// End: diff --git a/arangod/Aql/error.h b/arangod/Aql/error.h new file mode 100644 index 0000000000..85ad867070 --- /dev/null +++ b/arangod/Aql/error.h @@ -0,0 +1,92 @@ +//////////////////////////////////////////////////////////////////////////////// +/// @brief Aql, error handling +/// +/// @file +/// +/// DISCLAIMER +/// +/// Copyright 2014 ArangoDB GmbH, Cologne, Germany +/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany +/// +/// Licensed under the Apache License, Version 2.0 (the "License"); +/// you may not use this file except in compliance with the License. +/// You may obtain a copy of the License at +/// +/// http://www.apache.org/licenses/LICENSE-2.0 +/// +/// Unless required by applicable law or agreed to in writing, software +/// distributed under the License is distributed on an "AS IS" BASIS, +/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +/// See the License for the specific language governing permissions and +/// limitations under the License. +/// +/// Copyright holder is ArangoDB GmbH, Cologne, Germany +/// +/// @author Jan Steemann +/// @author Copyright 2014, ArangoDB GmbH, Cologne, Germany +/// @author Copyright 2012-2013, triAGENS GmbH, Cologne, Germany +//////////////////////////////////////////////////////////////////////////////// + +#ifndef ARANGODB_AQL_ERROR_H +#define ARANGODB_AQL_ERROR_H 1 + +#include "Basics/Common.h" + +namespace triagens { + namespace aql { + +// ----------------------------------------------------------------------------- +// --SECTION-- public types +// ----------------------------------------------------------------------------- + +//////////////////////////////////////////////////////////////////////////////// +/// @brief query error structure +/// +/// This struct is used to hold information about errors that happen during +/// query execution. The data will be passed to the end user. +//////////////////////////////////////////////////////////////////////////////// + + struct query_error_t { + + query_error_t () + : _code(TRI_ERROR_NO_ERROR), + _data(nullptr) { + } + + ~query_error_t () { + if (_data != nullptr) { + TRI_Free(TRI_UNKNOWN_MEM_ZONE, _data); + } + } + + inline int getCode () const { + return _code; + } + + char* getMessage () const; + + char* getContextError (char const*, + size_t, + size_t, + size_t) const; + + int _code; + char* _message; + char* _data; + const char* _file; + int _line; + }; + + } +} + +#endif + +// ----------------------------------------------------------------------------- +// --SECTION-- END-OF-FILE +// ----------------------------------------------------------------------------- + +// Local Variables: +// mode: outline-minor +// outline-regexp: "/// @brief\\|/// {@inheritDoc}\\|/// @page\\|// --SECTION--\\|/// @\\}" +// End: diff --git a/arangod/Makefile.files b/arangod/Makefile.files index bb61267a27..e21737b288 100644 --- a/arangod/Makefile.files +++ b/arangod/Makefile.files @@ -38,6 +38,7 @@ arangod_libarangod_a_SOURCES = \ arangod/Ahuacatl/ahuacatl-statementlist.cpp \ arangod/Ahuacatl/ahuacatl-tokens.cpp \ arangod/Ahuacatl/ahuacatl-variable.cpp \ + arangod/Aql/error.cpp \ arangod/BitIndexes/bitarray.cpp \ arangod/BitIndexes/bitarrayIndex.cpp \ arangod/CapConstraint/cap-constraint.cpp \ diff --git a/arangod/V8Server/v8-vocbase.cpp b/arangod/V8Server/v8-vocbase.cpp index d13e28d7e4..640ab66f4b 100644 --- a/arangod/V8Server/v8-vocbase.cpp +++ b/arangod/V8Server/v8-vocbase.cpp @@ -5333,6 +5333,52 @@ static v8::Handle JS_ForgetApplierReplication (v8::Arguments const& a return scope.Close(v8::True()); } +// ----------------------------------------------------------------------------- +// --SECTION-- AQL +// ----------------------------------------------------------------------------- + +//////////////////////////////////////////////////////////////////////////////// +/// @brief executes an AQL query +//////////////////////////////////////////////////////////////////////////////// + +static v8::Handle JS_ExecuteAql (v8::Arguments const& argv) { + v8::HandleScope scope; + + if (argv.Length() < 1 || argv.Length() > 3) { + TRI_V8_EXCEPTION_USAGE(scope, "AQL_EXECUTE(, , )"); + } + + TRI_vocbase_t* vocbase = GetContextVocBase(); + + if (vocbase == nullptr) { + TRI_V8_EXCEPTION(scope, TRI_ERROR_ARANGO_DATABASE_NOT_FOUND); + } + + // get the query string + if (! argv[0]->IsString()) { + TRI_V8_TYPE_ERROR(scope, "expecting string for "); + } + + string const&& queryString = TRI_ObjectToString(argv[0]); + + // bind parameters + TRI_json_t* parameters = nullptr; + + if (argv.Length() > 1 && argv[1]->IsObject()) { + parameters = TRI_ObjectToJson(argv[1]); + } + + // execute the query + std::cout << "ABOUT TO EXECUTE AQL QUERY '" << queryString << "'" << std::endl; + v8::Handle result = v8::Object::New(); + + if (parameters != nullptr) { + TRI_FreeJson(TRI_UNKNOWN_MEM_ZONE, parameters); + } + + return scope.Close(result); +} + // ----------------------------------------------------------------------------- // --SECTION-- AHUACATL // ----------------------------------------------------------------------------- @@ -10597,6 +10643,9 @@ void TRI_InitV8VocBridge (v8::Handle context, TRI_AddGlobalFunctionVocbase(context, "AHUACATL_RUN", JS_RunAhuacatl, true); TRI_AddGlobalFunctionVocbase(context, "AHUACATL_EXPLAIN", JS_ExplainAhuacatl, true); TRI_AddGlobalFunctionVocbase(context, "AHUACATL_PARSE", JS_ParseAhuacatl, true); + + // new AQL functions. not intended to be used directly by end users + TRI_AddGlobalFunctionVocbase(context, "AQL_EXECUTE", JS_ExecuteAql, true); // cursor functions. not intended to be used by end users TRI_AddGlobalFunctionVocbase(context, "CURSOR", JS_Cursor, true);