GetColumnsFullHierarchy() public method

This gets all columns from this and all base classes
public GetColumnsFullHierarchy ( ) : ColumnCollection
return ColumnCollection
            public static string GetBody(Table table, ModelRoot model)
            {
                try
                {
                    var allColumns = new List<Column>();
                    foreach (var column in table.GetColumnsFullHierarchy().Where(x => x.Generated).OrderBy(x => x.Name))
                    {
                        if (!(column.DataType == System.Data.SqlDbType.Binary ||
                            column.DataType == System.Data.SqlDbType.Image ||
                            column.DataType == System.Data.SqlDbType.NText ||
                            column.DataType == System.Data.SqlDbType.Text ||
                            column.DataType == System.Data.SqlDbType.Timestamp ||
                            column.DataType == System.Data.SqlDbType.Udt ||
                            column.DataType == System.Data.SqlDbType.VarBinary ||
                            column.DataType == System.Data.SqlDbType.Variant ||
                        column.DataType == System.Data.SqlDbType.Money))
                        {
                            allColumns.Add(column);
                        }
                    }

                    if (allColumns.Count != 0)
                    {
                        return BuildStoredProcedure(table, model, allColumns);
                    }

                    return "";

                }
                catch (Exception ex)
                {
                    throw new Exception(table.DatabaseName + ": Failed on generation of paging select statement", ex);
                }
            }
        private void AppendTableClass(Table table)
        {
            try
            {
                sb.AppendLine("	#region " + table.PascalName + "Query");
                sb.AppendLine();

                sb.AppendLine("	/// <summary>");
                sb.AppendLine("	/// This is a helper object for running LINQ queries on the " + table.PascalName + " collection.");
                sb.AppendLine("	/// </summary>");
                sb.AppendLine("	[Serializable]");
                if (table.IsTenant)
                    sb.AppendLine("	[Table(Name = \"" + _model.TenantPrefix + "_" + table.DatabaseName + "\")]");
                else
                    sb.AppendLine("	[Table(Name = \"" + table.DatabaseName + "\")]");
                sb.AppendLine("	[System.CodeDom.Compiler.GeneratedCode(\"nHydrateModelGenerator\", \"" + _model.ModelToolVersion + "\")]");
                sb.AppendLine("	public partial class " + table.PascalName + "Query : IBusinessObjectLINQQuery");
                sb.AppendLine("	{");

                sb.AppendLine("		#region Properties");
                var allTables = table.GetTableHierarchy();

                var columnList = table.GetColumnsFullHierarchy().Where(x => x.Generated).ToList();
                foreach (var c in columnList.OrderBy(x => x.Name))
                {
                    var description = c.Description.Trim();
                    if (!string.IsNullOrEmpty(description)) description += "\r\n";
                    description += "(Maps to the '" + table.DatabaseName + "." + c.DatabaseName + "' database field)";

                    sb.AppendLine("		/// <summary>");
                    StringHelper.LineBreakCode(sb, description, "		/// ");
                    sb.AppendLine("		/// </summary>");
                    sb.Append("		[Column(");
                    sb.Append("Name = \"" + c.DatabaseName + "\", ");
                    sb.Append("DbType = \"" + c.DatabaseTypeRaw + "\", ");
                    sb.Append("CanBeNull = " + c.AllowNull.ToString().ToLower() + ", ");
                    sb.Append("IsPrimaryKey = " + table.PrimaryKeyColumns.Contains(c).ToString().ToLower());
                    sb.AppendLine(")]");
                    sb.AppendLine("		[System.Diagnostics.DebuggerNonUserCode()]");
                    sb.AppendLine("		public virtual " + c.GetCodeType(true) + " " + c.PascalName + " { get; set; }");
                }

                if (table.AllowCreateAudit)
                {
                    sb.AppendLine("		/// <summary>");
                    sb.AppendLine("		/// The date of creation");
                    sb.AppendLine("		/// </summary>");
                    sb.AppendLine("		[Column(Name = \"" + _model.Database.CreatedDateColumnName + "\", DbType = \"DateTime\", CanBeNull = true)]");
                    sb.AppendLine("		[System.Diagnostics.DebuggerNonUserCode()]");
                    sb.AppendLine("		public virtual DateTime? " + _model.Database.CreatedDatePascalName + " { get; set; }");

                    sb.AppendLine("		/// <summary>");
                    sb.AppendLine("		/// The name of the creating entity");
                    sb.AppendLine("		/// </summary>");
                    sb.AppendLine("		[Column(Name = \"" + _model.Database.CreatedByColumnName + "\", DbType = \"VarChar(100)\", CanBeNull = true)]");
                    sb.AppendLine("		[System.Diagnostics.DebuggerNonUserCode()]");
                    sb.AppendLine("		public virtual string " + _model.Database.CreatedByPascalName + " { get; set; }");
                }

                if (table.AllowModifiedAudit)
                {
                    sb.AppendLine("		/// <summary>");
                    sb.AppendLine("		/// The date of last modification");
                    sb.AppendLine("		/// </summary>");
                    sb.AppendLine("		[Column(Name = \"" + _model.Database.ModifiedDateColumnName + "\", DbType = \"DateTime\", CanBeNull = true)]");
                    sb.AppendLine("		public virtual DateTime? " + _model.Database.ModifiedDatePascalName + " { get; set; }");

                    sb.AppendLine("		/// <summary>");
                    sb.AppendLine("		/// The name of the last modifing entity");
                    sb.AppendLine("		/// </summary>");
                    sb.AppendLine("		[Column(Name = \"" + _model.Database.ModifiedByColumnName + "\", DbType = \"VarChar(100)\", CanBeNull = true)]");
                    sb.AppendLine("		[System.Diagnostics.DebuggerNonUserCode()]");
                    sb.AppendLine("		public virtual string " + _model.Database.ModifiedByPascalName + " { get; set; }");
                }

                if (table.AllowTimestamp)
                {
                    sb.AppendLine("		/// <summary>");
                    sb.AppendLine("		/// This is an internal field and is not to be used.");
                    sb.AppendLine("		/// </summary>");
                    sb.AppendLine("		[Column(Name = \"" + _model.Database.TimestampColumnName + "\", DbType = \"Binary\", CanBeNull = false)]");
                    sb.AppendLine("		[System.Diagnostics.DebuggerNonUserCode()]");
                    sb.AppendLine("		public virtual byte[] " + _model.Database.TimestampPascalName + " { get; set; }");
                }

                ////Add child relationships
                //foreach (var relation in _model.Database.Relations.FindByParentTable(table, true).Where(x => x.IsGenerated))
                //{
                //    //Relation relation = (Relation)reference.Object;
                //    var parentTable = (Table)relation.ParentTableRef.Object;
                //    var childTable = (Table)relation.ChildTableRef.Object;
                //    //Column pkColumn = (Column)relation.ColumnRelationships[0].ChildColumnRef.Object;

                //    var thisKey = string.Empty;
                //    var otherKey = string.Empty;
                //    foreach (var columnRelationship in relation.ColumnRelationships.AsEnumerable())
                //    {
                //        thisKey += ((Column)columnRelationship.ParentColumnRef.Object).PascalName + ",";
                //        otherKey += ((Column)columnRelationship.ChildColumnRef.Object).PascalName + ",";
                //    }
                //    if (!string.IsNullOrEmpty(thisKey)) thisKey = thisKey.Substring(0, thisKey.Length - 1);
                //    if (!string.IsNullOrEmpty(otherKey)) otherKey = otherKey.Substring(0, otherKey.Length - 1);

                //    if (childTable.Generated & (childTable.TypedTable != TypedTableConstants.EnumOnly) && (!allTables.Contains(childTable)))
                //    {
                //        sb.AppendLine("		/// <summary>");
                //        sb.AppendLine("		/// This is a mapping of the relationship with the " + childTable.PascalName + " entity." + (relation.PascalRoleName == "" ? "" : " (Role: '" + relation.RoleName + "')"));
                //        sb.AppendLine("		/// </summary>");
                //        sb.AppendLine("		[Association(ThisKey = \"" + thisKey + "\", OtherKey = \"" + otherKey + "\")]");
                //        if (relation.IsOneToOne)
                //            sb.AppendLine("		public " + this.GetLocalNamespace() + "." + childTable.PascalName + "Query " + relation.PascalRoleName + childTable.PascalName + " { get; private set; }");
                //        else
                //            sb.AppendLine("		public " + this.GetLocalNamespace() + "." + childTable.PascalName + "Query " + relation.PascalRoleName + childTable.PascalName + "List { get; private set; }");
                //        sb.AppendLine();
                //    }

                //}

                //Add parent relationships
                foreach (var relation in _model.Database.Relations.FindByChildTable(table, true).Where(x => x.IsGenerated))
                {
                    var parentTable = (Table)relation.ParentTableRef.Object;
                    var childTable = (Table)relation.ChildTableRef.Object;

                    //Do not process self-referencing relationships
                    if (parentTable != table)
                    {
                        var thisKey = string.Empty;
                        var otherKey = string.Empty;
                        foreach (ColumnRelationship columnRelationship in relation.ColumnRelationships)
                        {
                            thisKey += ((Column)columnRelationship.ChildColumnRef.Object).PascalName + ",";
                            otherKey += ((Column)columnRelationship.ParentColumnRef.Object).PascalName + ",";
                        }
                        if (!string.IsNullOrEmpty(thisKey)) thisKey = thisKey.Substring(0, thisKey.Length - 1);
                        if (!string.IsNullOrEmpty(otherKey)) otherKey = otherKey.Substring(0, otherKey.Length - 1);

                        if (parentTable.Generated && (parentTable.TypedTable != TypedTableConstants.EnumOnly) && (!allTables.Contains(parentTable)))
                        {
                            sb.AppendLine("		/// <summary>");
                            sb.AppendLine("		/// This is a mapping of the relationship with the " + parentTable.PascalName + " entity." + (relation.PascalRoleName == "" ? "" : " (Role: '" + relation.RoleName + "')"));
                            sb.AppendLine("		/// </summary>");
                            sb.AppendLine("		[Association(ThisKey = \"" + thisKey + "\", OtherKey = \"" + otherKey + "\")]");
                            sb.AppendLine("		public " + this.GetLocalNamespace() + "." + parentTable.PascalName + "Query " + relation.PascalRoleName + parentTable.PascalName + " { get; private set; }");
                            sb.AppendLine();
                        }

                    }
                }

                sb.AppendLine();
                sb.AppendLine("		#endregion");
                sb.AppendLine();
                sb.AppendLine("	}");
                sb.AppendLine();

                sb.AppendLine("	#endregion");
                sb.AppendLine();

            }
            catch (Exception ex)
            {
                throw;
            }
        }
        private string BuildParameterList(Table table, ModelRoot model)
        {
            var output = new StringBuilder();
            var columnList = table.GetColumnsFullHierarchy().Where(x => x.Generated && !x.ComputedColumn && !x.IsReadOnly).OrderBy(x => x.Name).AsEnumerable();
            var index = 0;
            foreach (var column in columnList)
            {
                //Get the default value and make it null if none exists
                var defaultValue = column.GetSQLDefault();
                if (string.IsNullOrEmpty(defaultValue) && column.AllowNull)
                    defaultValue = "null";
                if (string.IsNullOrEmpty(defaultValue) && column.PrimaryKey && (column.ParentTable.GetBasePKColumn(column).Identity == IdentityTypeConstants.Database))
                    defaultValue = "null";
                if (string.IsNullOrEmpty(defaultValue) && (column.Identity == IdentityTypeConstants.Database))
                    defaultValue = "null";

                //if there is a value then add the '=' sign to it for SQL
                if (!string.IsNullOrEmpty(defaultValue))
                    defaultValue = " = " + defaultValue;

                output.Append("	@" + column.ToDatabaseCodeIdentifier() + " " + column.GetSQLDefaultType() + defaultValue);
                if (index < columnList.Count() - 1 || (table.AllowCreateAudit) || (table.AllowModifiedAudit))
                    output.Append(",");
                output.AppendLine();
                index++;
            }

            if (table.AllowCreateAudit)
            {
                //Create Date
                output.AppendFormat("	@{0} [DateTime] = null", model.Database.CreatedDateColumnName);
                output.Append(",");
                output.AppendLine();

                //Create By
                output.AppendFormat("	@{0} [NVarchar] (50) = null", model.Database.CreatedByColumnName);
                if (table.AllowModifiedAudit)
                    output.Append(",");
                output.AppendLine();
            }

            if (table.AllowModifiedAudit)
            {
                if (_model.EFVersion == EFVersionConstants.EF6)
                {
                    //Modified Date - This is a placeholder parameter for EF6 runtime. The Stored Procedure body will overwrite this value.
                    output.AppendFormat("	@{0} [DateTime] = null,", model.Database.ModifiedDateColumnName);
                    output.AppendLine("--Entity Framework 6 Required Modified Date be passed in");
                }

                //Modified By
                output.AppendFormat("	@{0} [NVarchar] (50) = null", model.Database.ModifiedByColumnName);
                output.AppendLine();
            }

            return output.ToString();
        }
