Beispiel #1
0
 /// <summary>
 /// Compara dos indices y devuelve true si son iguales, caso contrario, devuelve false.
 /// </summary>
 public static Boolean Compare(Index origen, Index destino)
 {
     if (destino == null)
     {
         throw new ArgumentNullException("destino");
     }
     if (origen == null)
     {
         throw new ArgumentNullException("origen");
     }
     if (origen.IsUnique != destino.IsUnique)
     {
         return(false);
     }
     if (origen.Type != destino.Type)
     {
         return(false);
     }
     if (!IndexColumns.Compare(origen.Columns, destino.Columns))
     {
         return(false);
     }
     return(true);
 }
Beispiel #2
0
 public Index(ISchemaBase parent)
     : base(parent, Enums.ObjectType.Index)
 {
     FilterDefintion = "";
     Columns = new IndexColumns(parent);
 }
 public override string ToString()
 {
     return
         ($"{DbTableName,-20} - {IndexName,-20} - {IndexColumns.Select(z => z.ToString()).StringJoin(", ")} - {IncludedColumns.StringJoin(",")}");
 }
Beispiel #4
0
 public Index(Table table) : base(StatusEnum.ObjectTypeEnum.Index)
 {
     Parent  = table;
     columns = new IndexColumns(table);
 }
Beispiel #5
0
        private Model.Table GetNewTable(string tableName)
        {
            ArchAngel.Interfaces.ProjectHelper.RaiseObjectBeingProcessedEvent(tableName, "Table");
            //_columns = null; // Reset the columns
            //_indexColumns = null;
            //_indexes = null;
            //_indexReferencedColumns = null;
            //_referencedColumns = null;
            Model.Table table = new Model.Table(tableName, false);

            #region Columns

            DataRow[] columnRows = Columns.Select(string.Format("TABLE_NAME = '{0}'", tableName));

            foreach (DataRow columnRow in columnRows)
            {
                Column column = new Column(
                    (string)columnRow["COLUMN_NAME"],
                    false,
                    table,
                    (int)columnRow["ORDINAL_POSITION"],
                    Slyce.Common.Utility.StringsAreEqual((string)columnRow["IS_NULLABLE"], "YES", false),
                    (string)columnRow["DATA_TYPE"],
                    columnRow.IsNull("CHARACTER_MAXIMUM_LENGTH") ? 0 : (int)columnRow["CHARACTER_MAXIMUM_LENGTH"],
                    (int)columnRow["InPrimaryKey"] == 1,
                    columnRow.IsNull("IsIdentity") ? false : (int)columnRow["IsIdentity"] == 1,
                    columnRow.IsNull("COLUMN_DEFAULT") ? "" : (string)columnRow["COLUMN_DEFAULT"],
                    columnRow.IsNull("IsComputed") ? false : (int)columnRow["IsComputed"] == 1);

                if (IsSupported(column))
                {
                    table.AddColumn(column);
                }
            }

            #endregion

            #region Indexes

            DataRow[] indexRows = Indexes.Select(string.Format("TABLE_NAME = '{0}'", tableName));
            foreach (DataRow indexRow in indexRows)
            {
                string indexType;
                string indexKeyType = indexRow["CONSTRAINT_TYPE"].ToString();
                if (indexKeyType == "PRIMARY KEY")
                {
                    continue;
                }
                else if (indexKeyType == "FOREIGN KEY")
                {
                    continue;
                }
                else if (indexKeyType == "UNIQUE")
                {
                    continue;
                    //indexType = DatabaseConstant.IndexType.Unique;
                }
                else if (indexKeyType == "CHECK")
                {
                    indexType = DatabaseConstant.IndexType.Check;
                }
                else if (indexKeyType == "NONE")    //TODO check is NONE
                {
                    indexType = DatabaseConstant.IndexType.None;
                }
                else
                {
                    //continue;
                    throw new Exception("IndexType " + indexKeyType + " Not Defined");
                }
                DataRow[] indexColumnRows;// = IndexColumns.Select(string.Format("TABLE_NAME = '{0}' AND CONSTRAINT_NAME  = '{1}'", tableName, indexRow["CONSTRAINT_NAME"]));

                if (indexKeyType == "NONE")
                {
                    indexColumnRows = Columns.Select(string.Format("TABLE_NAME = '{0}' AND COLUMN_NAME  = '{1}'", tableName, indexRow["ColumnName"]));
                }
                else
                {
                    indexColumnRows = IndexColumns.Select(string.Format("TABLE_NAME = '{0}' AND CONSTRAINT_NAME  = '{1}'", tableName, indexRow["CONSTRAINT_NAME"]));
                }
                Index index = new Index(indexRow["CONSTRAINT_NAME"].ToString(), false, indexType, table);

                // Fill Columns
                foreach (DataRow indexColumnRow in indexColumnRows)
                {
                    Column indexColumn = new Column(indexColumnRow["COLUMN_NAME"].ToString(), false);
                    index.AddColumn(indexColumn);
                }
                index.ResetDefaults();
                table.AddIndex(index);
            }

            // Indexes -- that should be keys
            foreach (DataRow keyRow in indexRows)
            {
                string keyType;
                string indexKeyType = keyRow["CONSTRAINT_TYPE"].ToString();
                if (indexKeyType == "PRIMARY KEY")
                {
                    keyType = DatabaseConstant.KeyType.Primary;
                }
                else if (indexKeyType == "FOREIGN KEY")
                {
                    keyType = DatabaseConstant.KeyType.Foreign;
                }
                else if (indexKeyType == "UNIQUE")
                {
                    keyType = DatabaseConstant.KeyType.Unique;
                }
                else if (indexKeyType == "CHECK")
                {
                    continue;
                }
                else if (indexKeyType == "NONE")
                {
                    continue;
                    //keyType = DatabaseConstant.KeyType.None;
                }
                else
                {
                    //continue;
                    throw new Exception("KeyType " + indexKeyType + " Not Defined");
                }

                // Create Alias
                string    keyAlias      = keyType + "_";
                DataRow[] keyColumnRows = IndexColumns.Select(string.Format("TABLE_NAME = '{0}' AND CONSTRAINT_NAME = '{1}'", tableName, keyRow["CONSTRAINT_NAME"]));
                Key       key           = new Key(keyRow["CONSTRAINT_NAME"].ToString(), false, keyType, table, false);

                // Fill Columns
                foreach (DataRow keyColumnRow in keyColumnRows)
                {
                    Column keyColumn = new Column(keyColumnRow["COLUMN_NAME"].ToString(), false);
                    keyColumn.DataType = (string)keyColumnRow["DATA_TYPE"];
                    key.AddColumn(keyColumn);
                }
                if (keyType == DatabaseConstant.KeyType.Foreign)
                {
                    DataRow[] keyReferencedColumnRows     = IndexReferencedColumns.Select(string.Format("ForeignKey = '{0}'", keyRow["CONSTRAINT_NAME"]));
                    DataRow   firstKeyReferencedColumnRow = keyReferencedColumnRows[0];
                    // Fill References
                    key.ReferencedTable = new Model.Table(firstKeyReferencedColumnRow["ReferencedTable"].ToString(), false);

                    //if (dmoKey.ReferencedKey != null)
                    //{
                    key.ReferencedKey = new Key(firstKeyReferencedColumnRow["ReferencedKey"].ToString(), false, true);
                    //}

                    // Fill Referenced Columns
                    foreach (DataRow keyReferencedColumnRow in keyReferencedColumnRows)
                    {
                        Column keyReferencedColumn = new Column(keyReferencedColumnRow["ReferencedColumn"].ToString(), false);
                        key.AddReferencedColumn(keyReferencedColumn);
                    }
                }
                key.ResetDefaults();
                table.AddKey(key);
            }

            #endregion

            return(table);
        }
