// Gets the data source of a mapping public static Wrapper getMappingDataSource(string targetField) { //Find the mapping, get the ID of the first source data source DataTable mapping = MappingEngine.getMappingSource(targetField); int sourceDataSource = (int)mapping.Rows[0]["SourceDataSource"]; return(getDataSourceWrapper(sourceDataSource)); }
//Query the virtual data source public static Dictionary <Wrapper, DataTable> query(string query) { // Basic implementation // Get the WHERE clause (this is all we are passing in for now) List <string> qWhere = query.Split(' ').ToList <string>(); string qWhereField = qWhere[0]; string qWhereOperator = qWhere[1]; string qWhereValue = qWhere[2]; // Find the mapping for the WHERE field DataTable mapping = MappingEngine.getMappingSource(qWhereField); // Store the table ID int whereSourceTable = (int)mapping.Rows[0]["SourceTable"]; // Store the datas source ID int whereDataSource = (int)mapping.Rows[0]["SourceDataSource"]; // Build a dictionary to hold our results Dictionary <Wrapper, DataTable> results = new Dictionary <Wrapper, DataTable>(); // We must now loop through all of our data sources and query them for their data foreach (KeyValuePair <string, Wrapper> dataSource in Program.dataSources) { // Store the wrapper so it's easier to reference Wrapper wrapper = dataSource.Value; Dictionary <string, Dictionary <string, string> > where = null; // Check if our mapping table contains this data source's ID bool contains = mapping.AsEnumerable().Any(row => dataSource.Value.getID() == row.Field <int>("SourceDataSource")); Console.WriteLine(contains); // Does our WHERE apply to this data source? if (dataSource.Value.getID() == whereDataSource) { // We need to build a WHERE clause for this data source // Get the name of the table the WHERE applies to string whereTableName = MappingEngine.getTableName(whereSourceTable); // Build the WHERE clause dictionary where = new Dictionary <string, Dictionary <string, string> >(); // Add the field name to the dictionary where.Add(qWhereField, new Dictionary <string, string>()); // Add the operator and value to the dictionary for that field where[qWhereField].Add("operator", qWhereOperator); where[qWhereField].Add("value", qWhereValue); } // Generate a list of columns that we want from this data source, for now we just get them all // TODO - Only get the columns we actually need (can we do this?) List <string> columns = new List <string> { "*" }; // Generate a list of tables that we want for this data source, for now we just get all the mapped tables // TODO - Only get tables we actually need (can we do this?) DataTable mappedTables = MappingEngine.getMappedTables(wrapper); List <string> tables = mappedTables.AsEnumerable().Select(row => row.Field <string>("Name")).ToList(); results[Program.dataSources[dataSource.Key]] = wrapper.queryWithWhere(columns, tables, where); } return(results); }