Exemple #1
0
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            if (e.LeftButton == MouseButtonState.Pressed)
            {
                try
                {
                    DataObject data = new DataObject();

                    DatabaseRelation dragData = new DatabaseRelation
                    {
                        RelationType = this.relationTypeObject
                    };

                    data.SetData(DataFormats.StringFormat, "Relation");

                    data.SetData(dragData);

                    DragDrop.DoDragDrop(this, data, DragDropEffects.Copy | DragDropEffects.Move);
                }
                catch (Exception err)
                {
                    // DO NOHTING: This will break if the relationsships table is not on the same canvas
                }
            }
        }
        public void InitializeColumnRemations()
        {
            this.ColumnRelationModel = new Dictionary <string, DatabaseRelation>();

            Dictionary <string, ColumnRelationMapModel[]> foreignKeys = this.Table.Columns
                                                                        .Where(c => c.IsForeignkey)
                                                                        .GroupBy(t => t.ForeignKeyTable)
                                                                        .ToDictionary(d => d.Key, d => d
                                                                                      .Select(a => new ColumnRelationMapModel
            {
                ParentTable           = a.ForeignKeyTable,
                ParentColumn          = a.ForeignKeyColumn,
                ChildTable            = this.Table.TableName,
                ChildColumn           = a.ColumnName,
                ForeignConstraintName = a.ForeignConstraintName,
                RelationTypes         = a.IsVertualRelation ? RelationTypesEnum.VirtualRelation : RelationTypesEnum.DatabaseRelation
            }).ToArray());

            foreach (KeyValuePair <string, ColumnRelationMapModel[]> item in foreignKeys)
            {
                DatabaseRelation relation = new DatabaseRelation
                {
                    RelationshipName = Guid.NewGuid().ToString().Replace('-', '_'),
                    ChildTable       = this.Table.TableName,
                    ParentTable      = item.Key,
                    Columns          = new List <ColumnRelationMapModel>(item.Value),
                    RelationType     = item.Value[0].RelationTypes
                };

                string dictionaryKey = $"{item.Key}||{this.Table.TableName}";

                this.ColumnRelationModel.Add(dictionaryKey, relation);
            }
        }
        private void AddNewRelation(DatabaseRelation relation)
        {
            RelationEditor editor = new RelationEditor(relation);

            bool?result = editor.ShowDialog();

            if (!result.HasValue || !result.Value)
            {
                return;
            }

            relation.DatabaseRelationDelete += this.DatabaseRelation_Delete;

            UIElement tableControl = this.FindVisualControls(typeof(TableObject)).FirstOrDefault(t => ((TableObject)t).Table.TableName == editor.DatabaseRelation.ChildTable);

            UIElement parentTableControl = this.FindVisualControls(typeof(TableObject)).FirstOrDefault(t => ((TableObject)t).Table.TableName == editor.DatabaseRelation.ParentTable);

            UIElement[] tableControls = this.FindVisualControls(typeof(TableObject));

            if (tableControl != null)
            {
                TableObject childTable = (TableObject)tableControl;

                string dictionaryKey = $"{editor.DatabaseRelation.ParentTable}||{editor.DatabaseRelation.ChildTable}";

                childTable.ColumnRelationModel.Add(dictionaryKey, editor.DatabaseRelation);

                this.columnRelationModel.Add(dictionaryKey, editor.DatabaseRelation);

                this.DrawRelation((TableObject)parentTableControl, tableControls, new KeyValuePair <string, DatabaseRelation>(dictionaryKey, editor.DatabaseRelation));
            }
        }
        private void DatabaseRelation_Delete(DatabaseRelation sender)
        {
            try
            {
                Integrity.RemoveForeighKey(sender);

                string dictionaryKey = $"{sender.ParentTable}||{sender.ChildTable}";

                UIElement tableControl = this.FindVisualControls(typeof(TableObject)).FirstOrDefault(t => ((TableObject)t).Table.TableName == sender.ChildTable);

                UIElement parentTableControl = this.FindVisualControls(typeof(TableObject)).FirstOrDefault(t => ((TableObject)t).Table.TableName == sender.ParentTable);

                if (tableControl != null)
                {
                    TableObject childTable = (TableObject)tableControl;

                    childTable.ColumnRelationModel.Remove(dictionaryKey);

                    childTable.LinkedRelations.Remove(dictionaryKey);
                }

                if (parentTableControl != null)
                {
                    TableObject childTable = (TableObject)parentTableControl;

                    childTable.ColumnRelationModel.Remove(dictionaryKey);

                    childTable.LinkedRelations.Remove(dictionaryKey);
                }

                foreach (Path relationPath in this.FindVisualControls(typeof(Path)).Where(p => ((Path)p).Name.EndsWith(sender.RelationshipName)))
                {
                    this.Children.Remove(relationPath);
                }

                this.columnRelationModel.Remove(dictionaryKey);

                this.CanvasChanged?.Invoke(this, sender);

                sender.Dispose();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.InnerExceptionMessage());
            }
        }
        private void RedrawTableRelations(TableObject table)
        {
            List <KeyValuePair <string, DatabaseRelation> > changedRelations = new List <KeyValuePair <string, DatabaseRelation> >();

            foreach (string mapedRelation in table.LinkedRelations)
            {
                if (!this.columnRelationModel.ContainsKey(mapedRelation))
                {
                    continue;
                }

                DatabaseRelation relationKeyValue = this.columnRelationModel[mapedRelation];

                foreach (Path relationPath in this.FindVisualControls(typeof(Path)).Where(p => ((Path)p).Name.EndsWith(relationKeyValue.RelationshipName)))
                {
                    this.Children.Remove(relationPath);
                }

                changedRelations.Add(new KeyValuePair <string, DatabaseRelation>(mapedRelation, relationKeyValue));
            }

            UIElement[] tableControls = this.FindVisualControls(typeof(TableObject));

            foreach (KeyValuePair <string, DatabaseRelation> relation in changedRelations)
            {
                string[] tablesUsed = relation.Key.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);

                UIElement parentTable = tableControls.FirstOrDefault(to => ((TableObject)to).Table.TableName == tablesUsed[0]);

                if (parentTable == null)
                {
                    this.DrawStubRelation(table, relation);

                    continue;
                }

                this.DrawRelation((TableObject)parentTable, tableControls, relation);
            }

            table.InvalidateVisual();
        }
