private void FixupColumns(Table table, IList<Column> columns)
 {
     for (int i = 0; i < columns.Count; ++i)
     {
         columns[i] = this.ResolveColumnReference(table, columns[i]);
     }
 }
 private void ResolveForeignKeyConstraints(DatabaseSchema schema, Table table)
 {
     foreach (ForeignKey foreignKey in table.ForeignKeys)
     {
         foreignKey.Target = this.ResolveTableReference(schema, foreignKey.Target);
         this.FixupColumns(table, foreignKey.SourceColumns);
         this.FixupColumns(foreignKey.Target, foreignKey.TargetColumns);
     }
 }
        private Column ResolveColumnReference(Table table, Column column)
        {
            ColumnReference columnRef = column as ColumnReference;
            if (columnRef == null)
            {
                return column;
            }

            var result = table.Columns.SingleOrDefault(c => c.Name == columnRef.Name);
            if (result == null)
            {
                throw new TaupoInvalidOperationException("The column '" + columnRef.Name + "' was not found in '" + table + "'");
            }

            return result;
        }
        private Dictionary<EntitySet, Table> ConvertEntitySetsToTables(DatabaseSchema schema, EntityContainer container)
        {
            Dictionary<EntitySet, Table> entitySetToTableMapping = new Dictionary<EntitySet, Table>();

            foreach (EntitySet entitySet in container.EntitySets)
            {
                Table table = new Table(container.Name, entitySet.Name);
                schema.Tables.Add(table);
                entitySetToTableMapping.Add(entitySet, table);
                var type = entitySet.EntityType;
                var pk = new PrimaryKey("PK_" + table.Name);
                Dictionary<MemberProperty, Column> propertyToColumnMap = new Dictionary<MemberProperty, Column>();
                foreach (MemberProperty prop in type.Properties)
                {
                    var column = this.ConvertPropertyToColumn(prop);
                    propertyToColumnMap.Add(prop, column);

                    if (prop.IsPrimaryKey)
                    {
                        pk.Columns.Add(column);
                    }

                    table.Columns.Add(column);
                }

                if (pk.Columns.Count > 0)
                {
                    table.PrimaryKey = pk;
                }

                foreach (EdmUniqueConstraint edmUniqueConstraint in type.EdmUniqueConstraints)
                {
                    var uniqueConstraint = new UniqueConstraint("UC_" + table.Name + "_" + edmUniqueConstraint.Name);
                    foreach (var prop in edmUniqueConstraint.Properties)
                    {
                        Column column;
                        ExceptionUtilities.Assert(propertyToColumnMap.TryGetValue(prop, out column), "Edm Unique Constraint's property '{0}' is not found on entity type '{1}'.", prop.Name, type.FullName);
                        uniqueConstraint.Add(column);
                    }

                    table.Add(uniqueConstraint);
                }
            }

            return entitySetToTableMapping;
        }
        private Table ResolveTableReference(DatabaseSchema schema, Table table)
        {
            TableReference tableRef = table as TableReference;
            if (tableRef == null)
            {
                return table;
            }

            var result = schema.Tables.Concat(schema.Views.Cast<Table>())
                .Where(t => t.Catalog == tableRef.Catalog && t.Schema == tableRef.Schema && t.Name == tableRef.Name)
                .SingleOrDefault();

            if (result == null)
            {
                throw new TaupoInvalidOperationException("The table or view '" + tableRef + "' was not found in the schema.");
            }

            return result;
        }
Beispiel #6
0
 /// <summary>
 /// Adds given <see cref="Table"/> the <see cref="Tables"/> collection.
 /// </summary>
 /// <param name="table">Table to add.</param>
 public void Add(Table table)
 {
     this.Tables.Add(table);
 }
Beispiel #7
0
        /// <summary>
        /// Sets up  <see cref="Target"/> table and target columns.
        /// </summary>
        /// <param name="target">Target table.</param>
        /// <param name="targetColumns">Target columns.</param>
        /// <returns>This object (useful for chaining multiple calls)</returns>
        public ForeignKey References(Table target, params Column[] targetColumns)
        {
            if (this.TargetColumns.Count > 0)
            {
                throw new TaupoInvalidOperationException("References() can only be called on a Foreign Key without target columns.");
            }

            this.Target = target;
            foreach (Column c in targetColumns)
            {
                this.TargetColumns.Add(c);
            }

            return this;
        }
Beispiel #8
0
 /// <summary>
 /// Sets up  <see cref="Target"/> table and target columns.
 /// </summary>
 /// <param name="target">Target table.</param>
 /// <param name="targetColumns">Target column names.</param>
 /// <returns>This object (useful for chaining multiple calls)</returns>
 public ForeignKey References(Table target, params string[] targetColumns)
 {
     return this.References(target, targetColumns.Select(c => new ColumnReference(c)).ToArray());
 }
 private void ResolveColumnsInConstraint(Table table, ColumnsConstraintBase constraint)
 {
     this.FixupColumns(table, constraint.Columns);
 }