public static void LoadInitialIndexes(nHydrateModel modelRoot) { //Setup primary keys foreach (var entity in modelRoot.Entities) { if (entity.Indexes.Count(x => x.IndexType == IndexTypeConstants.PrimaryKey) == 0 && entity.PrimaryKeyFields.Count > 0) { var newIndex = new Index(entity.Partition); newIndex.ParentEntityID = entity.Id; newIndex.IndexType = IndexTypeConstants.PrimaryKey; newIndex.Clustered = true; entity.Indexes.Add(newIndex); foreach (var field in entity.PrimaryKeyFields) { var newColumn = new IndexColumn(field.Partition); newColumn.FieldID = field.Id; newColumn.IsInternal = true; newIndex.IndexColumns.Add(newColumn); } } } var allIndexedField = modelRoot.Entities.SelectMany(x => x.Fields).Where(x => x.IsIndexed && !x.IsPrimaryKey); var allIndexes = modelRoot.Entities.SelectMany(x => x.Indexes); foreach (var field in allIndexedField) { var index = allIndexes.FirstOrDefault(x => x.IndexColumns.Count == 1 && x.IndexColumns.First().FieldID == field.Id && x.IndexColumns.First().Ascending); if (index == null) { var newIndex = new Index(modelRoot.Partition); newIndex.ParentEntityID = field.Entity.Id; newIndex.IndexType = IndexTypeConstants.IsIndexed; field.Entity.Indexes.Add(newIndex); var newColumn = new IndexColumn(modelRoot.Partition); newColumn.FieldID = field.Id; newColumn.IsInternal = true; newIndex.IndexColumns.Add(newColumn); } } }
protected override void OnChildConfiguring(Microsoft.VisualStudio.Modeling.Diagrams.ShapeElement child, bool createdDuringViewFixup) { base.OnChildConfiguring(child, createdDuringViewFixup); try { if (!this.IsLoading) { //Add a default field to entities if (child.ModelElement is Entity) { var item = child.ModelElement as Entity; var model = item.nHydrateModel; if (item.Fields.Count == 0) { var field = new Field(item.Partition) { DataType = DataTypeConstants.Int, Identity = IdentityTypeConstants.Database, Name = "ID", }; item.Fields.Add(field); //Add then set PK so it will trigger index code field.IsPrimaryKey = true; } #region Pasting //If there are invalid indexes then try to remap them foreach (var index in item.Indexes.Where(x => x.FieldList.Any(z => z == null))) { foreach (var c in index.IndexColumns.Where(x => x.Field == null && x.FieldID != Guid.Empty)) { var f = model.Entities.SelectMany(x => x.Fields).FirstOrDefault(x => x.Id == c.FieldID); if (f != null) { var f2 = item.Fields.FirstOrDefault(x => x.Name == f.Name); if (f2 != null) c.FieldID = f2.Id; } } } //Add a PK index if not one if (!item.Indexes.Any(x => x.IndexType == IndexTypeConstants.PrimaryKey) && item.PrimaryKeyFields.Count > 0) { var index = new Index(item.Partition) { IndexType = IndexTypeConstants.PrimaryKey }; item.Indexes.Add(index); var loop = 0; foreach (var field in item.PrimaryKeyFields) { var newIndexColumn = new IndexColumn(item.Partition); index.IndexColumns.Add(newIndexColumn); newIndexColumn.FieldID = field.Id; newIndexColumn.SortOrder = loop; loop++; } } #endregion } else if (child.ModelElement is View) { var item = child.ModelElement as View; //if ((item.nHydrateModel.DiagramVisibility & VisibilityTypeConstants.View) != VisibilityTypeConstants.View) //{ // if (MessageBox.Show("This type of object cannot be created by dragging onto the diagram because it is not visualized on the diagram. You can change the 'DiagramVisibility' property of the model to visualize it. Otherwise you can only create this object type by using the model tree window.\n\nWould you like to toggle on visualization for this object?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information) != DialogResult.Yes) // { // child.Delete(); // return; // } // item.nHydrateModel.DiagramVisibility = (item.nHydrateModel.DiagramVisibility | VisibilityTypeConstants.View); //} if (item.Fields.Count == 0) { var field = new ViewField(item.Partition) { DataType = DataTypeConstants.Int, Name = "Field1", }; item.Fields.Add(field); } } else if (child.ModelElement is StoredProcedure) { var item = child.ModelElement as StoredProcedure; //if ((item.nHydrateModel.DiagramVisibility & VisibilityTypeConstants.StoredProcedure) != VisibilityTypeConstants.StoredProcedure) //{ // if (MessageBox.Show("This type of object cannot be created by dragging onto the diagram because it is not visualized on the diagram. You can change the 'DiagramVisibility' property of the model to visualize it. Otherwise you can only create this object type by using the model tree window.\n\nWould you like to toggle on visualization for this object?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information) != DialogResult.Yes) // { // child.Delete(); // return; // } // item.nHydrateModel.DiagramVisibility = (item.nHydrateModel.DiagramVisibility | VisibilityTypeConstants.StoredProcedure); //} if (item.Fields.Count == 0) { var field = new StoredProcedureField(item.Partition) { DataType = DataTypeConstants.Int, Name = "Field1", }; item.Fields.Add(field); } if (item.Parameters.Count == 0) { var field = new StoredProcedureParameter(item.Partition) { DataType = DataTypeConstants.Int, Name = "Parameter1", }; item.Parameters.Add(field); } } else if (child.ModelElement is Function) { var item = child.ModelElement as Function; //if ((item.nHydrateModel.DiagramVisibility & VisibilityTypeConstants.Function) != VisibilityTypeConstants.Function) //{ // if (MessageBox.Show("This type of object cannot be created by dragging onto the diagram because it is not visualized on the diagram. You can change the 'DiagramVisibility' property of the model to visualize it. Otherwise you can only create this object type by using the model tree window.\n\nWould you like to toggle on visualization for this object?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information) != DialogResult.Yes) // { // child.Delete(); // return; // } // item.nHydrateModel.DiagramVisibility = (item.nHydrateModel.DiagramVisibility | VisibilityTypeConstants.Function); //} if (item.Fields.Count == 0) { var field = new FunctionField(item.Partition) { DataType = DataTypeConstants.Int, Name = "Field1", }; item.Fields.Add(field); } if (item.Parameters.Count == 0) { var field = new FunctionParameter(item.Partition) { DataType = DataTypeConstants.Int, Name = "Parameter1", }; item.Parameters.Add(field); } } this.OnShapeConfiguring(new ModelElementEventArgs() { Shape = child }); } } catch (Exception ex) { throw; } }
protected override void OnValueChanged(FieldBase element, bool oldValue, bool newValue) { base.OnValueChanged(element, oldValue, newValue); try { //Do not process if we are already doing something //if (element.Store.TransactionManager.CurrentTransaction != null) //{ // if (element.Store.TransactionManager.CurrentTransaction.HasPendingChanges) return; //} if (element.Entity != null && element.Entity.nHydrateModel != null && !element.Entity.nHydrateModel.IsLoading) { //Processes Index list if (element.IsPrimaryKey) //Must use real property since there is logic there { //This is a PK so determine if there is a key for this and add this field to a new or the existing index var existing = element.Entity.Indexes.FirstOrDefault(x => x.IndexType == IndexTypeConstants.PrimaryKey); if (existing == null) { //The PK index does not exist so create one using (var transaction = element.Store.TransactionManager.BeginTransaction(Guid.NewGuid().ToString())) { var newIndex = new Index(element.Partition); newIndex.ParentEntityID = element.Entity.Id; newIndex.Clustered = true; element.Entity.Indexes.Add(newIndex); var newColumn = new IndexColumn(element.Partition); newColumn.FieldID = element.Id; newIndex.IndexColumns.Add(newColumn); newColumn.IsInternal = true; newIndex.IndexType = IndexTypeConstants.PrimaryKey; //Do this last //If use modules then add the PK to all modules that contain this entity if (element.Entity.nHydrateModel.UseModules) { foreach (var module in element.Modules) { element.Entity.nHydrateModel.IndexModules.Add(new IndexModule(element.Entity.nHydrateModel.Partition) { IndexID = newIndex.Id, ModuleId = module.Id }); } } transaction.Commit(); } } else { //The PK does exist so add this field to it using (var transaction = element.Store.TransactionManager.BeginTransaction(Guid.NewGuid().ToString())) { if (existing.IndexColumns.Count(x => x.FieldID == element.Id) == 0) { existing.IndexType = IndexTypeConstants.User; var newColumn = new IndexColumn(element.Partition); newColumn.FieldID = element.Id; newColumn.IsInternal = true; existing.IndexColumns.Add(newColumn); //Just in case there are invalid fields existing.IndexColumns.Remove(x => x.GetField() == null); existing.IndexType = IndexTypeConstants.PrimaryKey; //Do this last transaction.Commit(); } } } //Remove the IsIndex ones if exist Func<Index, bool> where = x => x.IndexType == IndexTypeConstants.IsIndexed && x.IndexColumns.Count == 1 && x.IndexColumns.First().FieldID == element.Id && x.IndexType == IndexTypeConstants.IsIndexed; element.Entity.Indexes.Where(where).ToList().ForEach(x => x.IndexType = IndexTypeConstants.User); element.Entity.Indexes.Remove(where); } else //Remove Index { var existing = element.Entity.Indexes.FirstOrDefault(x => x.IndexType == IndexTypeConstants.PrimaryKey); if (existing != null) { using (var transaction = element.Store.TransactionManager.BeginTransaction(Guid.NewGuid().ToString())) { existing.IndexType = IndexTypeConstants.User; existing.IndexColumns.Remove(x => x.FieldID == element.Id); if (element.Entity.Fields.Count(x => x.IsPrimaryKey) == 0) //No more primary keys element.Entity.Indexes.Remove(existing); else existing.IndexType = IndexTypeConstants.PrimaryKey; //If use modules then add the PK to all modules that contain this entity if (element.Entity.nHydrateModel.UseModules && existing.FieldList.Count == 0) { foreach (var module in element.Modules) { element.Entity.nHydrateModel.IndexModules.Remove(x => (x.IndexID == existing.Id) && (x.ModuleId == module.Id)); } } transaction.Commit(); } } } } } catch (Exception ex) { throw; } }
protected override void OnValueChanged(FieldBase element, bool oldValue, bool newValue) { base.OnValueChanged(element, oldValue, newValue); try { if (element.Entity != null && element.Entity.nHydrateModel != null && !element.Entity.nHydrateModel.IsLoading) { //Processes Index list if (newValue) //element.IsIndexed //Must use real property since there is logic there { //Add an Asc single field index var existing = element.Entity.Indexes.FirstOrDefault(x => x.IndexColumns.Count == 1 && x.IndexColumns.First().FieldID == element.Id && x.IndexColumns.First().Ascending && x.IndexType == IndexTypeConstants.IsIndexed); if (existing == null) { using (var transaction = element.Store.TransactionManager.BeginTransaction(Guid.NewGuid().ToString())) { var newIndex = new Index(element.Partition); newIndex.ParentEntityID = element.Entity.Id; newIndex.IndexType = IndexTypeConstants.IsIndexed; newIndex.Clustered = false; element.Entity.Indexes.Add(newIndex); var newColumn = new IndexColumn(element.Partition); newColumn.FieldID = element.Id; newColumn.IsInternal = true; newIndex.IndexColumns.Add(newColumn); transaction.Commit(); } } } else //Remove Index { var existingList = element.Entity.Indexes .Where(x => x.IndexType == IndexTypeConstants.IsIndexed && x.IndexColumns.Count == 1 && x.IndexColumns.First().FieldID == element.Id && x.IndexType == IndexTypeConstants.IsIndexed) .ToList(); using (var transaction = element.Store.TransactionManager.BeginTransaction(Guid.NewGuid().ToString())) { while (existingList.Count > 0) { var item = existingList.First(); item.IndexType = IndexTypeConstants.User; element.Entity.Indexes.Remove(item); existingList.Remove(item); } transaction.Commit(); } } } } catch (Exception ex) { throw; } }