From 058f89a7a15197e894b592f12015e8ae2795e32b Mon Sep 17 00:00:00 2001 From: Nick Franceschina Date: Mon, 31 Dec 2018 16:18:00 +0700 Subject: [PATCH] adding null check for particular case of computed view columns for which type cannot be determined. also adding IHasColumns interface that both Table and View implement, so it's possible to more easily use a single routine for generating a class for either --- src/SqlSharpener/Model/Column.cs | 2 +- src/SqlSharpener/Model/IHasColumns.cs | 30 +++++++++++++++++++++++++++ src/SqlSharpener/Model/Table.cs | 2 +- src/SqlSharpener/Model/View.cs | 2 +- src/SqlSharpener/SqlSharpener.csproj | 1 + 5 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 src/SqlSharpener/Model/IHasColumns.cs diff --git a/src/SqlSharpener/Model/Column.cs b/src/SqlSharpener/Model/Column.cs index d081a88..d80b3ff 100644 --- a/src/SqlSharpener/Model/Column.cs +++ b/src/SqlSharpener/Model/Column.cs @@ -216,7 +216,7 @@ from r in t.Value private void SetProperties(dac.TSqlObject tSqlObject) { - var sqlDataTypeName = tSqlObject.GetReferenced(dac.Column.DataType).ToList().First().Name.Parts.Last(); + var sqlDataTypeName = tSqlObject.GetReferenced(dac.Column.DataType).ToList().FirstOrDefault()?.Name.Parts.Last(); this.DataTypes = DataTypeHelper.Instance.GetMap(TypeFormat.SqlServerDbType, sqlDataTypeName); this.IsIdentity = dac.Column.IsIdentity.GetValue(tSqlObject); this.IsNullable = dac.Column.Nullable.GetValue(tSqlObject); diff --git a/src/SqlSharpener/Model/IHasColumns.cs b/src/SqlSharpener/Model/IHasColumns.cs new file mode 100644 index 0000000..33bae7e --- /dev/null +++ b/src/SqlSharpener/Model/IHasColumns.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSharpener.Model +{ + /// + /// Represents a table or view in the model. + /// + public interface IHasColumns + { + /// + /// Gets the name of the table or view. + /// + /// + /// The name of the table or view. + /// + string Name { get; } + + /// + /// Gets the columns. + /// + /// + /// The columns. + /// + IEnumerable Columns { get; } + } +} diff --git a/src/SqlSharpener/Model/Table.cs b/src/SqlSharpener/Model/Table.cs index 599f00c..a571ae6 100644 --- a/src/SqlSharpener/Model/Table.cs +++ b/src/SqlSharpener/Model/Table.cs @@ -13,7 +13,7 @@ namespace SqlSharpener.Model /// Represents a table in the model. /// [Serializable] - public class Table + public class Table : IHasColumns { /// /// Initializes a new instance of the class. diff --git a/src/SqlSharpener/Model/View.cs b/src/SqlSharpener/Model/View.cs index 056453e..16fafcc 100644 --- a/src/SqlSharpener/Model/View.cs +++ b/src/SqlSharpener/Model/View.cs @@ -12,7 +12,7 @@ namespace SqlSharpener.Model /// Represents a view in the model. /// [Serializable] - public class View + public class View : IHasColumns { /// /// Initializes a new instance of the class. diff --git a/src/SqlSharpener/SqlSharpener.csproj b/src/SqlSharpener/SqlSharpener.csproj index d87187e..945df28 100644 --- a/src/SqlSharpener/SqlSharpener.csproj +++ b/src/SqlSharpener/SqlSharpener.csproj @@ -78,6 +78,7 @@ +