예제 #1
0
        public void WriteToFile(List <object> records, StreamWriter writer)
        {
            if (records.Count > 0)
            {
                foreach (object record in records)
                {
                    string dataRow = string.Empty;

                    Type           recordtype   = record.GetType();
                    PropertyInfo[] propertyInfo = recordtype.GetProperties();

                    foreach (PropertyInfo info in propertyInfo)
                    {
                        try
                        {
                            FixedLengthAttribute[] attributes = info.GetCustomAttributes(typeof(FixedLengthAttribute), false).Cast <FixedLengthAttribute>().ToArray();
                            FixedLengthAttribute   ffa        = attributes.FirstOrDefault();
                            if (ffa == null)
                            {
                                continue;
                            }

                            string outputString = Convert.ToString(info.GetValue(record, null));

                            if (info.PropertyType == typeof(decimal))
                            {
                                outputString = outputString.Replace(".", "");
                            }

                            //Remove Special Characters
                            if (!string.IsNullOrEmpty(ffa.RegexReplacePattern))
                            {
                                outputString = Regex.Replace(@outputString, ffa.RegexReplacePattern, string.Empty);
                            }

                            //Read value as per length and read position
                            outputString = outputString.Length > ffa.FieldLength ? outputString.Substring(0, ffa.FieldLength) : outputString;

                            switch (ffa.AlphaCharacterCaseStyle)
                            {
                            case AlphaCharacterCaseEnum.Lower:
                                outputString = outputString.ToLower();
                                break;

                            case AlphaCharacterCaseEnum.Upper:
                                outputString = outputString.ToUpper();
                                break;
                            }

                            if (outputString.Length < ffa.FieldLength)
                            {
                                outputString = ffa.PaddingStyle == PaddingEnum.Left ? outputString.PadLeft(ffa.FieldLength, ffa.PaddingChar) : outputString.PadRight(ffa.FieldLength, ffa.PaddingChar);
                            }

                            if (outputString.Length == ffa.FieldLength)
                            {
                                if (dataRow.Length < ffa.StartPosition)
                                {
                                    dataRow += outputString;
                                }
                                else
                                {
                                    // current field falls inside the middle of the existing output string.
                                    // split based on start position and then concatenate
                                    string leftSide  = dataRow.Substring(0, ffa.StartPosition - 1);
                                    string rightSide = dataRow.Substring(ffa.StartPosition, dataRow.Length);
                                    dataRow = leftSide + outputString + rightSide;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new PXException(PXMessages.LocalizeFormatNoPrefixNLA(Messages.WrittingError, record.GetType().Name, info.Name, ex.Message));
                        }
                    }

                    // write to file
                    writer.WriteLine(dataRow);
                }
            }
        }
예제 #2
0
        private FunctionDescriptor BuildFunctionDescriptor(MethodInfo mi)
        {
            DbFunctionAttribute attrFunction = mi.GetCustomAttribute <DbFunctionAttribute>();

            if (attrFunction == null)
            {
                throw new InvalidOperationException(string.Format("Method {0} of type {1} must be marked by'DbFunction...' attribute.", mi.Name, mi.DeclaringType));
            }
            DbFunctionExAttribute attrFunctionEx = attrFunction as DbFunctionExAttribute;
            string methodName     = mi.Name;
            string functionName   = !string.IsNullOrWhiteSpace(attrFunction.FunctionName) ? attrFunction.FunctionName : methodName;
            string databaseSchema = attrFunctionEx != null && !string.IsNullOrWhiteSpace(attrFunctionEx.Schema) ? attrFunctionEx.Schema : _databaseSchema;

            if (string.IsNullOrWhiteSpace(databaseSchema))
            {
                throw new InvalidOperationException(string.Format("Database schema for method {0} of type {1} is not defined.", mi.Name, mi.DeclaringType));
            }
            //
            bool isExtension = mi.IsDefined(typeof(ExtensionAttribute), false);

            ParameterDescriptor[] parameters = mi.GetParameters()
                                               .SkipWhile(pi => isExtension && pi.Position == 0 && pi.ParameterType.IsAssignableFrom(typeof(DbContext)))
                                               .OrderBy(pi => pi.Position)
                                               .Select((pi, i) =>
            {
                FunctionParameterAttribute parameterAttr = pi.GetCustomAttribute <FunctionParameterAttribute>();
                Type parameterType = pi.ParameterType == typeof(ObjectParameter) ? parameterAttr.Type : pi.ParameterType;
                if (parameterType == null)
                {
                    throw new InvalidOperationException(string.Format("Method parameter '{0}' at position {1} is not defined.", pi.Name, pi.Position));
                }
                //
                MinLengthAttribute minLengthAttr           = pi.GetCustomAttribute <MinLengthAttribute>();
                MaxLengthAttribute maxLengthAttr           = pi.GetCustomAttribute <MaxLengthAttribute>();
                FixedLengthAttribute fixedLengthAttr       = pi.GetCustomAttribute <FixedLengthAttribute>();
                PrecisionScaleAttribute precisionScaleAttr = pi.GetCustomAttribute <PrecisionScaleAttribute>();
                //
                return(new ParameterDescriptor(i, parameterType.IsByRef ? pi.IsOut ? ParameterDirection.Output : ParameterDirection.InputOutput : ParameterDirection.Input, parameterType)
                {
                    Name = parameterAttr != null && !string.IsNullOrWhiteSpace(parameterAttr.Name) ? parameterAttr.Name : pi.Name,
                    StoreTypeName = parameterAttr != null && !string.IsNullOrWhiteSpace(parameterAttr.TypeName) ? parameterAttr.TypeName : null,
                    Length = maxLengthAttr != null ? maxLengthAttr.Length : default(int?),
                    IsFixedLength = minLengthAttr != null && maxLengthAttr != null ? minLengthAttr.Length == maxLengthAttr.Length : fixedLengthAttr != null ? fixedLengthAttr.IsFixedLength : default(bool?),
                    Precision = precisionScaleAttr != null ? precisionScaleAttr.Precision : default(byte?),
                    Scale = precisionScaleAttr != null ? precisionScaleAttr.Scale : default(byte?)
                });
            })
                                               .ToArray();
            //  IQueryable<>
            Type returnType = mi.ReturnType.IsGenericType && mi.ReturnType.GetGenericTypeDefinition() == typeof(IQueryable <>) ? mi.ReturnType :
                              mi.ReturnType.GetInterfaces().SingleOrDefault(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IQueryable <>));

            if (returnType != null)
            {
                FunctionResultAttribute attrResult = mi.ReturnParameter.GetCustomAttribute <FunctionResultAttribute>();
                ResultDescriptor        result     = new ResultDescriptor(returnType.GetGenericArguments()[0])
                {
                    ColumnName    = attrResult != null ? attrResult.ColumnName : _resultColumnName,
                    StoreTypeName = attrResult != null ? attrResult.TypeName : null
                };
                return(new FunctionDescriptor(_namespaceName, databaseSchema, functionName, true,
                                              attrFunctionEx != null ? attrFunctionEx.IsComposable : default(bool?),
                                              isExtension ? attrFunctionEx != null ? attrFunctionEx.IsBuiltIn : default(bool?) : false,
                                              false,
                                              parameters.Length == 0 ? attrFunctionEx != null ? attrFunctionEx.IsNiladic : default(bool?) : false,
                                              attrFunctionEx != null ? attrFunctionEx.ParameterTypeSemantics : default(ParameterTypeSemantics?),
                                              parameters, Enumerable.Repeat(result, 1)));
            }
            //  IQueryable
            returnType = mi.ReturnType == typeof(IQueryable) ? mi.ReturnType : mi.ReturnType.GetInterfaces().SingleOrDefault(t => t == typeof(IQueryable));
            if (returnType != null)
            {
                FunctionResultAttribute attrResult = mi.ReturnParameter.GetCustomAttribute <FunctionResultAttribute>();
                if (attrResult != null)
                {
                    ResultDescriptor result = new ResultDescriptor(attrResult.Type)
                    {
                        ColumnName    = attrResult.ColumnName ?? _resultColumnName,
                        StoreTypeName = attrResult.TypeName
                    };
                    return(new FunctionDescriptor(_namespaceName, databaseSchema, functionName, true,
                                                  attrFunctionEx != null ? attrFunctionEx.IsComposable : default(bool?),
                                                  isExtension ? attrFunctionEx != null ? attrFunctionEx.IsBuiltIn : default(bool?) : false,
                                                  false,
                                                  parameters.Length == 0 ? attrFunctionEx != null ? attrFunctionEx.IsNiladic : default(bool?) : false,
                                                  attrFunctionEx != null ? attrFunctionEx.ParameterTypeSemantics : default(ParameterTypeSemantics?),
                                                  parameters, Enumerable.Repeat(result, 1)));
                }
                throw new InvalidOperationException("Result type is not specified in function");
            }
            //  IEnumerable<>
            returnType = mi.ReturnType == typeof(string) || mi.ReturnType == typeof(byte[]) ? null :
                         mi.ReturnType.IsGenericType && mi.ReturnType.GetGenericTypeDefinition() == typeof(IEnumerable <>) ? mi.ReturnType :
                         mi.ReturnType.GetInterfaces().SingleOrDefault(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable <>));
            if (returnType != null)
            {
                FunctionResultAttribute        attrResult = mi.ReturnParameter.GetCustomAttribute <FunctionResultAttribute>();
                IEnumerable <ResultDescriptor> results    =
                    Enumerable.Repeat(new ResultDescriptor(returnType.GetGenericArguments()[0])
                {
                    ColumnName    = attrResult != null ? attrResult.ColumnName : _resultColumnName,
                    StoreTypeName = attrResult != null ? attrResult.TypeName : null
                }, 1)
                    .Concat(mi.GetCustomAttributes <FunctionResultAttribute>()
                            .Select(a => new ResultDescriptor(a.Type)
                {
                    ColumnName    = a.ColumnName ?? _resultColumnName,
                    StoreTypeName = a.TypeName
                }));
                return(new FunctionDescriptor(_namespaceName, databaseSchema, functionName, true, false, false, false,
                                              parameters.Length == 0 ? attrFunctionEx != null ? attrFunctionEx.IsNiladic : default(bool?) : false,
                                              attrFunctionEx != null ? attrFunctionEx.ParameterTypeSemantics : default(ParameterTypeSemantics?),
                                              parameters, results));
            }
            //  IEnumerable
            returnType = mi.ReturnType == typeof(string) || mi.ReturnType == typeof(byte[]) ? null :
                         mi.ReturnType == typeof(IEnumerable) ? mi.ReturnType : mi.ReturnType.GetInterfaces().SingleOrDefault(t => t == typeof(IEnumerable));
            if (returnType != null)
            {
                IEnumerable <ResultDescriptor> results = mi.GetCustomAttributes <FunctionResultAttribute>()
                                                         .Select(a => new ResultDescriptor(a.Type)
                {
                    ColumnName    = a.ColumnName ?? _resultColumnName,
                    StoreTypeName = a.TypeName
                });
                return(new FunctionDescriptor(_namespaceName, databaseSchema, functionName, true, false, false, false,
                                              parameters.Length == 0 ? attrFunctionEx != null ? attrFunctionEx.IsNiladic : default(bool?) : false,
                                              attrFunctionEx != null ? attrFunctionEx.ParameterTypeSemantics : default(ParameterTypeSemantics?),
                                              parameters, results));
            }
            //  Scalar result
            returnType = mi.ReturnType;
            FunctionResultAttribute attr             = mi.ReturnParameter.GetCustomAttribute <FunctionResultAttribute>();
            ResultDescriptor        resultDescriptor = new ResultDescriptor(mi.ReturnType)
            {
                StoreTypeName = attr != null ? attr.TypeName : null
            };

            return(new FunctionDescriptor(_namespaceName, databaseSchema, functionName, false,
                                          attrFunctionEx != null ? attrFunctionEx.IsComposable : default(bool?),
                                          isExtension ? attrFunctionEx != null ? attrFunctionEx.IsBuiltIn : default(bool?) : false,
                                          attrFunctionEx != null ? attrFunctionEx.IsAggregate : default(bool?),
                                          parameters.Length == 0 ? attrFunctionEx != null ? attrFunctionEx.IsNiladic : default(bool?) : false,
                                          attrFunctionEx != null ? attrFunctionEx.ParameterTypeSemantics : default(ParameterTypeSemantics?),
                                          parameters, Enumerable.Repeat(resultDescriptor, 1)));
        }