Beispiel #1
0
 public IndexSchema(TableSchema table, string name, bool isPrimaryKey, bool isUnique, bool isClustered, string[] memberColumns, ExtendedProperty[] extendedProperties)
     : this(table, name, isPrimaryKey, isUnique, isClustered, memberColumns)
 {
     this.database = table.Database;
     this.extendedProperties = new List<ExtendedProperty>();
     this.extendedProperties.AddRange(extendedProperties);
 }
Beispiel #2
0
 public TableKeySchema(DatabaseSchema database, string name, string[] foreignKeyMemberColumns, string foreignKeyTableOwner, string foreignKeyTable, string[] primaryKeyMemberColumns, string primaryKeyTableOwner, string primaryKeyTable)
 {
     this.database = database;
     this.name = name;
     if (foreignKeyTableOwner.Length > 0)
     {
         this.foreignKeyTable = this.Database.Tables.Where(p => p.Owner == foreignKeyTableOwner && p.Name == foreignKeyTable).FirstOrDefault();
     }
     else
     {
         this.foreignKeyTable = this.Database.Tables.Where(p => p.Name == foreignKeyTable).FirstOrDefault();
     }
     this.foreignKeyMemberColumns = new List<MemberColumnSchema>();
     for (int i = 0; i < foreignKeyMemberColumns.Length; i++)
     {
         string name2 = foreignKeyMemberColumns[i];
         this.foreignKeyMemberColumns.Add(new MemberColumnSchema(this.foreignKeyTable.Columns.Where(p => p.Name == name2).FirstOrDefault()));
     }
     if (primaryKeyTableOwner.Length > 0)
     {
         this.primaryKeyTable = this.Database.Tables.Where(p => p.Owner == foreignKeyTableOwner && p.Name == foreignKeyTable).FirstOrDefault();
     }
     else
     {
         this.primaryKeyTable = this.Database.Tables.Where(p => p.Name == foreignKeyTable).FirstOrDefault();
     }
     this.frimaryKeyMemberColumns = new List<MemberColumnSchema>();
     for (int j = 0; j < primaryKeyMemberColumns.Length; j++)
     {
         string name3 = primaryKeyMemberColumns[j];
         this.frimaryKeyMemberColumns.Add(new MemberColumnSchema(this.primaryKeyTable.Columns.Where(p => p.Name == name3).FirstOrDefault()));
     }
     this.extendedProperties = new List<ExtendedProperty>();
 }
Beispiel #3
0
 public TableKeySchema(string name, TableSchema foreignKeyTable, TableSchema primaryKeyTable, ExtendedProperty[] extendedProperties)
 {
     this.name = name;
     this.foreignKeyTable = foreignKeyTable;
     this.primaryKeyTable = primaryKeyTable;
     this.extendedProperties = new List<ExtendedProperty>();
     this.extendedProperties.AddRange(extendedProperties);
 }
Beispiel #4
0
 public IndexSchema(TableSchema table, string name, bool isPrimaryKey, bool isUnique, bool isClustered)
 {
     this.database = table.Database;
     this.table = table;
     this.name = name;
     this.isPrimaryKey = isPrimaryKey;
     this.isUnique = isUnique;
     this.isClustered = isClustered;
 }
Beispiel #5
0
 public PrimaryKeySchema(TableSchema table, string name, string[] memberColumns)
     : this(table, name)
 {
     this.memberColumns = new List<MemberColumnSchema>();
     for (int i = 0; i < memberColumns.Length; i++)
     {
         string name2 = memberColumns[i];
         this.memberColumns.Add(new MemberColumnSchema(this.Table.Columns.Where(p=>p.Name==name2).FirstOrDefault()));
     }
 }
Beispiel #6
0
 public IndexSchema(TableSchema table, string name, bool isPrimaryKey, bool isUnique, bool isClustered, string[] memberColumns)
     : this(table, name, isPrimaryKey, isUnique, isClustered)
 {
     this.database = table.Database;
     this.memberColumns = new List<MemberColumnSchema>();
     for (int i = 0; i < memberColumns.Length; i++)
     {
         string name2 = memberColumns[i];
         this.memberColumns.Add(new MemberColumnSchema(this.Table.Columns.Where(p => p.Name == name2).FirstOrDefault()));
     }
 }
