Example #1
0
        private void AlignColumns(Table referencedTable)
        {
            if (referencedTable.PrimaryKey.ColumnSpan != ColumnSpan)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("Foreign key (")
                .Append(Name + ":")
                .Append(Table.Name)
                .Append(" [");
                AppendColumns(sb, ColumnIterator);
                sb.Append("])")
                .Append(") must have same number of columns as the referenced primary key (")
                .Append(referencedTable.Name).Append(" [");
                AppendColumns(sb, referencedTable.PrimaryKey.ColumnIterator);
                sb.Append("])");
                throw new FKUnmatchingColumnsException(sb.ToString());
            }
            IEnumerator <Column> fkCols = ColumnIterator.GetEnumerator();
            IEnumerator <Column> pkCols = referencedTable.PrimaryKey.ColumnIterator.GetEnumerator();

            while (fkCols.MoveNext() && pkCols.MoveNext())
            {
                fkCols.Current.Length = pkCols.Current.Length;
            }
        }
Example #2
0
        public IIdentifierGenerator CreateIdentifierGenerator(Dialect.Dialect dialect, string defaultCatalog,
                                                              string defaultSchema, RootClass rootClass)
        {
            Dictionary <string, string> @params = new Dictionary <string, string>();

            //if the hibernate-mapping did not specify a schema/catalog, use the defaults
            //specified by properties - but note that if the schema/catalog were specified
            //in hibernate-mapping, or as params, they will already be initialized and
            //will override the values set here (they are in identifierGeneratorProperties)
            if (!string.IsNullOrEmpty(defaultSchema))
            {
                @params[PersistentIdGeneratorParmsNames.Schema] = defaultSchema;
            }
            if (!string.IsNullOrEmpty(defaultCatalog))
            {
                @params[PersistentIdGeneratorParmsNames.Catalog] = defaultCatalog;
            }

            //pass the entity-name, if not a collection-id
            if (rootClass != null)
            {
                @params[IdGeneratorParmsNames.EntityName] = rootClass.EntityName;
            }

            //init the table here instead of earlier, so that we can get a quoted table name
            //TODO: would it be better to simply pass the qualified table name, instead of
            //      splitting it up into schema/catalog/table names
            string tableName = Table.GetQuotedName(dialect);

            @params[PersistentIdGeneratorParmsNames.Table] = tableName;

            //pass the column name (a generated id almost always has a single column and is not a formula)
            IEnumerator enu = ColumnIterator.GetEnumerator();

            enu.MoveNext();
            string columnName = ((Column)enu.Current).GetQuotedName(dialect);

            @params[PersistentIdGeneratorParmsNames.PK] = columnName;

            if (rootClass != null)
            {
                StringBuilder tables      = new StringBuilder();
                bool          commaNeeded = false;
                foreach (Table identityTable in rootClass.IdentityTables)
                {
                    if (commaNeeded)
                    {
                        tables.Append(StringHelper.CommaSpace);
                    }
                    commaNeeded = true;
                    tables.Append(identityTable.GetQuotedName(dialect));
                }
                @params[PersistentIdGeneratorParmsNames.Tables] = tables.ToString();
            }
            else
            {
                @params[PersistentIdGeneratorParmsNames.Tables] = tableName;
            }

            if (identifierGeneratorProperties != null)
            {
                ArrayHelper.AddAll(@params, identifierGeneratorProperties);
            }

            return(IdentifierGeneratorFactory.Create(identifierGeneratorStrategy, Type, @params, dialect));
        }