context: using System;
  using System.Data;
  using System.Collections.Generic;
  
  namespace Cdev.Frameworks.SchemaExplorer
  {
      [Serializable]
      public class TableSchema : TabularObjectBase
      {
          private List<ColumnSchema> columns;
          private DateTime dateCreated;
          private List<ColumnSchema> foreignKeyColumns;
          private List<TableKeySchema> foreignKeys;
          private List<IndexSchema> indexes;
          private List<TableKeySchema> keys;
          private List<ColumnSchema> nonForeignKeyColumns;
          private List<ColumnSchema> nonKeyColumns;
          private List<ColumnSchema> nonPrimaryKeyColumns;
          private string owner;
          private PrimaryKeySchema primaryKey;
          private List<TableKeySchema> primaryKeys;
  
          public TableSchema(DatabaseSchema database, string name, string owner, DateTime dateCreated)
          {
              this.database = database;
              this.name = name;
              this.owner = owner;
              this.dateCreated = dateCreated;
          }
  
          public TableSchema(DatabaseSchema database, string name, string owner, DateTime dateCreated, ExtendedProperty[] extendedProperties)
              : this(database, name, owner, dateCreated)
          {
              this.ExtendedProperties = new List<ExtendedProperty>();
              this.ExtendedProperties.AddRange(extendedProperties);
          }
  
          public List<ColumnSchema> Columns
          {
              get
              {
                  List<ColumnSchema> columns2;
                  if (this.columns != null)
                  {
                      columns2 = this.columns;
                  }
                  else
                  {
                      ColumnSchema[] columns = this.database.Provider.GetTableColumns(this.database.ConnectionString, this);
                      this.columns = new List<ColumnSchema>();
                      this.columns.AddRange(columns);
                      columns2 = this.columns;
                  }
                  return columns2;
              }
              set
              {
                  this.columns = value;
              }
          }
  
          public DateTime DateCreated
          {
              get
              {
                  return this.dateCreated;
              }
          }
  
          public List<ColumnSchema> ForeignKeyColumns
          {
              get
              {
                  List<ColumnSchema> foreignKeyColumns;
                  if (this.foreignKeyColumns != null)
                  {
                      foreignKeyColumns = this.foreignKeyColumns;
                  }
                  else
                  {
                      this.foreignKeyColumns = new List<ColumnSchema>();
                      foreach (ColumnSchema item in this.Columns)
                      {
                          if (item.IsForeignKeyMember)
                          {
                              this.foreignKeyColumns.Add(item);
                          }
                      }
                      foreignKeyColumns = this.foreignKeyColumns;
                  }
                  return foreignKeyColumns;
              }
          }
  
          public List<TableKeySchema> ForeignKeys
          {
              get
              {
                  List<TableKeySchema> foreignKeys;
                  if (this.foreignKeys != null)
                  {
                      foreignKeys = this.foreignKeys;
                  }
                  else
                  {
                      this.foreignKeys = new List<TableKeySchema>();
                      foreach (TableKeySchema item in this.Keys)
                      {
                          if (item.ForeignKeyTable == this)
                          {
                              this.foreignKeys.Add(item);
                          }
                      }
                      foreignKeys = this.foreignKeys;
                  }
                  return foreignKeys;
              }
          }
  
          public override string FullName
          {
              get
              {
                  return TableSchema.FormatFullName(this.Owner, this.Name);
              }
          }
  
          public bool HasPrimaryKey
          {
              get
              {
                  if (this.primaryKey == null)
                  {
                      PrimaryKeySchema tablePrimaryKey = this.Database.Provider.GetTablePrimaryKey(this.Database.ConnectionString, this);
                      this.primaryKey = tablePrimaryKey;
                  }
                  return this.primaryKey.MemberColumns != null && this.primaryKey.MemberColumns.Count > 0;
              }
          }
  
          public List<IndexSchema> Indexes
          {
              get
              {
                  List<IndexSchema> indexes;
                  if (this.indexes != null)
                  {
                      indexes = this.indexes;
                  }
                  else
                  {
                      this.indexes = new List<IndexSchema>(this.Database.Provider.GetTableIndexes(this.Database.ConnectionString, this));
                      indexes = this.indexes;
                  }
                  return indexes;
              }
              set
              {
                  this.indexes = value;
              }
          }
  
          public List<TableKeySchema> Keys
          {
              get
              {
                  List<TableKeySchema> keys;
                  if (this.keys != null)
                  {
                      keys = this.keys;
                  }
                  else
                  {
                      this.keys = new List<TableKeySchema>(this.Database.Provider.GetTableKeys(this.Database.ConnectionString, this));
                      keys = this.keys;
                  }
                  return keys;
              }
              set
              {
                  this.keys = value;
              }
          }
  
          public List<ColumnSchema> NonForeignKeyColumns
          {
              get
              {
                  List<ColumnSchema> nonForeignKeyColumns;
                  if (this.nonForeignKeyColumns != null)
                  {
                      nonForeignKeyColumns = this.nonForeignKeyColumns;
                  }
                  else
                  {
                      this.nonForeignKeyColumns = new List<ColumnSchema>();
                      foreach (ColumnSchema item in this.Columns)
                      {
                          if (!item.IsForeignKeyMember)
                          {
                              this.nonForeignKeyColumns.Add(item);
                          }
                      }
                      nonForeignKeyColumns = this.nonForeignKeyColumns;
                  }
                  return nonForeignKeyColumns;
              }
          }
  
          public List<ColumnSchema> NonKeyColumns
          {
              get
              {
                  List<ColumnSchema> nonKeyColumns;
                  if (this.nonKeyColumns != null)
                  {
                      nonKeyColumns = this.nonKeyColumns;
                  }
                  else
                  {
                      this.nonKeyColumns = new List<ColumnSchema>();
                      foreach (ColumnSchema col in this.Columns)
                      {
                          if (!col.IsPrimaryKeyMember && !col.IsForeignKeyMember)
                          {
                              this.nonKeyColumns.Add(col);
                          }
                      }
                      nonKeyColumns = this.nonKeyColumns;
                  }
                  return nonKeyColumns;
              }
          }
  
          public List<ColumnSchema> NonPrimaryKeyColumns
          {
              get
              {
                  List<ColumnSchema> nonPrimaryKeyColumns;
                  if (this.nonPrimaryKeyColumns != null)
                  {
                      nonPrimaryKeyColumns = this.nonPrimaryKeyColumns;
                  }
                  else
                  {
                      this.nonPrimaryKeyColumns = new List<ColumnSchema>();
                      foreach (ColumnSchema item in this.Columns)
                      {
                          if (!item.IsPrimaryKeyMember)
                          {
                              this.nonPrimaryKeyColumns.Add(item);
                          }
                      }
                      nonPrimaryKeyColumns = this.nonPrimaryKeyColumns;
                  }
                  return nonPrimaryKeyColumns;
              }
          }
  
          public string Owner
          {
              get
              {
                  return this.owner;
              }
          }
  
          public PrimaryKeySchema PrimaryKey
          {
              get
              {
                  PrimaryKeySchema primaryKey;
                  if (this.primaryKey != null)
                  {
                      primaryKey = this.primaryKey;
                  }
                  else
                  {
                      PrimaryKeySchema tablePrimaryKey = this.Database.Provider.GetTablePrimaryKey(this.Database.ConnectionString, this);
                      this.primaryKey = tablePrimaryKey;
                      primaryKey = this.primaryKey;
                  }
                  return primaryKey;
              }
              set
              {
                  this.primaryKey = value;
              }
          }
  
          public List<TableKeySchema> PrimaryKeys
          {
              get
              {
                  List<TableKeySchema> primaryKeys;
                  if (this.primaryKeys != null)
                  {
                      primaryKeys = this.primaryKeys;
                  }
                  else
                  {
                      this.primaryKeys = new List<TableKeySchema>();
                      foreach (TableKeySchema item in this.Keys)
                      {
                          if (item.PrimaryKeyTable == this)
                          {
                              this.primaryKeys.Add(item);
                          }
                      }
                      primaryKeys = this.primaryKeys;
                  }
                  return primaryKeys;
              }
          }
  
          public string SortName
          {
              get
              {
                  string result;
                  if (!string.IsNullOrEmpty(this.Owner))
                  {
  
gt:                     result = string.Format("{0} ({1})", this.Name, this.Owner);