public static XElement BuildElements(CDatasource dataSource, string schema) { var doc = XDocument.Load(new StringReader(schema)); //doc.Save("schema.xml"); var dataSourceName = dataSource.serverName .Split('.').First().Split('\\').First() + "-" + dataSource.databaseName; return(new XElement("Configuration", new XAttribute("defaultDatasource", dataSourceName), new XAttribute("defaultDataset", dataSourceName), new XElement("Datasource", new XAttribute("name", dataSourceName), new XAttribute("provider", dataSource.providerName), new XElement("ConnectionString", dataSource.ConnectionString)), new XElement("Dataset", new XAttribute("name", dataSourceName), new XAttribute("dataSourceName", dataSourceName), new XAttribute("defaultTable", doc.Root.Elements("Table").First().Attribute("name").Value), from t in doc.Root.Elements("Table") select new XElement("Table", new XAttribute("catalog", t.Attribute("catalog").Value), new XAttribute("schemaName", t.Attribute("schemaName").Value), new XAttribute("name", t.Attribute("name").Value), new XAttribute("rows", t.Attribute("rows").Value), from c in t.Elements("Column") select new XElement("Column", from a in c.Attributes() select new XAttribute(a.Name.LocalName, a.Value) ) ), from t in doc.Root.Elements("Table") from c in t.Elements("Column") from r in c.Elements("Relationship") let isForeignKey = c.Attribute("isForeignKey").Value == "1" let fromTable = t.Attribute("name").Value let fromColumn = c.Attribute("name").Value let toTable = r.Attribute("toTable").Value where isForeignKey select new XElement("Relationship", new XAttribute("name", string.Format("{0}->{1}", fromTable, toTable)), new XAttribute("fromTable", fromTable), new XAttribute("toTable", toTable), new XElement("ColumnRelationship", new XAttribute("fromColumn", fromColumn), new XAttribute("toColumn", r.Attribute("toColumn").Value))) ) )); }
public static XElement BuildQueryElements(CDatasource dataSource, string schema) { var doc = XDocument.Load(new StringReader(schema)); var dataSourceName = dataSource.serverName .Split('.').First().Split('\\').First() + "-" + dataSource.databaseName; return(new XElement("Configuration", new XAttribute("defaultDatasource", dataSourceName), new XAttribute("defaultDataset", dataSourceName), new XElement("Datasource", new XAttribute("name", dataSourceName), new XAttribute("provider", dataSource.providerName), new XElement("ConnectionString", dataSource.ConnectionString)), new XElement("Dataset", new XAttribute("name", dataSourceName), new XAttribute("dataSourceName", dataSourceName), new XAttribute("defaultTable", doc.Root.Elements("Query").First().Attribute("name").Value), from q in doc.Root.Elements("Query") select new XElement("Query", new XAttribute("catalog", q.Attribute("catalog").Value), new XAttribute("schemaName", q.Attribute("schemaName").Value), new XAttribute("name", q.Attribute("name").Value), new XAttribute("type", q.Attribute("type").Value), new XAttribute("isStoreProcedure", q.Attribute("type").Value.Contains("P")), from p in q.Elements("Parameter") select new XElement("Parameter", from a in p.Attributes() select new XAttribute(a.Name.LocalName, a.Value) ), from t in q.Elements("Text") select new XCData(t.Value) )))); }
private async Task <CConfiguration> LoadAndSaveQueries(CDatasource ds, string datasetName) { var schema = await _dataSourceProvider.GetStoreProcsSchema(ds); if (!string.IsNullOrEmpty(schema)) { var builder = XmlBuilder.BuildQueryElements(ds, schema); if (null != builder) { var config = XmlHelper <CConfiguration> .LoadFromString(builder.ToString()); if (!File.Exists(DefaultStoreProcsConfigFile)) { XmlHelper <CConfiguration> .Save(DefaultStoreProcsConfigFile, config); } else { config.Dataset.FirstOrDefault().dataSourceName = datasetName; config.Dataset.FirstOrDefault().name = datasetName; config.Datasource.FirstOrDefault().name = datasetName; var currentConfig = XmlHelper <CConfiguration> .Load(DefaultStoreProcsConfigFile); currentConfig.Datasource.Add(config.Datasource.FirstOrDefault()); currentConfig.Dataset.Add(config.Dataset.FirstOrDefault()); XmlHelper <CConfiguration> .Save(DefaultStoreProcsConfigFile, currentConfig); return(currentConfig); } return(config); } } return(new CConfiguration()); }
private void LoadAndSaveConfiguration(CDatasource ds) { IsBusy = true; _schedulerProvider.Background.Schedule(async() => { try { _dataSourceProvider = _dataSourceProviders.FirstOrDefault(p => p.Name.Equals(ds.providerName)); _dataSourceProvider.SetLog(Logger.Log); var schema = await _dataSourceProvider.GetSchema(ds); if (!string.IsNullOrEmpty(schema)) { var builder = XmlBuilder.BuildElements(ds, schema); if (null != builder) { var config = XmlHelper <CConfiguration> .LoadFromString(builder.ToString()); if (!File.Exists(DefaultConfigFile)) { //fix relationship names foreach (var dset in config.Dataset) { foreach (var rel in dset.Relationship) { rel.name = rel.GetName(); } } XmlHelper <CConfiguration> .Save(DefaultConfigFile, config); Configuration = Configuration ?? InitConfiguration(config); SetDataset.Execute(Configuration.Dataset.FirstOrDefault()); IsEmpty = false; var queryConfig = await LoadAndSaveQueries(ds, SelectedDataset.name); _schedulerProvider.MainThread.Schedule(() => { SelectedDataset.Query.AddRange(queryConfig.Dataset.FirstOrDefault().Query); SelectedQuery = SelectedDataset.Query.FirstOrDefault(q => q.isStoreProcedure); SelectedScriptQuery = SelectedDataset.Query.LastOrDefault(q => q.isScript); }); } else { if (Configuration.Dataset.Any(cds => cds.name.Equals(config.defaultDataset))) { for (var i = 1; i < 10000; i++) { if (!Configuration.Dataset.Any(cds => cds.name.Equals(config.defaultDataset + i))) { var newName = config.Dataset.FirstOrDefault().name + i; config.Dataset.FirstOrDefault().name = newName; config.Dataset.FirstOrDefault().dataSourceName = newName; config.Datasource.FirstOrDefault().name = newName; break; } } } _schedulerProvider.MainThread.Schedule(() => { Configuration.Datasource.Add(config.Datasource.FirstOrDefault()); Configuration.Dataset.Add(config.Dataset.FirstOrDefault()); foreach (var dset in Configuration.Dataset) { dset.Query.Clear(); } XmlHelper <CConfiguration> .Save(DefaultConfigFile, Configuration); _dataSetConsumers.ToList().ForEach(c => c.OnConnected(Configuration, Logger.Log)); }); var queryConfig = await LoadAndSaveQueries(ds, config.Dataset.FirstOrDefault().name); _schedulerProvider.MainThread.Schedule(() => { foreach (var dset in queryConfig.Dataset) { Configuration.Dataset.FirstOrDefault(d => d.name.Equals(dset.name)) .Query.AddRange(dset.Query); } }); } } } } catch (Exception e) { Logger.Log.Error(e, "Exception while loading and saving configuration for {0}", ds.ConnectionString.SecureString()); MessageQueue.Enqueue(e.Message); } finally { _schedulerProvider.MainThread.Schedule(() => IsBusy = false); } }); }