예제 #1
0
 /// <summary>
 /// Removes a SqlChildToParentLink item to the collection.
 /// </summary>
 /// <param name="item">The item to remove.</param>
 public void Remove(SqlChildToParentLink item)
 {
     if (item == null)
     {
         throw new ArgumentNullException("item");
     }
     List.Remove(item);
 }
예제 #2
0
 /// <summary>
 /// Adds a SqlChildToParentLink instance to the collection.
 /// </summary>
 /// <param name="item">The item to add.</param>
 public void Add(SqlChildToParentLink item)
 {
     if (item == null)
     {
         throw new ArgumentNullException("item");
     }
     this.AssignItem(item);
     List.Add(item);
 }
예제 #3
0
 /// <summary>
 /// Inserts a SqlChildToParentLink instance into the collection.
 /// </summary>
 /// <param name="item">The item to add.</param>
 public void Insert(int index, SqlChildToParentLink item)
 {
     if (item == null)
     {
         throw new ArgumentNullException("item");
     }
     this.AssignItem(item);
     List.Insert(index, item);
 }
예제 #4
0
 /// <summary>
 /// Assigns an item.
 /// </summary>
 /// <param name="item"></param>
 private void AssignItem(SqlChildToParentLink item)
 {
     if (item == null)
     {
         throw new ArgumentNullException("item");
     }
     if (this.Table != null)
     {
         item.SetTable(this.Table);
     }
 }
예제 #5
0
 /// <summary>
 /// Discovers if the given item is in the collection.
 /// </summary>
 /// <param name="item">The item to find.</param>
 /// <returns>Returns true if the given item is in the collection.</returns>
 public bool Contains(SqlChildToParentLink item)
 {
     if (IndexOf(item) == -1)
     {
         return(false);
     }
     else
     {
         return(true);
     }
 }
예제 #6
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="foreignKey"></param>
 internal CreateForeignKeySchemaWorkUnit(EntityType entityType, SqlChildToParentLink foreignKey) : base(entityType)
 {
     _foreignKey = foreignKey;
 }
