Exemple #1
0
        /// <summary>
        /// Creates a SQL table from an entity type.
        /// </summary>
        /// <param name="entityType"></param>
        public SqlTable(SqlSchema schema, EntityType entityType)
            : base(entityType.NativeName.Name, entityType.Name)
        {
            // store the entity type for later use
            _entityType = entityType;

            // init...
            this.Initialize();

            // mbr - 04-10-2007 - set the schema...
            this.SetSchema(schema);

            // columns...
            foreach (EntityField field in entityType.Fields)
            {
                // mbr - 07-12-2005 - only non-extended...
                if (!(field.IsExtendedProperty))
                {
                    // mbr - 04-10-2007 - set schema...
                    SqlColumn column = new SqlColumn(field);
                    column.SetSchema(schema);
                    this.Columns.Add(column);
                }
            }

            // indexes...
            foreach (EntityIndex index in entityType.Indexes)
            {
                // mbr - 04-10-2007 - set schema...
                SqlIndex sqlIndex = new SqlIndex(this, index);
                sqlIndex.SetSchema(schema);
                this.Indexes.Add(sqlIndex);
            }

            // relationships...
            foreach (ChildToParentEntityLink relationship in entityType.Links)
            {
                this.LinksToParents.Add(new SqlChildToParentLink(schema, relationship));
            }
        }
        /// <summary>
        /// Creates a table from the given row.
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private SqlTable GetSchemaTable(DataRow row, SqlSchema schema)
        {
            if (row == null)
            {
                throw new ArgumentNullException("row");
            }
            if (schema == null)
            {
                throw new ArgumentNullException("schema");
            }

            // get the name...
            string nativeName = (string)row["table_name"];

            if (nativeName == null)
            {
                throw new ArgumentNullException("nativeName");
            }
            if (nativeName.Length == 0)
            {
                throw new ArgumentOutOfRangeException("'nativeName' is zero-length.");
            }

            try
            {
                // name...
                string name = SqlTable.SuggestSingularName(nativeName);
                name = CodeDomHelper.GetPascalName(name);

                // create...
                SqlTable schemaTable = new SqlTable(nativeName, name);

                // mbr - 04-10-2007 - set schema...
                schemaTable.SetSchema(schema);

                // get the columns...
                if (Connection == null)
                {
                    throw new InvalidOperationException("Connection is null.");
                }
                DataTable table = this.Connection.ExecuteDataTable(new SqlStatement("select column_name, is_nullable, data_type, character_maximum_length from information_schema.columns where table_name=@p0 order by ordinal_position",
                                                                                    new object[] { nativeName }, this.Dialect));
                foreach (DataRow columnRow in table.Rows)
                {
                    // create...
                    SqlColumn schemaColumn = this.GetSchemaColumn(columnRow);
                    schemaTable.Columns.Add(schemaColumn);

                    // mbr - 04-10-2007 - set owner...
                    schemaColumn.SetSchema(schemaTable.Schema);

                    // mbr - 01-11-2005 - added SQL Server specific stuff...
                    this.ColumnDiscovered(schemaColumn);
                }

                // fix...
                FixupCommonFlags(schemaTable);

                // mbr - 01-11-2005 - added opportunity to fixup...
                TableDiscovered(schemaTable);

                // return...
                return(schemaTable);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(string.Format("Failed when processing table '{0}'.", nativeName), ex);
            }
        }