private static DeliveryConvertor DetermineWideningDeliveryConvertor(
            bool firstParameterIsEPStatement,
            EPStatement statement,
            Type[] selectClauseTypes,
            Type[] parameterTypes,
            MethodInfo method,
            string engineURI)
        {
            var needWidener = false;

            for (var i = 0; i < selectClauseTypes.Length; i++)
            {
                var optionalWidener = GetWidener(i, selectClauseTypes[i], parameterTypes[i], method, statement.Name, engineURI);
                if (optionalWidener != null)
                {
                    needWidener = true;
                    break;
                }
            }
            if (!needWidener)
            {
                return(firstParameterIsEPStatement
                    ? (DeliveryConvertor) new DeliveryConvertorNullWStatement(statement)
                    : (DeliveryConvertor)DeliveryConvertorNull.INSTANCE);
            }
            var wideners = new TypeWidener[selectClauseTypes.Length];

            for (var i = 0; i < selectClauseTypes.Length; i++)
            {
                wideners[i] = GetWidener(i, selectClauseTypes[i], parameterTypes[i], method, statement.Name, engineURI);
            }
            return(firstParameterIsEPStatement
                ? (DeliveryConvertor) new DeliveryConvertorWidenerWStatement(wideners, statement)
                : (DeliveryConvertor) new DeliveryConvertorWidener(wideners));
        }
 internal Item(int toIndex, int optionalFromIndex, ExprEvaluator evaluator, TypeWidener optionalWidener)
 {
     ToIndex           = toIndex;
     OptionalFromIndex = optionalFromIndex;
     Evaluator         = evaluator;
     OptionalWidener   = optionalWidener;
 }
Ejemplo n.º 3
0
 internal Item(int toIndex, String optionalPropertyName, ExprEvaluator evaluator, TypeWidener optionalWidener)
 {
     ToIndex = toIndex;
     OptionalPropertyName = optionalPropertyName;
     Evaluator            = evaluator;
     OptionalWidener      = optionalWidener;
 }
Ejemplo n.º 4
0
        private static DeliveryConvertor DetermineWideningDeliveryConvertor(Type[] selectClauseTypes,
                                                                            Type[] parameterTypes,
                                                                            MethodInfo method)
        {
            bool needWidener = false;

            for (int i = 0; i < selectClauseTypes.Length; i++)
            {
                TypeWidener optionalWidener = GetWidener(i, selectClauseTypes[i], parameterTypes[i], method);
                if (optionalWidener != null)
                {
                    needWidener = true;
                    break;
                }
            }
            if (!needWidener)
            {
                return(DeliveryConvertorNull.INSTANCE);
            }
            var wideners = new TypeWidener[selectClauseTypes.Length];

            for (int i = 0; i < selectClauseTypes.Length; i++)
            {
                wideners[i] = GetWidener(i, selectClauseTypes[i], parameterTypes[i], method);
            }
            return(new DeliveryConvertorWidener(wideners));
        }
Ejemplo n.º 5
0
 internal Item(Field toToField, Field optionalFromField, ExprEvaluator evaluator, TypeWidener optionalWidener)
 {
     ToField           = toToField;
     OptionalFromField = optionalFromField;
     Evaluator         = evaluator;
     OptionalWidener   = optionalWidener;
 }
Ejemplo n.º 6
0
 public EventBeanUpdateItem(
     ExprEvaluator expression,
     string optinalPropertyName,
     EventPropertyWriter optionalWriter,
     bool notNullableField,
     TypeWidener optionalWidener)
 {
     Expression           = expression;
     OptionalPropertyName = optinalPropertyName;
     OptionalWriter       = optionalWriter;
     IsNotNullableField   = notNullableField;
     OptionalWidener      = optionalWidener;
 }
