Inheritance: ViewModelBase
        public void GetDdlTest1()
        {
            var dbVm = new DbViewModel();
            var table = new TableViewModel("TEST");
            dbVm.Tables.Add(table);

            var col1 = new ColumInfo("COL1", new FieldType(8, null, null, null, null, 4), new ColumConstraintsInfo(ConstraintsKind.Primary), "RDB$1", false, true, "");
            table.Colums.Add(new ColumViewMoodel(col1));
            var col2 = new ColumInfo("COL2", new FieldType(37, null, 100, null, null, 400), new ColumConstraintsInfo(ConstraintsKind.None), "RDB$2", false, true, "");
            table.Colums.Add(new ColumViewMoodel(col2));
            var col3 = new ColumInfo("COL3", new FieldType(35, null, null, null, null, 8), new ColumConstraintsInfo(ConstraintsKind.None), "RDB$3", false, true, "");
            table.Colums.Add(new ColumViewMoodel(col3));
            var col4 = new ColumInfo("COL4", new FieldType(261, null, null, null, null, 8), new ColumConstraintsInfo(ConstraintsKind.None), "RDB$4", true, true, "");
            table.Colums.Add(new ColumViewMoodel(col4));

            var idx = new IndexInfo();
            idx.Name = "RDB$PRIMARYKEY1";
            idx.TableName = table.TableName;
            idx.Kind = ConstraintsKind.Primary;
            idx.FieldNames.Add(col1.ColumName);
            var idxVm = new IndexViewModel(idx);

            dbVm.Indexes.Add(idxVm);
            table.Indexs.Add(idxVm);

            table.GetDdl(dbVm).Is(
            "CREATE TABLE TEST (" + Environment.NewLine +
            "  COL1 INTEGER NOT NULL," + Environment.NewLine +
            "  COL2 VARCHAR(100) NOT NULL," + Environment.NewLine +
            "  COL3 TIMESTAMP NOT NULL," + Environment.NewLine +
            "  COL4 BLOB," + Environment.NewLine +
            "  PRIMARY KEY (COL1)" + Environment.NewLine +
            ")");
        }
 public AdditionalDbInfoControl(DbViewModel db)
 {
     ContentChange = new RelayCommand<string>(x => ChangeContent(x));
     _db = db;
     CurrentContent = ContentList;
     RaisePropertyChanged(nameof(CurrentContent));
 }
        public AdditionalDbInfoControl(DbViewModel db)
        {
            ContentChange = new RelayCommand <string>(x => ChangeContent(x));

            RefrechData(db);

            RaisePropertyChanged(nameof(CurrentContent));
        }
        public void RefrechData(DbViewModel db)
        {
            ContentList.Clear();

            ContentList.Add(new AdditionalDbInfoViewModel("Trigger", db.Triggers.Cast <IAddtionalDbInfo>().ToList()));
            ContentList.Add(new AdditionalDbInfoViewModel("Index", db.Indexes.Cast <IAddtionalDbInfo>().ToList()));
            ContentList.Add(new AdditionalDbInfoViewModel("Domain", db.Domains.Cast <IAddtionalDbInfo>().ToList()));

            CurrentContent = ContentList;
        }
        private ITableViewModel GetTreeViewTableName(DbViewModel db, object treeitem)
        {
            var table = treeitem as ITableViewModel;

            if (table == null)
            {
                return(db.Tables.Where(x => 0 < x.Colums.Count(c => c == (ColumViewMoodel)treeitem)).First());
            }
            return(table);
        }
        public string GetDdl(DbViewModel dbVm)
        {
            var colums = Colums.Select(x =>
            {
                var sql = $"{x.ColumName} {x.ColumType}";
                if (!x.NullFlag)
                {
                    sql += " not null";
                }
                return(sql.ToUpper());
            });

            var index = Indexs
                        .Select(x =>
            {
                var sql = x.IndexName.StartsWith("rdb", System.StringComparison.OrdinalIgnoreCase) ? "" : $"CONSTRAINT {x.IndexName} ";
                switch (x.Kind)
                {
                case ConstraintsKind.Primary:
                    sql += $"PRIMARY KEY ({string.Join(", ", x.FieldNames.ToArray())})";
                    break;

                case ConstraintsKind.Foreign:
                    var idx = dbVm.Indexes.Where(dbIdx => dbIdx.IndexName == x.IndexName).First();
                    sql    += $"FOREIGN KEY({string.Join(", ", x.FieldNames.ToArray())}) REFERENCES {idx.TableName} ({string.Join(", ", idx.FieldNames.ToArray())})";
                    break;

                case ConstraintsKind.Unique:
                    sql += $"UNIQUE ({string.Join(", ", x.FieldNames.ToArray())})";
                    break;

                default:
                    return("");
                }
                return(sql);
            });

            var domain = Colums.Where(x => x.IsDomainType)
                         .Select(x => new { x.ColumType, x.ColumDataType })
                         .Distinct()
                         .Select(x => $"CREATE DOMAIN {x.ColumType} AS {x.ColumDataType};\r\n");
            var domainStr = string.Join("", domain.ToArray());

            return(domainStr + $"CREATE TABLE {TableName} ({Environment.NewLine}  { string.Join($",{Environment.NewLine}  ", colums.Union(index).ToArray()) + Environment.NewLine})");
        }
        public void GetDdlTest2()
        {
            var dbVm = new DbViewModel();
            var table = new TableViewModel("TEST");
            dbVm.Tables.Add(table);

            var col1 = new ColumInfo("COL1", new FieldType(8, null, null, null, null, 4), new ColumConstraintsInfo(ConstraintsKind.Primary), "RDB$1", false, true, "");
            table.Colums.Add(new ColumViewMoodel(col1));
            var col2 = new ColumInfo("COL2", new FieldType(37, null, 100, null, null, 400), new ColumConstraintsInfo(ConstraintsKind.Primary), "RDB$2", false, true, "");
            table.Colums.Add(new ColumViewMoodel(col2));
            var col3 = new ColumInfo("COL3", new FieldType(35, null, null, null, null, 8), new ColumConstraintsInfo(ConstraintsKind.None), "RDB$3", false, true, "");
            table.Colums.Add(new ColumViewMoodel(col3));
            var col4 = new ColumInfo("COL4", new FieldType(261, null, null, null, null, 8), new ColumConstraintsInfo(ConstraintsKind.None), "RDB$4", true, true, "");
            table.Colums.Add(new ColumViewMoodel(col4));
            var col5 = new ColumInfo("COL5", new FieldType(7, 1, null, 4, 0, 4), new ColumConstraintsInfo(ConstraintsKind.None), "RDB$4", true, true, "");
            table.Colums.Add(new ColumViewMoodel(col5));
            var col6 = new ColumInfo("COL6", new FieldType(8, 2, null, 4, -2, 4), new ColumConstraintsInfo(ConstraintsKind.None), "RDB$4", true, true, "");
            table.Colums.Add(new ColumViewMoodel(col6));
            var col7 = new ColumInfo("COL7", new FieldType(16, 2, null, 10, -4, 8), new ColumConstraintsInfo(ConstraintsKind.None), "RDB$4", true, true, "");
            table.Colums.Add(new ColumViewMoodel(col7));

            var idx = new IndexInfo();
            idx.Name = "COMPLEXKEY";
            idx.TableName = table.TableName;
            idx.Kind = ConstraintsKind.Primary;
            idx.FieldNames.Add(col1.ColumName);
            idx.FieldNames.Add(col2.ColumName);
            var idxVm = new IndexViewModel(idx);

            dbVm.Indexes.Add(idxVm);
            table.Indexs.Add(idxVm);

            table.GetDdl(dbVm).Is(
            "CREATE TABLE TEST (" + Environment.NewLine +
            "  COL1 INTEGER NOT NULL," + Environment.NewLine +
            "  COL2 VARCHAR(100) NOT NULL," + Environment.NewLine +
            "  COL3 TIMESTAMP NOT NULL," + Environment.NewLine +
            "  COL4 BLOB," + Environment.NewLine +
            "  COL5 NUMERIC(4)," + Environment.NewLine +
            "  COL6 DECIMAL(4,2)," + Environment.NewLine +
            "  COL7 DECIMAL(10,4)," + Environment.NewLine +
            "  CONSTRAINT COMPLEXKEY PRIMARY KEY (COL1, COL2)" + Environment.NewLine +
            ")");
        }
