static string JS_client_client = "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief AvocadoShell client API\n" "///\n" "/// @file\n" "///\n" "/// DISCLAIMER\n" "///\n" "/// Copyright 2012 triagens GmbH, Cologne, Germany\n" "///\n" "/// Licensed under the Apache License, Version 2.0 (the \"License\");\n" "/// you may not use this file except in compliance with the License.\n" "/// You may obtain a copy of the License at\n" "///\n" "/// http://www.apache.org/licenses/LICENSE-2.0\n" "///\n" "/// Unless required by applicable law or agreed to in writing, software\n" "/// distributed under the License is distributed on an \"AS IS\" BASIS,\n" "/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" "/// See the License for the specific language governing permissions and\n" "/// limitations under the License.\n" "///\n" "/// Copyright holder is triAGENS GmbH, Cologne, Germany\n" "///\n" "/// @author Achim Brandt\n" "/// @author Copyright 2012, triAGENS GmbH, Cologne, Germany\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @addtogroup AvocadoShell\n" "/// @{\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "// -----------------------------------------------------------------------------\n" "// --SECTION-- global variables\n" "// -----------------------------------------------------------------------------\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief change internal.output to shell output\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "ModuleCache[\"/internal\"].exports.output = TRI_SYS_OUTPUT;\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief default collection for saving queries\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "var DEFAULT_QUERY_COLLECTION = \"query\";\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief help texts\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "var HELP = \"\";\n" "var helpQueries = \"\";\n" "var helpAvocadoDatabase = \"\";\n" "var helpAvocadoCollection = \"\";\n" "var helpAvocadoQueryCursor = \"\";\n" "var helpAvocadoStoredStatement = \"\";\n" "var helpAvocadoStatement = \"\";\n" "var helpExtended = \"\";\n" "\n" "// -----------------------------------------------------------------------------\n" "// --SECTION-- helper functions\n" "// -----------------------------------------------------------------------------\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief return a formatted type string for object\n" "/// \n" "/// if the object has an id, it will be included in the string\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "function getIdString (object, typeName) {\n" " var result = \"[object \" + typeName;\n" " \n" " if (object._id) {\n" " result += \":\" + object._id;\n" " }\n" " else if (object.data && object.data._id) {\n" " result += \":\" + object.data._id;\n" " }\n" " result += \"]\";\n" "\n" " return result;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief turn off pretty printing\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "function printPlain (data) {\n" " var p = PRETTY_PRINT;\n" " PRETTY_PRINT = false;\n" " var c;\n" " if (typeof(COLOR_OUTPUT) != undefined) {\n" " c = COLOR_OUTPUT;\n" " COLOR_OUTPUT = undefined;\n" " }\n" " \n" " try {\n" " print(data);\n" " PRETTY_PRINT = p;\n" " if (typeof(c) != undefined) {\n" " COLOR_OUTPUT = c;\n" " } \n" " }\n" " catch (e) {\n" " PRETTY_PRINT = p;\n" " if (typeof(c) != undefined) {\n" " COLOR_OUTPUT = c;\n" " } \n" " throw e.message; \n" " } \n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief handle error results\n" "/// \n" "/// return try if the result contains an error. in this case, the function will\n" "/// also print the error details\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "function isErrorResult (requestResult) {\n" " if (requestResult == undefined) { \n" " requestResult = {\n" " \"error\" : true,\n" " \"code\" : 0,\n" " \"errorNum\" : 0,\n" " \"errorMessage\" : \"Unknown error. Request result is empty\"\n" " } \n" " }\n" " \n" " if (requestResult[\"error\"] != undefined && requestResult[\"error\"]) { \n" " var code = requestResult[\"code\"];\n" " var errorNum = requestResult[\"errorNum\"];\n" " var errorMessage = requestResult[\"errorMessage\"];\n" "\n" " if ( typeof(COLOR_BRIGHT) != \"undefined\" ) {\n" " internal.output(COLOR_BRIGHT);\n" " internal.output(\"Error: \");\n" " internal.output(COLOR_OUTPUT_RESET);\n" " }\n" " else {\n" " internal.output(\"Error: \"); \n" " }\n" "\n" " internal.output(\"[\"); \n" " internal.output(code); \n" " internal.output(\":\"); \n" " internal.output(errorNum); \n" " internal.output(\"] \"); \n" " print(errorMessage);\n" " \n" " return true;\n" " } \n" " return false;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief stop pretty printing\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "function stop_pretty_print () {\n" " print(\"stop pretty printing\");\n" " PRETTY_PRINT=false;\n" " return undefined;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief start pretty printing\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "function start_pretty_print () {\n" " print(\"use pretty printing\");\n" " PRETTY_PRINT=true;\n" " return undefined;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief stop pretty printing\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "function stop_color_print () {\n" " print(\"stop color printing\");\n" " COLOR_OUTPUT = undefined;\n" " return undefined;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief start pretty printing with optional color\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "function start_color_print (color) {\n" " if (typeof(color) == \"string\") {\n" " COLOR_OUTPUT = color;\n" " }\n" " else {\n" " COLOR_OUTPUT = COLOR_BRIGHT;\n" " }\n" " print(\"start \" + COLOR_OUTPUT + \"color\" + COLOR_OUTPUT_RESET + \" printing\");\n" " return undefined;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief create a formatted headline text \n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "function getHeadline (text) {\n" " var x = parseInt(Math.abs(78 - text.length) / 2);\n" " \n" " var p = \"\";\n" " for (var i = 0; i < x; ++i) {\n" " p += \"-\";\n" " }\n" " \n" " return \"\\n\" + p + \" \" + text + \" \" + p + \"\\n\";\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief print the overall help\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "function help () {\n" " print(HELP);\n" " print(helpQueries);\n" " print(helpAvocadoDatabase);\n" " print(helpAvocadoCollection);\n" " print(helpAvocadoStatement);\n" " print(helpAvocadoQueryCursor);\n" " print(helpExtended);\n" "}\n" "\n" "// -----------------------------------------------------------------------------\n" "// --SECTION-- Module \"internal\"\n" "// -----------------------------------------------------------------------------\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @addtogroup AvocadoShell\n" "/// @{\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief log function\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "ModuleCache[\"/internal\"].exports.log = function(level, msg) {\n" " internal.output(level, \": \", msg, \"\\n\");\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief starts the pager\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "ModuleCache[\"/internal\"].exports.start_pager = SYS_START_PAGER;\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief stops the pager\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "ModuleCache[\"/internal\"].exports.stop_pager = SYS_STOP_PAGER;\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @}\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "// -----------------------------------------------------------------------------\n" "// --SECTION-- AvocadoCollection\n" "// -----------------------------------------------------------------------------\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief constructor\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "function AvocadoCollection (database, data) {\n" " this._database = database;\n" "\n" " if (typeof data === \"string\") {\n" " this.name = data;\n" " }\n" " else {\n" " for (var i in data) {\n" " this[i] = data[i];\n" " }\n" " }\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief return all documents from the collection\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoCollection.prototype.all = function () {\n" " var requestResult = this._database._connection.get(\"/_api/documents/\" + encodeURIComponent(this.name));\n" " \n" " if (isErrorResult(requestResult)) {\n" " return undefined;\n" " }\n" "\n" " return requestResult[\"documents\"]; \n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief return a single document from the collection, identified by its id\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoCollection.prototype.document = function (id) {\n" " var requestResult = this._database._connection.get(\"/_api/document/\" + encodeURIComponent(this.name) + \"/\" + encodeURIComponent(id));\n" "\n" " if (isErrorResult(requestResult)) {\n" " return undefined;\n" " }\n" "\n" " return requestResult[\"document\"];\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief save a document in the collection, return its id\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoCollection.prototype.save = function (data) { \n" " var requestResult = this._database._connection.post(\"/_api/document/\" + encodeURIComponent(this.name), JSON.stringify(data));\n" " \n" " if (isErrorResult(requestResult)) {\n" " return undefined;\n" " }\n" "\n" " return requestResult[\"_id\"];\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief delete a document in the collection, identified by its id\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoCollection.prototype.delete = function (id) { \n" " var requestResult = this._database._connection.delete(\"/_api/document/\" + encodeURIComponent(this.name) + \"/\" + encodeURIComponent(id));\n" "\n" " return !isErrorResult(requestResult);\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief update a document in the collection, identified by its id\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoCollection.prototype.update = function (id, data) { \n" " var requestResult = this._database._connection.put(\"/_api/document/\" + encodeURIComponent(this.name) + \"/\" + encodeURIComponent(id), JSON.stringify(data));\n" "\n" " return !isErrorResult(requestResult);\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief print the help for AvocadoCollection\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoCollection.prototype._help = function () { \n" " print(helpAvocadoCollection);\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief return a string representation of the collection\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoCollection.prototype.toString = function () { \n" " return getIdString(this, \"AvocadoCollection\");\n" "}\n" "\n" "// -----------------------------------------------------------------------------\n" "// --SECTION-- AvocadoQueryCursor\n" "// -----------------------------------------------------------------------------\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief constructor\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "function AvocadoQueryCursor (database, data) {\n" " this._database = database;\n" " this.data = data;\n" " this._hasNext = false;\n" " this._hasMore = false;\n" " this._pos = 0;\n" " this._count = 0;\n" " this._total = 0;\n" " \n" " if (data.result != undefined) {\n" " this._count = data.result.length;\n" " \n" " if (this._pos < this._count) {\n" " this._hasNext = true;\n" " }\n" " \n" " if (data.hasMore != undefined && data.hasMore) {\n" " this._hasMore = true;\n" " } \n" " }\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief return whether there are more results available in the cursor\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoQueryCursor.prototype.hasNext = function () {\n" " return this._hasNext;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief return the next result document from the cursor\n" "///\n" "/// If no more results are available locally but more results are available on\n" "/// the server, this function will make a roundtrip to the server\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoQueryCursor.prototype.next = function () {\n" " if (!this._hasNext) {\n" " throw \"No more results\";\n" " }\n" " \n" " var result = this.data.result[this._pos];\n" " this._pos++;\n" " \n" " if (this._pos == this._count) {\n" " // reached last result\n" " \n" " this._hasNext = false;\n" " this._pos = 0;\n" " \n" " if (this._hasMore && this.data._id) {\n" " this._hasMore = false;\n" " \n" " // load more results \n" " var requestResult = this._database._connection.put(\"/_api/cursor/\"+ encodeURIComponent(this.data._id), \"\");\n" " \n" " if (isErrorResult(requestResult)) {\n" " return undefined;\n" " }\n" " \n" " this.data = requestResult;\n" " this._count = requestResult.result.length;\n" " \n" " if (this._pos < this._count) {\n" " this._hasNext = true;\n" " }\n" " \n" " if (requestResult.hasMore != undefined && requestResult.hasMore) {\n" " this._hasMore = true;\n" " } \n" " } \n" " }\n" " \n" " return result;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief return all remaining result documents from the cursor\n" "///\n" "/// If no more results are available locally but more results are available on\n" "/// the server, this function will make one or multiple roundtrips to the \n" "/// server. Calling this function will also fully exhaust the cursor.\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoQueryCursor.prototype.elements = function () { \n" " var result = [];\n" " \n" " while (this.hasNext()) { \n" " result.push( this.next() ); \n" " }\n" " \n" " return result;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief explicitly dispose the cursor\n" "///\n" "/// Calling this function will mark the cursor as deleted on the server. It will\n" "/// therefore make a roundtrip to the server. Using a cursor after it has been\n" "/// disposed is considered a user error\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoQueryCursor.prototype.dispose = function () {\n" " if (!this.data._id) {\n" " // client side only cursor\n" " return;\n" " }\n" "\n" " var requestResult = this._database._connection.delete(\"/_api/cursor/\"+ encodeURIComponent(this.data._id), \"\");\n" " \n" " if (!isErrorResult(requestResult)) {\n" " this.data._id = undefined;\n" " }\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief return the total number of documents in the cursor\n" "///\n" "/// The number will remain the same regardless how much result documents have\n" "/// already been fetched from the cursor.\n" "///\n" "/// This function will return the number only if the cursor was constructed \n" "/// with the \"doCount\" attribute. Otherwise it will return undefined.\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoQueryCursor.prototype.count = function () {\n" " if (!this.data._id) {\n" " throw \"cursor has been disposed\";\n" " }\n" "\n" " return this.data.count;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief print the help for the cursor\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoQueryCursor.prototype._help = function () {\n" " print(helpAvocadoQueryCursor);\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief return a string representation of the cursor\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoQueryCursor.prototype.toString = function () { \n" " return getIdString(this, \"AvocadoQueryCursor\");\n" "}\n" "\n" "// -----------------------------------------------------------------------------\n" "// --SECTION-- AvocadoDatabase\n" "// -----------------------------------------------------------------------------\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief constructor\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "function AvocadoDatabase (connection) {\n" " this._connection = connection;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief return all collections from the database\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoDatabase.prototype._collections = function () {\n" " var requestResult = this._connection.get(\"/_api/collections\");\n" " \n" " if (isErrorResult(requestResult)) {\n" " return undefined;\n" " }\n" "\n" " if (requestResult[\"collections\"] != undefined) {\n" " \n" " // add all collentions to object\n" " for (var i in requestResult[\"collections\"]) {\n" " this[i] = new AvocadoCollection(this, requestResult[\"collections\"][i]);\n" " }\n" " \n" " return requestResult[\"collections\"];\n" " }\n" " \n" " return undefined;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief return a single collection, identified by its id\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoDatabase.prototype._collection = function (id) {\n" " var requestResult = this._connection.get(\"/_api/collection/\" + encodeURIComponent(id));\n" " \n" " if (isErrorResult(requestResult)) {\n" " return undefined;\n" " }\n" "\n" " if (requestResult[\"name\"] != undefined) {\n" " \n" " this[requestResult[\"name\"]] = new AvocadoCollection(this, requestResult);\n" " \n" " return requestResult;\n" " }\n" " \n" " return undefined;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief return a single collection, identified by its id\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoDatabase.prototype._create = function (name) {\n" " var body = {\n" " \"name\" : name\n" " };\n" "\n" " var str = this._connection.post(\"/_api/database/collection\", JSON.stringify(body));\n" "\n" " print(str);\n" " \n" " var requestResult = undefined;\n" "\n" " if (str != undefined) {\n" " requestResult = JSON.parse(str);\n" " }\n" " \n" " if (isErrorResult(requestResult)) {\n" " return undefined;\n" " }\n" "\n" " return requestResult;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief print the help for AvocadoDatabase\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoDatabase.prototype._help = function () { \n" " print(helpAvocadoDatabase);\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief return a string representation of the database object\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoDatabase.prototype.toString = function () { \n" " return \"[object AvocadoDatabase]\";\n" "}\n" "/*\n" "// -----------------------------------------------------------------------------\n" "// --SECTION-- AvocadoStoredStatement\n" "// -----------------------------------------------------------------------------\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief constructor\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "function AvocadoStoredStatement (database, data) {\n" " this._database = database;\n" " this._doCount = false;\n" " this._batchSize = null;\n" " this._bindVars = {};\n" " this._id = null;\n" " this.document = {\n" " \"queryCollection\" : DEFAULT_QUERY_COLLECTION\n" " };\n" "\n" " if (!(data instanceof Object)) {\n" " throw \"AvocadoStoredStatement needs a data attribute\";\n" " }\n" " \n" " if (data[\"name\"] != undefined) {\n" " this.document.name = data[\"name\"];\n" " }\n" " \n" " if (data[\"query\"] != undefined) {\n" " this.document.query = data[\"query\"];\n" " }\n" "\n" " if (data[\"queryCollection\"] != undefined) {\n" " this.document.queryCollection = data[\"queryCollection\"];\n" " } \n" " \n" " this._isNew = (data[\"query\"] != undefined); \n" "\n" " this.validate();\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief update a stored statement\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoStoredStatement.prototype.update = function (data) {\n" " // update query string\n" " if (data[\"query\"] != undefined) {\n" " this.document.query = data[\"query\"];\n" " }\n" "\n" " this.validate();\n" "\n" " var queryCollection = new AvocadoCollection(this._database, this.document.queryCollection);\n" " if (!queryCollection) {\n" " throw \"Could not determine collection for AvocadoStoredStatement\";\n" " }\n" "\n" " if (this._isNew) {\n" " var requestResult = queryCollection.save(this.document);\n" " if (requestResult == undefined) {\n" " throw \"Could not save AvocadoStoredStatement\";\n" " }\n" "\n" " // document saved\n" " this._id = requestResult;\n" " this._isNew = false;\n" " return true;\n" " }\n" "\n" " if (!queryCollection.update(this.document._id, this.document)) {\n" " throw \"Could not update AvocadoStoredStatement\";\n" " }\n" " \n" " return true;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief save a stored statement\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoStoredStatement.prototype.save = function () {\n" " return this.update(this.document);\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief validate the data of an AvocadoStoredStatement\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoStoredStatement.prototype.validate = function () {\n" " if (this._isNew) {\n" " if (this.document.query == undefined || this.document.query == \"\") {\n" " throw \"AvocadoStoredStatement needs a valid query\";\n" " }\n" " }\n" "\n" " if (this.document.name == undefined || this.document.name == \"\") {\n" " throw \"AvocadoStoredStatement needs a name attribute\";\n" " }\n" " \n" " if (this.document.queryCollection == undefined || this.document.queryCollection == \"\") {\n" " throw \"AvocadoStoredStatement needs a queryCollection\";\n" " }\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief lookup the data of an AvocadoStoredStatement\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoStoredStatement.prototype.lookup = function () {\n" " if (this.isNew) {\n" " throw \"Cannot lookup a new AvocadoStoredStatement\";\n" " }\n" "\n" " var data = {\n" " \"query\" : \"SELECT c FROM `\" + this.document.queryCollection + \n" " \"` c WHERE c.name == '\" + QuoteJSONString(this.document.name) + \"'\"\n" " } \n" " var statement = new AvocadoStatement(this._database, data);\n" " var result = statement.execute();\n" " if (result instanceof AvocadoQueryError) {\n" " throw result.message;\n" " }\n" "\n" " if (!result.hasNext()) {\n" " throw \"Could not find stored statement for the given parameters\";\n" " }\n" "\n" " var row = result.next();\n" " this._id = row[\"id\"];\n" " this._query = row[\"query\"];\n" " this._isNew = false;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief delete a stored statement\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoStoredStatement.prototype.delete = function () {\n" " if (this._isNew) {\n" " throw \"Cannot delete a new AvocadoStoredStatement\";\n" " }\n" " \n" " if (this._id == undefined || this._id == null) {\n" " this.lookup();\n" " }\n" "\n" " var queryCollection = new AvocadoCollection(this._database, this.document.collection);\n" " if (!queryCollection) {\n" " throw \"Could not determine collection for AvocadoStoredStatement\";\n" " }\n" "\n" " if (!queryCollection.delete(this.document._id)) {\n" " this.document = {};\n" " this._isNew = true;\n" " this._bindVars = {};\n" " this._id = null;\n" " return true;\n" " }\n" " \n" " return false;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief bind a parameter to the statement\n" "///\n" "/// This function can be called multiple times, once for each bind parameter.\n" "/// All bind parameters will be transferred to the server in one go when \n" "/// execute() is called.\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoStoredStatement.prototype.bind = function (key, value) {\n" " if (typeof(key) != \"string\") {\n" " throw \"bind parameter name must be a string\";\n" " }\n" "\n" " if (this._bindVars[key] != undefined) {\n" " throw \"redeclaration of bind parameter\";\n" " }\n" "\n" " this._bindVars[key] = value;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief set the count flag for the statement\n" "///\n" "/// Setting the count flag will make the query instance's cursor return the\n" "/// total number of result documents. The count flag is not set by default.\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoStoredStatement.prototype.setCount = function (bool) {\n" " this._doCount = bool ? true : false;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief set the maximum number of results documents the cursor will return\n" "/// in a single server roundtrip.\n" "/// The higher this number is, the less server roundtrips will be made when\n" "/// iterating over the result documents of a cursor.\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoStoredStatement.prototype.setBatchSize = function (value) {\n" " if (parseInt(value) > 0) {\n" " this._batchSize = parseInt(value);\n" " }\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief execute the query\n" "///\n" "/// Invoking execute() will transfer the query and all bind parameters to the\n" "/// server. It will return a cursor with the query results in case of success.\n" "/// In case of an error, the error will be printed\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoStoredStatement.prototype.execute = function () {\n" " if (this._isNew) {\n" " this.save();\n" " }\n" " \n" " var body = {\n" " \"name\" : this.document.name,\n" " \"count\" : this._doCount,\n" " \"bindVars\" : this._bindVars,\n" " \"_id\" : this._id\n" " }\n" "\n" " if (this._batchSize) {\n" " body[\"batchSize\"] = this._batchSize;\n" " }\n" " \n" " var requestResult = this._database._connection.post(\"/_api/cursor\", JSON.stringify(body));\n" " \n" " if (isErrorResult(requestResult)) {\n" " return undefined;\n" " }\n" "\n" " return new AvocadoQueryCursor(this._database, requestResult);\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief print the help for AvocadoStoredStatement\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoStoredStatement.prototype._help = function () {\n" " print(helpAvocadoStoredStatement);\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief return a string representation of the stored statement\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoStoredStatement.prototype.toString = function () { \n" " return getIdString(this, \"AvocadoStoredStatement\");\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief factory method to create a new stored statement\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoDatabase.prototype._createStoredStatement = function (data) { \n" " return new AvocadoStoredStatement(this, data);\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief factory method to retrieve an existing stored statement\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoDatabase.prototype._getStoredStatement = function (data) { \n" " return new AvocadoStoredStatement(this, data);\n" "}\n" "\n" "*/\n" "// -----------------------------------------------------------------------------\n" "// --SECTION-- AvocadoStatement\n" "// -----------------------------------------------------------------------------\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief constructor\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "function AvocadoStatement (database, data) {\n" " this._database = database;\n" " this._doCount = false;\n" " this._batchSize = null;\n" " this._bindVars = {};\n" " \n" " if (!(data instanceof Object)) {\n" " throw \"AvocadoStatement needs initial data\";\n" " }\n" " \n" " if (data.query == undefined || data.query == \"\") {\n" " throw \"AvocadoStatement needs a valid query attribute\";\n" " }\n" " this.setQuery(data.query);\n" "\n" " if (data.count != undefined) {\n" " this.setCount(data.count);\n" " }\n" " if (data.batchSize != undefined) {\n" " this.setBatchSize(data.batchSize);\n" " }\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief bind a parameter to the statement\n" "///\n" "/// This function can be called multiple times, once for each bind parameter.\n" "/// All bind parameters will be transferred to the server in one go when \n" "/// execute() is called.\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoStatement.prototype.bind = function (key, value) {\n" " if (key instanceof Object) {\n" " if (value != undefined) {\n" " throw \"invalid bind parameter declaration\";\n" " }\n" " this._bindVars = key;\n" " }\n" " else if (typeof(key) == \"string\") {\n" " if (this._bindVars[key] != undefined) {\n" " throw \"redeclaration of bind parameter\";\n" " }\n" " this._bindVars[key] = value;\n" " }\n" " else if (typeof(key) == \"number\") {\n" " var strKey = String(parseInt(key));\n" " if (strKey != String(key)) {\n" " throw \"invalid bind parameter declaration\";\n" " }\n" " if (this._bindVars[strKey] != undefined) {\n" " throw \"redeclaration of bind parameter\";\n" " }\n" " this._bindVars[strKey] = value;\n" " }\n" " else {\n" " throw \"invalid bind parameter declaration\";\n" " }\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief return the bind variables already set\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoStatement.prototype.getBindVariables = function () {\n" " return this._bindVars;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief get the count flag for the statement\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoStatement.prototype.getCount = function () {\n" " return this._doCount;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief get the maximum number of results documents the cursor will return\n" "/// in a single server roundtrip.\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoStatement.prototype.getMax = function () {\n" " return this._batchSize;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief get query string\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoStatement.prototype.getQuery = function () {\n" " return this._query;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief set the count flag for the statement\n" "///\n" "/// Setting the count flag will make the statement's result cursor return the\n" "/// total number of result documents. The count flag is not set by default.\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoStatement.prototype.setCount = function (bool) {\n" " this._doCount = bool ? true : false;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief set the maximum number of results documents the cursor will return\n" "/// in a single server roundtrip.\n" "/// The higher this number is, the less server roundtrips will be made when\n" "/// iterating over the result documents of a cursor.\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoStatement.prototype.setBatchSize = function (value) {\n" " if (parseInt(value) > 0) {\n" " this._batchSize = parseInt(value);\n" " }\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief set the query string\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoStatement.prototype.setQuery = function (query) {\n" " this._query = query;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief parse a query and return the results\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoStatement.prototype.parse = function () {\n" " var body = {\n" " \"query\" : this._query,\n" " }\n" "\n" " var requestResult = this._database._connection.post(\"/_api/query\", JSON.stringify(body));\n" " \n" " if (isErrorResult(requestResult)) {\n" " return undefined;\n" " }\n" "\n" " return true;\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief execute the query\n" "///\n" "/// Invoking execute() will transfer the query and all bind parameters to the\n" "/// server. It will return a cursor with the query results in case of success.\n" "/// In case of an error, the error will be printed\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoStatement.prototype.execute = function () {\n" " var body = {\n" " \"query\" : this._query,\n" " \"count\" : this._doCount,\n" " \"bindVars\" : this._bindVars\n" " }\n" "\n" " if (this._batchSize) {\n" " body[\"batchSize\"] = this._batchSize;\n" " }\n" "\n" " var requestResult = this._database._connection.post(\"/_api/cursor\", JSON.stringify(body));\n" " \n" " if (isErrorResult(requestResult)) {\n" " return undefined;\n" " }\n" "\n" " return new AvocadoQueryCursor(this._database, requestResult);\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief print the help for AvocadoStatement\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoStatement.prototype._help = function () {\n" " print(helpAvocadoStatement);\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief return a string representation of the statement\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoStatement.prototype.toString = function () { \n" " return getIdString(this, \"AvocadoStatement\");\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief factory method to create a new statement\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "AvocadoDatabase.prototype._createStatement = function (data) { \n" " return new AvocadoStatement(this, data);\n" "}\n" "\n" "// -----------------------------------------------------------------------------\n" "// --SECTION-- initialisers\n" "// -----------------------------------------------------------------------------\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief initialise help texts\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "HELP = \n" "getHeadline(\"Help\") +\n" "'Predefined objects: ' + \"\\n\" +\n" "' avocado: AvocadoConnection ' + \"\\n\" +\n" "' db: AvocadoDatabase ' + \"\\n\" +\n" "'Example: ' + \"\\n\" +\n" "' > db._collections(); list all collections ' + \"\\n\" +\n" "' > db..all(); list all documents ' + \"\\n\" +\n" "' > id = db..save({ ... }); save a document ' + \"\\n\" +\n" "' > db..delete(<_id>); delete a document ' + \"\\n\" +\n" "' > db..document(<_id>); get a document ' + \"\\n\" +\n" "' > help show help pages ' + \"\\n\" +\n" "' > helpQueries query help ' + \"\\n\" +\n" "' > exit ';\n" "\n" "helpQueries = \n" "getHeadline(\"Select query help\") +\n" "\n" "'Create a select query: ' + \"\\n\" +\n" "' > st = new AvocadoStatement(db, { \"query\" : \"select...\" }); ' + \"\\n\" +\n" "' > st = db._createStatement({ \"query\" : \"select...\" }); ' + \"\\n\" +\n" "'Set query options: ' + \"\\n\" +\n" "' > st.setBatchSize(); set the max. number of results ' + \"\\n\" +\n" "' to be transferred per roundtrip ' + \"\\n\" +\n" "' > st.setCount(); set count flag (return number of ' + \"\\n\" +\n" "' results in \"count\" attribute) ' + \"\\n\" +\n" "'Get query options: ' + \"\\n\" +\n" "' > st.getMax(); return the max. number of results ' + \"\\n\" +\n" "' to be transferred per roundtrip ' + \"\\n\" +\n" "' > st.getCount(); return count flag (return number of' + \"\\n\" +\n" "' results in \"count\" attribute) ' + \"\\n\" +\n" "' > st.getQuery(); return query string ' + \"\\n\" +\n" "' results in \"count\" attribute) ' + \"\\n\" +\n" "'Bind parameters to a query: ' + \"\\n\" +\n" "' > st.bind(, ); bind single variable ' + \"\\n\" +\n" "' > st.bind(); bind multiple variables ' + \"\\n\" +\n" "'Execute query: ' + \"\\n\" +\n" "' > c = st.execute(); returns a cursor ' + \"\\n\" +\n" "'Get all results in an array: ' + \"\\n\" +\n" "' > e = c.elements(); ' + \"\\n\" +\n" "'Or loop over the result set: ' + \"\\n\" +\n" "' > while (c.hasNext()) { print( c.next() ); } ';\n" "\n" "helpAvocadoDatabase = \n" "getHeadline(\"AvocadoDatabase help\") +\n" "'AvocadoDatabase constructor: ' + \"\\n\" +\n" "' > db2 = new AvocadoDatabase(connection); ' + \"\\n\" +\n" "'Functions: ' + \"\\n\" +\n" "' _collections(); list all collections ' + \"\\n\" +\n" "' returns: list of AvocadoCollection ' + \"\\n\" +\n" "' _collection(); get collection by name ' + \"\\n\" +\n" "' returns: AvocadoCollection ' + \"\\n\" +\n" "' _createStatement(); create and return select query ' + \"\\n\" +\n" "' returns: AvocadoStatement ' + \"\\n\" +\n" "' _help(); this help ' + \"\\n\" +\n" "'Attributes: ' + \"\\n\" +\n" "' ';\n" "\n" "helpAvocadoCollection = \n" "getHeadline(\"AvocadoCollection help\") +\n" "'AvocadoCollection constructor: ' + \"\\n\" +\n" "' > col = db.mycoll; ' + \"\\n\" +\n" "'Functions: ' + \"\\n\" +\n" "' save(); create document and return id ' + \"\\n\" +\n" "' document(); get document by id ' + \"\\n\" +\n" "' update(, ); over writes document by id ' + \"\\n\" +\n" "' delete(); deletes document by id ' + \"\\n\" +\n" "' _help(); this help ' + \"\\n\" +\n" "'Attributes: ' + \"\\n\" +\n" "' _database database object ' + \"\\n\" +\n" "' _id collection id ' + \"\\n\" +\n" "' name collection name ' + \"\\n\" +\n" "' status status id ' + \"\\n\" +\n" "' figures ';\n" "\n" "helpAvocadoQueryCursor = \n" "getHeadline(\"AvocadoQueryCursor help\") +\n" "'AvocadoQueryCursor constructor: ' + \"\\n\" +\n" "' > cu1 = qi1.execute(); ' + \"\\n\" +\n" "'Functions: ' + \"\\n\" +\n" "' hasMore(); returns true if there ' + \"\\n\" +\n" "' are more results ' + \"\\n\" +\n" "' next(); returns the next document ' + \"\\n\" +\n" "' elements(); returns all documents ' + \"\\n\" +\n" "' _help(); this help ' + \"\\n\" +\n" "'Attributes: ' + \"\\n\" +\n" "' _database database object ' + \"\\n\" +\n" "'Example: ' + \"\\n\" +\n" "' > st = db._createStatement({ \"query\" : \"select a from colA a\" }); ' + \"\\n\" +\n" "' > c = st.execute(); ' + \"\\n\" +\n" "' > documents = c.elements(); ' + \"\\n\" +\n" "' > c = st.execute(); ' + \"\\n\" +\n" "' > while (c.hasNext()) { print( c.next() ); } ';\n" "\n" "helpAvocadoStatement = \n" "getHeadline(\"AvocadoStatement help\") +\n" "'AvocadoStatement constructor: ' + \"\\n\" +\n" "' > st = new AvocadoStatement({ \"query\" : \"select...\" }); ' + \"\\n\" +\n" "' > st = db._createStatement({ \"query\" : \"select ....\" }); ' + \"\\n\" +\n" "'Functions: ' + \"\\n\" +\n" "' bind(, ); bind single variable ' + \"\\n\" +\n" "' bind(); bind multiple variables ' + \"\\n\" +\n" "' setBatchSize(); set max. number of results ' + \"\\n\" +\n" "' to be transferred per roundtrip ' + \"\\n\" +\n" "' setCount(); set count flag (return number of ' + \"\\n\" +\n" "' results in \"count\" attribute) ' + \"\\n\" +\n" "' getMax(); return max. number of results ' + \"\\n\" +\n" "' to be transferred per roundtrip ' + \"\\n\" +\n" "' getCount(); return count flag (return number of' + \"\\n\" +\n" "' results in \"count\" attribute) ' + \"\\n\" +\n" "' getQuery(); return query string ' + \"\\n\" +\n" "' execute(); execute query and return cursor ' + \"\\n\" +\n" "' _help(); this help ' + \"\\n\" +\n" "'Attributes: ' + \"\\n\" +\n" "' _database database object ' + \"\\n\" +\n" "'Example: ' + \"\\n\" +\n" "' > st = db._createStatement({ \"query\" : \"select a from colA a ' + \"\\n\" +\n" "' where a.x = @a@ and a.y = @b@\" }); ' + \"\\n\" +\n" "' > st.bind(\"a\", \"hello\"); ' + \"\\n\" +\n" "' > st.bind(\"b\", \"world\"); ' + \"\\n\" +\n" "' > c = st.execute(); ' + \"\\n\" +\n" "' > print(c.elements()); ';\n" "/*\n" "helpAvocadoStoredStatement = \n" "getHeadline(\"AvocadoQueryTemplate help\") +\n" "'AvocadoQueryTemplate constructor: ' + \"\\n\" +\n" "' > qt1 = db._createQueryTemplate(\"select ...\"); simple query ' + \"\\n\" +\n" "' > qt2 = db._createQueryTemplate( complex query ' + \"\\n\" +\n" "' {query:\"select...\", ' + \"\\n\" +\n" "' name:\"qname\", ' + \"\\n\" +\n" "' collection:\"q\" ' + \"\\n\" +\n" "' ... } ' + \"\\n\" +\n" "'Functions: ' + \"\\n\" +\n" "' update(); update query template ' + \"\\n\" +\n" "' delete(); delete query template by id ' + \"\\n\" +\n" "' getInstance(); get a query instance ' + \"\\n\" +\n" "' returns: AvocadoQueryInstance' + \"\\n\" +\n" "' _help(); this help ' + \"\\n\" +\n" "'Attributes: ' + \"\\n\" +\n" "' _database database object ' + \"\\n\" +\n" "' _id template id ' + \"\\n\" +\n" "' name collection name ' + \"\\n\" +\n" "'Example: ' + \"\\n\" +\n" "' > qt1 = db._getQueryTemplate(\"4334:2334\"); ' + \"\\n\" +\n" "' > qt1.update(\"select a from collA a\"); ' + \"\\n\" +\n" "' > qi1 = qt1.getInstance(); ' + \"\\n\" +\n" "' > qt1.delete(\"4334:2334\"); ';\n" "*/\n" "helpExtended = \n" "getHeadline(\"More help\") +\n" "'Pager: ' + \"\\n\" +\n" "' > internal.stop_pager() stop the pager output ' + \"\\n\" +\n" "' > internal.start_pager() start the pager ' + \"\\n\" +\n" "'Pretty printing: ' + \"\\n\" +\n" "' > stop_pretty_print() stop pretty printing ' + \"\\n\" +\n" "' > start_pretty_print() start pretty printing ' + \"\\n\" +\n" "'Color output: ' + \"\\n\" +\n" "' > stop_color_print() stop color printing ' + \"\\n\" +\n" "' > start_color_print() start color printing ' + \"\\n\" +\n" "' > start_color_print(COLOR_BLUE) set color ' + \"\\n\" +\n" "'Print function: ' + \"\\n\" +\n" "' > print(x) std. print function ' + \"\\n\" +\n" "' > printPlain(x) print without pretty printing' + \"\\n\" +\n" "' and without colors ';\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @brief create the global db object and load the collections\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "try {\n" " // default database\n" " db = new AvocadoDatabase(avocado);\n" "\n" " // load collection data\n" " db._collections();\n" "\n" " ModuleCache[\"/internal\"].exports.db = db;\n" "\n" " print(HELP);\n" "}\n" "catch (err) {\n" " print(COLOR_RED + \"connection failure: \" + err + COLOR_BLACK);\n" "}\n" "\n" "////////////////////////////////////////////////////////////////////////////////\n" "/// @}\n" "////////////////////////////////////////////////////////////////////////////////\n" "\n" "// Local Variables:\n" "// mode: outline-minor\n" "// outline-regexp: \"^\\\\(/// @brief\\\\|/// @addtogroup\\\\|// --SECTION--\\\\|/// @page\\\\|/// @}\\\\)\"\n" "// End:\n" ;