Ejemplo n.º 7
0
        public InternalEventRouterDesc GetValidatePreprocessing(EventType eventType, UpdateDesc desc, Attribute[] annotations)
        {
            if (Log.IsDebugEnabled)
            {
                Log.Debug("Validating route preprocessing for type '" + eventType.Name + "'");
            }

            if (!(eventType is EventTypeSPI))
            {
                throw new ExprValidationException("Update statements require the event type to implement the " + typeof(EventTypeSPI) + " interface");
            }

            var eventTypeSPI = (EventTypeSPI)eventType;
            var wideners     = new TypeWidener[desc.Assignments.Count];
            var properties   = new List <String>();

            for (int i = 0; i < desc.Assignments.Count; i++)
            {
                var xxx            = desc.Assignments[i];
                var assignmentPair = ExprNodeUtility.CheckGetAssignmentToProp(xxx.Expression);
                if (assignmentPair == null)
                {
                    throw new ExprValidationException("Missing property assignment expression in assignment number " + i);
                }

                var writableProperty = eventTypeSPI.GetWritableProperty(assignmentPair.First);
                if (writableProperty == null)
                {
                    throw new ExprValidationException("Property '" + assignmentPair.First + "' is not available for write access");
                }

                wideners[i] = TypeWidenerFactory.GetCheckPropertyAssignType(
                    ExprNodeUtility.ToExpressionStringMinPrecedenceSafe(assignmentPair.Second),
                    assignmentPair.Second.ExprEvaluator.ReturnType,
                    writableProperty.PropertyType,
                    assignmentPair.First);
                properties.Add(assignmentPair.First);
            }

            // check copy-able
            var copyMethod = eventTypeSPI.GetCopyMethod(properties.ToArray());

            if (copyMethod == null)
            {
                throw new ExprValidationException("The update-clause requires the underlying event representation to support copy (via Serializable by default)");
            }

            return(new InternalEventRouterDesc(desc, copyMethod, wideners, eventType, annotations));
        }
        private static SelectExprProcessor InitializeJoinWildcardInternal(
            EventType eventType,
            ICollection <WriteablePropertyDescriptor> writables,
            string[] streamNames,
            EventType[] streamTypes,
            EngineImportService engineImportService,
            EventAdapterService eventAdapterService,
            string statementName,
            string engineURI)
        {
            var typeWidenerCustomizer  = eventAdapterService.GetTypeWidenerCustomizer(eventType);
            var writablePropertiesList = new List <WriteablePropertyDescriptor>();
            var evaluatorsList         = new List <ExprEvaluator>();
            var widenersList           = new List <TypeWidener>();

            // loop over all columns selected, if any
            for (var i = 0; i < streamNames.Length; i++)
            {
                WriteablePropertyDescriptor selectedWritable = null;
                TypeWidener widener = null;

                foreach (var desc in writables)
                {
                    if (!desc.PropertyName.Equals(streamNames[i]))
                    {
                        continue;
                    }

                    widener = TypeWidenerFactory.GetCheckPropertyAssignType(
                        streamNames[i], streamTypes[i].UnderlyingType, desc.PropertyType, desc.PropertyName, false,
                        typeWidenerCustomizer, statementName, engineURI);
                    selectedWritable = desc;
                    break;
                }

                if (selectedWritable == null)
                {
                    var message = "Stream underlying object for stream '" + streamNames[i] +
                                  "' could not be assigned to any of the properties of the underlying type (missing column names, event property or setter method?)";
                    throw new ExprValidationException(message);
                }

                var streamNum  = i;
                var returnType = streamTypes[streamNum].UnderlyingType;
                var evaluator  = new ProxyExprEvaluator
                {
                    ProcEvaluate = evaluateParams =>
                    {
                        EventBean theEvent = evaluateParams.EventsPerStream[streamNum];
                        if (theEvent != null)
                        {
                            return(theEvent.Underlying);
                        }
                        return(null);
                    },

                    ProcReturnType = () => returnType
                };

                // add
                writablePropertiesList.Add(selectedWritable);
                evaluatorsList.Add(evaluator);
                widenersList.Add(widener);
            }

            // assign
            var writableProperties = writablePropertiesList.ToArray();
            var exprEvaluators     = evaluatorsList.ToArray();
            var wideners           = widenersList.ToArray();

            EventBeanManufacturer eventManufacturer;

            try
            {
                eventManufacturer = eventAdapterService.GetManufacturer(
                    eventType, writableProperties, engineImportService, false);
            }
            catch (EventBeanManufactureException e)
            {
                throw new ExprValidationException(e.Message, e);
            }

            return(new SelectExprInsertNativeWidening(eventType, eventManufacturer, exprEvaluators, wideners));
        }
        private static SelectExprProcessor InitializeSetterManufactor(
            EventType eventType,
            ICollection <WriteablePropertyDescriptor> writables,
            bool isUsingWildcard,
            StreamTypeService typeService,
            ExprEvaluator[] expressionNodes,
            string[] columnNames,
            Object[] expressionReturnTypes,
            EngineImportService engineImportService,
            EventAdapterService eventAdapterService,
            string statementName)
        {
            var typeWidenerCustomizer  = eventAdapterService.GetTypeWidenerCustomizer(eventType);
            var writablePropertiesList = new List <WriteablePropertyDescriptor>();
            var evaluatorsList         = new List <ExprEvaluator>();
            var widenersList           = new List <TypeWidener>();

            // loop over all columns selected, if any
            for (var i = 0; i < columnNames.Length; i++)
            {
                WriteablePropertyDescriptor selectedWritable = null;
                TypeWidener widener   = null;
                var         evaluator = expressionNodes[i];

                foreach (var desc in writables)
                {
                    if (!desc.PropertyName.Equals(columnNames[i]))
                    {
                        continue;
                    }

                    var columnType = expressionReturnTypes[i];
                    if (columnType == null)
                    {
                        TypeWidenerFactory.GetCheckPropertyAssignType(
                            columnNames[i], null, desc.PropertyType, desc.PropertyName, false, typeWidenerCustomizer,
                            statementName, typeService.EngineURIQualifier);
                    }
                    else if (columnType is EventType)
                    {
                        var columnEventType = (EventType)columnType;
                        var returnType      = columnEventType.UnderlyingType;
                        widener = TypeWidenerFactory.GetCheckPropertyAssignType(
                            columnNames[i], columnEventType.UnderlyingType, desc.PropertyType, desc.PropertyName, false,
                            typeWidenerCustomizer, statementName, typeService.EngineURIQualifier);

                        // handle evaluator returning an event
                        if (TypeHelper.IsSubclassOrImplementsInterface(returnType, desc.PropertyType))
                        {
                            selectedWritable = desc;
                            widener          = input =>
                            {
                                var eventBean = input as EventBean;
                                if (eventBean != null)
                                {
                                    return(eventBean.Underlying);
                                }
                                return(input);
                            };
                            continue;
                        }

                        // find stream
                        var streamNum = 0;
                        for (var j = 0; j < typeService.EventTypes.Length; j++)
                        {
                            if (Equals(typeService.EventTypes[j], columnEventType))
                            {
                                streamNum = j;
                                break;
                            }
                        }
                        var streamNumEval = streamNum;
                        evaluator = new ProxyExprEvaluator
                        {
                            ProcEvaluate = evaluateParams =>
                            {
                                EventBean theEvent = evaluateParams.EventsPerStream[streamNumEval];
                                if (theEvent != null)
                                {
                                    return(theEvent.Underlying);
                                }
                                return(null);
                            },

                            ProcReturnType = () => returnType
                        };
                    }
                    else if (columnType is EventType[])
                    {
                        // handle case where the select-clause contains an fragment array
                        var columnEventType     = ((EventType[])columnType)[0];
                        var componentReturnType = columnEventType.UnderlyingType;
                        var arrayReturnType     = Array.CreateInstance(componentReturnType, 0).GetType();

                        var allowObjectArrayToCollectionConversion = eventType is AvroSchemaEventType;
                        widener = TypeWidenerFactory.GetCheckPropertyAssignType(
                            columnNames[i], arrayReturnType, desc.PropertyType, desc.PropertyName,
                            allowObjectArrayToCollectionConversion, typeWidenerCustomizer, statementName,
                            typeService.EngineURIQualifier);
                        var inner = evaluator;
                        evaluator = new ProxyExprEvaluator
                        {
                            ProcEvaluate = evaluateParams =>
                            {
                                var result = inner.Evaluate(evaluateParams);
                                if (!(result is EventBean[]))
                                {
                                    return(null);
                                }
                                var events = (EventBean[])result;
                                var values = Array.CreateInstance(componentReturnType, events.Length);
                                for (var ii = 0; ii < events.Length; ii++)
                                {
                                    values.SetValue(events[ii].Underlying, ii);
                                }
                                return(values);
                            },

                            ProcReturnType = () => componentReturnType
                        };
                    }
                    else if (!(columnType is Type))
                    {
                        var message = "Invalid assignment of column '" + columnNames[i] +
                                      "' of type '" + columnType +
                                      "' to event property '" + desc.PropertyName +
                                      "' typed as '" + desc.PropertyType.FullName +
                                      "', column and parameter types mismatch";
                        throw new ExprValidationException(message);
                    }
                    else
                    {
                        widener = TypeWidenerFactory.GetCheckPropertyAssignType(
                            columnNames[i], (Type)columnType, desc.PropertyType, desc.PropertyName, false,
                            typeWidenerCustomizer, statementName, typeService.EngineURIQualifier);
                    }

                    selectedWritable = desc;
                    break;
                }

                if (selectedWritable == null)
                {
                    var message = "Column '" + columnNames[i] +
                                  "' could not be assigned to any of the properties of the underlying type (missing column names, event property, setter method or constructor?)";
                    throw new ExprValidationException(message);
                }

                // add
                writablePropertiesList.Add(selectedWritable);
                evaluatorsList.Add(evaluator);
                widenersList.Add(widener);
            }

            // handle wildcard
            if (isUsingWildcard)
            {
                var sourceType = typeService.EventTypes[0];
                foreach (var eventPropDescriptor in sourceType.PropertyDescriptors)
                {
                    if (eventPropDescriptor.RequiresIndex || (eventPropDescriptor.RequiresMapKey))
                    {
                        continue;
                    }

                    WriteablePropertyDescriptor selectedWritable = null;
                    TypeWidener   widener   = null;
                    ExprEvaluator evaluator = null;

                    foreach (var writableDesc in writables)
                    {
                        if (!writableDesc.PropertyName.Equals(eventPropDescriptor.PropertyName))
                        {
                            continue;
                        }

                        widener = TypeWidenerFactory.GetCheckPropertyAssignType(
                            eventPropDescriptor.PropertyName, eventPropDescriptor.PropertyType, writableDesc.PropertyType,
                            writableDesc.PropertyName, false, typeWidenerCustomizer, statementName,
                            typeService.EngineURIQualifier);
                        selectedWritable = writableDesc;

                        var propertyName = eventPropDescriptor.PropertyName;
                        var propertyType = eventPropDescriptor.PropertyType;
                        evaluator = new ProxyExprEvaluator
                        {
                            ProcEvaluate = evaluateParams =>
                            {
                                EventBean theEvent = evaluateParams.EventsPerStream[0];
                                if (theEvent != null)
                                {
                                    return(theEvent.Get(propertyName));
                                }
                                return(null);
                            },

                            ProcReturnType = () => propertyType
                        };
                        break;
                    }

                    if (selectedWritable == null)
                    {
                        var message = "Event property '" + eventPropDescriptor.PropertyName +
                                      "' could not be assigned to any of the properties of the underlying type (missing column names, event property, setter method or constructor?)";
                        throw new ExprValidationException(message);
                    }

                    writablePropertiesList.Add(selectedWritable);
                    evaluatorsList.Add(evaluator);
                    widenersList.Add(widener);
                }
            }

            // assign
            var writableProperties = writablePropertiesList.ToArray();
            var exprEvaluators     = evaluatorsList.ToArray();
            var wideners           = widenersList.ToArray();

            EventBeanManufacturer eventManufacturer;

            try
            {
                eventManufacturer = eventAdapterService.GetManufacturer(
                    eventType, writableProperties, engineImportService, false);
            }
            catch (EventBeanManufactureException e)
            {
                throw new ExprValidationException(e.Message, e);
            }

            return(new SelectExprInsertNativeWidening(eventType, eventManufacturer, exprEvaluators, wideners));
        }