Beispiel #7
0
 public ColumnSchema(TableSchema table, string name, DbType dataType, string nativeType, int size, int precision, int scale, bool allowDBNull)
 {
     this.database = table.Database;
     this.table = table;
     this.name = name;
     this.dataType = dataType;
     this.nativeType = nativeType;
     this.size = size;
     this.precision = precision;
     this.scale = scale;
     this.isAllowDBNull = allowDBNull;
 }
Beispiel #8
0
 public PrimaryKeySchema(TableSchema table, string name, string[] memberColumns, ExtendedProperty[] extendedProperties)
     : this(table, name, memberColumns)
 {
     this.extendedProperties = new List<ExtendedProperty>();
     this.extendedProperties.AddRange(extendedProperties);
 }
Beispiel #9
0
 public PrimaryKeySchema(TableSchema table, string name)
 {
     this.database = table.Database;
     this.table = table;
     this.name = name;
 }
Beispiel #10
0
 private bool IsDependantOfInternal(TableSchema table, List<TableSchema> checkedTables)
 {
     bool result;
     foreach (TableKeySchema item in this.PrimaryKeys)
     {
         if (item.PrimaryKeyTable.Equals(table))
         {
             result = true;
             return result;
         }
     }
     result = false;
     return result;
 }
Beispiel #11
0
		public ColumnSchema[] GetTableColumns(string connectionString, TableSchema table)
		{
			string commandText = string.Format("SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_OCTET_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE\r\n, CASE IS_NULLABLE WHEN 'NO' THEN 0 ELSE 1 END IS_NULLABLE,isnull(MS_Description.value,'') as Description\r\nFROM INFORMATION_SCHEMA.COLUMNS as a\r\n left join sys.extended_properties as MS_Description\r\n  on MS_Description.major_id=OBJECT_ID(a.TABLE_NAME) and MS_Description.minor_id=a.ORDINAL_Position\r\n            WHERE TABLE_CATALOG = '{0}' AND TABLE_NAME = '{1}' ORDER BY ORDINAL_POSITION", table.Database.Name, table.Name);
			List<ColumnSchema> list = new List<ColumnSchema>();
			using (DbConnection dbConnection = MsSqlSchemaProvider.CreateConnection(connectionString))
			{
				dbConnection.Open();
				DbCommand dbCommand = dbConnection.CreateCommand();
				dbCommand.CommandText = commandText;
				dbCommand.Connection = dbConnection;
				using (IDataReader dataReader = dbCommand.ExecuteReader(CommandBehavior.CloseConnection))
				{
					while (dataReader.Read())
					{
						string @string = dataReader.GetString(0);
						string string2 = dataReader.GetString(1);
						long num = (!dataReader.IsDBNull(2)) ? ((long)dataReader.GetInt32(2)) : 0L;
						int precision = (!dataReader.IsDBNull(3)) ? dataReader.GetByte(3) : 0;
						int scale = (!dataReader.IsDBNull(4)) ? dataReader.GetInt32(4) : 0;
						int ob = dataReader.GetInt32(5);
						bool allowDBNull = ob == 1;
						int size = (num < 2147483647L) ? ((int)num) : 2147483647;
						string Description = dataReader["Description"].ToString();
						DbType dbType = MsSqlSchemaProvider.GetDbType(string2, false);
						ExtendedProperty[] extendedProperties = new ExtendedProperty[1];
						list.Add(new ColumnSchema(table, @string, dbType, string2, size, precision, scale, allowDBNull)
						{
							Description = Description
						});
					}
					if (!dataReader.IsClosed)
					{
						dataReader.Close();
					}
				}
				if (dbConnection.State != ConnectionState.Closed)
				{
					dbConnection.Close();
				}
			}
			return list.ToArray();
		}