Esempio n. 8
0
        private void CreateGraphToVisualize(DbViewModel db)
        {
            foreach (var item in db.Tables)
            {
                var table = new EntityTableModel {
                    TableName = item.TableName, Colums = item.Colums
                };
                Graph.AddVertex(table);
                _data.Add(table);
            }

            foreach (var table in _data)
            {
                foreach (var col in table.Colums.Where(x => (x.ConstraintsInf?.Kind & ConstraintsKind.Foreign) == ConstraintsKind.Foreign))
                {
                    Graph.AddEdge(new Edge <object>(_data.Find(x => x.TableName == col.ConstraintsInf.ForeignKeyTableName), table));
                }
            }
        }
        public void GetDdlTest_ForignKey1()
        {
            var dbVm = new DbViewModel();
            var table = new TableViewModel("TEST");
            dbVm.Tables.Add(table);

            var col1 = new ColumInfo("COL1", new FieldType(8, null, null, null, null, 4), new ColumConstraintsInfo(ConstraintsKind.Primary), "RDB$1", false, true, "");
            table.Colums.Add(new ColumViewMoodel(col1));
            var col2 = new ColumInfo("COL2", new FieldType(37, null, 100, null, null, 400), new ColumConstraintsInfo(ConstraintsKind.Foreign), "RDB$2", false, true, "");
            table.Colums.Add(new ColumViewMoodel(col2));
            var col3 = new ColumInfo("COL3", new FieldType(35, null, null, null, null, 8), new ColumConstraintsInfo(ConstraintsKind.Foreign), "RDB$3", false, true, "");
            table.Colums.Add(new ColumViewMoodel(col3));

            var idx = new IndexInfo();
            idx.Name = "RDB$PRIMARYKEY1";
            idx.TableName = table.TableName;
            idx.Kind = ConstraintsKind.Primary;
            idx.FieldNames.Add(col1.ColumName);
            var idxVm = new IndexViewModel(idx);

            dbVm.Indexes.Add(idxVm);
            table.Indexs.Add(idxVm);

            var fpIdx1 = new IndexInfo();
            fpIdx1.Name = "RDB$PRIMARYKEY2";
            fpIdx1.TableName = "MASTER";
            fpIdx1.Kind = ConstraintsKind.Primary;
            fpIdx1.FieldNames.Add("M_COL1");
            var fpIdxVm1 = new IndexViewModel(fpIdx1);

            dbVm.Indexes.Add(fpIdxVm1);

            var idx1 = new IndexInfo();
            idx1.Name = "RDB$FOREIGNKEY1";
            idx1.TableName = table.TableName;
            idx1.Kind = ConstraintsKind.Foreign;
            idx1.ForigenKeyName = "RDB$PRIMARYKEY2";
            idx1.FieldNames.Add(col2.ColumName);
            var idxVm1 = new IndexViewModel(idx1);

            dbVm.Indexes.Add(idxVm1);
            table.Indexs.Add(idxVm1);

            var fpIdx2 = new IndexInfo();
            fpIdx2.Name = "RDB$PRIMARYKEY3";
            fpIdx2.TableName = "MASTER";
            fpIdx2.Kind = ConstraintsKind.Primary;
            fpIdx2.FieldNames.Add("M_COL2");
            var fpIdxVm2 = new IndexViewModel(fpIdx2);

            dbVm.Indexes.Add(fpIdxVm2);

            var idx2 = new IndexInfo();
            idx2.Name = "C_FOREIGNKEY";
            idx2.TableName = table.TableName;
            idx2.Kind = ConstraintsKind.Foreign;
            idx2.ForigenKeyName = "RDB$PRIMARYKEY3";
            idx2.DeleteRule = "SET DEFAULT";
            idx2.UpdateRule = "CASCADE";
            idx2.FieldNames.Add(col3.ColumName);
            var idxVm2 = new IndexViewModel(idx2);

            dbVm.Indexes.Add(idxVm2);
            table.Indexs.Add(idxVm2);

            table.GetDdl(dbVm).Is(
            "CREATE TABLE TEST (" + Environment.NewLine +
            "  COL1 INTEGER NOT NULL," + Environment.NewLine +
            "  COL2 VARCHAR(100) NOT NULL," + Environment.NewLine +
            "  COL3 TIMESTAMP NOT NULL," + Environment.NewLine +
            "  PRIMARY KEY (COL1)," + Environment.NewLine +
            "  FOREIGN KEY (COL2) REFERENCES MASTER (M_COL1)," + Environment.NewLine +
            "  CONSTRAINT C_FOREIGNKEY FOREIGN KEY (COL3) REFERENCES MASTER (M_COL2) ON DELETE SET DEFAULT ON UPDATE CASCADE" + Environment.NewLine +
            ")");
        }
        public void GetDdlTest4()
        {
            var dbVm = new DbViewModel();
            var table = new TableViewViewModel("SNOW_LINE", "SELECT CITY, STATE, ALTITUDE FROM CITIES WHERE ALTITUDE > 5000");
            dbVm.Tables.Add(table);

            var col1 = new ColumInfo("CITY", new FieldType(8, null, null, null, null, 4), new ColumConstraintsInfo(ConstraintsKind.Primary), "SARARY", false, true, "");
            table.Colums.Add(new ColumViewMoodel(col1));
            var col2 = new ColumInfo("STATE", new FieldType(37, null, 100, null, null, 400), new ColumConstraintsInfo(ConstraintsKind.Primary), "NAME", false, true, "");
            table.Colums.Add(new ColumViewMoodel(col2));
            var col3 = new ColumInfo("SNOW_ALTITUDE", new FieldType(35, null, null, null, null, 8), new ColumConstraintsInfo(ConstraintsKind.None), "RDB$3", false, true, "");
            table.Colums.Add(new ColumViewMoodel(col3));

            table.GetDdl(dbVm).Is(
            "CREATE VIEW SNOW_LINE (CITY, STATE, SNOW_ALTITUDE) AS" + Environment.NewLine +
            "SELECT CITY, STATE, ALTITUDE FROM CITIES WHERE ALTITUDE > 5000");
        }
        public void GetDdlTest3()
        {
            var dbVm = new DbViewModel();
            var table = new TableViewModel("TEST");
            dbVm.Tables.Add(table);

            var col1 = new ColumInfo("COL1", new FieldType(8, null, null, null, null, 4), new ColumConstraintsInfo(ConstraintsKind.Primary), "SARARY", false, true, "");
            table.Colums.Add(new ColumViewMoodel(col1));
            var col2 = new ColumInfo("COL2", new FieldType(37, null, 100, null, null, 400), new ColumConstraintsInfo(ConstraintsKind.Primary), "NAME", false, true, "");
            table.Colums.Add(new ColumViewMoodel(col2));
            var col3 = new ColumInfo("COL3", new FieldType(35, null, null, null, null, 8), new ColumConstraintsInfo(ConstraintsKind.None), "RDB$3", false, true, "");
            table.Colums.Add(new ColumViewMoodel(col3));
            var col4 = new ColumInfo("COL4", new FieldType(261, null, null, null, null, 8), new ColumConstraintsInfo(ConstraintsKind.None), "RDB$4", true, true, "");
            table.Colums.Add(new ColumViewMoodel(col4));
            var col5 = new ColumInfo("COL5", new FieldType(37, null, 100, null, null, 400), new ColumConstraintsInfo(ConstraintsKind.None), "NAME", true, true, "");
            table.Colums.Add(new ColumViewMoodel(col5));
            var col6 = new ColumInfo("COL6", new FieldType(37, null, 100, null, null, 400), new ColumConstraintsInfo(ConstraintsKind.None), "MEMO", true, true, "DEFAULT 'HOGE'");
            table.Colums.Add(new ColumViewMoodel(col6));
            var col7 = new ColumInfo("COL7", new FieldType(37, null, 100, null, null, 400), new ColumConstraintsInfo(ConstraintsKind.None), "MEMO2", true, false, "DEFAULT 'HOGE2'");
            table.Colums.Add(new ColumViewMoodel(col7));

            var idx = new IndexInfo();
            idx.Name = "COMPLEXKEY";
            idx.TableName = table.TableName;
            idx.Kind = FAManagementStudio.Common.ConstraintsKind.Primary;
            idx.FieldNames.Add(col1.ColumName);
            idx.FieldNames.Add(col2.ColumName);
            var idxVm = new IndexViewModel(idx);

            dbVm.Indexes.Add(idxVm);
            table.Indexs.Add(idxVm);

            table.GetDdl(dbVm).Is(
            "CREATE DOMAIN SARARY AS INTEGER;" + Environment.NewLine +
            "CREATE DOMAIN NAME AS VARCHAR(100);" + Environment.NewLine +
            "CREATE DOMAIN MEMO AS VARCHAR(100) DEFAULT 'HOGE';" + Environment.NewLine +
            "CREATE DOMAIN MEMO2 AS VARCHAR(100) NOT NULL DEFAULT 'HOGE2';" + Environment.NewLine +
            "CREATE TABLE TEST (" + Environment.NewLine +
            "  COL1 SARARY NOT NULL," + Environment.NewLine +
            "  COL2 NAME NOT NULL," + Environment.NewLine +
            "  COL3 TIMESTAMP NOT NULL," + Environment.NewLine +
            "  COL4 BLOB," + Environment.NewLine +
            "  COL5 NAME," + Environment.NewLine +
            "  COL6 MEMO," + Environment.NewLine +
            "  COL7 MEMO2," + Environment.NewLine +
            "  CONSTRAINT COMPLEXKEY PRIMARY KEY (COL1, COL2)" + Environment.NewLine +
            ")");
        }
        private ITableViewModel GetTreeViewTableName(DbViewModel db, object treeitem)
        {
            var table = treeitem as ITableViewModel;

            if (table == null)
            {

                return db.Tables.Where(x => 0 < x.Colums.Count(c => c == (ColumViewMoodel)treeitem)).First();
            }
            return table;
        }
 public EntityRelationshipViewModel(DbViewModel db)
 {
     CreateGraphToVisualize(db);
     this.LayoutAlgorithmType = "Tree";
 }
        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);
            });
        }
Esempio n. 15
0
 public EntityRelationshipViewModel(DbViewModel db)
 {
     CreateGraphToVisualize(db);
     this.LayoutAlgorithmType = "Tree";
 }
        public void SetCommand()
        {
            CreateDatabase = new RelayCommand(async () =>
            {
                var vm = new NewDatabaseSettingsViewModel();
                MessengerInstance.Send(new MessageBase(vm, "NewDbSettingsWindowOpen"));

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

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

            LoadDatabase = new RelayCommand<string>(async (path) =>
            {
                if (string.IsNullOrEmpty(path)) return;
                if (!File.Exists(path)) return;

                var dbInf = new DatabaseInfo(new FirebirdInfo(path));
                if (!dbInf.CanLoadDatabase) return;

                var db = new DbViewModel();
                Databases.Add(db);
                await db.LoadDatabase(dbInf);
                _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);
            });
        }
        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 string GetDdl(DbViewModel dbVm)
 {
     return($"CREATE VIEW {TableName} ({string.Join(", ", Colums.Select(x => x.ColumName).ToArray())}) AS" + Environment.NewLine
            + Source);
 }