Ejemplo n.º 10
0
        public static SelectExprProcessor Make(EventType[] eventTypes, SelectExprContext selectExprContext, int streamNumber, EventType targetType, ExprNode[] exprNodes, EngineImportService engineImportService)
        {
            var mapResultType = (MapEventType)targetType;
            var mapStreamType = (MapEventType)eventTypes[streamNumber];

            // (A) fully assignment-compatible: same number, name and type of fields, no additional expressions: Straight repackage
            String typeSameMssage = BaseNestableEventType.IsDeepEqualsProperties(mapResultType.Name, mapResultType.Types, mapStreamType.Types);

            if (typeSameMssage == null && selectExprContext.ExpressionNodes.Length == 0)
            {
                return(new MapInsertProcessorSimpleRepackage(selectExprContext, streamNumber, targetType));
            }

            // (B) not completely assignable: find matching properties
            ICollection <WriteablePropertyDescriptor> writables = selectExprContext.EventAdapterService.GetWriteableProperties(mapResultType, true);
            IList <Item> items = new List <Item>();
            IList <WriteablePropertyDescriptor> written = new List <WriteablePropertyDescriptor>();

            // find the properties coming from the providing source stream
            int count = 0;

            foreach (WriteablePropertyDescriptor writeable in writables)
            {
                String propertyName = writeable.PropertyName;

                if (mapStreamType.Types.ContainsKey(propertyName))
                {
                    Object setOneType      = mapStreamType.Types.Get(propertyName);
                    Object setTwoType      = mapResultType.Types.Get(propertyName);
                    bool   setTwoTypeFound = mapResultType.Types.ContainsKey(propertyName);
                    String message         = BaseNestableEventUtil.ComparePropType(propertyName, setOneType, setTwoType, setTwoTypeFound, mapResultType.Name);
                    if (message != null)
                    {
                        throw new ExprValidationException(message);
                    }
                    items.Add(new Item(count, propertyName, null, null));
                    written.Add(writeable);
                    count++;
                }
            }

            // find the properties coming from the expressions of the select clause
            for (int i = 0; i < selectExprContext.ExpressionNodes.Length; i++)
            {
                String        columnName = selectExprContext.ColumnNames[i];
                ExprEvaluator evaluator  = selectExprContext.ExpressionNodes[i];
                ExprNode      exprNode   = exprNodes[i];

                WriteablePropertyDescriptor writable = FindWritable(columnName, writables);
                if (writable == null)
                {
                    throw new ExprValidationException("Failed to find column '" + columnName + "' in target type '" + mapResultType.Name + "'");
                }

                TypeWidener widener = TypeWidenerFactory.GetCheckPropertyAssignType(
                    ExprNodeUtility.ToExpressionStringMinPrecedenceSafe(exprNode),
                    exprNode.ExprEvaluator.ReturnType,
                    writable.PropertyType, columnName);
                items.Add(new Item(count, null, evaluator, widener));
                written.Add(writable);
                count++;
            }

            // make manufacturer
            Item[] itemsArr = items.ToArray();
            EventBeanManufacturer manufacturer;

            try {
                manufacturer = selectExprContext.EventAdapterService.GetManufacturer(mapResultType,
                                                                                     written.ToArray(), engineImportService, true);
            }
            catch (EventBeanManufactureException e) {
                throw new ExprValidationException("Failed to write to type: " + e.Message, e);
            }

            return(new MapInsertProcessorAllocate(streamNumber, itemsArr, manufacturer, targetType));
        }
