/// <summary> /// Generates a column list from a data reader. /// </summary> /// <param name="dr"></param> internal void DetectColumns(IDataReader dr) { var dt = dr.GetSchemaTable(); DataFileColumn[] cols; if (this.Columns.Count == dt.Rows.Count) { cols = Columns.ToArray(); // *** TODO verify type mismatch, or update types // keep column name and format } else { cols = new DataFileColumn[dt.Rows.Count]; for (int i = 0; i < dt.Rows.Count; i++) { cols[i] = new DataFileColumn(); TypeUtil.CopyColumnFromSchemaTableRow(cols[i], dt.Rows[i]); } } CreateColumns(cols); }
/// <summary> /// Returns a delegate for formatting the column /// </summary> /// <param name="column"></param> /// <returns></returns> /// <remarks> /// Characters and strings have to be quoted as may contain the separator character /// </remarks> protected override FormattedDataFileBlock.FormatterDelegate GetFormatterDelegate(DataFileColumn column) { var t = column.DataType.Type; if (t == typeof(Char)) { return delegate(object o, string f) { return File.Quote + ((string)o).Replace(File.Quote.ToString(), File.Quote.ToString() + File.Quote) + File.Quote; }; } else if (t == typeof(String)) { return delegate(object o, string f) { // TODO: maybe do some optimization here return File.Quote + ((string)o).Replace(File.Quote.ToString(), File.Quote.ToString() + File.Quote) + File.Quote; }; } else { return base.GetFormatterDelegate(column); } }
public DataFileColumn(DataFileColumn old) { CopyMembers(old); }
private void CopyMembers(DataFileColumn old) { this.id = old.id; this.name = old.name; this.dataType = old.dataType; this.metadata = new DataFileColumnMetadata(old.metadata); this.isNullable = old.isNullable; this.isIdentity = old.isIdentity; this.isKey = old.isKey; this.isHidden = old.isHidden; }
/// <summary> /// Detects columns from a set of values. /// </summary> /// <param name="parts"></param> /// <param name="useNames"></param> /// <param name="cols"></param> /// <param name="colRanks"></param> /// <remarks> /// If it is the first line and useName is true, it will use them as column names, /// otherwise parts are only counted, columns are created for each and automatically generated /// names are used. /// </remarks> protected void DetectColumnsFromParts(string[] parts, bool useNames, out DataFileColumn[] cols, out int[] colRanks) { cols = new DataFileColumn[parts.Length]; colRanks = new int[parts.Length]; for (int i = 0; i < cols.Length; i++) { cols[i] = new DataFileColumn(); if (useNames) { cols[i].Name = parts[i].Trim(); } else { cols[i].Name = String.Format("Col{0}", i); // *** TODO: use constants } } }
private void AddSchemaTableColumn(DataTable dt, DataFileColumn col, int ordinal) { var dr = dt.NewRow(); dr[SchemaTableColumn.ColumnName] = col.Name; dr[SchemaTableColumn.ColumnOrdinal] = ordinal; dr[SchemaTableColumn.ColumnSize] = col.DataType.Length; dr[SchemaTableColumn.NumericPrecision] = col.DataType.Precision; dr[SchemaTableColumn.NumericScale] = col.DataType.Scale; dr[SchemaTableColumn.IsUnique] = col.IsIdentity; dr[SchemaTableColumn.IsKey] = col.IsIdentity; dr[SchemaTableColumn.DataType] = col.DataType.Type; dr[SchemaTableColumn.AllowDBNull] = col.IsNullable; dr[SchemaTableColumn.ProviderType] = col.DataType.Name; dr[SchemaTableColumn.IsAliased] = false; dr[SchemaTableColumn.IsExpression] = false; //dr[SchemaTableOptionalColumn.IsIdentity] = col.IsIdentity; dr[SchemaTableOptionalColumn.IsAutoIncrement] = col.IsIdentity; dr[SchemaTableOptionalColumn.IsRowVersion] = false; dr[SchemaTableOptionalColumn.IsHidden] = false; dr[SchemaTableColumn.IsLong] = col.DataType.IsMaxLength; dr[SchemaTableOptionalColumn.IsReadOnly] = true; dr[SchemaTableOptionalColumn.ProviderSpecificDataType] = col.DataType.Name; dt.Rows.Add(dr); }
/// <summary> /// Call this function to set column list from derived classes /// </summary> /// <param name="columns"></param> protected void CreateColumns(DataFileColumn[] columns) { this.columns.Clear(); if (file.FileMode == DataFileMode.Read && file.GenerateIdentityColumn) { var col = new DataFileColumn("__ID", DataType.SqlBigInt); // *** TODO col.IsIdentity = true; this.columns.Add(col); } this.columns.AddRange(columns); OnColumnsCreated(); }
private ParserDelegate GetParserDelegate(DataFileColumn column) { var t = column.DataType.Type; if (t == typeof(SByte)) { return delegate(string s, out object p) { SByte v; var r = SByte.TryParse(s, File.NumberStyle, File.Culture, out v); p = v; return r; }; } else if (t == typeof(Int16)) { return delegate(string s, out object p) { Int16 v; var r = Int16.TryParse(s, File.NumberStyle, File.Culture, out v); p = v; return r; }; } else if (t == typeof(Int32)) { return delegate(string s, out object p) { Int32 v; var r = Int32.TryParse(s, File.NumberStyle, File.Culture, out v); p = v; return r; }; } else if (t == typeof(Int64)) { return delegate(string s, out object p) { Int64 v; var r = Int64.TryParse(s, File.NumberStyle, File.Culture, out v); p = v; return r; }; } else if (t == typeof(Byte)) { return delegate(string s, out object p) { Byte v; var r = Byte.TryParse(s, File.NumberStyle, File.Culture, out v); p = v; return r; }; } else if (t == typeof(UInt16)) { return delegate(string s, out object p) { UInt16 v; var r = UInt16.TryParse(s, File.NumberStyle, File.Culture, out v); p = v; return r; }; } else if (t == typeof(UInt32)) { return delegate(string s, out object p) { UInt32 v; var r = UInt32.TryParse(s, File.NumberStyle, File.Culture, out v); p = v; return r; }; } else if (t == typeof(UInt64)) { return delegate(string s, out object p) { UInt64 v; var r = UInt64.TryParse(s, File.NumberStyle, File.Culture, out v); p = v; return r; }; } else if (t == typeof(Boolean)) { // *** TODO: parse numbers! return delegate(string s, out object p) { bool v; var r = bool.TryParse(s, out v); p = v; return r; }; } else if (t == typeof(Single)) { return delegate(string s, out object p) { float v; var r = float.TryParse(s, File.NumberStyle, File.Culture, out v); p = v; return r; }; } else if (t == typeof(Double)) { return delegate(string s, out object p) { double v; var r = double.TryParse(s, File.NumberStyle, File.Culture, out v); p = v; return r; }; } else if (t == typeof(Decimal)) { return delegate(string s, out object p) { decimal v; var r = decimal.TryParse(s, File.NumberStyle, File.Culture, out v); p = v; return r; }; } else if (t == typeof(Char)) { return delegate(string s, out object p) { if (s.Length == 1) { p = s[0]; return true; } else { p = null; return false; } }; } else if (t == typeof(String)) { return delegate(string s, out object p) { p = s; return true; }; } else if (t == typeof(DateTime)) { return delegate(string s, out object p) { DateTime v; var r = DateTime.TryParse(s, File.Culture, File.DateTimeStyle, out v); p = v; return r; }; } else if (t == typeof(Guid)) { return delegate(string s, out object p) { Guid v; var r = Guid.TryParse(s, out v); p = v; return r; }; } throw new NotImplementedException(); }
protected virtual FormatterDelegate GetFormatterDelegate(DataFileColumn column) { var t = column.DataType.Type; if (t == typeof(SByte)) { return delegate(object o, string f) { return String.Format(File.Culture, f, (SByte)o); }; } else if (t == typeof(Int16)) { return delegate(object o, string f) { return String.Format(File.Culture, f, (Int16)o); }; } else if (t == typeof(Int32)) { return delegate(object o, string f) { return String.Format(File.Culture, f, (Int32)o); }; } else if (t == typeof(Int64)) { return delegate(object o, string f) { return String.Format(File.Culture, f, (Int64)o); }; } else if (t == typeof(Byte)) { return delegate(object o, string f) { return String.Format(File.Culture, f, (Byte)o); }; } else if (t == typeof(UInt16)) { return delegate(object o, string f) { return String.Format(File.Culture, f, (UInt16)o); }; } else if (t == typeof(UInt32)) { return delegate(object o, string f) { return String.Format(File.Culture, f, (UInt32)o); }; } else if (t == typeof(UInt64)) { return delegate(object o, string f) { return String.Format(File.Culture, f, (UInt64)o); }; } else if (t == typeof(Boolean)) { // TODO: use numbers? return delegate(object o, string f) { return String.Format(File.Culture, f, (bool)o); }; } else if (t == typeof(Single)) { return delegate(object o, string f) { return String.Format(File.Culture, f, (float)o); }; } else if (t == typeof(Double)) { return delegate(object o, string f) { return String.Format(File.Culture, f, (double)o); }; } else if (t == typeof(Decimal)) { return delegate(object o, string f) { return String.Format(File.Culture, f, (decimal)o); }; } else if (t == typeof(Char)) { return delegate(object o, string f) { return String.Format(File.Culture, f, (string)o); }; } else if (t == typeof(String)) { return delegate(object o, string f) { return (string)o; }; } else if (t == typeof(DateTime)) { return delegate(object o, string f) { return String.Format(File.Culture, f, (DateTime)o); }; } else if (t == typeof(Guid)) { return delegate(object o, string f) { return String.Format(File.Culture, f, (Guid)o); }; } throw new NotImplementedException(); }
/// <summary> /// Detect column types from a set of values /// </summary> /// <param name="parts"></param> /// <param name="cols"></param> /// <param name="colranks"></param> protected void DetectColumnTypes(string[] parts, DataFileColumn[] cols, int[] colranks) { for (int i = 0; i < cols.Length; i++) { Type type; int size, rank; if (!GetBestColumnTypeEstimate(parts[i], out type, out size, out rank)) { cols[i].IsNullable = true; } if (cols[i].DataType == null || colranks[i] < rank) { cols[i].DataType = DataType.Create(type, (short)size); } // Make column longer if necessary if (cols[i].DataType.HasLength && cols[i].DataType.Length < size) { cols[i].DataType.Length = (short)size; } } }