示例#1
0
        public override void GenerateCode(IFunctionBuilder functionBuilder)
        {
            var    outputTypeReference = FunctionData.Properties[MongoDBReadShared.Names.OutputType].GetValue <ITypeReference>();
            string outputType          = functionBuilder.GetTypeName(outputTypeReference);

            if (string.IsNullOrEmpty(outputType))
            {
                throw new ArgumentException("MongoDBRead has no OutputType set");
            }

            functionBuilder.AddAssemblyReference(typeof(MongoDBX));

            MongoDBCodeGenerator.CompileExpression(functionBuilder,
                                                   FunctionData,
                                                   MongoDBReadShared.Names.AggregationPipeline,
                                                   MongoDBReadShared.Names.AggregationPipelineExpressions);

            MongoDBCodeGenerator.CompileExpression(functionBuilder,
                                                   FunctionData,
                                                   MongoDBReadShared.Names.Query,
                                                   MongoDBReadShared.Names.QueryExpressions);

            MongoDBCodeGenerator.CompileExpression(functionBuilder,
                                                   FunctionData,
                                                   MongoDBReadShared.Names.Query,
                                                   MongoDBReadShared.Names.QueryExpressions);

            MongoDBCodeGenerator.CompileExpression(functionBuilder,
                                                   FunctionData,
                                                   MongoDBReadShared.Names.Fields,
                                                   MongoDBReadShared.Names.FieldsExpressions);


            MongoDBReadShared.OperationType operation = FunctionData.Properties[MongoDBReadShared.Names.Operation].GetValue <MongoDBReadShared.OperationType>();
            switch (operation)
            {
            case MongoDBReadShared.OperationType.Aggregate:
                AddAggregateCode(functionBuilder, outputTypeReference);
                break;

            case MongoDBReadShared.OperationType.Find:
                AddFindCode(functionBuilder, outputTypeReference);
                break;

            default:
                throw new NotSupportedException(string.Format("Invalid operation type [{0}] specified.", operation));
            }

            MongoDBReadShared.ReturnModeType returnMode = FunctionData.Properties[MongoDBReadShared.Names.ReturnOptionsPropertyName].GetValue <MongoDBReadShared.ReturnModeType>();
            switch (returnMode)
            {
            case MongoDBReadShared.ReturnModeType.RowByRow:
            {
                functionBuilder.AddCode(string.Format(@"{0} = results.Select(v => new Twenty57.Linx.Plugin.Common.CodeGeneration.NextResult(""{1}"", v));",
                                                      functionBuilder.ExecutionPathOutParamName,
                                                      MongoDBReadShared.Names.ExecutionPath));
                break;
            }

            case MongoDBReadShared.ReturnModeType.ListOfRows:
            {
                functionBuilder.AddCode("return results.ToList();");
                break;
            }

            case MongoDBReadShared.ReturnModeType.FirstRow:
            {
                functionBuilder.AddCode("if (results.Count() > 0) return results.First(); else throw new Exception(\"No rows returned by query.\");");
                break;
            }

            case MongoDBReadShared.ReturnModeType.FirstRowElseEmptyRow:
            {
                functionBuilder.AddCode("return results.FirstOrDefault();");
                break;
            }
            }
            functionBuilder.AddCode("}");
        }