Exemple #6
0
        public static void RemoveForeighKey(DatabaseRelation relation)
        {
            foreach (ColumnRelationMapModel mappedColumn in relation.Columns)
            {
                string columnKey = $"{mappedColumn.ChildTable.ToLower()}||{mappedColumn.ChildColumn.ToLower()}";

                ColumnObjectModel column = Integrity.columnObjectModels[columnKey];

                string constraintKey = $"{mappedColumn.ChildTable}||{column.ForeignConstraintName}";

                if (!Integrity.DropRelations.Contains(constraintKey))
                {
                    Integrity.DropRelations.Add(constraintKey);
                }

                column.ForeignConstraintName = string.Empty;

                column.ForeignKeyColumn = string.Empty;

                column.ForeignKeyTable = string.Empty;

                column.IsForeignkey = false;
            }
        }
Exemple #7
0
        public static ITable ToSchemaTable(this Type type, IDataProvider provider)
        {
            string tableName = type.Name;

            /*
             * 修 改 人:Empty(AllEmpty)
             * QQ    群:327360708
             * 博客地址:http://www.cnblogs.com/EmptyFS/
             * 修改时间:2013-07-27
             * 修改说明:去除生成的数据表对应名称自动加复数(s)功能以及由此产生的异常
             *********************************************/

            //复数处理,即如果当前表名为指定的字符串相匹配,则在名称的后面加s,做复数处理
            //tableName = tableName.MakePlural();
            /*********************************************/

            var result = new DatabaseTable(tableName, provider);

            result.ClassName = type.Name;

            var typeAttributes = type.GetCustomAttributes(typeof(IClassMappingAttribute), false);

            foreach (IClassMappingAttribute attr in typeAttributes)
            {
                if (attr.Accept(result))
                {
                    attr.Apply(result);
                }
            }

            var props = type.GetProperties();

            foreach (var prop in props)
            {
                var attributes = prop.GetCustomAttributes(false);
                if (ColumnIsIgnored(attributes))
                {
                    continue;
                }

                if (CanGenerateSchemaFor(prop.PropertyType))
                {
                    var column = new DatabaseColumn(prop.Name, result);
                    column.PropertyName = prop.Name;
                    bool isNullable = prop.PropertyType.Name.Contains("Nullable");

                    column.DataType = IdentifyColumnDataType(prop.PropertyType, isNullable);

                    if (column.DataType == DbType.Decimal || column.DataType == DbType.Double)
                    {
                        //default to most common;
                        column.NumberScale      = 2;
                        column.NumericPrecision = 10;
                    }
                    else if (column.DataType == DbType.String)
                    {
                        column.MaxLength = 255;
                    }
                    else if (column.DataType == DbType.Binary)
                    {
                        isNullable = true;
                    }

                    if (isNullable)
                    {
                        column.IsNullable = true;
                    }

                    // Now work with attributes
                    foreach (IPropertyMappingAttribute attr in attributes.Where(x => x is IPropertyMappingAttribute))
                    {
                        /*
                         *
                         * 修改时间:2014-05-12
                         * 修改说明:标识主键列
                         *********************************************/
                        //当主键的自定义属性设置了[SubSonicPrimaryKeyAttribute]时,则认为该列为表中的主键
                        if (attr.GetType().Name == "SubSonicPrimaryKeyAttribute")
                        {
                            column.IsPrimaryKey = true;
                        }

                        if (attr.Accept(column))
                        {
                            attr.Apply(column);
                        }
                    }

                    result.Columns.Add(column);
                }
                else if (IsRelationColumm(attributes))
                {
                    if (!IsPropertyDeclaredVirtual(prop))
                    {
                        throw new InvalidOperationException(String.Format(
                                                                "Property {0} of type {1} marked as relation must be declared virtual to allow dynamic proxy mechanisms work correctly!",
                                                                prop.Name, prop.DeclaringType.Name));
                    }

                    var innerProp = prop;

                    result.AddRelation(() =>
                    {
                        var relation = new DatabaseRelation(innerProp.Name, result);

                        foreach (IRelationMappingAttribute attr in attributes.Where(x => x is IRelationMappingAttribute))
                        {
                            if (attr.Accept(relation, innerProp))
                            {
                                attr.Apply(relation, innerProp);
                            }
                        }

                        return(relation);
                    });
                }
            }

            //if the PK is still null-look for a column called [tableName]ID - if it's there then make it PK
            if (result.PrimaryKey == null)
            {
                var pk = (result.GetColumn(type.Name + "_ID") ?? result.GetColumn(type.Name + "ID") ?? result.GetColumn("ID")) ?? result.GetColumn("Key");

                if (pk != null)
                {
                    pk.IsPrimaryKey = true;
                    //if it's an INT then AutoIncrement it
                    if (pk.IsNumeric)
                    {
                        pk.AutoIncrement = true;
                    }
                    else if (pk.IsString && pk.MaxLength == 0)
                    {
                        pk.MaxLength = 255;
                    }
                    //} else {
                    //    pk = new DatabaseColumn(type.Name + "ID", result);
                    //    pk.DataType = DbType.Int32;
                    //    pk.IsPrimaryKey = true;
                    //    pk.AutoIncrement = true;
                    //    result.Columns.Insert(0, pk);
                }
            }

            //we should have a PK at this point
            //if not, throw :)
            if (result.PrimaryKey == null)
            {
                throw new InvalidOperationException("Can't decide which property to consider the Key - you can create one called 'ID' or mark one with SubSonicPrimaryKey attribute");
            }
            return(result);
        }
        public static ITable ToSchemaTable(this Type type, IDataProvider provider)
        {
            string tableName = type.Name;

            tableName = tableName.MakePlural();
            var result = new DatabaseTable(tableName, provider);

            result.ClassName = type.Name;

            var typeAttributes = type.GetCustomAttributes(typeof(IClassMappingAttribute), false);

            foreach (IClassMappingAttribute attr in typeAttributes)
            {
                if (attr.Accept(result))
                {
                    attr.Apply(result);
                }
            }

            var props = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);

            foreach (var prop in props)
            {
                if (prop.GetSetMethod() == null)
                {
                    continue;
                }

                var attributes = prop.GetCustomAttributes(false);
                if (ColumnIsIgnored(attributes))
                {
                    continue;
                }

                if (CanGenerateSchemaFor(prop.PropertyType))
                {
                    var column = new DatabaseColumn(prop.Name, result);
                    column.PropertyName = prop.Name;
                    bool isNullable = prop.PropertyType.Name.Contains("Nullable");

                    column.DataType = IdentifyColumnDataType(prop.PropertyType, isNullable);

                    if (column.DataType == DbType.Decimal || column.DataType == DbType.Double)
                    {
                        //default to most common;
                        column.NumberScale      = 2;
                        column.NumericPrecision = 10;
                    }

                    /*else if(column.DataType == DbType.String)
                     * {
                     *  column.MaxLength = 255;
                     * }*/
                    else if (column.DataType == DbType.Binary)
                    {
                        isNullable = true;
                    }

                    if (isNullable)
                    {
                        column.IsNullable = true;
                    }

                    // Now work with attributes
                    foreach (IPropertyMappingAttribute attr in attributes.Where(x => x is IPropertyMappingAttribute))
                    {
                        if (attr.Accept(column))
                        {
                            attr.Apply(column);
                        }
                    }

                    result.Columns.Add(column);
                }
                else if (IsRelationColumm(attributes))
                {
                    if (!IsPropertyDeclaredVirtual(prop))
                    {
                        throw new InvalidOperationException(String.Format(
                                                                "Property {0} of type {1} marked as relation must be declared virtual to allow dynamic proxy mechanisms work correctly!",
                                                                prop.Name, prop.DeclaringType.Name));
                    }

                    var innerProp = prop;

                    result.AddRelation(() =>
                    {
                        var relation = new DatabaseRelation(innerProp.Name, result);

                        foreach (IRelationMappingAttribute attr in attributes.Where(x => x is IRelationMappingAttribute))
                        {
                            if (attr.Accept(relation, innerProp))
                            {
                                attr.Apply(relation, innerProp);
                            }
                        }

                        return(relation);
                    });
                }
            }

            //if the PK is still null-look for a column called [tableName]ID - if it's there then make it PK
            if (result.PrimaryKey == null)
            {
                var pk = (result.GetColumn(type.Name + "ID") ?? result.GetColumn("ID")) ?? result.GetColumn("Key");

                if (pk != null)
                {
                    pk.IsPrimaryKey = true;
                    //if it's an INT then AutoIncrement it
                    if (pk.IsNumeric)
                    {
                        pk.AutoIncrement = true;
                    }
                    else if (pk.IsString && pk.MaxLength == 0)
                    {
                        pk.MaxLength = 255;
                    }
                    //} else {
                    //    pk = new DatabaseColumn(type.ColumnName + "ID", result);
                    //    pk.DataType = DbType.Int32;
                    //    pk.IsPrimaryKey = true;
                    //    pk.AutoIncrement = true;
                    //    result.Columns.Insert(0, pk);
                }
            }

            //we should have a PK at this point
            //if not, throw :)
            if (result.PrimaryKey == null)
            {
                throw new InvalidOperationException("Can't decide which property to consider the Key - you can create one called 'ID' or mark one with SubSonicPrimaryKey attribute");
            }
            return(result);
        }
Exemple #9
0
 protected internal DatabaseStore(DatabaseRelation dr)
 {
     this.primaryKey = dr.PrimaryKey; this.foreignKey = dr.ForeignKey;
 }