示例#1
0
        protected override void fillFromDataSet(DataSet ds, DataRow dr, List <TableInfo> tables)
        {
            if (tables.Exists(t => {
                return(t.TableName.ToUpper().Trim() == TableName.ToUpper().Trim());
            }))
            {
                // this table has already been added.
                return;
            }

            // pull info from datarow
            PrimaryKeySortType = dr["primary_key_sort_type"].ToString();
            CharacterSet       = dr["charset"].ToString();
            Collation          = dr["collation"].ToString();
            Engine             = dr["engine"].ToString();
            IsSelected         = Toolkit.ToBoolean(dr["is_selected"], false);
            RowCount           = Toolkit.ToInt32(dr["row_count"], 0);

            // look up all fields associated with this table
            DataTable dtTableField = ds.Tables["TableFieldInfo"];

            DataRow[] drTableFields = dtTableField.Select("parent_id = " + dr["id"]);
            foreach (DataRow dr2 in drTableFields)
            {
                Fields.Add(FieldInfo.GetInstance(this).FillFromDataSet(dr2, null, tables));
            }

            // look up all indexes associated with this table
            var drIndexes = ds.Tables["IndexInfo"].Select("parent_id = " + dr["id"]);

            IndexInfo idx = null;

            foreach (DataRow dr3 in drIndexes)
            {
                if (idx == null || idx.IndexName.ToLower() != dr3["index_name"].ToString().ToLower())
                {
                    idx = IndexInfo.GetInstance(this);
                    idx.FillFromDataSet(ds, dr3);
                    this.Indexes.Add(idx);
                }
            }


            DataRow[]      drConstraints = ds.Tables["ConstraintInfo"].Select("parent_id = " + dr["id"]);
            ConstraintInfo ci            = null;

            foreach (DataRow dr4 in drConstraints)
            {
                if (ci == null || ci.ConstraintName.ToLower() != dr4["constraint_name"].ToString().ToLower())
                {
                    ci = ConstraintInfo.GetInstance(this);
                    ci.FillFromDataSet(ds, dr4, tables);
                    Constraints.Add(ci);
                }
            }
        }
示例#2
0
        public ConstraintInfo Clone(TableInfo newParent)
        {
            ConstraintInfo ci = ConstraintInfo.GetInstance(newParent);

            ci.ConstraintName     = ConstraintName;
            ci.DataConnectionSpec = DataConnectionSpec;
            ci.OnDeleteAction     = this.OnDeleteAction;
            ci.OnUpdateAction     = this.OnUpdateAction;

            // TODO: is this ok? we're not cloning the references table...
            if (ci.ReferencesTable != ci.Table)
            {
                // self-referential.  no need to clone it.
                Debug.WriteLine("NOT cloning ci.ReferencesTable as it is self-referential");
            }
            else
            {
                // pointing at a different table, clone it
                ci.ReferencesTable = this.ReferencesTable.Clone(true, true, true);
            }

            ci.ReferencesTableName = this.ReferencesTableName;
            ci.SyncAction          = SyncAction;
            ci.Table     = newParent;
            ci.TableName = newParent.TableName;
            foreach (FieldInfo fiSrc in SourceFields)
            {
                ci.SourceFields.Add(fiSrc.Clone(newParent));
            }
            ci.SourceFieldNames = SourceFieldNames.ToList();

            foreach (FieldInfo fiRef in ReferencesFields)
            {
                ci.ReferencesFields.Add(fiRef.Clone(newParent));
            }
            ci.ReferencesFieldNames = ReferencesFieldNames.ToList();

            return(ci);
        }
