From bde4bd84752d87d4beaa7722054280265409cb67 Mon Sep 17 00:00:00 2001 From: Martin Willey Date: Thu, 2 Feb 2017 20:19:55 +0100 Subject: [PATCH] Stop using mapper for SqlServer columns --- .../Databases/SqlServer/Columns.cs | 38 +++++++++++++++---- .../Databases/SqlServer/ViewColumns.cs | 8 +--- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/DatabaseSchemaReader/ProviderSchemaReaders/Databases/SqlServer/Columns.cs b/DatabaseSchemaReader/ProviderSchemaReaders/Databases/SqlServer/Columns.cs index 03001a1a..c14c949f 100644 --- a/DatabaseSchemaReader/ProviderSchemaReaders/Databases/SqlServer/Columns.cs +++ b/DatabaseSchemaReader/ProviderSchemaReaders/Databases/SqlServer/Columns.cs @@ -1,16 +1,15 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Data; using System.Data.Common; +using System.Globalization; using DatabaseSchemaReader.DataSchema; -using DatabaseSchemaReader.ProviderSchemaReaders.Converters.KeyMaps; -using DatabaseSchemaReader.ProviderSchemaReaders.Converters.RowConverters; namespace DatabaseSchemaReader.ProviderSchemaReaders.Databases.SqlServer { internal class Columns : SqlExecuter { private readonly string _tableName; - private readonly ColumnRowConverter _converter; public Columns(string owner, string tableName) { @@ -37,9 +36,6 @@ JOIN INFORMATION_SCHEMA.TABLES t TABLE_TYPE = 'BASE TABLE' order by c.TABLE_SCHEMA, c.TABLE_NAME, ORDINAL_POSITION"; - - var keyMap = new ColumnsKeyMap(); - _converter = new ColumnRowConverter(keyMap); } public IList Execute(DbConnection connection) @@ -56,8 +52,34 @@ protected override void AddParameters(DbCommand command) protected override void Mapper(IDataRecord record) { - var col = _converter.Convert(record); + var col = Convert(record); Result.Add(col); } + + public static DatabaseColumn Convert(IDataRecord row) + { + var column = new DatabaseColumn + { + Name = row["COLUMN_NAME"].ToString(), + TableName = row["TABLE_NAME"].ToString(), + SchemaOwner = row["TABLE_SCHEMA"].ToString(), + Ordinal = System.Convert.ToInt32(row["ORDINAL_POSITION"], CultureInfo.CurrentCulture), + DbDataType = row["DATA_TYPE"].ToString(), + Nullable = row.GetBoolean("IS_NULLABLE"), + Length = row.GetNullableInt("CHARACTER_MAXIMUM_LENGTH"), + Precision = row.GetNullableInt("NUMERIC_PRECISION"), + Scale = row.GetNullableInt("NUMERIC_SCALE"), + DateTimePrecision = row.GetNullableInt("DATETIME_PRECISION") + }; + AddColumnDefault(row, "COLUMN_DEFAULT", column); + + return column; + } + private static void AddColumnDefault(IDataRecord row, string defaultKey, DatabaseColumn column) + { + if (string.IsNullOrEmpty(defaultKey)) return; + string d = row[defaultKey].ToString(); + if (!string.IsNullOrEmpty(d)) column.DefaultValue = d.Trim(new[] { ' ', '\'', '=' }); + } } } diff --git a/DatabaseSchemaReader/ProviderSchemaReaders/Databases/SqlServer/ViewColumns.cs b/DatabaseSchemaReader/ProviderSchemaReaders/Databases/SqlServer/ViewColumns.cs index 7740ad8c..b8d42a80 100644 --- a/DatabaseSchemaReader/ProviderSchemaReaders/Databases/SqlServer/ViewColumns.cs +++ b/DatabaseSchemaReader/ProviderSchemaReaders/Databases/SqlServer/ViewColumns.cs @@ -2,15 +2,12 @@ using System.Data; using System.Data.Common; using DatabaseSchemaReader.DataSchema; -using DatabaseSchemaReader.ProviderSchemaReaders.Converters.KeyMaps; -using DatabaseSchemaReader.ProviderSchemaReaders.Converters.RowConverters; namespace DatabaseSchemaReader.ProviderSchemaReaders.Databases.SqlServer { internal class ViewColumns : SqlExecuter { private readonly string _viewName; - private readonly ColumnRowConverter _converter; public ViewColumns(string owner, string viewName) { @@ -36,9 +33,6 @@ JOIN INFORMATION_SCHEMA.VIEWS v (c.TABLE_NAME = @TableName or (@TableName is null)) order by c.TABLE_SCHEMA, c.TABLE_NAME, ORDINAL_POSITION"; - - var keyMap = new ColumnsKeyMap(); - _converter = new ColumnRowConverter(keyMap); } public IList Execute(DbConnection connection) @@ -55,7 +49,7 @@ protected override void AddParameters(DbCommand command) protected override void Mapper(IDataRecord record) { - var col = _converter.Convert(record); + var col = Columns.Convert(record); Result.Add(col); } }