Exemplo n.º 1
0
        private void OpenFile(FileInfo file)
        {
            IsBusy = true;
            Logger.Log.Verbose("OpenFile({@FullName})", file.FullName);

            _schedulerProvider.MainThread.Schedule(() =>
            {
                try
                {
                    Configuration = InitConfiguration(XmlHelper <CConfiguration> .Load(file.FullName));
                    IsEmpty       = false;
                    _dataSetConsumers.ToList().ForEach(c => c.OnConnected(Configuration, Logger.Log));
                    SetDataset.Execute(Configuration.Dataset.FirstOrDefault());

                    var qryConfig = XmlHelper <CConfiguration> .Load(file.FullName.Replace(".xml", "-store-procs.xml"));
                    if (qryConfig.Dataset.Any())
                    {
                        foreach (var ds in qryConfig.Dataset)
                        {
                            Configuration.Dataset.FirstOrDefault(d => d.name.Equals(ds.name))
                            .Query.AddRange(ds.Query);
                        }
                        SelectedQuery = qryConfig.Dataset.FirstOrDefault()
                                        .Query.FirstOrDefault(q => q.name.Equals(
                                                                  qryConfig.Dataset.FirstOrDefault().defaultTable));
                        SelectedScriptQuery = qryConfig.Dataset.LastOrDefault()
                                              .Query.FirstOrDefault(q => q.isScript);
                    }

                    if (File.Exists(file.FullName.Replace(".xml", "-store-procs-hist.xml")))
                    {
                        var histConfig = XmlHelper <CConfiguration> .Load(file.FullName.Replace(".xml", "-store-procs-hist.xml"));
                        if (histConfig.Dataset.Any())
                        {
                            foreach (var ds in histConfig.Dataset)
                            {
                                Configuration.Dataset.FirstOrDefault(d => d.name.Equals(ds.name))
                                .QueryHistory.AddRange(ds.Query);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Logger.Log.Error(e, "Exception while opening {@fullname}", file.FullName);
                    MessageQueue.Enqueue(e.Message);
                }
                finally
                {
                    IsBusy = false;
                }
            });
        }
Exemplo n.º 2
0
        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);
                }
            });
        }