Example #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("}");
        }
Example #2
0
        private IFunctionData AddMissingProperties(IFunctionData data)
        {
            IPropertyData criteriaProperty;
            IPropertyData aggregationPipelineProperty;

            object criteria            = string.Empty;
            object aggregationPipeline = string.Empty;

            MongoDBReadShared.OperationType operationType = MongoDBReadShared.OperationType.Find;

            if (data.TryFindPropertyById("Criteria", out criteriaProperty) &&
                data.TryFindPropertyById("Aggregation pipeline", out aggregationPipelineProperty))
            {
                criteria            = criteriaProperty.Value;
                aggregationPipeline = aggregationPipelineProperty.Value;

                if (string.IsNullOrEmpty(criteria.ToString()) && !string.IsNullOrEmpty(aggregationPipeline.ToString()))
                {
                    operationType = MongoDBReadShared.OperationType.Aggregate;
                }
                data = data.RemoveProperty(criteriaProperty);
                data = data.RemoveProperty(aggregationPipelineProperty);
            }

            if (data.FindPropertyById(MongoDBReadShared.Names.Operation) == null)
            {
                data = data.AddProperty(new Property(MongoDBReadShared.Names.Operation, typeof(MongoDBReadShared.OperationType), ValueUseOption.DesignTime, operationType));
            }

            if (data.FindPropertyById(MongoDBReadShared.Names.AggregationPipeline) == null)
            {
                data = data.AddProperty(new Property(MongoDBReadShared.Names.AggregationPipeline, typeof(string), ValueUseOption.RuntimeRead, aggregationPipeline)
                {
                    IsVisible = true
                });
            }

            if (data.FindPropertyById(MongoDBReadShared.Names.Query) == null)
            {
                data = data.AddProperty(new Property(MongoDBReadShared.Names.Query, typeof(string), ValueUseOption.RuntimeRead, criteria)
                {
                    IsVisible = true
                });
            }

            if (data.FindPropertyById(MongoDBReadShared.Names.Fields) == null)
            {
                data = data.AddProperty(new Property(MongoDBReadShared.Names.Fields, typeof(string), ValueUseOption.RuntimeRead, string.Empty)
                {
                    IsVisible = true
                });
            }

            if (data.FindPropertyById(MongoDBReadShared.Names.Sort) == null)
            {
                data = data.AddProperty(new Property(MongoDBReadShared.Names.Sort, typeof(string), ValueUseOption.RuntimeRead, string.Empty)
                {
                    IsVisible = true
                });
            }

            if (data.FindPropertyById(MongoDBReadShared.Names.Skip) == null)
            {
                data = data.AddProperty(new Property(MongoDBReadShared.Names.Skip, typeof(int), ValueUseOption.RuntimeRead, 0)
                {
                    IsVisible = true
                });
            }

            if (data.FindPropertyById(MongoDBReadShared.Names.Limit) == null)
            {
                data = data.AddProperty(new Property(MongoDBReadShared.Names.Limit, typeof(int), ValueUseOption.RuntimeRead, 0)
                {
                    IsVisible = true
                });
            }

            string        criteriaExpressionKey = "CriteriaExpression";
            List <string> criteriaExpressionIDs = data.Properties.Keys.Where(key => key.StartsWith(criteriaExpressionKey)).ToList();

            foreach (string criteriaExpressionID in criteriaExpressionIDs)
            {
                IPropertyData criteriaExpressionProperty = data.Properties[criteriaExpressionID];
                string        queryExpressionName        = criteriaExpressionID.Replace(criteriaExpressionKey, MongoDBReadShared.Names.QueryExpressions);
                data = data.AddProperty(new Property(queryExpressionName, criteriaExpressionProperty.TypeReference, criteriaExpressionProperty.ValueUsage, criteriaExpressionProperty.Value)
                {
                    IsVisible = criteriaExpressionProperty.IsVisible
                });
                data = data.RemoveProperty(criteriaExpressionProperty);
            }

            return(data);
        }