Example #1
0
        internal void LoadLayout(RowLayout layout)
        {
            if (Mode == SearchMode.And || Mode == SearchMode.Or || Mode == SearchMode.None)
            {
                return;
            }

            if (Layout != null)
            {
                if (ReferenceEquals(layout, Layout))
                {
                    return;
                }
            }

            Layout = layout ?? throw new ArgumentNullException(nameof(layout));
            if (FieldName == null)
            {
                throw new InvalidOperationException($"Property {nameof(FieldName)} has to be set!");
            }
            FieldNumber     = layout.GetFieldIndex(FieldName, true);
            FieldProperties = layout[FieldNumber];

            if (Mode == SearchMode.In)
            {
                var result = new Set <object>();
                foreach (var value in (Set <object>)FieldValue)
                {
                    result.Add(ConvertValue(value));
                }

                FieldValue = result;
            }
            else if (Mode == SearchMode.Like)
            {
                // Do nothing
            }
            else
            {
                if ((FieldValue != null) && (FieldProperties.ValueType != FieldValue.GetType()))
                {
                    FieldValue = ConvertValue(FieldValue);
                }
            }
        }
Example #2
0
        /// <summary>Initializes a new instance of the <see cref="CsvReader" /> class.</summary>
        /// <param name="properties">Properties to apply to the reader.</param>
        /// <param name="layout">Layout to use when reading the csv data.</param>
        /// <param name="stream">Stream to read data from.</param>
        /// <param name="closeBaseStream">if set to <c>true</c> [close base stream on close].</param>
        public CsvReader(RowLayout layout, Stream stream, CsvProperties properties = default, bool closeBaseStream = false)
        {
            Layout          = layout;
            BaseStream      = stream ?? throw new ArgumentNullException(nameof(stream));
            Properties      = properties.Valid ? properties : CsvProperties.Default;
            CloseBaseStream = closeBaseStream;
            reader          = new DataReader(stream, Properties.Encoding, Properties.NewLineMode);
            if (!Properties.NoHeader)
            {
                var header = reader.ReadLine();
                currentRowNumber++;
                var fields = header.Split(Properties.Separator);
                if (!Properties.AllowFieldMatching)
                {
                    if (fields.Length != Layout.FieldCount)
                    {
                        if ((fields.Length - 1) != Layout.FieldCount)
                        {
                            throw new InvalidDataException($"Invalid header fieldcount (expected '{Layout.FieldCount}' got '{fields.Length}')!");
                        }
                    }
                }
                else
                {
                    if (fields.Length != Layout.FieldCount)
                    {
                        fieldNumberMatching = new int[Layout.FieldCount];
                    }
                }

                var count = Math.Min(Layout.FieldCount, fields.Length);
                for (var i = 0; i < count; i++)
                {
                    var fieldName  = fields[i].UnboxText(false);
                    var fieldIndex = Layout.GetFieldIndex(fieldName, false);
                    if (fieldIndex < 0)
                    {
                        throw new InvalidDataException(
                                  $"Error loading CSV Header! Got field name '{fieldName}' instead of '{Layout[i].Name}' at type '{Layout.Name}'");
                    }

                    if (!Properties.AllowFieldMatching)
                    {
                        if (fieldIndex != i)
                        {
                            throw new InvalidDataException($"Fieldposition of Field '{fieldName}' does not match!");
                        }

                        if (!string.Equals(Layout[fieldIndex].Name, fieldName))
                        {
                            throw new InvalidDataException(
                                      $"Invalid header value at field number '{i}' name '{fieldName}' expected '{Layout[fieldIndex].Name}'!");
                        }
                    }
                    else
                    {
                        if ((fieldNumberMatching == null) && (fieldIndex != i))
                        {
                            fieldNumberMatching = new int[Layout.FieldCount];
                        }
                    }
                }

                if (fieldNumberMatching != null)
                {
                    var i = 0;
                    for (; i < count; i++)
                    {
                        var fieldName = fields[i].UnboxText(false);
                        fieldNumberMatching[i] = Layout.GetFieldIndex(fieldName, false);
                    }

                    for (; i < Layout.FieldCount; i++)
                    {
                        fieldNumberMatching[i] = -1;
                    }
                }
            }
        }
Example #3
0
 /// <summary>Gets the content of the field with the specified <paramref name="fieldName" />.</summary>
 /// <param name="fieldName">Name of the field.</param>
 /// <returns>Returns a value or null.</returns>
 public object this[string fieldName] => Values[Layout.GetFieldIndex(fieldName, true)];