Beispiel #4
0
        public static string BuildSelectList(Table table, ModelRoot model, bool useFullHierarchy)
        {
            var index = 0;
            var output = new StringBuilder();
            var columnList = new List<Column>();
            if (useFullHierarchy)
            {
                foreach (var c in table.GetColumnsFullHierarchy().Where(x => x.Generated).OrderBy(x => x.Name))
                    columnList.Add(c);
            }
            else
            {
                columnList.AddRange(table.GeneratedColumns);
            }

            foreach (var column in columnList.OrderBy(x => x.Name))
            {
                var parentTable = column.ParentTable;
                output.AppendFormat("\t[{2}].[{0}].[{1}]", GetTableDatabaseName(model, parentTable), column.DatabaseName, parentTable.GetSQLSchema());
                if ((index < columnList.Count - 1) || (table.AllowCreateAudit) || (table.AllowModifiedAudit) || (table.AllowTimestamp))
                    output.Append(",");
                output.AppendLine();
                index++;
            }

            if (table.AllowCreateAudit)
            {
                output.AppendFormat("	[{2}].[{0}].[{1}],", GetTableDatabaseName(model, table), model.Database.CreatedByColumnName, table.GetSQLSchema());
                output.AppendLine();

                output.AppendFormat("	[{2}].[{0}].[{1}]", GetTableDatabaseName(model, table), model.Database.CreatedDateColumnName, table.GetSQLSchema());
                if ((table.AllowModifiedAudit) || (table.AllowTimestamp))
                    output.Append(",");
                output.AppendLine();
            }

            if (table.AllowModifiedAudit)
            {
                output.AppendFormat("	[{2}].[{0}].[{1}],", GetTableDatabaseName(model, table), model.Database.ModifiedByColumnName, table.GetSQLSchema());
                output.AppendLine();

                output.AppendFormat("	[{2}].[{0}].[{1}]", GetTableDatabaseName(model, table), model.Database.ModifiedDateColumnName, table.GetSQLSchema());
                if (table.AllowTimestamp)
                    output.Append(",");
                output.AppendLine();
            }

            if (table.AllowTimestamp)
            {
                output.AppendFormat("	[{2}].[{0}].[{1}]", GetTableDatabaseName(model, table.GetAbsoluteBaseTable()), model.Database.TimestampColumnName, table.GetAbsoluteBaseTable().GetSQLSchema());
                output.AppendLine();
            }

            return output.ToString();
        }