Ejemplo n.º 1
0
        /// <summary>
        /// Creates the schema.
        /// </summary>
        /// <returns></returns>
        // mbr - 02-10-2007 - added optional specification.
        public virtual SqlSchema GetSchema(GetSchemaArgs args)
        {
            // create...
            SqlSchema schema = new SqlSchema();

            // gets a datatable of information_schema...
            if (Connection == null)
            {
                throw new InvalidOperationException("Connection is null.");
            }

            // sql...
            SqlStatement sql = new SqlStatement(this.Dialect);

            // mbr - 02-10-2007 - build the sql, now including the search specification...
            StringBuilder builder = new StringBuilder();

            builder.Append("SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'");
            if (args.ConstrainTableNames.Count > 0)
            {
                builder.Append(" AND (");
                this.AppendTableNameConstraint(sql, builder, args.ConstrainTableNames);
                builder.Append(")");
            }
            if (args.ConstrainSchemaNames.Count > 0)
            {
                builder.Append(" AND (");
                this.AppendSchemaNameConstraint(sql, builder, args.ConstrainSchemaNames);
                builder.Append(")");
            }
            builder.Append(" ORDER BY TABLE_NAME");

            // get...
            sql.CommandText = builder.ToString();
            DataTable table = this.Connection.ExecuteDataTable(sql);

            // walk...
            foreach (DataRow row in table.Rows)
            {
                // create a new schema table...
                SqlTable schemaTable = this.GetSchemaTable(row, schema);
                schema.Tables.Add(schemaTable);

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

            // return...
            return(schema);
        }
Ejemplo n.º 2
0
        /// <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);
            }
        }