Beispiel #12
0
		public IndexSchema[] GetTableIndexes(string connectionString, TableSchema table)
		{
			List<IndexSchema> indexes = new List<IndexSchema>();
			List<string> memberColumns = new List<string>();
			IndexSchema[] result;
			using (DbConnection dbConnection = MsSqlSchemaProvider.CreateConnection(connectionString))
			{
				dbConnection.Open();
				DbCommand dbCommand = dbConnection.CreateCommand();
				dbCommand.CommandText = string.Format("declare  @tablename  nvarchar(200) select @tablename='{0}'  {1}", table.Owner + "." + table.Name, this.SQL_GetTableIndexes);
				dbCommand.Connection = dbConnection;
				using (IDataReader reader = dbCommand.ExecuteReader(CommandBehavior.CloseConnection))
				{
					while (reader.Read())
					{
						string name = reader.GetString(0);
						int status = reader.GetInt32(1);
						bool isPrimaryKey = (status & 2048) == 2048;
						bool isUnique = (status & 2) == 2;
						bool isClustered = (status & 16) == 16;
						bool isIgnoreDupKey = (status & 1) == 1;
						bool isHypothetical = (status & 32) == 32;
						bool isPadIndex = (status & 256) == 256;
						bool isDRIUniqueKey = (status & 2048) == 2048;
						bool isDRIIndex = (status & 6144) == 6144;
						bool isDropExist = (status & 32768) == 32768;
						bool isNoRecompute = (status & 16777216) == 16777216;
						bool isFullTextKey = (reader.GetInt32(53) & 1) == 1;
						bool isTable = (reader.GetInt32(54) & 1) == 1;
						bool isStatistics = (reader.GetInt32(55) & 1) == 1;
						bool isAutoStatistics = (reader.GetInt32(56) & 1) == 1;
						bool isConstraint = (reader.GetInt32(57) & 1) == 1;
						memberColumns.Clear();
						for (int x = 0; x < 16; x++)
						{
							if (!reader.IsDBNull(x + 4))
							{
								memberColumns.Add(reader.GetString(x + 4));
							}
						}
						IndexSchema idx = new IndexSchema(table, name, isPrimaryKey, isUnique, isClustered, memberColumns.ToArray());
						idx.ExtendedProperties.Add(new ExtendedProperty("CS_FileGroup", reader.GetString(52), DbType.AnsiString, PropertyStateEnum.ReadOnly));
						idx.ExtendedProperties.Add(new ExtendedProperty("CS_IsFullTextKey", isFullTextKey, DbType.Boolean, PropertyStateEnum.ReadOnly));
						idx.ExtendedProperties.Add(new ExtendedProperty("CS_IsTable", isTable, DbType.Boolean, PropertyStateEnum.ReadOnly));
						idx.ExtendedProperties.Add(new ExtendedProperty("CS_IsStatistics", isStatistics, DbType.Boolean, PropertyStateEnum.ReadOnly));
						idx.ExtendedProperties.Add(new ExtendedProperty("CS_IsAutoStatistics", isAutoStatistics, DbType.Boolean, PropertyStateEnum.ReadOnly));
						idx.ExtendedProperties.Add(new ExtendedProperty("CS_IsHypothetical", isHypothetical, DbType.Boolean, PropertyStateEnum.ReadOnly));
						idx.ExtendedProperties.Add(new ExtendedProperty("CS_IgnoreDupKey", isIgnoreDupKey, DbType.Boolean, PropertyStateEnum.ReadOnly));
						idx.ExtendedProperties.Add(new ExtendedProperty("CS_PadIndex", isPadIndex, DbType.Boolean, PropertyStateEnum.ReadOnly));
						idx.ExtendedProperties.Add(new ExtendedProperty("CS_DRIPrimaryKey", isPrimaryKey, DbType.Boolean, PropertyStateEnum.ReadOnly));
						idx.ExtendedProperties.Add(new ExtendedProperty("CS_DRIUniqueKey", isDRIUniqueKey, DbType.Boolean, PropertyStateEnum.ReadOnly));
						idx.ExtendedProperties.Add(new ExtendedProperty("CS_DRIIndex", isDRIIndex, DbType.Boolean, PropertyStateEnum.ReadOnly));
						idx.ExtendedProperties.Add(new ExtendedProperty("CS_DropExist", isDropExist, DbType.Boolean, PropertyStateEnum.ReadOnly));
						idx.ExtendedProperties.Add(new ExtendedProperty("CS_NoRecompute", isNoRecompute, DbType.Boolean, PropertyStateEnum.ReadOnly));
						idx.ExtendedProperties.Add(new ExtendedProperty("CS_IsConstraint", isConstraint, DbType.Boolean, PropertyStateEnum.ReadOnly));
						idx.ExtendedProperties.Add(new ExtendedProperty("CS_OrigFillFactor", reader.GetByte(3), DbType.Byte, PropertyStateEnum.ReadOnly));
						for (int x = 0; x < idx.MemberColumns.Count; x++)
						{
							if (!reader.IsDBNull(x + 20))
							{
								idx.MemberColumns[x].ExtendedProperties.Add(new ExtendedProperty("CS_IsDescending", reader.GetInt32(x + 20), DbType.Int32, PropertyStateEnum.ReadOnly));
							}
							if (!reader.IsDBNull(x + 36))
							{
								idx.MemberColumns[x].ExtendedProperties.Add(new ExtendedProperty("CS_IsComputed", reader.GetInt32(x + 36), DbType.Int32, PropertyStateEnum.ReadOnly));
							}
						}
						indexes.Add(idx);
					}
				}
				result = indexes.ToArray();
			}
			return result;
		}
