private string GetScriptPath(DataRequest request)
        {
            string scriptName = null;

            switch (request.Operation) {
                case DataOperation.Lookup:
                    scriptName = "lookup";
                    break;
                case DataOperation.Query:
                    scriptName = String.IsNullOrEmpty(request.OperationName) ? "query" : "query." + request.OperationName;
                    break;
                case DataOperation.Insert:
                    scriptName = "insert";
                    break;
                case DataOperation.Update:
                    scriptName = "update";
                    break;
                case DataOperation.Merge:
                    // TODO: If update behaves as upsert, then this should just be update
                    scriptName = "merge";
                    break;
                case DataOperation.Delete_:
                    scriptName = "delete";
                    break;
                case DataOperation.Execute:
                    scriptName = request.OperationName;
                    break;
            }

            return Path.Join(_path, scriptName + ".js");
        }
        public Task<object> ExecuteRequest(DataRequest request)
        {
            DataRequestObject requestObject = new DataRequestObject(request);

            object result = _source.Application.Scripts.Execute(_path, GetScriptPath(request), "request", requestObject);
            if (Script.IsNullOrUndefined(result) == false) {
                Task<object> taskResult = result as Task<object>;
                if (taskResult != null) {
                    return taskResult;
                }
            }

            return Deferred.Create<object>(result).Task;
        }
Exemple #3
0
        public Task<object> Execute(DataRequest request, Dictionary<string, object> options)
        {
            if (options != null) {
                object result = options["result"];
                if (Script.IsUndefined(result) == false) {
                    return Deferred.Create<object>(result).Task;
                }
            }

            if (request.Operation == DataOperation.Execute) {
                return Deferred.Create<object>(Script.Undefined).Task;
            }
            else if ((request.Operation == DataOperation.Lookup) || (request.Operation == DataOperation.Query)) {
                return ExecuteQuery(request, options);
            }
            else {
                return ExecuteNonQuery(request, options);
            }
        }
        public DataRequestObject(DataRequest request)
        {
            Operation = request.Operation;
            OperationName = request.OperationName;
            Arguments = request.OperationArguments;

            ID = request.Query.ID;

            Partition = request.Partition;
            Query = request.Query;
            Item = request.Item;

            Execute = delegate(Dictionary<string, object> options) {
                request.Partition = Partition;
                request.Query = Query;
                request.Item = Item;

                return request.Query.Collection.Source.Execute(request, options);
            };
        }
Exemple #5
0
 protected abstract Task<object> ExecuteQuery(DataRequest request, Dictionary<string, object> options);
Exemple #6
0
 protected virtual Task<object> ExecuteNonQuery(DataRequest request, Dictionary<string, object> options)
 {
     return Deferred.Create<object>(Script.Undefined).Task;
 }
 public bool SupportsRequest(DataRequest request)
 {
     // TODO: Support operations enabled via config
     return FileSystem.ExistsSync(GetScriptPath(request));
 }
Exemple #8
0
 protected abstract Task <object> ExecuteQuery(DataRequest request, Dictionary <string, object> options);
Exemple #9
0
 protected virtual Task <object> ExecuteNonQuery(DataRequest request, Dictionary <string, object> options)
 {
     return(Deferred.Create <object>(Script.Undefined).Task);
 }
Exemple #10
0
        private Task<ServerResponse> ExecuteRequest(DataCollection collection, DataRequest request)
        {
            Deferred<ServerResponse> deferred = Deferred.Create<ServerResponse>();
            Task<object> resultTask = collection.ExecuteRequest(request);

            resultTask.Done(delegate(object result) {
                deferred.Resolve(CreateServerResponse(request, result));
            })
            .Fail(delegate(Exception e) {
                deferred.Resolve(ServerResponse.CreateServerError(e.Message));
            });

            return deferred.Task;
        }
Exemple #11
0
        private ServerResponse CreateServerResponse(DataRequest request, object result)
        {
            DataOperation operation = request.Operation;
            if (Script.IsUndefined(result)) {
                return new ServerResponse(HttpStatusCode.MethodNotAllowed);
            }
            else if (Script.IsNull(result)) {
                if (operation == DataOperation.Lookup) {
                    return ServerResponse.NotFound;
                }
                else if (operation == DataOperation.Query) {
                    return new ServerResponse(HttpStatusCode.OK).AddObjectContent(new object[0]);
                }
                else if (operation == DataOperation.Execute) {
                    return ServerResponse.NoContent;
                }
            }

            if ((operation == DataOperation.Insert) ||
                (operation == DataOperation.Update) ||
                (operation == DataOperation.Merge) ||
                (operation == DataOperation.Delete_)) {
                if (result is bool) {
                    bool resultFlag = Script.Boolean(result);
                    if ((resultFlag == false) && (operation == DataOperation.Insert)) {
                        return ServerResponse.Conflict;
                    }
                    if (resultFlag) {
                        return ServerResponse.NoContent;
                    }
                    else {
                        return ServerResponse.NotFound;
                    }
                }
            }
            else if (operation == DataOperation.Lookup) {
                Array items = result as Array;
                if (items != null) {
                    result = items[0];
                }
            }

            return new ServerResponse(HttpStatusCode.OK).AddObjectContent(result);
        }
Exemple #12
0
        private DataRequest CreateDataRequest(ServerRequest serverRequest, DataCollection collection)
        {
            DataOperation operation;

            string id = serverRequest.UrlData.Query["id"];
            string partition = serverRequest.UrlData.Query["partition"];
            string operationName = serverRequest.Route.Parameters["operationName"];

            switch (serverRequest.HttpRequest.Method) {
                case HttpVerb.GET:
                    if (String.IsNullOrEmpty(id) == false) {
                        operation = DataOperation.Lookup;
                    }
                    else {
                        operation = DataOperation.Query;
                    }
                    break;
                case HttpVerb.POST:
                    if (String.IsNullOrEmpty(operationName)) {
                        operation = DataOperation.Update;
                    }
                    else {
                        operation = DataOperation.Execute;
                    }
                    break;
                case HttpVerb.PUT:
                    operation = DataOperation.Insert;
                    break;
                case HttpVerb.DELETE:
                    operation = DataOperation.Delete_;
                    break;
                default:
                    return null;
            }

            DataRequest dataRequest = new DataRequest(operation, operationName, serverRequest.UrlData.Query);
            dataRequest.Query = new DataQuery(collection, id);
            dataRequest.Partition = partition;

            return dataRequest;
        }