예제 #7
0
        /// <summary>
        /// Gets the schema work units specific to this table.
        /// </summary>
        /// <param name="existingTable"></param>
        /// <returns></returns>
        internal WorkUnitCollection GetSchemaWorkUnits(SqlTable existingTable)
        {
            if (_entityType == null)
            {
                throw new ArgumentNullException("entityType");
            }

            if (existingTable == null)
            {
                throw new ArgumentNullException("existingTable");
            }

            // results...
            WorkUnitCollection results = new WorkUnitCollection();

            // Removed indexes.
            SqlIndexCollection removedIndexes = new SqlIndexCollection();

            // Removed indexes.
            SqlChildToParentLinkCollection removedForeignKeys = new SqlChildToParentLinkCollection();

            // find missing columns...
            foreach (SqlColumn column in this.Columns)
            {
                SqlColumn existingColumn = null;
                foreach (SqlColumn scanColumn in existingTable.Columns)
                {
                    if (string.Compare(column.NativeName, scanColumn.NativeName, true, System.Globalization.CultureInfo.InvariantCulture) == 0)
                    {
                        existingColumn = scanColumn;
                        break;
                    }
                }

                // found?
                if (existingColumn == null)
                {
                    results.Add(new AddColumnSchemaWorkUnit(_entityType, column));
                }
                else
                {
                    // get the new column metrics.  this is tricky, because if the length changes to be less than what it was,
                    // we don't want to change the length, but we might want to change other metrics.
                    bool   changed = false;
                    string reason  = null;

                    // check...
                    long newLength = existingColumn.Length;
                    if (column.Length > newLength && newLength != -1)
                    {
                        newLength = column.Length;

                        // mbr - 24-07-2007 - case 321 - added reason...
                        changed = true;
                        reason  = string.Format("Length changed from {0} to {1}", existingColumn.Length, column.Length);
                    }

                    // flags?
                    bool newIsNullable = false;
                    if (!(existingColumn.IsKey))
                    {
                        newIsNullable = existingColumn.IsNullable;
                        if (column.IsNullable != newIsNullable)
                        {
                            newIsNullable = column.IsNullable;

                            // mbr - 24-07-2007 - case 321 - added reason...
                            changed = true;
                            reason  = string.Format("Nullability changed to '{0}'", column.IsNullable);
                        }
                    }
                    else
                    {
                        newIsNullable = false;
                    }

                    // type...
                    DbType newType    = existingColumn.DbType;
                    bool   newIsLarge = existingColumn.IsLarge;
                    if (column.DbType != newType || column.IsLarge != newIsLarge)
                    {
                        // mbr - 24-07-2007 - case 308 - this didn't do anything, hence have changed the code
                        // to the original intention - i.e. if something was an int and it's now a string, it will try and
                        // convert.  the original meaning of CanUpgradeType was to make sure the fx knew how
                        // to go from

                        //						// are the compatible?
                        //						bool ok = false;
                        //						try
                        //						{
                        //							this.CanUpgradeType(newType, newIsLarge, column.DbType, column.IsLarge);
                        //						}
                        //						catch(Exception ex)
                        //						{
                        //							throw new InvalidOperationException(string.Format("Failed to check upgradability of '{0}' on '{1}'.", column, column.Table), ex);
                        //						}

                        // can we?
                        //if(ok)
                        {
                            newType    = column.DbType;
                            newIsLarge = column.IsLarge;

                            // mbr - 24-07-2007 - case 321 - added reason...
                            changed = true;
                            string fromAsString = existingColumn.DbType.ToString();
                            if (existingColumn.IsLarge)
                            {
                                fromAsString += " (large)";
                            }
                            string toAsString = column.DbType.ToString();
                            if (column.IsLarge)
                            {
                                toAsString += " (large)";
                            }
                            reason = string.Format("Type changed from '{0}' to '{1}'", fromAsString, toAsString);
                        }
                    }

                    // now check the default default...
                    SqlDatabaseDefault existingDefault = existingColumn.DefaultExpression;
                    SqlDatabaseDefault newDefault      = column.DefaultExpression;

                    // alter...
                    if (changed && !(column.IsLarge))
                    {
                        // Now we can check the indexes are on this column as they need to be removed
                        foreach (SqlIndex index in existingTable.Indexes)
                        {
                            if (index.Columns.Contains(column.NativeName))
                            {
                                removedIndexes.Add(index);
                                results.Add(new DropIndexSchemaWorkUnit(_entityType, existingTable, index));
                            }
                        }

                        // Now we can check the foreign keys are on this column as they need to be removed
                        foreach (SqlChildToParentLink foreignKey in existingTable.LinksToParents)
                        {
                            if (foreignKey.Columns.Contains(column.NativeName))
                            {
                                removedForeignKeys.Add(foreignKey);
                                results.Add(new DropForeignKeySchemaWorkUnit(_entityType, foreignKey));
                            }
                        }

                        // mbr - 24-07-2007 - case 321 - added reason.
                        results.Add(new AlterColumnSchemaWorkUnit(_entityType, column, newType, newLength,
                                                                  newIsLarge, newIsNullable, newDefault, reason));
                    }

                    if (((existingDefault != null && newDefault == null) || (existingDefault == null && newDefault != null)) ||
                        (existingDefault != null && newDefault != null && !(existingDefault.Equals(newDefault))))
                    {
                        if (existingDefault != null)
                        {
                            results.Add(new DropConstraintSchemaWorkUnit(_entityType, column, existingDefault));
                        }

                        if (newDefault != null)
                        {
                            results.Add(new AddConstraintSchemaWorkUnit(_entityType, column, newDefault));
                        }
                    }
                }
            }


            // Check existing indexes
            foreach (SqlIndex index in Indexes)
            {
                SqlIndex existingIndex = null;
                foreach (SqlIndex scanIndex in existingTable.Indexes)
                {
                    //
                    if (string.Compare(scanIndex.NativeName, index.NativeName, true, System.Globalization.CultureInfo.InvariantCulture) == 0)
                    {
                        // mbr - 2011-11-02 - do we need to drop it?
                        if (!(scanIndex.DoesMatch(index)))
                        {
                            // drop...
                            results.Add(new DropIndexSchemaWorkUnit(_entityType, this, index));
                        }
                        else
                        {
                            // create...
                            existingIndex = scanIndex;
                        }

                        // stop...
                        break;
                    }
                }

                // found?
                if (existingIndex == null || removedIndexes[existingIndex.Name] != null)
                {
                    results.Add(new CreateIndexSchemaWorkUnit(_entityType, this, index));
                }
            }

            // Check existing foreign keys
            foreach (SqlChildToParentLink foreignKey in LinksToParents)
            {
                SqlChildToParentLink existingForeignKey = null;
                foreach (SqlChildToParentLink scanForeignKey in existingTable.LinksToParents)
                {
                    //
                    if (string.Compare(scanForeignKey.NativeName, foreignKey.NativeName, true, System.Globalization.CultureInfo.InvariantCulture) == 0)
                    {
                        existingForeignKey = scanForeignKey;
                        break;
                    }
                }

                // found?
                if (existingForeignKey == null || removedIndexes[existingForeignKey.Name] != null)
                {
                    // mbr - 04-10-2007 - case 825 - only do this if the parent is referenced in the schema...
                    if (Schema == null)
                    {
                        throw new InvalidOperationException("Schema is null.");
                    }
                    bool ok = foreignKey.IsSupported(this.Schema);

                    // ok?
                    if (ok)
                    {
                        results.Add(new CreateForeignKeySchemaWorkUnit(_entityType, foreignKey));
                    }
                }
            }

            // return...
            return(results);
        }
