Skip to content

Commit

Permalink
Stop using mapper for SqlServer columns
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Willey committed Feb 2, 2017
1 parent 2f39d6b commit bde4bd8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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<DatabaseColumn>
{
private readonly string _tableName;
private readonly ColumnRowConverter _converter;

public Columns(string owner, string tableName)
{
Expand All @@ -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<DatabaseColumn> Execute(DbConnection connection)
Expand All @@ -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[] { ' ', '\'', '=' });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<DatabaseColumn>
{
private readonly string _viewName;
private readonly ColumnRowConverter _converter;

public ViewColumns(string owner, string viewName)
{
Expand All @@ -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<DatabaseColumn> Execute(DbConnection connection)
Expand All @@ -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);
}
}
Expand Down

0 comments on commit bde4bd8

Please sign in to comment.