1
0
Fork 0
arangodb/js/client/js-client.h

1142 lines
49 KiB
C

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 helpAvocadoQueryInstance = \"\";\n"
"var helpAvocadoQueryTemplate = \"\";\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(helpAvocadoQueryTemplate);\n"
" print(helpAvocadoQueryInstance);\n"
" print(helpAvocadoQueryCursor);\n"
" print(helpExtended);\n"
"}\n"
"\n"
"// -----------------------------------------------------------------------------\n"
"// --SECTION-- Module \"internal\"\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"
"// --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 str = this._database._connection.get(\"/_api/documents/\" + encodeURIComponent(this.name));\n"
"\n"
" var requestResult = undefined;\n"
" if (str != undefined) {\n"
" requestResult = JSON.parse(str);\n"
" }\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 str = this._database._connection.get(\"/_api/document/\" + encodeURIComponent(this.name) + \"/\" + encodeURIComponent(id));\n"
" \n"
" var requestResult = undefined;\n"
" if (str != undefined) {\n"
" requestResult = JSON.parse(str);\n"
" }\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 str = this._database._connection.post(\"/_api/document/\" + encodeURIComponent(this.name), JSON.stringify(data));\n"
" \n"
" var requestResult = undefined;\n"
" if (str != undefined) {\n"
" requestResult = JSON.parse(str);\n"
" }\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 str = this._database._connection.delete(\"/_api/document/\" + encodeURIComponent(this.name) + \"/\" + encodeURIComponent(id));\n"
" \n"
" var requestResult = undefined;\n"
" if (str != undefined) {\n"
" requestResult = JSON.parse(str);\n"
" }\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 str = this._database._connection.put(\"/_api/document/\" + encodeURIComponent(this.name) + \"/\" + encodeURIComponent(id), JSON.stringify(data));\n"
" \n"
" var requestResult = undefined;\n"
" if (str != undefined) {\n"
" requestResult = JSON.parse(str);\n"
" }\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"
" if (!this.data._id) {\n"
" return false;\n"
" }\n"
"\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.data._id) {\n"
" throw \"cursor has been disposed\";\n"
" }\n"
"\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) {\n"
" this._hasMore = false;\n"
" \n"
" // load more results \n"
" var str = this._database._connection.put(\"/_api/cursor/\"+ encodeURIComponent(this.data._id), \"\");\n"
" var requestResult = undefined;\n"
" if (str != undefined) {\n"
" requestResult = JSON.parse(str);\n"
" }\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"
" if (!this.data._id) {\n"
" throw \"cursor has been disposed\";\n"
" }\n"
"\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"
" throw \"cursor has been disposed\";\n"
" }\n"
"\n"
" var str = this._database._connection.delete(\"/_api/cursor/\"+ encodeURIComponent(this.data._id), \"\");\n"
" var requestResult = undefined;\n"
" if (str != undefined) {\n"
" requestResult = JSON.parse(str);\n"
" }\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-- AvocadoQueryInstance\n"
"// -----------------------------------------------------------------------------\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"/// @brief constructor\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"function AvocadoQueryInstance (database, data) {\n"
" this._database = database;\n"
" this.doCount = false;\n"
" this.maxResults = null;\n"
" this.bindVars = {};\n"
" \n"
" if (typeof data === \"string\") {\n"
" this.query = data;\n"
" }\n"
" else if (data instanceof AvocadoQueryTemplate) {\n"
" if (data.document.query == undefined) {\n"
" data.load();\n"
" if (data.document.query == undefined) {\n"
" throw \"could not load query string\";\n"
" }\n"
" }\n"
" this.query = data.document.query;\n"
" } \n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"/// @brief bind a parameter to the query instance\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"
"AvocadoQueryInstance.prototype.bind = function (key, value) {\n"
" this.bindVars[key] = value;\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"/// @brief set the count flag for the query instance\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"
"AvocadoQueryInstance.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"
"AvocadoQueryInstance.prototype.setMax = function (value) {\n"
" if (parseInt(value) > 0) {\n"
" this.maxResults = 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"
"AvocadoQueryInstance.prototype.execute = function () {\n"
" var body = {\n"
" query : this.query,\n"
" count : this.doCount,\n"
" bindVars : this.bindVars\n"
" }\n"
"\n"
" if (this.maxResults) {\n"
" body[\"maxResults\"] = this.maxResults;\n"
" }\n"
" \n"
" var str = this._database._connection.post(\"/_api/cursor\", JSON.stringify(body));\n"
" var requestResult = undefined;\n"
" if (str != undefined) {\n"
" requestResult = JSON.parse(str);\n"
" }\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 AvocadoQueryInstance\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"AvocadoQueryInstance.prototype._help = function () {\n"
" print(helpAvocadoQueryInstance);\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"/// @brief return a string representation of the query instance\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"AvocadoQueryInstance.prototype.toString = function () { \n"
" return getIdString(this, \"AvocadoQueryInstance\");\n"
"}\n"
"\n"
"// -----------------------------------------------------------------------------\n"
"// --SECTION-- AvocadoQueryTemplate\n"
"// -----------------------------------------------------------------------------\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"/// @brief constructor\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"function AvocadoQueryTemplate (database, data) {\n"
" this._database = database;\n"
"\n"
" this.document = {\n"
" collection : DEFAULT_QUERY_COLLECTION\n"
" };\n"
" \n"
" if (typeof data === \"string\") {\n"
" this.document.query = data;\n"
" }\n"
" else { \n"
" if (data[\"query\"] != undefined) {\n"
" this.document.query = data[\"query\"];\n"
" }\n"
"\n"
" if (data[\"_id\"] != undefined) {\n"
" this.document._id = data[\"_id\"];\n"
" this._id = this.document._id;\n"
" }\n"
"\n"
" if (data[\"collection\"] != undefined) {\n"
" this.document.collection = data[\"collection\"];\n"
" } \n"
"\n"
" if (data[\"name\"] != undefined) {\n"
" this.document.name = data[\"name\"];\n"
" } \n"
"\n"
" // TODO additional attributes\n"
" }\n"
" \n"
" if (this.document.query == undefined && this.document._id == undefined) {\n"
" throw \"AvocadoQueryTemplate needs query or _id.\";\n"
" }\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"/// @brief return a new instance (AvocadoQueryInstance) for the query template\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"AvocadoQueryTemplate.prototype.getInstance = function () {\n"
" if (this.document._id == undefined) {\n"
" throw \"no _id found\"; \n"
" }\n"
" \n"
" return new AvocadoQueryInstance(this._database, this);\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"/// @brief update a query template, identified by its id\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"AvocadoQueryTemplate.prototype.update = function (data) {\n"
" if (this.document._id == undefined) {\n"
" throw \"no _id found\"; \n"
" }\n"
" \n"
" if (typeof data === \"string\") {\n"
" this.document.query = data;\n"
" }\n"
" else {\n"
" // update query or name \n"
" if (data[\"query\"] != undefined) {\n"
" this.document.query = data[\"query\"];\n"
" }\n"
"\n"
" if (data[\"name\"] != undefined) {\n"
" this.document.name = data[\"name\"];\n"
" } \n"
"\n"
" // TODO additional attributes\n"
" }\n"
" \n"
" var queryCollection = new AvocadoCollection(this._database, this.document.collection);\n"
" if (queryCollection.update(this.document._id, this.document)) {\n"
" return true;\n"
" }\n"
" \n"
" return false;\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"/// @brief delete a query template, identified by its id\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"AvocadoQueryTemplate.prototype.delete = function () {\n"
" if (this.document._id == undefined) {\n"
" throw \"no _id found\"; \n"
" }\n"
"\n"
" var queryCollection = new AvocadoCollection(this._database, this.document.collection);\n"
" if (queryCollection.delete(this.document._id)) {\n"
" this.document = {};\n"
" return true;\n"
" }\n"
" \n"
" return false;\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"/// @brief load a query template, identified by its id\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"AvocadoQueryTemplate.prototype.load = function () {\n"
" if (this.document._id == undefined) {\n"
" throw \"AvocadoQueryTemplate needs _id for loading\"; \n"
" }\n"
"\n"
" var coll = this.document.collection;\n"
" var queryCollection = new AvocadoCollection(this._database, coll);\n"
" var requestResult = queryCollection.document(this.document._id);\n"
" \n"
" if (requestResult != undefined) {\n"
"\n"
" if (requestResult.query == undefined) {\n"
" throw \"document is not a query\"; \n"
" }\n"
" // loaded\n"
" this.document = requestResult;\n"
" \n"
" if (this.document.collection == undefined) {\n"
" this.document.collection = coll;\n"
" }\n"
" \n"
" return true;\n"
" }\n"
"\n"
" return false; \n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"/// @brief save a query template, and set its id in case of success\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"AvocadoQueryTemplate.prototype.save = function () {\n"
" if (this.document._id != undefined) {\n"
" throw \"use update to save changes\"; \n"
" }\n"
" \n"
" var queryCollection = new AvocadoCollection(this._database, this.document.collection);\n"
" var requestResult = queryCollection.save(this.document);\n"
" \n"
" if (requestResult != undefined) {\n"
" // document saved\n"
" this.document._id = requestResult;\n"
" this._id = this.document._id;\n"
" return true;\n"
" }\n"
" \n"
" return false; \n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"/// @brief print the help for AvocadoQueryTemplate\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"AvocadoQueryTemplate.prototype._help = function () {\n"
" print(helpAvocadoQueryTemplate);\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"/// @brief return a string representation of the template\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"AvocadoQueryTemplate.prototype.toString = function () { \n"
" return getIdString(this, \"AvocadoQueryTemplate\");\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 str = this._connection.get(\"/_api/collections\");\n"
" \n"
" var requestResult = undefined;\n"
" if (str != undefined) {\n"
" requestResult = JSON.parse(str);\n"
" }\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 str = this._connection.get(\"/_api/collection/\" + encodeURIComponent(id));\n"
" \n"
" var requestResult = undefined;\n"
" if (str != undefined) {\n"
" requestResult = JSON.parse(str);\n"
" }\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 factory method to create a new query template\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"AvocadoDatabase.prototype._createQueryTemplate = function (queryData) { \n"
" var qt = new AvocadoQueryTemplate(this, queryData);\n"
" if (qt.save()) {\n"
" return qt;\n"
" }\n"
" \n"
" return undefined;\n"
"}\n"
"\n"
"AvocadoDatabase.prototype._getQueryTemplate = function (id) { \n"
" var qt = new AvocadoQueryTemplate(this, {\"_id\" : id});\n"
" if (qt.load()) {\n"
" return qt;\n"
" }\n"
" \n"
" return undefined;\n"
"}\n"
"\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"/// @brief factory method to create a new query instance\n"
"////////////////////////////////////////////////////////////////////////////////\n"
"\n"
"AvocadoDatabase.prototype._createQueryInstance = function (queryData) { \n"
" return new AvocadoQueryInstance(this, queryData);\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-- 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.<coll_name>.all(); list all documents ' + \"\\n\" +\n"
"' > id = db.<coll_name>.save({ ... }); save a document ' + \"\\n\" +\n"
"' > db.<coll_name>.delete(<_id>); delete a document ' + \"\\n\" +\n"
"' > db.<coll_name>.document(<_id>); get a document ' + \"\\n\" +\n"
"' > help show help pages ' + \"\\n\" +\n"
"' > helpQueries query help ' + \"\\n\" +\n"
"' > exit ';\n"
"\n"
"helpQueries = \n"
"getHeadline(\"Simple queries help\") +\n"
"'Create query template: ' + \"\\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"
"' > qt3 = db._getQueryTemplate(\"4334:2334\"); query by id ' + \"\\n\" +\n"
"' > qt1.update(\"select ...\"); update ' + \"\\n\" +\n"
"' > qt1.delete(\"4334:2334\"); delete ' + \"\\n\" +\n"
"'Create query instance: ' + \"\\n\" +\n"
"' > qi1 = qt1.getInstance(); from tmplate ' + \"\\n\" +\n"
"' > qi2 = db.createQueryInstance(\"select...\"); without template ' + \"\\n\" +\n"
"' > qi2.bind(\"key\", \"value\"); ' + \"\\n\" +\n"
"'Execute query: ' + \"\\n\" +\n"
"' > cu1 = qi1.execute(); returns cursor ' + \"\\n\" +\n"
"'Get all elements: ' + \"\\n\" +\n"
"' > el = cu1.elements(); ' + \"\\n\" +\n"
"'or loop over all results: ' + \"\\n\" +\n"
"' > while (cu1.hasNext()) { print( cu1.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(<name>); get collection by name ' + \"\\n\" +\n"
"' returns: AvocadoCollection ' + \"\\n\" +\n"
"' createQueryTemplate(<data>); create and return query template ' + \"\\n\" +\n"
"' returns: AvocadoQueryTemplate ' + \"\\n\" +\n"
"' getQueryTemplate(<id>); get query template by id ' + \"\\n\" +\n"
"' returns: dvocadoQueryTemplate ' + \"\\n\" +\n"
"' createQueryInstance(<data>); create and return query instance ' + \"\\n\" +\n"
"' returns: AvocadoQueryInstance ' + \"\\n\" +\n"
"' _help(); this help ' + \"\\n\" +\n"
"'Attributes: ' + \"\\n\" +\n"
"' <collection names> ';\n"
"\n"
"helpAvocadoCollection = \n"
"getHeadline(\"AvocadoCollection help\") +\n"
"'AvocadoCollection constructor: ' + \"\\n\" +\n"
"' > col = db.mycoll; ' + \"\\n\" +\n"
"'Functions: ' + \"\\n\" +\n"
"' save(<data>); create document and return id ' + \"\\n\" +\n"
"' document(<id>); get document by id ' + \"\\n\" +\n"
"' update(<id>, <new data>); over writes document by id ' + \"\\n\" +\n"
"' delete(<id>); 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"
"' > qi2 = db.createQueryInstance(\"select a from colA a\"); ' + \"\\n\" +\n"
"' > cu1 = qi2.execute(); ' + \"\\n\" +\n"
"' > documents = cu1.elements(); ' + \"\\n\" +\n"
"' > cu1 = qi2.execute(); ' + \"\\n\" +\n"
"' > while (cu1.hasNext()) { print( cu1.next() ); } ';\n"
"\n"
"helpAvocadoQueryInstance = \n"
"getHeadline(\"AvocadoQueryInstance help\") +\n"
"'AvocadoQueryInstance constructor: ' + \"\\n\" +\n"
"' > qi1 = qt1.getInstance(); ' + \"\\n\" +\n"
"' > qi2 = db._createQueryInstance(\"select ....\"); ' + \"\\n\" +\n"
"'Functions: ' + \"\\n\" +\n"
"' bind(<key>, <value>); bind vars ' + \"\\n\" +\n"
"' setCount(true); ' + \"\\n\" +\n"
"' setMax(<max>); ' + \"\\n\" +\n"
"' execute(); execute query and return ' + \"\\n\" +\n"
"' query cursor ' + \"\\n\" +\n"
"' _help(); this help ' + \"\\n\" +\n"
"'Attributes: ' + \"\\n\" +\n"
"' _database database object ' + \"\\n\" +\n"
"' bindVars bind vars ' + \"\\n\" +\n"
"' doCount count results in server ' + \"\\n\" +\n"
"' maxResults maximum number of results ' + \"\\n\" +\n"
"' query the query string ' + \"\\n\" +\n"
"'Example: ' + \"\\n\" +\n"
"' > qi2 = db._createQueryInstance(\"select a from colA a ' + \"\\n\" +\n"
"' where a.x = @a@ and a.y = @b@\"); ' + \"\\n\" +\n"
"' > qi2.bind(\"a\", \"hello\"); ' + \"\\n\" +\n"
"' > qi2.bind(\"b\", \"world\"); ' + \"\\n\" +\n"
"' > cu1 = qi1.execute(); ';\n"
"\n"
"helpAvocadoQueryTemplate = \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(<new data>); update query template ' + \"\\n\" +\n"
"' delete(<id>); 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"
"' > stop_pager() stop the pager output ' + \"\\n\" +\n"
"' > 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"
" 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"
;