Пример #1
0
        public void SetCommand()
        {
            CreateDatabase = new RelayCommand(() =>
            {
                if (string.IsNullOrEmpty(this.InputPath))
                {
                    return;
                }
                if (File.Exists(this.InputPath))
                {
                    return;
                }

                var db = new DbViewModel();
                db.CreateDatabase(this.InputPath);
                db.LoadDatabase(this.InputPath);
                Databases.Add(db);
                _history.DataAdd(this.InputPath);
            });

            LoadDatabase = new RelayCommand <string>(async(path) =>
            {
                if (string.IsNullOrEmpty(path))
                {
                    return;
                }
                if (!File.Exists(path))
                {
                    return;
                }
                var db = new DbViewModel();
                await TaskEx.Run(() =>
                {
                    db.LoadDatabase(path);
                });
                Databases.Add(db);
                _history.DataAdd(path);
            });

            ExecuteQuery = new RelayCommand(async() =>
            {
                if (CurrentDatabase == null || !CurrentDatabase.CanExecute())
                {
                    return;
                }
                var QueryResult = Datasource[0];
                QueryResult.Result.Clear();
                await TaskEx.Run(() =>
                {
                    QueryResult.GetExecuteResult(_queryInf, CurrentDatabase.ConnectionString, TagSelectedValue.Query);
                });
            });

            DropFile = new RelayCommand <string>((string path) =>
            {
                InputPath = path;
                RaisePropertyChanged(nameof(InputPath));
            });

            DbListDropFile = new RelayCommand <string>((string path) =>
            {
                LoadDatabase.Execute(path);
            });

            SetSqlTemplate = new RelayCommand <string>((string sqlKind) =>
            {
                TagSelectedValue.Query = CreateSqlSentence(SelectedTableItem, sqlKind);
                RaisePropertyChanged(nameof(Queries));
            });

            ReloadDatabase = new RelayCommand(() => { CurrentDatabase.ReloadDatabase(); });

            ShutdownDatabase = new RelayCommand(() => { Databases.Remove(CurrentDatabase); });

            AddTab = new RelayCommand(() =>
            {
                TagSelectedValue.Header = $"Query{Queries.Count}";
                Queries.Add(QueryTabViewModel.GetNewInstance());
                RaisePropertyChanged(nameof(Queries));
            });

            DeleteTabItem = new RelayCommand(() =>
            {
                var item         = TagSelectedValue;
                var idx          = Queries.IndexOf(item);
                TagSelectedIndex = idx - 1;
                RaisePropertyChanged(nameof(TagSelectedIndex));
                Queries.Remove(item);
            });

            LoadHistry = new RelayCommand(() => _history.LoadData(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)));

            SaveHistry = new RelayCommand(() => _history.SaveData(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)));

            OpenGitPage = new RelayCommand(() => System.Diagnostics.Process.Start("https://github.com/degarashi0913/FAManagementStudio"));

            PinedCommand = new RelayCommand(() =>
            {
                Datasource[0].Header = $"Pin{Datasource.Count}";
                Datasource.Insert(0, new QueryResultViewModel("Result"));
                SelectedResultIndex = 0;
                RaisePropertyChanged(nameof(SelectedResultIndex));
            });

            PinCommand = new RelayCommand <QueryResultViewModel>((data) =>
            {
                Datasource.Remove(data);
            });

            SetSqlDataTemplate = new RelayCommand <string>((s) =>
            {
                var table  = GetTreeViewTableName(SelectedTableItem);
                var result = "";
                if (s == "table")
                {
                    result = table.GetDdl();
                }
                else if (s == "insert")
                {
                    var colums           = table.Colums.Select(x => x.ColumName).ToArray();
                    var escapedColumsStr = string.Join(", ", colums.Select(x => EscapeKeyWord(x)).ToArray());

                    var insertTemplate = $"insert into {table.TableName} ({escapedColumsStr})";

                    var qry = new QueryInfo();
                    var res = qry.ExecuteQuery(CurrentDatabase.ConnectionString, CreateSelectStatement(table.TableName, colums)).First();

                    var sb = new StringBuilder();

                    foreach (DataRow row in res.View.Rows)
                    {
                        sb.Append(insertTemplate + " values(");
                        sb.Append(string.Join(", ", row.ItemArray.Select(x => x.GetType() == typeof(string) ? $"\'{x}\'" : $"{x}").ToArray()));
                        sb.AppendLine(");");
                    }
                    result = sb.ToString();
                }

                var idx            = Queries.IndexOf(TagSelectedValue);
                Queries[idx].Query = result;
                RaisePropertyChanged(nameof(Queries));
            });
        }
        public void SetCommand()
        {
            CreateDatabase = new RelayCommand(() =>
            {
                var vm = new NewDatabaseSettingsViewModel();
                MessengerInstance.Send(new MessageBase(vm, "NewDbSettingsWindowOpen"));

                if (string.IsNullOrEmpty(vm.Path))
                {
                    return;
                }

                var db = new DbViewModel();
                db.CreateDatabase(vm.Path, vm.Type, vm.CharSet);
                db.LoadDatabase(vm.Path);
                Databases.Add(db);
                _history.DataAdd(vm.Path);
            });

            LoadDatabase = new RelayCommand <string>(async(path) =>
            {
                if (string.IsNullOrEmpty(path))
                {
                    return;
                }
                if (!File.Exists(path))
                {
                    return;
                }
                var db     = new DbViewModel();
                var isLoad = await Task.Run(() =>
                {
                    return(db.LoadDatabase(path));
                });
                if (isLoad)
                {
                    Databases.Add(db);
                    _history.DataAdd(path);
                }
            });

            ExecuteQuery = new RelayCommand(async() =>
            {
                if (CurrentDatabase == null || !CurrentDatabase.CanExecute())
                {
                    return;
                }
                if (TagSelectedValue.IsNewResult && 0 < Datasource[0].Result.Count)
                {
                    Datasource[0].Pined = true;
                }
                var QueryResult = Datasource[0];
                QueryResult.Result.Clear();
                await Task.Run(() =>
                {
                    var inf = new QueryInfo {
                        ShowExecutePlan = TagSelectedValue.IsShowExecutionPlan
                    };
                    QueryResult.GetExecuteResult(inf, CurrentDatabase.ConnectionString, TagSelectedValue.Query);
                });
            });

            DropFile = new RelayCommand <string>((string path) =>
            {
                InputPath = path;
                RaisePropertyChanged(nameof(InputPath));
            });

            DbListDropFile = new RelayCommand <string>((string path) =>
            {
                LoadDatabase.Execute(path);
            });

            SetSqlTemplate = new RelayCommand <SqlKind>((SqlKind sqlKind) =>
            {
                TagSelectedValue.Query = CreateSqlSentence(SelectedTableItem, sqlKind);
                RaisePropertyChanged(nameof(Queries));
            });

            ExecLimitedSql = new RelayCommand <string>((count) =>
            {
                TagSelectedValue.Query = CreateSqlSentence(SelectedTableItem, SqlKind.Select, int.Parse(count));
                RaisePropertyChanged(nameof(Queries));
                ExecuteQuery.Execute(null);
            });

            ExecSqlTemplate = new RelayCommand <SqlKind>((SqlKind sqlKind) =>
            {
                SetSqlTemplate.Execute(sqlKind);
                ExecuteQuery.Execute(null);
            });

            ShutdownDatabase = new RelayCommand(() => { Databases.Remove(CurrentDatabase); });

            ChangeConfig = new RelayCommand(() =>
            {
                if (CurrentDatabase == null)
                {
                    return;
                }
                var vm = new ConnectionSettingsViewModel(CurrentDatabase.DbInfo);
                MessengerInstance.Send(new MessageBase(vm, "WindowOpen"));
            });

            ShowEntity = new RelayCommand(() =>
            {
                if (CurrentDatabase == null)
                {
                    return;
                }
                var vm = new EntityRelationshipViewModel(CurrentDatabase);
                MessengerInstance.Send(new MessageBase(vm, "EintityWindowOpen"));
            });

            AddTab = new RelayCommand(() =>
            {
                TagSelectedValue.Header = $"Query{Queries.Count}";
                Queries.Add(QueryTabViewModel.GetNewInstance());
                RaisePropertyChanged(nameof(Queries));
            });

            DeleteTabItem = new RelayCommand(() =>
            {
                var item         = TagSelectedValue;
                var idx          = Queries.IndexOf(item);
                TagSelectedIndex = idx - 1;
                RaisePropertyChanged(nameof(TagSelectedIndex));
                Queries.Remove(item);
            });

            LoadHistry = new RelayCommand(() => _history.LoadData(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)));

            SaveHistry = new RelayCommand(() => _history.SaveData(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)));

            OpenGitPage = new RelayCommand(() => System.Diagnostics.Process.Start("https://github.com/degarashi0913/FAManagementStudio"));

            PinedCommand = new RelayCommand(() =>
            {
                Datasource[0].Header = $"Pin{Datasource.Count}";
                Datasource.Insert(0, new QueryResultViewModel("Result"));
                SelectedResultIndex = 0;
                RaisePropertyChanged(nameof(SelectedResultIndex));
            }, () => 0 < Datasource[0].Result.Count);

            ReleasePinCommand = new RelayCommand <QueryResultViewModel>((data) =>
            {
                Datasource.Remove(data);
            });

            SetSqlDataTemplate = new RelayCommand <string>((s) =>
            {
                var table  = GetTreeViewTableName(CurrentDatabase, SelectedTableItem);
                var result = "";
                if (s == "table")
                {
                    result = table.GetDdl(CurrentDatabase);
                }
                else if (s == "insert")
                {
                    if (table is TableViewViewModel)
                    {
                        return;
                    }
                    var colums           = table.Colums.Select(x => x.ColumName).ToArray();
                    var escapedColumsStr = string.Join(", ", colums.Select(x => EscapeKeyWord(x)).ToArray());

                    var insertTemplate = $"insert into {table.TableName} ({escapedColumsStr})";

                    var qry = new QueryInfo();
                    var res = qry.ExecuteQuery(CurrentDatabase.ConnectionString, CreateSelectStatement(table.TableName, colums, 0)).First();

                    var sb = new StringBuilder();

                    foreach (DataRow row in res.View.Rows)
                    {
                        sb.Append(insertTemplate + " values(");
                        sb.Append(string.Join(", ", row.ItemArray.Select(x => x.GetType() == typeof(string) ? $"\'{x}\'" : $"{x}").ToArray()));
                        sb.AppendLine(");");
                    }
                    result = sb.ToString();
                }

                var idx            = Queries.IndexOf(TagSelectedValue);
                Queries[idx].Query = result;
                RaisePropertyChanged(nameof(Queries));
            });

            ShowPathSettings = new RelayCommand(() =>
            {
                var vm = new BasePathSettingsViewModel(QueryProjects);
                MessengerInstance.Send(new MessageBase(vm, "BasePathSettingsWindowOpen"));
            });

            ProjectItemOpen = new RelayCommand <object>((obj) =>
            {
                var item = obj as QueryProjectFileViewModel;
                if (item == null)
                {
                    return;
                }
                var idx = Queries.IndexOf(TagSelectedValue);
                try
                {
                    Queries[idx].Header = Path.GetFileNameWithoutExtension(item.FullPath);
                    Queries[idx].Query  = Queries[idx].FileLoad(item.FullPath, Encoding.UTF8);
                }
                catch (IOException) { }
            });
            ProjectItemDrop = new RelayCommand <string>((string path) =>
            {
                QueryProjects.Add(QueryProjectViewModel.GetData(path).First());
                AppSettingsManager.QueryProjectBasePaths.Add(path);
            });
        }