Ejemplo n.º 11
0
        /// <summary>Returns the widener. </summary>
        /// <param name="columnName">name of column</param>
        /// <param name="columnType">type of column</param>
        /// <param name="writeablePropertyType">property type</param>
        /// <param name="writeablePropertyName">propery name</param>
        /// <param name="allowObjectArrayToCollectionConversion">whether we widen object-array to collection</param>
        /// <param name="customizer">customization if any</param>
        /// <param name="engineURI">engine URI</param>
        /// <param name="statementName">statement name</param>
        /// <exception cref="ExprValidationException">if type validation fails</exception>
        /// <returns>type widender</returns>
        /// <throws>ExprValidationException if type validation fails</throws>
        public static TypeWidener GetCheckPropertyAssignType(
            String columnName,
            Type columnType,
            Type writeablePropertyType,
            String writeablePropertyName,
            bool allowObjectArrayToCollectionConversion,
            TypeWidenerCustomizer customizer,
            string statementName,
            string engineURI)
        {
            Type columnClassBoxed = TypeHelper.GetBoxedType(columnType);
            Type targetClassBoxed = TypeHelper.GetBoxedType(writeablePropertyType);

            if (customizer != null)
            {
                TypeWidener custom = customizer.WidenerFor(columnName, columnType, writeablePropertyType, writeablePropertyName, statementName, engineURI);
                if (custom != null)
                {
                    return(custom);
                }
            }

            if (columnType == null)
            {
                if (writeablePropertyType.IsPrimitive)
                {
                    String message = "Invalid assignment of column '" + columnName +
                                     "' of null type to event property '" + writeablePropertyName +
                                     "' typed as '" + writeablePropertyType.FullName +
                                     "', nullable type mismatch";
                    throw new ExprValidationException(message);
                }
            }
            else if (columnClassBoxed != targetClassBoxed)
            {
                if (columnClassBoxed == typeof(string) && targetClassBoxed == typeof(char?))
                {
                    return(TypeWidenerStringToCharCoercer.Widen);
                }

                if (allowObjectArrayToCollectionConversion &&
                    columnClassBoxed.IsArray &&
                    !columnClassBoxed.GetElementType().IsPrimitive &&
                    targetClassBoxed.IsGenericCollection())
                {
                    return(OBJECT_ARRAY_TO_COLLECTION_COERCER);
                }

                if (columnClassBoxed.IsGenericDictionary() && targetClassBoxed.IsGenericDictionary())
                {
                    var columnClassGenerics = columnClassBoxed.GetGenericArguments();
                    var targetClassGenerics = targetClassBoxed.GetGenericArguments();
                    var transformMethod     = typeof(TransformDictionaryFactory)
                                              .GetMethod("Create", new[] { typeof(object) })
                                              .MakeGenericMethod(targetClassGenerics[0], targetClassGenerics[1], columnClassGenerics[0], columnClassGenerics[1]);

                    return(source =>
                    {
                        var parameters = new object[] { source };
                        return transformMethod.Invoke(null, BindingFlags.Static | BindingFlags.Public, null, parameters, null);
                    });
                }

                if ((columnClassBoxed == typeof(string)) &&
                    (targetClassBoxed == typeof(char[])))
                {
                    return(source =>
                    {
                        var sourceAsString = (string)source;
                        return sourceAsString != null?sourceAsString.ToCharArray() : null;
                    });
                }

                if ((columnClassBoxed == typeof(char[])) &&
                    (targetClassBoxed == typeof(string)))
                {
                    return(source =>
                    {
                        var sourceAsCharArray = (char[])source;
                        return sourceAsCharArray != null ? new string(sourceAsCharArray) : null;
                    });
                }

                if (columnClassBoxed.IsArray && targetClassBoxed.IsArray)
                {
                    var columnClassElement = columnClassBoxed.GetElementType();
                    var targetClassElement = targetClassBoxed.GetElementType();
                    if (columnClassElement.IsAssignmentCompatible(targetClassElement))
                    {
                        // By definition, columnClassElement and targetClassElement should be
                        // incompatible.  Question is, can we find a coercer between them?
                        var coercer = CoercerFactory.GetCoercer(columnClassElement, targetClassElement);
                        return(source => WidenArray(source, targetClassElement, coercer));
                    }
                }

                if (!columnClassBoxed.IsAssignmentCompatible(targetClassBoxed))
                {
                    var writablePropName = writeablePropertyType.FullName;
                    if (writeablePropertyType.IsArray)
                    {
                        writablePropName = writeablePropertyType.GetElementType().FullName + "[]";
                    }

                    var columnTypeName = columnType.FullName;
                    if (columnType.IsArray)
                    {
                        columnTypeName = columnType.GetElementType().FullName + "[]";
                    }

                    String message = "Invalid assignment of column '" + columnName +
                                     "' of type '" + columnTypeName +
                                     "' to event property '" + writeablePropertyName +
                                     "' typed as '" + writablePropName +
                                     "', column and parameter types mismatch";
                    throw new ExprValidationException(message);
                }

                if (writeablePropertyType.IsNumeric())
                {
                    var instance = new TypeWidenerBoxedNumeric(
                        CoercerFactory.GetCoercer(columnClassBoxed, targetClassBoxed));
                    return(instance.Widen);
                }
            }

            return(null);
        }
 public SelectExprProcessorEvalAvroArrayCoercer(ExprEvaluator eval, TypeWidener widener)
 {
     _eval    = eval;
     _widener = widener;
 }