Exemplo n.º 1
0
        public Retrieval <T> Include <TProperty>(Expression <Func <T, TProperty> > propertyExpr)
        {
            var propertyName = PropertyNameAttribute.GetFromType(propertyExpr);

            if (propertyName == "modifiedon" || propertyName == "createdon" || propertyName == "id")
            {
                return(this);
            }

            if (!IncludedColumns.Contains(propertyName.ToLower()))
            {
                IncludedColumns.Add(propertyName);
            }

            return(this);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the column names
        /// </summary>
        /// <remarks>Because DbfExporter is forward-reading, calling this method multiple times or calling it after <see cref="GetRowFields"/> will throw an exception.</remarks>
        /// <returns>Returns the columns of a Dbf as a <see cref="IEnumerable{T}"/> where T is a <see cref="string"/></returns>
        public IEnumerable <string> GetColumns()
        {
            if (FieldCount == 0)
            {
                ReadHeader();
            }
            if (Columns.Count > 0)
            {
                throw new Exception("Column names have already been read");
            }
            ColumnRenames   = ColumnRenames.ToDictionary(d => d.Key.ToUpper(), d => d.Value);
            IncludedColumns = IncludedColumns.Select(s => s.ToUpper()).ToList();
            bool          addColumn   = IncludedColumns.Count == 0;
            List <string> columnNames = new List <string>();

            byte[] columnHeader = new byte[FieldArraySize];
            DbfFile.Read(columnHeader, 0, FieldArraySize);
            int    baseOffset;
            int    numericDecimal;
            int    columnOffset = 0;
            string pgColumnType;

            for (int i = 0; i < FieldCount; ++i)
            {
                baseOffset = i * FieldDefinitionSize;
                byte[] nameBytes = new byte[FieldNameLength];
                Array.Copy(columnHeader, baseOffset, nameBytes, 0, FieldNameLength);
                string columnName = Encoding.ASCII.GetString(nameBytes).Replace("\0", string.Empty);
                if (ColumnRenames.ContainsKey(columnName))
                {
                    columnName = ColumnRenames[columnName];
                }
                if (ReservedWords.Contains(columnName))
                {
                    int increment = 0;
                    while (IncludedColumns.Contains($"{columnName}_{++increment}"))
                    {
                        ;
                    }
                    columnName = $"{columnName}_{increment}";
                }
                if (addColumn)
                {
                    IncludedColumns.Add(columnName);
                }
                var       dbfColumnType = Convert.ToChar(columnHeader[baseOffset + FieldTypeOffset]);
                DbfColumn column        = dbfColumnType == 'M' ? new MemoColumn() : new DbfColumn();
                column.Export = IncludedColumns.Contains(columnName) || addColumn;
                column.Type   = dbfColumnType;
                column.Length = columnHeader[baseOffset + FieldLengthOffset];
                column.Offset = columnOffset;
                columnOffset += column.Length;
                Columns.Add(column);
                if (CreateTable)
                {
                    pgColumnType = "TEXT";
                    if (column.Type == 'N' || column.Type == 'F')
                    {
                        if (ConvertNumericToText)
                        {
                            pgColumnType = "TEXT";
                        }
                        else
                        {
                            numericDecimal = columnHeader[baseOffset + FieldDecimalsOffset];
                            pgColumnType   = numericDecimal == 0 ?
                                             $"NUMERIC({column.Length})" :
                                             $"NUMERIC({column.Length},{numericDecimal})";
                        }
                    }
                    else if (column.Type == 'L')
                    {
                        if (ConvertBoolToVarChar)
                        {
                            column.Type  = 'C';
                            pgColumnType = "VARCHAR(1)";
                        }
                        else
                        {
                            pgColumnType = "BOOLEAN";
                        }
                    }
                    else if (column.Type == 'M')
                    {
                        pgColumnType = "TEXT";
                    }
                    else
                    {
                        pgColumnType = $"VARCHAR({column.Length})";
                    }
                    if (column.Export)
                    {
                        columnNames.Add(columnName + " " + pgColumnType);
                    }
                }
                else if (column.Export)
                {
                    columnNames.Add(columnName);
                }
            }
            InitMemoFile();
            DbfFile.Seek(SkipBytes + 1, SeekOrigin.Current);
            return(columnNames);
        }