Exemplo n.º 1
0
        protected void InitializeColumnMap(DataReaderWrapperParameters parameters)
        {
            var columns = new List <string>();

            if (parameters?.Columns != null)
            {
                columns.AddRange(parameters.Columns);
            }

            if (columns.Count <= 0)
            {
                for (int i = 0; i < _Reader.FieldCount; i++)
                {
                    columns.Add(_Reader.GetName(i));
                }
            }

            if ((parameters.SkipColumns?.Length ?? 0) > 0)
            {
                var dictSkip = new StringNoCaseDictionary <int>();

                foreach (string skipColumn in parameters.SkipColumns)
                {
                    if (!dictSkip.ContainsKey(skipColumn))
                    {
                        dictSkip.Add(skipColumn, 1);
                    }
                    else
                    {
                        dictSkip[skipColumn]++;
                    }
                }

                for (int i = columns.Count - 1; i >= 0; i--)
                {
                    if (dictSkip.ContainsKey(columns[i]))
                    {
                        columns.RemoveAt(i);
                    }
                }
            }

            for (int i = 0; i < columns.Count; i++)
            {
                string columnName = columns[i];
                int    ordinal    = _Reader.GetOrdinal(columnName);
                if (ordinal < 0)
                {
                    throw new Exception($"Cannot find column '{columnName}'");
                }
                ColumnMap.Add(i, ordinal);
                ColumnMapReverse.Add(ordinal, i);
            }
        }
Exemplo n.º 2
0
        protected void InitializeProperties()
        {
            _Properties.Clear();

            var properties = new StringNoCaseDictionary <PSPropertyInfo>();
            var dataTables = new List <DataTable>();

            foreach (var obj in _DataSource)
            {
                if (obj == null)
                {
                    continue;
                }

                if (obj.BaseObject is DataRow row)
                {
                    if (dataTables.Contains(row.Table))
                    {
                        continue;
                    }

                    foreach (DataColumn column in row.Table.Columns)
                    {
                        var propInfo = new PSPropertyInfo()
                        {
                            Name     = column.ColumnName,
                            TypeName = column.DataType.FullName,
                            Type     = column.DataType
                        };

                        properties.Add(propInfo.Name, propInfo);
                        _Properties.Add(propInfo);
                    }

                    dataTables.Add(row.Table);
                }
                else if (obj.BaseObject is DataRowView rowView)
                {
                    if (dataTables.Contains(rowView.DataView.Table))
                    {
                        continue;
                    }

                    foreach (DataColumn column in rowView.DataView.Table.Columns)
                    {
                        var propInfo = new PSPropertyInfo()
                        {
                            Name     = column.ColumnName,
                            TypeName = column.DataType.FullName,
                            Type     = column.DataType
                        };

                        properties.Add(propInfo.Name, propInfo);
                        _Properties.Add(propInfo);
                    }

                    dataTables.Add(rowView.DataView.Table);
                }
                else
                {
                    foreach (var property in obj.Properties)
                    {
                        if (property == null || string.IsNullOrWhiteSpace(property.Name) ||
                            !property.IsGettable || string.IsNullOrWhiteSpace(property.TypeNameOfValue))
                        {
                            continue;
                        }

                        if ((property.MemberType & (PSMemberTypes.Property | PSMemberTypes.CodeProperty | PSMemberTypes.ScriptMethod | PSMemberTypes.NoteProperty)) == 0)
                        {
                            continue;
                        }

                        Type propType = null;
                        try
                        {
                            propType = Utils.GetType(property.TypeNameOfValue, true);
                        }
                        catch (Exception)
                        {
                            propType = null;
                        }

                        if (propType == null)
                        {
                            continue;
                        }
                        else if (!properties.ContainsKey(property.Name))
                        {
                            var propInfo = new PSPropertyInfo()
                            {
                                Name     = property.Name,
                                TypeName = property.TypeNameOfValue,
                                Type     = propType
                            };

                            properties.Add(property.Name, propInfo);
                            _Properties.Add(propInfo);
                        }
                        else
                        {
                            var propInfo = properties[property.Name];
                            if (propInfo.Type != propType)
                            {
                                if (IsNumericType(propInfo.Type) && IsNumericType(propType))
                                {
                                    //If both are numeric - use double for column
                                    propInfo.TypeName = "System.Double";
                                    propInfo.Type     = typeof(double);
                                }
                                else if (IsStringType(propInfo.Type) && IsStringType(propType))
                                {
                                    //If both are strings - use string for column
                                    propInfo.TypeName = "System.String";
                                    propInfo.Type     = typeof(string);
                                }
                                else if ((IsNumericType(propInfo.Type) || IsStringType(propInfo.Type)) &&
                                         (IsNumericType(propType) || IsStringType(propType)))
                                {
                                    //Both are either string or number - use string for column
                                    propInfo.TypeName = "System.String";
                                    propInfo.Type     = typeof(string);
                                }
                                else
                                {
                                    //One or both are nor numeric neither string - skip this property
                                    propInfo.TypeName = null;
                                    propInfo.Type     = null;
                                }
                            }
                        }
                    }
                }
            }

            for (int i = _Properties.Count - 1; i >= 0; i--)
            {
                var property = _Properties[i];
                if (property.Type == null)
                {
                    _Properties.RemoveAt(i);
                    continue;
                }

                //Allow only properties of base type
                switch (Type.GetTypeCode(property.Type))
                {
                case TypeCode.Boolean:
                case TypeCode.Char:
                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                case TypeCode.UInt64:
                case TypeCode.Single:
                case TypeCode.Double:
                case TypeCode.Decimal:
                case TypeCode.DateTime:
                case TypeCode.String:
                case TypeCode.Object:
                    break;

                case TypeCode.Empty:
                //case TypeCode.Object:
                case TypeCode.DBNull:
                default:
                    _Properties.RemoveAt(i);
                    break;
                }
            }