mirror of https://gitee.com/bigwinds/arangodb
229 lines
8.1 KiB
JavaScript
229 lines
8.1 KiB
JavaScript
module.define("statement-basics", function(exports, module) {
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief Arango statements
|
|
///
|
|
/// @file
|
|
///
|
|
/// DISCLAIMER
|
|
///
|
|
/// Copyright 2012 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 triAGENS GmbH, Cologne, Germany
|
|
///
|
|
/// @author Jan Steemann
|
|
/// @author Copyright 2012, triAGENS GmbH, Cologne, Germany
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
var internal = require("internal");
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- ArangoStatement
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- constructors and destructors
|
|
// -----------------------------------------------------------------------------
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @addtogroup ArangoStatement
|
|
/// @{
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief constructor
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
function ArangoStatement (database, data) {
|
|
this._database = database;
|
|
this._doCount = false;
|
|
this._batchSize = null;
|
|
this._bindVars = {};
|
|
|
|
if (!(data instanceof Object)) {
|
|
throw "ArangoStatement needs initial data";
|
|
}
|
|
|
|
if (data.query === undefined || data.query === "") {
|
|
throw "ArangoStatement needs a valid query attribute";
|
|
}
|
|
this.setQuery(data.query);
|
|
|
|
if (data.count !== undefined) {
|
|
this.setCount(data.count);
|
|
}
|
|
if (data.batchSize !== undefined) {
|
|
this.setBatchSize(data.batchSize);
|
|
}
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief bind a parameter to the statement
|
|
///
|
|
/// This function can be called multiple times, once for each bind parameter.
|
|
/// All bind parameters will be transferred to the server in one go when
|
|
/// execute() is called.
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
ArangoStatement.prototype.bind = function (key, value) {
|
|
if (key instanceof Object) {
|
|
if (value !== undefined) {
|
|
throw "invalid bind parameter declaration";
|
|
}
|
|
|
|
this._bindVars = key;
|
|
}
|
|
else if (typeof(key) === "string") {
|
|
if (this._bindVars[key] !== undefined) {
|
|
throw "redeclaration of bind parameter";
|
|
}
|
|
|
|
this._bindVars[key] = value;
|
|
}
|
|
else if (typeof(key) === "number") {
|
|
var strKey = String(parseInt(key));
|
|
|
|
if (strKey !== String(key)) {
|
|
throw "invalid bind parameter declaration";
|
|
}
|
|
|
|
if (this._bindVars[strKey] !== undefined) {
|
|
throw "redeclaration of bind parameter";
|
|
}
|
|
|
|
this._bindVars[strKey] = value;
|
|
}
|
|
else {
|
|
throw "invalid bind parameter declaration";
|
|
}
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief return the bind variables already set
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
ArangoStatement.prototype.getBindVariables = function () {
|
|
return this._bindVars;
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief get the count flag for the statement
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
ArangoStatement.prototype.getCount = function () {
|
|
return this._doCount;
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief get the maximum number of results documents the cursor will return
|
|
/// in a single server roundtrip.
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
ArangoStatement.prototype.getBatchSize = function () {
|
|
return this._batchSize;
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief get query string
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
ArangoStatement.prototype.getQuery = function () {
|
|
return this._query;
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief set the count flag for the statement
|
|
///
|
|
/// Setting the count flag will make the statement's result cursor return the
|
|
/// total number of result documents. The count flag is not set by default.
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
ArangoStatement.prototype.setCount = function (bool) {
|
|
this._doCount = bool ? true : false;
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief set the maximum number of results documents the cursor will return
|
|
/// in a single server roundtrip.
|
|
/// The higher this number is, the less server roundtrips will be made when
|
|
/// iterating over the result documents of a cursor.
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
ArangoStatement.prototype.setBatchSize = function (value) {
|
|
if (parseInt(value) > 0) {
|
|
this._batchSize = parseInt(value);
|
|
}
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief set the query string
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
ArangoStatement.prototype.setQuery = function (query) {
|
|
this._query = query;
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief parse a query and return the results
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
ArangoStatement.prototype.parse = function () {
|
|
throw "cannot call abstract method parse()";
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief explain a query and return the results
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
ArangoStatement.prototype.explain = function () {
|
|
throw "cannot call abstract method explain()";
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @brief execute the query
|
|
///
|
|
/// Invoking execute() will transfer the query and all bind parameters to the
|
|
/// server. It will return a cursor with the query results in case of success.
|
|
/// In case of an error, the error will be printed
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
ArangoStatement.prototype.execute = function () {
|
|
throw "cannot call abstract method execute()";
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @}
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// --SECTION-- MODULE EXPORTS
|
|
// -----------------------------------------------------------------------------
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @addtogroup ArangoStatement
|
|
/// @{
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
exports.ArangoStatement = ArangoStatement;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
/// @}
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Local Variables:
|
|
// mode: outline-minor
|
|
// outline-regexp: "^\\(/// @brief\\|/// @addtogroup\\|// --SECTION--\\|/// @page\\|/// @}\\)"
|
|
// End:
|
|
});
|