Beispiel #6
0
        private Model.Table GetNewTable(string tableName)
        {
            ArchAngel.Interfaces.Events.RaiseObjectBeingProcessedEvent(tableName, "Table");
            //_columns = null; // Reset the columns
            //_indexes = null;
            //_dtReferencedColumns = null;
            //return new Model.Table();
            Model.Table table = new Model.Table(tableName, false);

            #region Columns
            DataRow[] columnRows = Columns.Select(string.Format("TABLE_NAME = '{0}'", tableName));

            foreach (DataRow row in columnRows)
            {
                bool isReadOnly = false;

                if (!row.IsNull("IsIdentity") && (int)row["IsIdentity"] == 1)
                {
                    isReadOnly = true;
                }
                else if (!row.IsNull("IsComputed") && (int)row["IsComputed"] == 1)
                {
                    isReadOnly = true;
                }
                else if (Slyce.Common.Utility.StringsAreEqual((string)row["DATA_TYPE"], "timestamp", false))
                {
                    isReadOnly = true;
                }
                // Check whether we have added this column before. Columns are repeated if they are both a PRIMARY_KEY and a FOREIGN_KEY
                Column column = new Column(
                    (string)row["COLUMN_NAME"],
                    false,
                    table,
                    (int)row["ORDINAL_POSITION"],
                    Slyce.Common.Utility.StringsAreEqual((string)row["IS_NULLABLE"], "YES", false),
                    (string)row["DATA_TYPE"],
                    row.IsNull("CHARACTER_MAXIMUM_LENGTH") ? 0 : Convert.ToInt32(row["CHARACTER_MAXIMUM_LENGTH"]),
                    (int)row["InPrimaryKey"] == 1,
                    row.IsNull("IsIdentity") ? false : Convert.ToInt32(row["IsIdentity"]) == 1,
                    row.IsNull("COLUMN_DEFAULT") ? "" : (string)row["COLUMN_DEFAULT"],
                    isReadOnly,
                    row.IsNull("IsComputed") ? false : Convert.ToInt32(row["IsComputed"]) == 1,
                    row.IsNull("NUMERIC_PRECISION") ? 0 : Convert.ToInt32(row["NUMERIC_PRECISION"]),
                    row.IsNull("NUMERIC_SCALE") ? 0 : Convert.ToInt32(row["NUMERIC_SCALE"]));

                table.AddColumn(column);
                //ordinalPosition++;
            }
            #endregion

            #region Indexes

            DataRow[] indexRows = Indexes.Select(string.Format("TABLE_NAME = '{0}'", tableName));
            foreach (DataRow indexRow in indexRows)
            {
                string indexType;
                string indexKeyType = indexRow["CONSTRAINT_TYPE"].ToString();
                if (indexKeyType == "PRIMARY KEY")
                {
                    continue;
                }
                else if (indexKeyType == "FOREIGN KEY")
                {
                    continue;
                }
                else if (indexKeyType == "UNIQUE")
                {
                    continue;
                    //indexType = DatabaseConstant.IndexType.Unique;
                }
                else if (indexKeyType == "CHECK")
                {
                    indexType = DatabaseConstant.IndexType.Check;
                }
                else if (indexKeyType == "NONE")    //TODO check is NONE
                {
                    indexType = DatabaseConstant.IndexType.None;
                }
                else
                {
                    //continue;
                    throw new Exception("IndexType " + indexKeyType + " Not Defined");
                }
                DataRow[] indexColumnRows;// = IndexColumns.Select(string.Format("TABLE_NAME = '{0}' AND CONSTRAINT_NAME  = '{1}'", tableName, indexRow["CONSTRAINT_NAME"]));

                if (indexKeyType == "NONE")
                {
                    indexColumnRows = Columns.Select(string.Format("TABLE_NAME = '{0}' AND COLUMN_NAME  = '{1}'", tableName, indexRow["ColumnName"]));
                }
                else
                {
                    indexColumnRows = IndexColumns.Select(string.Format("TABLE_NAME = '{0}' AND CONSTRAINT_NAME  = '{1}'", tableName, indexRow["CONSTRAINT_NAME"]));
                }
                Index index = new Index(indexRow["CONSTRAINT_NAME"].ToString(), false, indexType, table, (bool)indexRow["IS_UNIQUE"], (bool)indexRow["IS_CLUSTERED"]);

                // Fill Columns
                foreach (DataRow indexColumnRow in indexColumnRows)
                {
                    Column indexColumn = new Column(indexColumnRow["COLUMN_NAME"].ToString(), false);
                    index.AddColumn(indexColumn);
                }
                index.ResetDefaults();
                table.AddIndex(index);
            }

            // Indexes -- that should be keys
            foreach (DataRow keyRow in indexRows)
            {
                string keyType;
                string indexKeyType = keyRow["CONSTRAINT_TYPE"].ToString();
                if (indexKeyType == "PRIMARY KEY")
                {
                    keyType = DatabaseConstant.KeyType.Primary;
                }
                else if (indexKeyType == "FOREIGN KEY")
                {
                    keyType = DatabaseConstant.KeyType.Foreign;
                }
                else if (indexKeyType == "UNIQUE")
                {
                    keyType = DatabaseConstant.KeyType.Unique;
                }
                else if (indexKeyType == "CHECK")
                {
                    continue;
                }
                else if (indexKeyType == "NONE")
                {
                    continue;
                    //keyType = DatabaseConstant.KeyType.None;
                }
                else
                {
                    //continue;
                    throw new Exception("KeyType " + indexKeyType + " Not Defined");
                }
                Key       key           = new Key(keyRow["CONSTRAINT_NAME"].ToString(), false, keyType, table, false);
                DataRow[] keyColumnRows = IndexColumns.Select(string.Format("TABLE_NAME = '{0}' AND CONSTRAINT_NAME = '{1}'", tableName, keyRow["CONSTRAINT_NAME"]));

                // Fill Columns
                foreach (DataRow keyColumnRow in keyColumnRows)
                {
                    Column keyColumn = new Column(keyColumnRow["COLUMN_NAME"].ToString(), false);
                    keyColumn.DataType = (string)keyColumnRow["DATA_TYPE"];
                    key.AddColumn(keyColumn);
                }

                if (keyType == DatabaseConstant.KeyType.Foreign)
                {
                    DataRow[] keyReferencedColumnRows     = IndexReferencedColumns.Select(string.Format("ForeignKey = '{0}'", keyRow["CONSTRAINT_NAME"]));
                    DataRow   firstKeyReferencedColumnRow = keyReferencedColumnRows[0];
                    // Fill References
                    key.ReferencedTable = new Model.Table(firstKeyReferencedColumnRow["ReferencedTable"].ToString(), false);

                    //if (dmoKey.ReferencedKey != null)
                    //{
                    key.ReferencedKey = new Key(firstKeyReferencedColumnRow["ReferencedKey"].ToString(), false, true);
                    //}

                    // Fill Referenced Columns
                    foreach (DataRow keyReferencedColumnRow in keyReferencedColumnRows)
                    {
                        Column keyReferencedColumn = new Column(keyReferencedColumnRow["ReferencedColumn"].ToString(), false);
                        key.AddReferencedColumn(keyReferencedColumn);
                    }
                }
                key.ResetDefaults();
                table.AddKey(key);
            }

            #endregion

            //#region Indexes -- that should be keys
            //string prevConstraintName = "";
            //Key key = null;
            //DataRow[] indexRows = DtIndexes.Select(string.Format("TABLE_NAME = '{0}'", tableName));

            //for (int rowCounter = 0; rowCounter < indexRows.Length; rowCounter++)
            //{
            //    DataRow row = indexRows[rowCounter];
            //    for (int colCounter = 0; colCounter < DtIndexes.Columns.Count; colCounter++)
            //    {
            //        string colName = DtIndexes.Columns[colCounter].ColumnName;
            //    }
            //    string keyType;
            //    string indexKeyType = row.IsNull("Constraint_Type") ? "NONE" : (string)row["Constraint_Type"];
            //    string constraintName = row.IsNull("Constraint_Name") ? "" : (string)row["Constraint_Name"];
            //    string columnName = (string)row["COLUMN_NAME"];

            //    if (indexKeyType == "NONE")
            //    {
            //        keyType = DatabaseConstant.KeyType.None;
            //    }
            //    else if (indexKeyType == "PRIMARY KEY")
            //    {
            //        keyType = DatabaseConstant.KeyType.Primary;
            //    }
            //    else if (indexKeyType == "UNIQUE")
            //    {
            //        keyType = DatabaseConstant.KeyType.Unique;
            //    }
            //    else if (indexKeyType == "CHECK") // TODO: was 'None' for SMO
            //    {
            //        keyType = DatabaseConstant.KeyType.None;
            //    }
            //    else if (indexKeyType == "FOREIGN KEY") // TODO: was 'None' for SMO
            //    {
            //        keyType = DatabaseConstant.KeyType.Foreign;
            //    }
            //    else
            //    {
            //        throw new Exception("KeyType " + indexKeyType + " Not Defined");
            //    }
            //    // Create Alias
            //    if (string.Format("{0}{1}", constraintName, keyType) != prevConstraintName)
            //    {
            //        if (key != null)
            //        {
            //            // Reset the alias, because it is based on the Columns collection which has just finished being modified.
            //            key.ResetDefaults();
            //        }
            //        // Create a new Key
            //        key = new Key(constraintName, false, keyType, table, false);
            //        table.AddKey(key);
            //        prevConstraintName = string.Format("{0}{1}", constraintName, keyType);
            //        Column keyColumn = new Column(columnName, false);
            //        key.AddColumn(keyColumn);
            //    }
            //    else
            //    {
            //        // We are processing another column of the same Index as the previous index
            //        Column keyColumn = new Column(columnName, false);
            //        key.AddColumn(keyColumn);
            //    }
            //    if (keyType == DatabaseConstant.KeyType.Foreign)
            //    {
            //        DataRow[] referencedColumnRows = DtReferencedColumns.Select(string.Format("FOREIGN_KEY = '{0}'", key.Name));

            //        foreach (DataRow refColRow in referencedColumnRows)
            //        {
            //            // Fill References
            //            if (key.ReferencedTable == null)
            //            {
            //                string referencedTableName = (string)refColRow["Referenced_Table_Name"];
            //                string referencedKeyName = (string)refColRow["Referenced_Key"];

            //                key.ReferencedTable = new Model.Table(referencedTableName, false);
            //                key.ReferencedKey = new Key(referencedKeyName, false, true);
            //            }
            //            string referencedColumnName = (string)refColRow["Referenced_Column_Name"];

            //            // Fill Referenced Columns
            //            Column referencedKeyColumn = new Column(referencedColumnName, false);
            //            key.AddReferencedColumn(referencedKeyColumn);
            //        }
            //    }
            //    key.ResetDefaults();
            //}
            //#endregion

            return(table);
        }