예제 #8
0
        internal override void Merge(XmlElement element, bool createIfNotFound)
        {
            base.Merge(element, createIfNotFound);

            // walk tables...
            foreach (XmlElement columnElement in element.SelectNodes("Columns/SqlColumn"))
            {
                // get the name...
                string nativeName = XmlHelper.GetElementString(columnElement, "NativeName", OnNotFound.ThrowException);
                if (nativeName == null)
                {
                    throw new InvalidOperationException("'nativeName' is null.");
                }
                if (nativeName.Length == 0)
                {
                    throw new InvalidOperationException("'nativeName' is zero-length.");
                }

                // get...
                SqlColumn column = this.Columns[nativeName];
                if (column == null && createIfNotFound)
                {
                    column = new SqlColumn(nativeName, DbType.Int32, -1, EntityFieldFlags.Normal);
                    this.Columns.Add(column);
                }

                // add...
                if (column != null)
                {
                    column.Merge(columnElement, createIfNotFound);
                }
            }

            // walk the links...
            foreach (XmlElement linkElement in element.SelectNodes("ChildToParentLinks/SqlChildToParentLink"))
            {
                // get...
                string nativeName = XmlHelper.GetElementString(linkElement, "NativeName", OnNotFound.ReturnNull);
                if (nativeName == null)
                {
                    throw new InvalidOperationException("'nativeName' is null.");
                }
                if (nativeName.Length == 0)
                {
                    throw new InvalidOperationException("'nativeName' is zero-length.");
                }

                // find it...
                SqlChildToParentLink link = this.LinksToParents[nativeName];
                if (link == null && createIfNotFound)
                {
                    link = new SqlChildToParentLink(nativeName, this);
                    this.LinksToParents.Add(link);
                }

                // add....
                if (link != null)
                {
                    link.Merge(linkElement, createIfNotFound);
                }
            }

            // mbr - 21-09-2007 - do we actually have modifiers?
            string modifiersAsString = XmlHelper.GetElementString(element, "Modifiers", OnNotFound.ReturnNull);

            if (modifiersAsString != null && modifiersAsString.Length > 0)
            {
                this.Modifiers = (TableModifiers)Enum.Parse(typeof(TableModifiers), modifiersAsString, true);
            }

            // xml...
            var asString = element.GetElementString("GenerateDto", OnNotFound.ReturnNull);

            if (!(string.IsNullOrEmpty(asString)))
            {
                this.GenerateDto = ConversionHelper.ToBoolean(asString);
            }
            else
            {
                this.GenerateDto = false;
            }
        }
예제 #9
0
 /// <summary>
 /// Returns the index of the item in the collection.
 /// </summary>
 /// <param name="item">The item to find.</param>
 /// <returns>The index of the item, or -1 if it is not found.</returns>
 public int IndexOf(SqlChildToParentLink item)
 {
     return(List.IndexOf(item));
 }