/// <summary> /// /// </summary> /// <param name="table"></param> /// <returns>Zero-based index of table in TableBuilder; -1 if there is no such table</returns> public int GetIndex(Table table) { IEnumerator <Table> enumer = Tables.Cast <Table>().GetEnumerator(); int index = -1; if (enumer.MoveNext()) { index++; if (enumer.Current.Equals(table)) { return(index); } } return(-1); }
public DataSet ToUntypedDataSet() { var dataSet = new DataSet(); dataSet.DataSetName = DataSetName; dataSet.CaseSensitive = CaseSensitive; dataSet.EnforceConstraints = EnforceConstraints; dataSet.Locale = Locale; dataSet.Namespace = Namespace; dataSet.Prefix = Prefix; dataSet.RemotingFormat = RemotingFormat; foreach (var typedTable in Tables.Cast <DataTable>()) { var dataTable = new DataTable(); dataTable.TableName = typedTable.TableName; dataTable.DisplayExpression = typedTable.DisplayExpression; dataTable.MinimumCapacity = typedTable.MinimumCapacity; dataTable.Namespace = typedTable.Namespace; dataTable.Prefix = typedTable.Prefix; dataTable.RemotingFormat = typedTable.RemotingFormat; foreach (var column in typedTable.Columns.Cast <DataColumn>()) { var dataColumn = new DataColumn(); dataColumn.ColumnName = column.ColumnName; dataColumn.AllowDBNull = column.AllowDBNull; dataColumn.AutoIncrement = column.AutoIncrement; dataColumn.AutoIncrementSeed = column.AutoIncrementSeed; dataColumn.AutoIncrementStep = column.AutoIncrementStep; dataColumn.Caption = column.Caption; dataColumn.ColumnMapping = column.ColumnMapping; dataColumn.DataType = column.DataType; dataColumn.DateTimeMode = column.DateTimeMode; dataColumn.DefaultValue = column.DefaultValue; dataColumn.Expression = column.Expression; dataColumn.MaxLength = column.MaxLength; dataColumn.Namespace = column.Namespace; dataColumn.Prefix = column.Prefix; dataColumn.ReadOnly = column.ReadOnly; foreach (var property in column.ExtendedProperties.Cast <DictionaryEntry>()) { dataColumn.ExtendedProperties.Add(property.Key, property.Value); } dataTable.Columns.Add(dataColumn); } foreach (var row in typedTable.Rows.Cast <DataRow>()) { dataTable.ImportRow(row); } dataTable.PrimaryKey = typedTable.PrimaryKey .Select(col => dataTable.Columns[col.ColumnName]) .ToArray(); foreach (var property in typedTable.ExtendedProperties.Cast <DictionaryEntry>()) { dataTable.ExtendedProperties.Add(property.Key, property.Value); } foreach (var constraint in typedTable.Constraints.Cast <Constraint>()) { if (!Relations.Cast <DataRelation>().Any(rel => (rel.ChildKeyConstraint == constraint) || (rel.ParentKeyConstraint == constraint))) { if (constraint is UniqueConstraint uniqueConstraint) { if (!uniqueConstraint.IsPrimaryKey) { dataTable.Constraints.Add(new UniqueConstraint( name: uniqueConstraint.ConstraintName, column: dataTable.Columns[uniqueConstraint.Columns.Single().ColumnName])); } } else { throw new Exception($"Don't know how to clone a constraint of type {constraint.GetType()}"); } } } dataSet.Tables.Add(dataTable); } foreach (var property in ExtendedProperties.Cast <DictionaryEntry>()) { dataSet.ExtendedProperties.Add(property.Key, property.Value); } foreach (var relation in Relations.Cast <DataRelation>()) { // NB: In the context of the unit test, this is assuming that there is only one column in a relation. var dataRelation = new DataRelation( relation.RelationName, parentColumn: dataSet.Tables[relation.ParentTable.TableName].Columns[relation.ParentColumns.Single().ColumnName], childColumn: dataSet.Tables[relation.ChildTable.TableName].Columns[relation.ChildColumns.Single().ColumnName]); foreach (var property in relation.ExtendedProperties.Cast <DictionaryEntry>()) { dataRelation.ExtendedProperties.Add(property.Key, property.Value); } dataSet.Relations.Add(dataRelation); } return(dataSet); }
public async Task Load(IProgress <int> progress) { if (!File.Exists(FilePath)) { throw new FileNotFoundException("The specified file does not exist."); } using (reader = new StreamReader(File.OpenRead(FilePath))) { lines = File.ReadAllLines(FilePath).ToList(); int percent = 0; for (int i = 0; i < lines.Count; i++) { string str = lines[i]; if (String.IsNullOrWhiteSpace(str)) { continue; } str = str.Trim(); str = Regex.Replace(str, @"\t+", "\t"); if (str.StartsWith(";")) { var comment = str.Split(';')[1]; if (String.IsNullOrWhiteSpace(comment)) { continue; } } else if (str.ToLower().StartsWith("#table")) { Tables.Add(new ShineTable(str.Split('\t')[1])); } else if (str.ToLower().StartsWith("#columntype")) { if (Tables.Count == 0) { throw new Exception("There is not an existing table to add the column types to."); } ((ShineTable)Tables[Tables.Count - 1]).ColumnTypes .AddRange(str.Split('\t') .Skip(1)); } else if (str.ToLower().StartsWith("#columnname")) { if (Tables.Count == 0) { throw new Exception("There is not an existing table to add the column names to."); } List <string> columnNames = str.Split('\t').Skip(1).ToList(); for (int x = 0; x < columnNames.Count(); x++) { Tables[Tables.Count - 1].Columns.Add(columnNames[x]); } } else if (str.ToLower().StartsWith("#recordin")) { if (Tables.Count == 0) { throw new Exception("There is not an existing table to add the rows to."); } List <string> values = str.Split('\t').Skip(1).ToList(); string table = values[0]; var row = Tables.Cast <DataTable>().AsEnumerable().Where(x => x.TableName == table).First().NewRow(); values.RemoveAt(0); row.ItemArray = values.ToArray <string>(); Tables.Cast <DataTable>().AsEnumerable().Where(x => x.TableName == table).First().Rows.Add(row); } else if (str.ToLower().StartsWith("#record")) { if (Tables.Count == 0) { throw new Exception("There is not an existing table to add the rows to."); } string[] values = str.Split('\t').Skip(1).ToArray(); var row = Tables[Tables.Count - 1].NewRow(); row.ItemArray = values; Tables[Tables.Count - 1].Rows.Add(row); } else if (str.Contains(";")) { str = str.Replace(";", ""); str = str.Trim(); } percent = Convert.ToInt32(((double)i / lines.Count) * 100); progress.Report(percent); } } }