mirror of https://gitee.com/bigwinds/arangodb
copied query error
This commit is contained in:
parent
b7b5c661a7
commit
5e2ac40608
|
@ -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<char*>(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:
|
|
@ -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:
|
|
@ -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 \
|
||||
|
|
|
@ -5333,6 +5333,52 @@ static v8::Handle<v8::Value> JS_ForgetApplierReplication (v8::Arguments const& a
|
|||
return scope.Close(v8::True());
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// --SECTION-- AQL
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief executes an AQL query
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static v8::Handle<v8::Value> JS_ExecuteAql (v8::Arguments const& argv) {
|
||||
v8::HandleScope scope;
|
||||
|
||||
if (argv.Length() < 1 || argv.Length() > 3) {
|
||||
TRI_V8_EXCEPTION_USAGE(scope, "AQL_EXECUTE(<querystring>, <bindvalues>, <options>)");
|
||||
}
|
||||
|
||||
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 <querystring>");
|
||||
}
|
||||
|
||||
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<v8::Object> 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<v8::Context> 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);
|
||||
|
|
Loading…
Reference in New Issue