public IDictionary <string, object> GenerateValues() { IDictionary <string, object> generatedValues = new Dictionary <string, object>(); if (!HasIdentityColumn()) { var firstNonFKColumn = _tableColumnInfoList.First(c => c.IsForeignKey == false); object identityValue = RandomValues.ForColumn(firstNonFKColumn); generatedValues[firstNonFKColumn.Name] = identityValue; if (_dbDialect.RecordExists(_tableName, firstNonFKColumn.Name, identityValue)) { return(_dbDialect.GetValuesFromDatabase(_tableName, firstNonFKColumn.Name, identityValue)); } } // Generate the values GenerateValuesForColumns(generatedValues); // Insert values into database return(InsertInDatabase(generatedValues)); }
private void GenerateValuesForColumns(IDictionary <string, object> generatedValuesDict) { // For each column foreach (TableColumnInfo column in _tableColumnInfoList) { // Skip the column if it was already been generated. if (generatedValuesDict.ContainsKey(column.Name)) { continue; } // If the column type is a supported column type, and it is not Nullable if (column.ColumnType != ColumnType.Unsuported && !column.IsNullable) { if (column.IsForeignKey) { if (IsSelfReferencedForeignKey(column.ForeignKeyTable)) { if (_depth == 0) { // creates a new table generator to generate data for this foreign key TableGenerator foreignKeyTableGenerator = new TableGenerator(_dbDialect, column.ForeignKeyTable, _generatedRecords, _depth + 1); IDictionary <string, object> foreignKeyGeneratedData = foreignKeyTableGenerator.GenerateValues(); generatedValuesDict[column.ForeignKeyTable] = foreignKeyGeneratedData[column.ForeignKeyTable]; // TODO: Inner from database? } } // if (IsSelfReferencedForeignKey(column.ForeignKeyTable)) else { // Try to get an existing generated value for this foreign key column RecordIdentifier existingRecord = GetGeneratedRecord(column.ForeignKeyTable, column.ForeignKeyColumn); // record exists? if (existingRecord != null) { // Use the existing value generatedValuesDict[column.Name] = existingRecord.IdentifierValue; } else { if (column.IsNullable == false) { // creates a new table generator to generate data for this foreign key TableGenerator foreignKeyTableGenerator = new TableGenerator(_dbDialect, column.ForeignKeyTable, _generatedRecords, _depth + 1); generatedValuesDict[column.Name] = foreignKeyTableGenerator.GenerateValues()[column.ForeignKeyColumn]; } } } } // if (column.IsForeignKey) else if (column.IsIdentity == false) { object random = RandomValues.ForColumn(column); if (column.IsUnique) { // Keep generating random value while they are not unique while (_dbDialect.RecordExists(_tableName, column.Name, random)) { random = RandomValues.ForColumn(column); } } generatedValuesDict[column.Name] = random; } } } }