public static nHydrate.DataImport.SQLObject ToDatabaseObject(this Entity item) { var retval = new nHydrate.DataImport.Entity(); retval.Name = item.Name; retval.Schema = item.Schema; retval.AllowCreateAudit = item.AllowCreateAudit; retval.AllowModifyAudit = item.AllowModifyAudit; retval.AllowTimestamp = item.AllowTimestamp; retval.IsTenant = item.IsTenant; //Fields foreach (var f in item.Fields) { retval.FieldList.Add(new nHydrate.DataImport.Field() { Collate = f.Collate, DataType = (System.Data.SqlDbType)Enum.Parse(typeof(System.Data.SqlDbType), f.DataType.ToString(), true), DefaultValue = f.Default, Identity = (f.Identity == IdentityTypeConstants.Database), IsIndexed = f.IsIndexed, IsUnique = f.IsUnique, Length = f.Length, Name = f.Name, Nullable = f.Nullable, PrimaryKey = f.IsPrimaryKey, Scale = f.Scale, SortOrder = f.SortOrder, }); } return(retval); }
public static string GetChangedText(this nHydrate.DataImport.Entity item, nHydrate.DataImport.Entity target) { var retval = string.Empty; #region Fields var addedFields = target.FieldList.Where(x => !item.FieldList.Select(z => z.Name).Contains(x.Name)).ToList(); var deletedFields = item.FieldList.Where(x => !target.FieldList.Select(z => z.Name).Contains(x.Name)).ToList(); var commonFields = item.FieldList.Where(x => target.FieldList.Select(z => z.Name).Contains(x.Name)).ToList(); if (addedFields.Count > 0) { retval += "\r\nAdded fields: " + string.Join(", ", addedFields.Select(x => x.Name).OrderBy(x => x).ToList()) + "\r\n"; } if (deletedFields.Count > 0) { retval += "\r\nDeleted fields: " + string.Join(", ", deletedFields.Select(x => x.Name).OrderBy(x => x).ToList()) + "\r\n"; } foreach (var field in commonFields) { var t = field.GetChangedText(target.FieldList.FirstOrDefault(x => x.Name == field.Name)); if (!string.IsNullOrEmpty(t)) { retval += "Changed field (" + field.Name + ")\r\n" + t + "\r\n"; } } #endregion return(retval); }
/// <summary> /// Convert a new DSL model into a data import model /// </summary> /// <param name="model"></param> /// <param name="diagram"></param> /// <returns></returns> public static nHydrate.DataImport.Database Convert(nHydrateModel model, Microsoft.VisualStudio.Modeling.Diagrams.Diagram diagram) { var database = new nHydrate.DataImport.Database(); #region Load the entities foreach (var entity in model.Entities) { var newEntity = new nHydrate.DataImport.Entity(); newEntity.ID = entity.Id; newEntity.Name = entity.Name; newEntity.Schema = entity.Schema; newEntity.AllowCreateAudit = entity.AllowCreateAudit; newEntity.AllowModifyAudit = entity.AllowModifyAudit; newEntity.AllowTimestamp = entity.AllowTimestamp; newEntity.IsTenant = entity.IsTenant; database.EntityList.Add(newEntity); #region Load the fields foreach (var field in entity.Fields) { var newField = new nHydrate.DataImport.Field(); newField.ID = field.Id; newField.Nullable = field.Nullable; newField.DataType = (System.Data.SqlDbType)Enum.Parse(typeof(System.Data.SqlDbType), field.DataType.ToString()); newField.DefaultValue = field.Default; newField.Identity = (field.Identity == IdentityTypeConstants.Database); newField.IsIndexed = field.IsIndexed; newField.IsReadOnly = field.IsReadOnly; newField.Length = field.Length; newField.Name = field.Name; newField.PrimaryKey = field.IsPrimaryKey; newField.Scale = field.Scale; newEntity.FieldList.Add(newField); } #endregion } #endregion #region Load Relations foreach (var shape in diagram.NestedChildShapes) { if (shape is EntityAssociationConnector) { var connector = shape as EntityAssociationConnector; var parent = connector.FromShape.ModelElement as Entity; var child = connector.ToShape.ModelElement as Entity; var relation = connector.ModelElement as EntityHasEntities; var parentTable = database.EntityList.FirstOrDefault(x => parent != null && x.Name == parent.Name); var childTable = database.EntityList.FirstOrDefault(x => child != null && x.Name == child.Name); //If we found both parent and child tables... if (parentTable != null && childTable != null) { var newRelation = new nHydrate.DataImport.Relationship(); newRelation.ID = shape.Id; newRelation.TargetEntity = childTable; newRelation.RoleName = ((EntityHasEntities)connector.ModelElement).RoleName; newRelation.SourceEntity = parentTable; parentTable.RelationshipList.Add(newRelation); //Create the column links var fieldList = model.RelationFields.Where(x => relation != null && x.RelationID == relation.Id); foreach (var columnSet in fieldList) { var field1 = parent.Fields.FirstOrDefault(x => x.Id == columnSet.SourceFieldId); var field2 = child.Fields.FirstOrDefault(x => x.Id == columnSet.TargetFieldId); var column1 = parentTable.FieldList.FirstOrDefault(x => field1 != null && x.Name == field1.Name); var column2 = childTable.FieldList.FirstOrDefault(x => field2 != null && x.Name == field2.Name); newRelation.RelationshipColumnList.Add(new nHydrate.DataImport.RelationshipDetail() { ParentField = column1, ChildField = column2, } ); } } } } #endregion #region Load Views foreach (var view in model.Views) { var newView = new nHydrate.DataImport.View(); newView.ID = view.Id; newView.Name = view.Name; newView.Schema = view.Schema; newView.SQL = view.SQL; database.ViewList.Add(newView); //Load the fields foreach (var field in view.Fields) { var newField = new nHydrate.DataImport.Field(); newField.ID = field.Id; newField.Nullable = field.Nullable; //newField.Collate = field.Collate; newField.DataType = (System.Data.SqlDbType)Enum.Parse(typeof(System.Data.SqlDbType), field.DataType.ToString()); newField.DefaultValue = field.Default; //newField.Identity = (field.Identity == IdentityTypeConstants.Database); //newField.IsIndexed = field.IsIndexed; newField.Length = field.Length; newField.Name = field.Name; //newField.PrimaryKey = field.IsPrimaryKey; newField.Scale = field.Scale; newView.FieldList.Add(newField); } } #endregion return(database); }
/// <summary> /// Convert a new DSL model into a data import model /// </summary> /// <param name="model"></param> /// <param name="diagram"></param> /// <returns></returns> public static nHydrate.DataImport.Database Convert(nHydrateModel model, Microsoft.VisualStudio.Modeling.Diagrams.Diagram diagram) { var database = new nHydrate.DataImport.Database(); database.Collate = model.Collate; #region Load the entities foreach (var entity in model.Entities.Where(x => x.IsGenerated)) { var newEntity = new nHydrate.DataImport.Entity(); newEntity.ID = entity.Id; newEntity.Name = entity.Name; newEntity.Schema = entity.Schema; newEntity.AllowCreateAudit = entity.AllowCreateAudit; newEntity.AllowModifyAudit = entity.AllowModifyAudit; newEntity.AllowTimestamp = entity.AllowTimestamp; newEntity.IsTenant = entity.IsTenant; database.EntityList.Add(newEntity); #region Load the fields foreach (var field in entity.Fields) { var newField = new nHydrate.DataImport.Field(); newField.ID = field.Id; newField.Nullable = field.Nullable; newField.Collate = field.Collate; newField.DataType = (System.Data.SqlDbType)Enum.Parse(typeof(System.Data.SqlDbType), field.DataType.ToString()); newField.DefaultValue = field.Default; newField.Identity = (field.Identity == IdentityTypeConstants.Database); newField.IsIndexed = field.IsIndexed; newField.IsReadOnly = field.IsReadOnly; newField.Length = field.Length; newField.Name = field.Name; newField.PrimaryKey = field.IsPrimaryKey; newField.Scale = field.Scale; newField.IsBrowsable = field.IsBrowsable; newEntity.FieldList.Add(newField); } #endregion } #endregion #region Load Relations foreach (var shape in diagram.NestedChildShapes) { if (shape is EntityAssociationConnector) { var connector = shape as EntityAssociationConnector; var parent = connector.FromShape.ModelElement as Entity; var child = connector.ToShape.ModelElement as Entity; var relation = connector.ModelElement as EntityHasEntities; var parentTable = database.EntityList.FirstOrDefault(x => parent != null && x.Name == parent.Name); var childTable = database.EntityList.FirstOrDefault(x => child != null && x.Name == child.Name); //If we found both parent and child tables... if (parentTable != null && childTable != null) { var newRelation = new nHydrate.DataImport.Relationship(); newRelation.ID = shape.Id; newRelation.TargetEntity = childTable; newRelation.RoleName = ((EntityHasEntities)connector.ModelElement).RoleName; newRelation.SourceEntity = parentTable; parentTable.RelationshipList.Add(newRelation); //Create the column links var fieldList = model.RelationFields.Where(x => relation != null && x.RelationID == relation.Id); foreach (var columnSet in fieldList) { var field1 = parent.Fields.FirstOrDefault(x => x.Id == columnSet.SourceFieldId); var field2 = child.Fields.FirstOrDefault(x => x.Id == columnSet.TargetFieldId); var column1 = parentTable.FieldList.FirstOrDefault(x => field1 != null && x.Name == field1.Name); var column2 = childTable.FieldList.FirstOrDefault(x => field2 != null && x.Name == field2.Name); newRelation.RelationshipColumnList.Add(new nHydrate.DataImport.RelationshipDetail() { ParentField = column1, ChildField = column2, } ); } } } } #endregion #region Load Stored Procedures foreach (var storedProc in model.StoredProcedures.Where(x => x.IsGenerated)) { var newStoredProc = new nHydrate.DataImport.StoredProc(); newStoredProc.ID = storedProc.Id; newStoredProc.Name = storedProc.Name; newStoredProc.Schema = storedProc.Schema; newStoredProc.SQL = storedProc.SQL; database.StoredProcList.Add(newStoredProc); //Load the fields foreach (var field in storedProc.Fields) { var newField = new nHydrate.DataImport.Field(); newField.ID = field.Id; newField.Nullable = field.Nullable; //newField.Collate = field.Collate; newField.DataType = (System.Data.SqlDbType)Enum.Parse(typeof(System.Data.SqlDbType), field.DataType.ToString()); newField.DefaultValue = field.Default; //newField.Identity = (field.Identity == IdentityTypeConstants.Database); //newField.IsIndexed = field.IsIndexed; newField.Length = field.Length; newField.Name = field.Name; //newField.PrimaryKey = field.IsPrimaryKey; newField.Scale = field.Scale; newStoredProc.FieldList.Add(newField); } //Load the parameters foreach (var parameter in storedProc.Parameters) { var newParameter = new nHydrate.DataImport.Parameter(); newParameter.ID = parameter.Id; newParameter.Nullable = parameter.Nullable; //newField.Collate = field.Collate; newParameter.DataType = (System.Data.SqlDbType)Enum.Parse(typeof(System.Data.SqlDbType), parameter.DataType.ToString()); newParameter.DefaultValue = parameter.Default; //newField.Identity = (field.Identity == IdentityTypeConstants.Database); //newField.IsIndexed = field.IsIndexed; newParameter.Length = parameter.Length; newParameter.Name = parameter.Name; //newField.PrimaryKey = field.IsPrimaryKey; newParameter.Scale = parameter.Scale; newStoredProc.ParameterList.Add(newParameter); } } #endregion #region Load Views foreach (var view in model.Views.Where(x => x.IsGenerated)) { var newView = new nHydrate.DataImport.View(); newView.ID = view.Id; newView.Name = view.Name; newView.Schema = view.Schema; newView.SQL = view.SQL; database.ViewList.Add(newView); //Load the fields foreach (var field in view.Fields) { var newField = new nHydrate.DataImport.Field(); newField.ID = field.Id; newField.Nullable = field.Nullable; //newField.Collate = field.Collate; newField.DataType = (System.Data.SqlDbType)Enum.Parse(typeof(System.Data.SqlDbType), field.DataType.ToString()); newField.DefaultValue = field.Default; //newField.Identity = (field.Identity == IdentityTypeConstants.Database); //newField.IsIndexed = field.IsIndexed; newField.Length = field.Length; newField.Name = field.Name; //newField.PrimaryKey = field.IsPrimaryKey; newField.Scale = field.Scale; newView.FieldList.Add(newField); } } #endregion #region Load Functions foreach (var function in model.Functions.Where(x => x.IsGenerated)) { var newFunction = new nHydrate.DataImport.Function(); newFunction.ID = function.Id; newFunction.Name = function.Name; newFunction.Schema = function.Schema; newFunction.SQL = function.SQL; database.FunctionList.Add(newFunction); //Load the fields foreach (var field in function.Fields) { var newField = new nHydrate.DataImport.Field(); newField.ID = field.Id; newField.Nullable = field.Nullable; //newField.Collate = field.Collate; newField.DataType = (System.Data.SqlDbType)Enum.Parse(typeof(System.Data.SqlDbType), field.DataType.ToString()); newField.DefaultValue = field.Default; //newField.Identity = (field.Identity == IdentityTypeConstants.Database); //newField.IsIndexed = field.IsIndexed; newField.Length = field.Length; newField.Name = field.Name; //newField.PrimaryKey = field.IsPrimaryKey; newField.Scale = field.Scale; newFunction.FieldList.Add(newField); } //Load the parameters foreach (var parameter in function.Parameters) { var newParameter = new nHydrate.DataImport.Parameter(); newParameter.ID = parameter.Id; newParameter.Nullable = parameter.Nullable; //newField.Collate = field.Collate; newParameter.DataType = (System.Data.SqlDbType)Enum.Parse(typeof(System.Data.SqlDbType), parameter.DataType.ToString()); newParameter.DefaultValue = parameter.Default; //newField.Identity = (field.Identity == IdentityTypeConstants.Database); //newField.IsIndexed = field.IsIndexed; newParameter.Length = parameter.Length; newParameter.Name = parameter.Name; //newField.PrimaryKey = field.IsPrimaryKey; newParameter.Scale = parameter.Scale; newFunction.ParameterList.Add(newParameter); } } #endregion return database; }