1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 using System.Collections.Generic;
7 namespace System.Data.Common
10 internal class DataRowDbColumn : DbColumn
12 private DataColumnCollection schemaColumns;
13 private DataRow schemaRow;
15 public DataRowDbColumn(DataRow readerSchemaRow, DataColumnCollection readerSchemaColumns)
17 this.schemaRow = readerSchemaRow;
18 this.schemaColumns = readerSchemaColumns;
22 private void populateFields()
24 AllowDBNull = GetDbColumnValue<bool?>(SchemaTableColumn.AllowDBNull);
25 BaseCatalogName = GetDbColumnValue<string>(SchemaTableOptionalColumn.BaseCatalogName);
26 BaseColumnName = GetDbColumnValue<string>(SchemaTableColumn.BaseColumnName);
27 BaseSchemaName = GetDbColumnValue<string>(SchemaTableColumn.BaseSchemaName);
28 BaseServerName = GetDbColumnValue<string>(SchemaTableOptionalColumn.BaseServerName);
29 BaseTableName = GetDbColumnValue<string>(SchemaTableColumn.BaseTableName);
30 ColumnName = GetDbColumnValue<string>(SchemaTableColumn.ColumnName);
31 ColumnOrdinal = GetDbColumnValue<int?>(SchemaTableColumn.ColumnOrdinal);
32 ColumnSize = GetDbColumnValue<int?>(SchemaTableColumn.ColumnSize);
33 IsAliased = GetDbColumnValue<bool?>(SchemaTableColumn.IsAliased);
34 IsAutoIncrement = GetDbColumnValue<bool?>(SchemaTableOptionalColumn.IsAutoIncrement);
35 IsExpression = GetDbColumnValue<bool>(SchemaTableColumn.IsExpression);
36 IsHidden = GetDbColumnValue<bool?>(SchemaTableOptionalColumn.IsHidden);
37 IsIdentity = GetDbColumnValue<bool?>("IsIdentity");
38 IsKey = GetDbColumnValue<bool?>(SchemaTableColumn.IsKey);
39 IsLong = GetDbColumnValue<bool?>(SchemaTableColumn.IsLong);
40 IsReadOnly = GetDbColumnValue<bool?>(SchemaTableOptionalColumn.IsReadOnly);
41 IsUnique = GetDbColumnValue<bool?>(SchemaTableColumn.IsUnique);
42 NumericPrecision = GetDbColumnValue<int?>(SchemaTableColumn.NumericPrecision);
43 NumericScale = GetDbColumnValue<int?>(SchemaTableColumn.NumericScale);
44 UdtAssemblyQualifiedName = GetDbColumnValue<string>("UdtAssemblyQualifiedName");
45 DataType = GetDbColumnValue<Type>(SchemaTableColumn.DataType);
46 DataTypeName = GetDbColumnValue<string>("DataTypeName");
49 private T GetDbColumnValue<T>(string columnName)
51 if (!schemaColumns.Contains(columnName))
55 object schemaObject = schemaRow[columnName];
56 if (schemaObject is T)
58 return (T)schemaObject;
64 public static class DbDataReaderExtensions
66 public static System.Collections.ObjectModel.ReadOnlyCollection<DbColumn> GetColumnSchema(this DbDataReader reader)
68 IList<DbColumn> columnSchema = new List<DbColumn>();
69 DataTable schemaTable = reader.GetSchemaTable();
70 DataColumnCollection schemaTableColumns = schemaTable.Columns;
71 foreach (DataRow row in schemaTable.Rows)
73 DbColumn dbColumn = new DataRowDbColumn(row, schemaTableColumns);
74 columnSchema.Add(dbColumn);
76 System.Collections.ObjectModel.ReadOnlyCollection<DbColumn> readOnlyColumnSchema = new System.Collections.ObjectModel.ReadOnlyCollection<DbColumn>(columnSchema);
77 return readOnlyColumnSchema;
80 public static bool CanGetColumnSchema(this DbDataReader reader)