示例#2
0
        public override void GenerateCode(IFunctionBuilder functionBuilder)
        {
            IPropertyData opProp           = FunctionData.Properties[MongoDBWriteShared.Names.Operation];
            var           Operation        = FunctionData.Properties[MongoDBWriteShared.Names.Operation].GetValue <MongoDBWriteOperation>();
            var           InsertIfNotFound = FunctionData.Properties[MongoDBWriteShared.Names.InsertIfNotFound].GetValue <bool>();
            string        operation        = "";
            var           updateOperation  = functionBuilder.GetParamName(MongoDBWriteShared.Names.UpdateOperation);
            var           connectionString = functionBuilder.GetParamName(MongoDBWriteShared.Names.ConnectionString);

            ITypeReference dataTypeReference = functionBuilder.GetTypeReference(
                FunctionData.Properties[MongoDBWriteShared.Names.Data].Value
                );

            string dataType = functionBuilder.GetTypeName(dataTypeReference);

            if (!dataTypeReference.IsGenerated && dataTypeReference.IsCompiled)
            {
                functionBuilder.AddAssemblyReference(dataTypeReference.GetUnderlyingType());
            }

            MongoDBCodeGenerator.CompileExpression(functionBuilder,
                                                   FunctionData,
                                                   MongoDBWriteShared.Names.UpdateOperation,
                                                   MongoDBWriteShared.Names.UpdateOperationExpressions
                                                   );

            MongoDBCodeGenerator.CompileExpression(functionBuilder,
                                                   FunctionData,
                                                   MongoDBWriteShared.Names.Criteria,
                                                   MongoDBWriteShared.Names.CriteriaExpressions
                                                   );

            switch (Operation)
            {
            case MongoDBWriteOperation.DeleteAll:
                operation = "DeleteAll(Collection);";
                break;

            case MongoDBWriteOperation.Delete:
                operation = "Delete(Collection, Criteria);";
                break;

            case MongoDBWriteOperation.Update:
                operation = "Update(Collection, Criteria, " + updateOperation + ", " + (InsertIfNotFound ? "true" : "false") + ");";
                break;

            case MongoDBWriteOperation.Replace:
                operation = "Replace(Collection, Criteria, (" + dataType + ")Data, " + (InsertIfNotFound ? "true" : "false") + ");";
                break;

            case MongoDBWriteOperation.Insert:
            default:
                operation = "Insert(Collection, (" + dataType + ")Data);";
                break;
            }

            functionBuilder.AddAssemblyReference(typeof(MongoDBX));
            functionBuilder.AddAssemblyReference(typeof(SqlStringHandler));
            functionBuilder.AddCode(String.Format(
                                        @"using (var mongoX = new Twenty57.Linx.Components.MongoDB.MongoDBX({0})){{
						mongoX.LogEvent += message => {1}.Log(message);
						mongoX.{2}
					}}"                    , connectionString, functionBuilder.ContextParamName, operation));
        }
示例#3
0
        public override void GenerateCode(IFunctionBuilder functionBuilder)
        {
            var    outputTypeReference = FunctionData.Properties[MongoDBMapReduceShared.Names.OutputType].GetValue <ITypeReference>();
            string outputType          = functionBuilder.GetTypeName(outputTypeReference);

            if (string.IsNullOrEmpty(outputType))
            {
                throw new ArgumentException("MongoDBMapReduce has no OutputType set");
            }

            functionBuilder.AddAssemblyReference(typeof(MongoDBX));

            MongoDBCodeGenerator.CompileExpression(functionBuilder,
                                                   FunctionData,
                                                   MongoDBMapReduceShared.Names.Query,
                                                   MongoDBMapReduceShared.Names.QueryExpressions
                                                   );

            MongoDBCodeGenerator.CompileExpression(functionBuilder,
                                                   FunctionData,
                                                   MongoDBMapReduceShared.Names.Map,
                                                   MongoDBMapReduceShared.Names.MapExpressions
                                                   );

            MongoDBCodeGenerator.CompileExpression(functionBuilder,
                                                   FunctionData,
                                                   MongoDBMapReduceShared.Names.Reduce,
                                                   MongoDBMapReduceShared.Names.ReduceExpressions
                                                   );

            MongoDBCodeGenerator.CompileExpression(functionBuilder,
                                                   FunctionData,
                                                   MongoDBMapReduceShared.Names.Finalize,
                                                   MongoDBMapReduceShared.Names.FinalizeExpressions
                                                   );

            GenerateMapReduceCode(functionBuilder, outputTypeReference);

            MongoDBMapReduceShared.ReturnModeType returnMode = FunctionData.Properties[MongoDBMapReduceShared.Names.ReturnOptionsPropertyName].GetValue <MongoDBMapReduceShared.ReturnModeType>();
            switch (returnMode)
            {
            case MongoDBMapReduceShared.ReturnModeType.RowByRow:
            {
                functionBuilder.AddCode(string.Format(@"{0} = results.Select(v => new Twenty57.Linx.Plugin.Common.CodeGeneration.NextResult(""{1}"", v));",
                                                      functionBuilder.ExecutionPathOutParamName,
                                                      MongoDBMapReduceShared.Names.ExecutionPath));
                break;
            }

            case MongoDBMapReduceShared.ReturnModeType.ListOfRows:
            {
                functionBuilder.AddCode("return results.ToList();");
                break;
            }

            case MongoDBMapReduceShared.ReturnModeType.FirstRow:
            {
                functionBuilder.AddCode("if (results.Count() > 0) return results.First(); else throw new Exception(\"No rows returned by query.\");");
                break;
            }

            case MongoDBMapReduceShared.ReturnModeType.FirstRowElseEmptyRow:
            {
                functionBuilder.AddCode("return results.FirstOrDefault();");
                break;
            }
            }
            functionBuilder.AddCode("}");
        }