/// <summary> /// Check table contains this record or not /// </summary> /// <param name="tableName"></param> /// <param name="record"></param> /// <returns></returns> public bool Contains(string tableName, object record) { // // Initialize result bool result = false; // // First, try to extract main information of record RecordSnapshot recordSnapshot = this.GetRecordSnapshot(tableName, record); TableSnapshot tableSnapshot = this._listTableSnapshot.FirstOrDefault(x => x.TableName.Equals(tableName)); if (tableSnapshot != null) { result = tableSnapshot.Contains(recordSnapshot); if (result == false) { result = tableSnapshot.IsDuplicatedUniqueColumn(recordSnapshot); } } // // Return result return(result); }
/// <summary> /// Get tablesnapshot by name /// </summary> /// <param name="tableName"></param> /// <returns></returns> public TableSnapshot GetTableSnapshot(string tableName) { // // Initialize result TableSnapshot result = new TableSnapshot(tableName); // // Get list of primary keys // For each primary key // Try to get it's properties in entity // Then add to hasTable DbSet dbSet = this._mapDatabase.GetTable(tableName); dbSet.Load(); List <string> listPrimaryKeys = this._mapDatabase.GetPrimaryKeyName(tableName); List <GroupUniqueColumnInformation> listGroupUniqueInformation = this._mapDatabase.GetGroupUniqueColumnInformation(tableName); TypeAccessor typeAccessor = this._mapDatabase.GetTableTypeAccessor(tableName); // // Foreach record in database // Try to get RecordSnapshot foreach (object record in dbSet) { RecordSnapshot recordSnapshot = new RecordSnapshot(); // // Get primary key value foreach (string primaryKey in listPrimaryKeys) { PrimaryKeySnapshot primaryKeySnapshot = new PrimaryKeySnapshot(primaryKey, typeAccessor[record, primaryKey]); recordSnapshot.Add(primaryKeySnapshot); } // // Get unqiue column value foreach (GroupUniqueColumnInformation groupUnique in listGroupUniqueInformation) { GroupUniqueColumnSnapshot groupUniqueSnapshot = new GroupUniqueColumnSnapshot(groupUnique.ConstraintName); foreach (UniqueColumnInformation uniqueColumnInfo in groupUnique.ListUniqueColumn) { UniqueColumnSnapshot uniqueColumnSnapshot = new UniqueColumnSnapshot(uniqueColumnInfo.ColumnName, typeAccessor[record, uniqueColumnInfo.ColumnName]); groupUniqueSnapshot.Add(uniqueColumnSnapshot); } recordSnapshot.Add(groupUniqueSnapshot); } result.Add(recordSnapshot); } // // Return result return(result); }
/// <summary> /// Get records /// With exist in both table /// </summary> /// <param name="table"></param> /// <returns></returns> public static TableSnapshot GetCommonRecord(TableSnapshot first, TableSnapshot second) { // // Initialize result TableSnapshot result = new TableSnapshot(first.TableName); // // Try to compare two table snapshot // Get common record from both table foreach (RecordSnapshot record in first._listRecordSnapshot) { if (second.Contains(record)) { result.Add(record); } } // // Return result return(result); }
/// <summary> /// Add new record to table snapshot /// </summary> /// <param name="tableName"></param> /// <param name="record"></param> public void Add(string tableName, object record) { // // Check // Are there any table in this snapshot contains tableName // If not, just continue if (this._tableTypeReflection.ContainsKey(tableName) == false) { return; } // // Firt, try to get record snapshot from original record RecordSnapshot recordSnapshot = this.GetRecordSnapshot(tableName, record); if (recordSnapshot == null) { return; } // // Then try to add to tableSnapshot TableSnapshot tableSnapshot = this._listTableSnapshot.FirstOrDefault(x => x.TableName.Equals(tableName)); if (tableSnapshot == null) { tableSnapshot = new TableSnapshot(tableName); tableSnapshot.Add(recordSnapshot); this._listTableSnapshot.Add(tableSnapshot); } else { tableSnapshot.Add(recordSnapshot); } }
/// <summary> /// Add new TableSnapshot to database /// </summary> /// <param name="table"></param> public void Add(TableSnapshot table) { this._listTableSnapshot.Add(table); }
/// <summary> /// Get tablesnapshot by name /// </summary> /// <param name="tableName"></param> /// <returns></returns> public TableSnapshot GetTableSnapshot(string tableName) { // // Initialize result TableSnapshot result = new TableSnapshot(tableName); // // Get list of primary keys // For each primary key // Try to get it's properties in entity // Then add to hasTable DbSet dbSet = this._mapDatabase.GetTable(tableName); dbSet.Load(); List<string> listPrimaryKeys = this._mapDatabase.GetPrimaryKeyName(tableName); List<GroupUniqueColumnInformation> listGroupUniqueInformation = this._mapDatabase.GetGroupUniqueColumnInformation(tableName); TypeAccessor typeAccessor = this._mapDatabase.GetTableTypeAccessor(tableName); // // Foreach record in database // Try to get RecordSnapshot foreach (object record in dbSet) { RecordSnapshot recordSnapshot = new RecordSnapshot(); // // Get primary key value foreach (string primaryKey in listPrimaryKeys) { PrimaryKeySnapshot primaryKeySnapshot = new PrimaryKeySnapshot(primaryKey, typeAccessor[record, primaryKey]); recordSnapshot.Add(primaryKeySnapshot); } // // Get unqiue column value foreach (GroupUniqueColumnInformation groupUnique in listGroupUniqueInformation) { GroupUniqueColumnSnapshot groupUniqueSnapshot = new GroupUniqueColumnSnapshot(groupUnique.ConstraintName); foreach (UniqueColumnInformation uniqueColumnInfo in groupUnique.ListUniqueColumn) { UniqueColumnSnapshot uniqueColumnSnapshot = new UniqueColumnSnapshot(uniqueColumnInfo.ColumnName, typeAccessor[record, uniqueColumnInfo.ColumnName]); groupUniqueSnapshot.Add(uniqueColumnSnapshot); } recordSnapshot.Add(groupUniqueSnapshot); } result.Add(recordSnapshot); } // // Return result return result; }
/// <summary> /// Get records /// With exist in both table /// </summary> /// <param name="table"></param> /// <returns></returns> public static TableSnapshot GetCommonRecord(TableSnapshot first, TableSnapshot second) { // // Initialize result TableSnapshot result = new TableSnapshot(first.TableName); // // Try to compare two table snapshot // Get common record from both table foreach (RecordSnapshot record in first._listRecordSnapshot) { if (second.Contains(record)) { result.Add(record); } } // // Return result return result; }