Beispiel #13
0
		public TableKeySchema[] GetTableKeys(string connectionString, TableSchema table)
		{
			List<TableKeySchema> list = new List<TableKeySchema>();
			list.AddRange(this.GetMyTableKeys(connectionString, table));
			list.AddRange(this.GetOthersTableKeys(connectionString, table));
			return list.ToArray();
		}
Beispiel #14
0
		public PrimaryKeySchema GetTablePrimaryKey(string connectionString, TableSchema table)
		{
			string commandText = string.Format("SELECT t1.CONSTRAINT_NAME, t1.COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE t1  INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS t2  ON t2.TABLE_CATALOG = t1.TABLE_CATALOG  AND t2.TABLE_NAME = t1.TABLE_NAME  AND t2.CONSTRAINT_NAME = t1.CONSTRAINT_NAME WHERE t1.TABLE_CATALOG = '{0}' AND t1.TABLE_NAME = '{1}' AND t2.CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY t1.ORDINAL_POSITION", table.Database.Name, table.Name);
			DataSet dataSet = new DataSet();
			using (DbConnection dbConnection = MsSqlSchemaProvider.CreateConnection(connectionString))
			{
				dbConnection.ConnectionString = connectionString;
				dbConnection.Open();
				DbCommand dbCommand = dbConnection.CreateCommand();
				dbCommand.CommandText = commandText;
				dbCommand.Connection = dbConnection;
				dataSet = this.ConvertDataReaderToDataSet(dbCommand.ExecuteReader());
				if (dbConnection.State != ConnectionState.Closed)
				{
					dbConnection.Close();
				}
			}
			PrimaryKeySchema result;
			if (dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count > 0)
			{
				string name = dataSet.Tables[0].Rows[0]["CONSTRAINT_NAME"].ToString();
				string[] array = new string[dataSet.Tables[0].Rows.Count];
				for (int i = 0; i < dataSet.Tables[0].Rows.Count; i++)
				{
					array[i] = dataSet.Tables[0].Rows[i]["COLUMN_NAME"].ToString();
				}
				result = new PrimaryKeySchema(table, name, array);
			}
			else
			{
				result = null;
			}
			return result;
		}
Beispiel #15
0
 public bool IsDependantOf(TableSchema table)
 {
     List<TableSchema> checkedTables = new List<TableSchema>();
     return this.IsDependantOfInternal(table, checkedTables);
 }
Beispiel #16
0
 public ColumnSchema(TableSchema table, string name, DbType dataType, string nativeType, int size, byte precision, int scale, bool allowDBNull, ExtendedProperty[] extendedProperties)
     : this(table, name, dataType, nativeType, size, precision, scale, allowDBNull)
 {
     this.extendedProperties = new List<ExtendedProperty>();
     this.extendedProperties.AddRange(extendedProperties);
 }
Beispiel #17
0
		public DataTable GetTableData(string connectionString, TableSchema table)
		{
			string commandText = string.Format("SELECT * FROM {0}", table.Name);
			DataSet dataSet = new DataSet();
			using (DbConnection dbConnection = MsSqlSchemaProvider.CreateConnection(connectionString))
			{
				dbConnection.Open();
				DbCommand dbCommand = dbConnection.CreateCommand();
				dbCommand.CommandText = commandText;
				dbCommand.Connection = dbConnection;
				dataSet = this.ConvertDataReaderToDataSet(dbCommand.ExecuteReader());
				if (dbConnection.State != ConnectionState.Closed)
				{
					dbConnection.Close();
				}
			}
			DataTable result;
			if (dataSet.Tables.Count > 0)
			{
				result = dataSet.Tables[0];
			}
			else
			{
				result = new DataTable(table.Name);
			}
			return result;
		}