Esempio n. 1
0
        private static Key CreateForeignKey(MappingSet set, ITable primaryTable, ITable foreignTable)
        {
            var foreignKey = new Key(GetNextKeyName("FK_" + primaryTable.Name + "_" + foreignTable.Name, set), DatabaseKeyType.Foreign);
            var primaryKey = primaryTable.FirstPrimaryKey;
            foreignKey.ReferencedKey = primaryKey;
            foreignTable.AddKey(foreignKey);

            // Create the columns for the foreign keys
            foreach (var column in primaryKey.Columns)
            {
                var newColumn = CloneColumn(primaryTable, foreignTable, column, "Foreign_");
                foreignKey.AddColumn(newColumn.Name);
            }
            return foreignKey;
        }
        private void GetKeys(ITable table)
        {
            DataRow[] keyRows = connector.Indexes.Select(string.Format("TABLE_NAME = '{0}' AND TABLE_SCHEMA = '{1}'", table.Name.Replace("'", "''"), table.Schema));

            for (int i = 0; i < keyRows.Length; i++)
            {
                DataRow keyRow = keyRows[i];
                DatabaseKeyType keyType;
                string indexKeyType = keyRow["CONSTRAINT_TYPE"].ToString();

                if (indexKeyType == "PRIMARY KEY")
                {
                    keyType = DatabaseKeyType.Primary;
                }
                else if (indexKeyType == "UNIQUE")
                {
                    keyType = DatabaseKeyType.Unique;
                }
                else
                {
                    continue;
                }

                Key key = new Key
                {
                    Name = keyRow["INDEX_NAME"].ToString(),
                    IsUserDefined = false,
                    Keytype = keyType,
                    Parent = table,
                    IsUnique = true
                };

                // Fill Columns
                List<DataRow> keyColumnRows = new List<DataRow>();
                keyColumnRows.AddRange(connector.Columns.Select(string.Format("TABLE_NAME = '{0}' AND TABLE_SCHEMA = '{1}' AND COLUMN_NAME  = '{2}'", table.Name.Replace("'", "''"), table.Schema, keyRow["COLUMN_NAME"])));

                while ((i < keyRows.Length - 1) && (string)keyRows[i + 1]["TABLE_NAME"] == table.Name && (string)keyRows[i + 1]["INDEX_NAME"] == (string)keyRow["INDEX_NAME"])
                {
                    i++;
                    keyRow = keyRows[i];
                    keyColumnRows.AddRange(connector.Columns.Select(string.Format("TABLE_NAME = '{0}' AND TABLE_SCHEMA = '{1}' AND COLUMN_NAME  = '{2}'", table.Name.Replace("'", "''"), table.Schema, keyRow["COLUMN_NAME"])));
                }

                // Fill Columns
                foreach (DataRow keyColumnRow in keyColumnRows)
                {
                    key.AddColumn((string)keyColumnRow["COLUMN_NAME"]);
                }

                key.ResetDefaults();
                table.AddKey(key);
            }
        }
        private void GetKeys(ITable table)
        {
            List<DataRow> keyRows = connector.GetIndexRows(table.Schema, table.Name.Replace("'", "''"));

            for (int i = 0; i < keyRows.Count; i++)
            {
                DataRow keyRow = keyRows[i];
                DatabaseKeyType keyType;
                string indexKeyType = keyRow["CONSTRAINT_TYPE"].ToString();

                /*if (indexKeyType == "PRIMARY KEY")
                    keyType = DatabaseKeyType.Primary;
                else*/
                if (indexKeyType == "UNIQUE")
                    keyType = DatabaseKeyType.Unique;
                else
                    continue;

                Key key = new Key
                {
                    Name = keyRow["INDEX_NAME"].ToString(),
                    IsUserDefined = false,
                    Keytype = keyType,
                    Parent = table,
                    IsUnique = true
                };
                // Fill Columns
                List<DataRow> keyColumnRows = new List<DataRow>();

                keyColumnRows.AddRange(connector.ColumnsBySchema[table.Schema][table.Name.Replace("'", "''")].Where(r => r[OrdColumn].ToString() == keyRow["COLUMN_NAME"].ToString()));

                while ((i < keyRows.Count - 1) && (string)keyRows[i + 1]["TABLE_NAME"] == table.Name && (string)keyRows[i + 1]["INDEX_NAME"] == (string)keyRow["INDEX_NAME"])
                {
                    i++;
                    keyRow = keyRows[i];
                    keyColumnRows.AddRange(connector.ColumnsBySchema[table.Schema][table.Name.Replace("'", "''")].Where(c => c[OrdColumn].ToString() == keyRow["COLUMN_NAME"].ToString()));
                }

                // Fill Columns
                foreach (DataRow keyColumnRow in keyColumnRows)
                    key.AddColumn((string)keyColumnRow["COLUMN_NAME"]);

                key.ResetDefaults();
                table.AddKey(key);
            }
            // Add Primary Keys
            var primaryKeyNames = connector.PrimaryKeyNames;

            if (primaryKeyNames.ContainsKey(table.Schema) && primaryKeyNames[table.Schema].ContainsKey(table.Name))
            {
                Key key = new Key
                {
                    Name = primaryKeyNames[table.Schema][table.Name],
                    IsUserDefined = false,
                    Keytype = DatabaseKeyType.Primary,
                    Parent = table,
                    IsUnique = true
                };
                // Fill Columns
                foreach (var column in table.ColumnsInPrimaryKey)
                    key.AddColumn(column.Name);

                key.ResetDefaults();
                table.AddKey(key);
            }
        }