示例#3
0
        /// <summary>
        /// Assumes given object is the slave and the current object is the source. Creates a new TableInfo object representing the source's properties overriding the target's.
        /// </summary>
        /// <param name="slave"></param>
        /// <returns></returns>
        public TableInfo CreateDiff(TableInfo target)
        {
            // to create a table:
            // src.SyncAction = Create

            // to Drop a table:
            // src.SyncAction = Drop

            // to drop an index:
            // src.SyncAction = Nothing,
            // src.Indexes[blah].SyncAction = Drop

            // to drop a constraint:
            // src.SyncAction = Nothing,
            // src.Constraints[blah].SyncAction = Drop


            // to create a field on an existing table:
            // src.SyncAction = Alter
            // src.Fields[blah].SyncAction = Create

            // to modify a field on an existing table:
            // src.SyncAction = Alter
            // src.Fields[blah].SyncAction = Alter

            // to drop a field on an existing table:
            // src.SyncAction = Alter
            // src.Fields[blah].SyncAction = Drop


            TableInfo diff = null;

            if (target == null)
            {
                // target doesn't exist. if master says to alter or create, create it.

                // if master says to drop or nothing, ignore it.
                if (SyncAction == SyncAction.Alter || SyncAction == SyncAction.Create)
                {
                    diff            = this.Clone(true, true, true);
                    diff.SyncAction = SyncAction.Create;

                    // fields
                    foreach (FieldInfo fi in diff.Fields)
                    {
                        if (fi.SyncAction == SyncAction.Create || fi.SyncAction == SyncAction.Alter)
                        {
                            fi.SyncAction = SyncAction.Create;
                        }
                        else
                        {
                            fi.SyncAction = SyncAction.Nothing;
                        }
                    }

                    // indexes
                    foreach (IndexInfo ii in diff.Indexes)
                    {
                        if (ii.SyncAction == SyncAction.Create || ii.SyncAction == SyncAction.Alter)
                        {
                            ii.SyncAction = SyncAction.Create;
                        }
                        else
                        {
                            ii.SyncAction = SyncAction.Nothing;
                        }
                    }

                    //constraints
                    foreach (ConstraintInfo ci in diff.Constraints)
                    {
                        if (ci.SyncAction == SyncAction.Create || ci.SyncAction == SyncAction.Alter)
                        {
                            ci.SyncAction = SyncAction.Create;
                        }
                        else
                        {
                            ci.SyncAction = SyncAction.Nothing;
                        }
                    }
                }
                else
                {
                    // if it's drop or nothing, do nothing (as it doesn't exist)
                    diff            = this.Clone(false, false, false);
                    diff.SyncAction = SyncAction.Nothing;
                }
            }
            else
            {
                // target exists, perform a diff
                diff = this.Clone(true, true, true);
                switch (diff.SyncAction)
                {
                case SyncAction.Drop:
                    // target exists and action is drop.
                    // mark as drop for everything (constraints, indexes, table)
                    diff.SyncAction = SyncAction.Drop;

                    // indexes
                    foreach (IndexInfo ii in diff.Indexes)
                    {
                        ii.SyncAction = SyncAction.Drop;
                    }

                    // constraints
                    foreach (ConstraintInfo ci in Constraints)
                    {
                        ci.SyncAction = SyncAction.Drop;
                    }

                    break;

                case SyncAction.Nothing:
                    // target exists and action is nothing.
                    //
                    break;

                case SyncAction.Alter:
                case SyncAction.Create:
                    // target exists, so we can't create it.
                    // mark it as alter.
                    diff.SyncAction = SyncAction.Alter;
                    foreach (FieldInfo fiSrc in Fields)
                    {
                        // we always add the field.
                        diff.Fields.Add(fiSrc);

                        FieldInfo fiTgt = target.Fields.Find(f => {
                            return(f.Name == fiSrc.Name);
                        });

                        // the corresponding target field (if any) tells us the action to do against it

                        if (fiTgt != null)
                        {
                            // target contains this field already.
                            if (fiSrc != fiTgt)
                            {
                                // target's is different. mark it as alter
                                fiSrc.SyncAction = SyncAction.Alter;
                            }
                            else
                            {
                                // field is exactly the same.
                                // do nothing. (don't even add it to the Fields collection)
                                fiSrc.SyncAction = SyncAction.Nothing;
                            }
                        }
                        else
                        {
                            // tgt doesn't have it.
                            if (fiSrc.SyncAction == SyncAction.Alter || fiSrc.SyncAction == SyncAction.Create)
                            {
                                // alter or create should show up as create (can't alter a non-existant field)
                                fiSrc.SyncAction = SyncAction.Create;
                            }
                            else
                            {
                                // it's either Drop or Nothing.
                                // either way we don't touch it, it already has the right SyncAction
                            }
                        }
                    }

                    // indexes
                    foreach (IndexInfo ii in diff.Indexes)
                    {
                        IndexInfo iiTgt = target.Indexes.Find(i => {
                            return(i.IndexName == ii.IndexName);
                        });
                        if (iiTgt != null)
                        {
                            // index exists on target.
                            switch (ii.SyncAction)
                            {
                            case SyncAction.Alter:
                            case SyncAction.Create:
                                iiTgt.SyncAction = SyncAction.Alter;
                                break;

                            case SyncAction.Drop:
                                iiTgt.SyncAction = SyncAction.Drop;
                                break;

                            case SyncAction.Nothing:
                                iiTgt.SyncAction = SyncAction.Nothing;
                                break;
                            }
                        }
                    }

                    // constraints
                    foreach (ConstraintInfo ci in Constraints)
                    {
                        ConstraintInfo ciNew = ci.Clone(diff);
                        diff.Constraints.Add(ciNew);
                        ConstraintInfo ciTgt = target.Constraints.Find(ciTemp => {
                            return(ciTemp.ConstraintName == ciNew.ConstraintName);
                        });
                        if (ciTgt != null)
                        {
                            // index exists on target.
                            switch (ci.SyncAction)
                            {
                            case SyncAction.Alter:
                            case SyncAction.Create:
                                ciTgt.SyncAction = SyncAction.Alter;
                                break;

                            case SyncAction.Drop:
                                ciTgt.SyncAction = SyncAction.Drop;
                                break;

                            case SyncAction.Nothing:
                                ciTgt.SyncAction = SyncAction.Nothing;
                                break;
                            }
                        }
                    }

                    break;
                }
            }

            return(diff);
        }