예제 #1
0
파일: Exporter.cs 프로젝트: fjiang2/sqlcon
        public void ExportData()
        {
            string directory = cmd.OutputDirectory();

            if (directory != null)
            {
                xmlDbFile.XmlDbFolder = directory;
            }

            if (tname != null)
            {
                cout.WriteLine("start to generate {0} data file", tname);
                var dt   = new TableReader(tname).Table;
                var file = xmlDbFile.WriteData(tname, dt);
                cout.WriteLine("completed {0} =>{1}", tname.ShortName, file);
            }

            else if (dname != null)
            {
                cout.WriteLine("start to generate {0}", dname);
                var mt = new MatchedDatabase(dname, cmd);
                CancelableWork.CanCancel(cts =>
                {
                    foreach (var tname in mt.TableNames())
                    {
                        if (cts.IsCancellationRequested)
                        {
                            return;
                        }


                        cout.WriteLine("start to generate {0}", tname);
                        var dt   = new SqlBuilder().SELECT().TOP(cmd.Top).COLUMNS().FROM(tname).SqlCmd.FillDataTable();
                        var file = xmlDbFile.WriteData(tname, dt);
                        cout.WriteLine("completed {0} => {1}", tname.ShortName, file);
                    }
                    return;
                }
                                         );

                if (cmd.Top == 0)
                {
                    cout.WriteLine("completed");
                }
                else
                {
                    cout.WriteLine("completed to export TOP {0} row(s) for each table", cmd.Top);
                }
            }
            else
            {
                cerr.WriteLine("warning: table or database is not seleted");
            }
        }
예제 #2
0
        private bool DisplayServerNodes(TreeNode <IDataPath> pt, ApplicationCommand cmd)
        {
            if (pt != RootNode)
            {
                return(false);
            }

            int i     = 0;
            int count = 0;
            int h     = 0;

            CancelableWork.CanCancel(cts =>
            {
                List <string> values = new List <string>();
                foreach (var node in pt.Nodes)
                {
                    if (cts.IsCancellationRequested)
                    {
                        return;
                    }

                    ServerName sname = (ServerName)node.Item;
                    ++i;

                    if (IsMatch(cmd.wildcard, sname.Path))
                    {
                        count++;
                        if (node.Nodes.Count == 0)
                        {
                            ExpandServerName(node, Refreshing);
                        }

                        cout.WriteLine("{0,4} {1,26} <SVR> {2,10} Databases", sub(i), sname.Path, sname.Disconnected ? "?" : node.Nodes.Count.ToString());
                        h = PagePause(cmd, ++h);

                        values.Add(sname.Path);
                    }
                }

                Assign(cmd, values);
                cout.WriteLine("\t{0} Server(s)", count);
            });

            return(true);
        }
예제 #3
0
파일: Exporter.cs 프로젝트: fjiang2/sqlcon
        public void ExportDataSetXml()
        {
            if (dname == null)
            {
                cerr.WriteLine("select a database first");
                return;
            }

            string path = cmd.OutputPath(ConfigKey._GENERATOR_DS_PATH, $"{ConfigurationEnvironment.MyDocuments}\\ds");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            if (tname != null)
            {
                cout.WriteLine($"start to generate data file: {tname}");
                var dt = new TableReader(tname).Table;
                dt.TableName = tname.ShortName;

                string  file = Path.Combine(path, $"{tname.ShortName}.xml");
                DataSet ds   = dt.DataSet;
                ds.DataSetName = dname.Name;

                ds.WriteXml(file, XmlWriteMode.WriteSchema);
                cout.WriteLine($"completed {tname} => {file}");
            }
            else if (dname != null)
            {
                cout.WriteLine($"start to generate data file to directory: {dname}");
                CancelableWork.CanCancel(cts =>
                {
                    var md             = new MatchedDatabase(dname, cmd); //cfg.exportExcludedTables);
                    TableName[] tnames = md.TableNames();
                    DataSet ds         = new DataSet
                    {
                        DataSetName = dname.Name,
                    };

                    foreach (var tn in tnames)
                    {
                        if (cts.IsCancellationRequested)
                        {
                            return;
                        }

                        try
                        {
                            var dt       = new TableReader(tn).Table.Copy();
                            dt.TableName = tn.ShortName;
                            ds.Tables.Add(dt);
                            cout.WriteLine($"generated for {tn.ShortName}");
                        }
                        catch (Exception ex)
                        {
                            cerr.WriteLine($"failed to generate {tn.ShortName}, {ex.Message}");
                        }
                    }

                    string file = Path.Combine(path, $"{dname.Name}.xml");
                    ds.WriteXml(file, XmlWriteMode.WriteSchema);
                    cout.WriteLine($"completed generated: {file}");
                    return;
                });
            }
            else
            {
                cerr.WriteLine("warning: table or database is not seleted");
            }
        }
예제 #4
0
파일: Exporter.cs 프로젝트: fjiang2/sqlcon
        public void ExportEntityClass()
        {
            if (dname == null)
            {
                cerr.WriteLine("select a database first");
                return;
            }

            string path = cmd.OutputPath(ConfigKey._GENERATOR_DC_PATH, $"{ConfigurationEnvironment.MyDocuments}\\dc");
            string ns   = cmd.GetValue("ns", ConfigKey._GENERATOR_DC_NS, "Sys.DataModel.DataContracts");

            if (tname != null)
            {
                cout.WriteLine("start to generate {0} entity framework class file", tname);
                var builder = new EntityClassBuilder(cmd, tname)
                {
                };
                builder.SetNamespace(ns);
                if (!builder.IsAssocication)
                {
                    string file = builder.WriteFile(path);
                    cout.WriteLine("completed {0} => {1}", tname.ShortName, file);
                }
            }
            else if (dname != null)
            {
                cout.WriteLine("start to generate {0} entity framework class to directory: {1}", dname, path);
                CancelableWork.CanCancel(cts =>
                {
                    var md             = new MatchedDatabase(dname, cmd); //cfg.exportExcludedTables);
                    TableName[] tnames = md.TableNames();
                    foreach (var tn in tnames)
                    {
                        if (cts.IsCancellationRequested)
                        {
                            return;
                        }

                        try
                        {
                            var builder = new EntityClassBuilder(cmd, tn);
                            builder.SetNamespace(ns);
                            if (!builder.IsAssocication)
                            {
                                string file = builder.WriteFile(path);
                                cout.WriteLine("generated for {0} at {1}", tn.ShortName, path);
                            }
                        }
                        catch (Exception ex)
                        {
                            cerr.WriteLine($"failed to generate {tn.ShortName}, {ex.Message}");
                        }
                    }

                    cout.WriteLine("completed");
                    return;
                });
            }
            else
            {
                cerr.WriteLine("warning: table or database is not seleted");
            }
        }
예제 #5
0
파일: Exporter.cs 프로젝트: fjiang2/sqlcon
        public void ExportCsvFile()
        {
            string path = this.cmd.OutputPath(ConfigKey._GENERATOR_CSV_PATH, $"{ConfigurationEnvironment.MyDocuments}\\csv");

            string file;

            string fullName(TableName tname) => $"{path}\\{sname.Path}\\{dname.Name}\\{tname.ShortName}.csv";

            if (tname != null)
            {
                cout.WriteLine("start to generate {0} csv file", tname);
                file = this.cmd.OutputFileName();
                if (file == null)
                {
                    file = fullName(tname);
                }

                var dt = new SqlBuilder().SELECT().COLUMNS(cmd.Columns).FROM(tname).SqlCmd.FillDataTable();
                using (var writer = file.CreateStreamWriter(cmd.Append))
                {
                    CsvFile.Write(dt, writer, true);
                }
                cout.WriteLine("completed {0} => {1}", tname.ShortName, file);
            }
            else if (dname != null)
            {
                cout.WriteLine("start to generate {0} csv to directory: {1}", dname, path);
                CancelableWork.CanCancel(cts =>
                {
                    var md             = new MatchedDatabase(dname, cmd);
                    TableName[] tnames = md.TableNames();
                    foreach (var tn in tnames)
                    {
                        if (cts.IsCancellationRequested)
                        {
                            return;
                        }

                        try
                        {
                            file   = fullName(tn);
                            var dt = new TableReader(tn).Table;
                            using (var writer = file.CreateStreamWriter(cmd.Append))
                            {
                                CsvFile.Write(dt, writer, true);
                            }
                            cout.WriteLine("generated for {0} at {1}", tn.ShortName, path);
                        }
                        catch (Exception ex)
                        {
                            cerr.WriteLine($"failed to generate {tn.ShortName}, {ex.Message}");
                        }
                    }

                    cout.WriteLine("completed");
                    return;
                });
            }
            else
            {
                cerr.WriteLine("warning: table or database is not seleted");
            }
        }
예제 #6
0
파일: Exporter.cs 프로젝트: fjiang2/sqlcon
        public void ExportClass()
        {
            DpoOption option = new DpoOption
            {
                NameSpace          = cfg.GetValue <string>(ConfigKey._GENERATOR_DPO_NS, "Sys.DataModel.Dpo"),
                OutputPath         = cmd.OutputPath(ConfigKey._GENERATOR_DPO_PATH, $"{ConfigurationEnvironment.MyDocuments}\\DataModel\\Dpo"),
                Level              = cfg.GetValue <Level>(ConfigKey._GENERATOR_DPO_LEVEL, Level.Application),
                HasProvider        = cfg.GetValue <bool>(ConfigKey._GENERATOR_DPO_HASPROVIDER, false),
                HasTableAttribute  = cfg.GetValue <bool>(ConfigKey._GENERATOR_DPO_HASTABLEATTR, true),
                HasColumnAttribute = cfg.GetValue <bool>(ConfigKey._GENERATOR_DPO_HASCOLUMNATTR, true),
                IsPack             = cfg.GetValue <bool>(ConfigKey._GENERATOR_DPO_ISPACK, true),
                CodeSorted         = cmd.Has("sort"),

                ClassNameSuffix = cfg.GetValue <string>(ConfigKey._GENERATOR_DPO_SUFFIX, Setting.DPO_CLASS_SUFFIX_CLASS_NAME)
            };

            option.ClassNameRule =
                name => name.Substring(0, 1).ToUpper() + name.Substring(1).ToLower() + option.ClassNameSuffix;

            if (tname != null)
            {
                var clss = new DpoGenerator(tname)
                {
                    Option = option
                };
                clss.CreateClass();
                cout.WriteLine("generated class {0} at {1}", tname.ShortName, option.OutputPath);
            }
            else if (dname != null)
            {
                cout.WriteLine("start to generate database {0} class to directory: {1}", dname, option.OutputPath);
                CancelableWork.CanCancel(cts =>
                {
                    var md             = new MatchedDatabase(dname, cmd);
                    TableName[] tnames = md.TableNames();
                    foreach (var tn in tnames)
                    {
                        if (cts.IsCancellationRequested)
                        {
                            return;
                        }


                        try
                        {
                            var clss = new DpoGenerator(tn)
                            {
                                Option = option
                            };
                            clss.CreateClass();
                            cout.WriteLine("generated class for {0} at {1}", tn.ShortName, option.OutputPath);
                        }
                        catch (Exception ex)
                        {
                            cerr.WriteLine($"failed to generate class {tn.ShortName}, {ex.Message}");
                        }
                    }

                    cout.WriteLine("completed");
                    return;
                });
            }
            else
            {
                cerr.WriteLine("warning: database is not selected");
            }
        }
예제 #7
0
파일: Exporter.cs 프로젝트: fjiang2/sqlcon
        public void ExportInsertOrUpdateData(SqlScriptType type)
        {
            var option = new SqlScriptGenerationOption
            {
                HasIfExists          = cmd.HasIfExists,
                InsertWithoutColumns = cmd.Has("no-columns"),
            };

            if (tname != null)
            {
                var node  = mgr.GetCurrentNode <Locator>();
                int count = 0;
                using (var writer = SqlFileName.CreateStreamWriter(cmd.Append))
                {
                    //cout.WriteLine($"start to generate {tname} script to file: \"{SqlFileName}\"");
                    Locator locator = null;
                    string  WHERE   = "";
                    if (node != null)
                    {
                        locator = mgr.GetCombinedLocator(node);
                        WHERE   = $" WHERE {locator}";
                    }

                    long cnt = tname.GetTableRowCount(locator);
                    count = Tools.ForceLongToInteger(cnt);
                    using (var progress = new ProgressBar {
                        Count = count
                    })
                    {
                        count = Compare.GenerateRows(type, writer, new TableSchema(tname), locator, option, progress);
                    }
                    cout.WriteLine($"{type} clauses (SELECT * FROM {tname}{WHERE}) generated to \"{SqlFileName}\", Done on rows({cnt})");
                }
            }
            else if (dname != null)
            {
                //cout.WriteLine($"start to generate {dname} script to file: \"{SqlFileName}\"");
                using (var writer = SqlFileName.CreateStreamWriter(cmd.Append))
                {
                    var         md     = new MatchedDatabase(dname, cmd);
                    TableName[] tnames = md.TableNames();

                    if (tnames.Length > 5 && !cin.YesOrNo($"Are you sure to export {tnames.Length} tables on {dname} (y/n)?"))
                    {
                        return;
                    }

                    CancelableWork.CanCancel(cts =>
                    {
                        foreach (var tn in tnames)
                        {
                            if (cts.IsCancellationRequested)
                            {
                                return;
                            }

                            long cnt = tn.GetTableRowCount();
                            if (cnt > cfg.MaxRows)
                            {
                                if (!cin.YesOrNo($"Are you sure to export {cnt} rows on {tn.ShortName} (y/n)?"))
                                {
                                    cout.WriteLine("\n{0,10} skipped", tn.ShortName);
                                    continue;
                                }
                            }

                            int count = Tools.ForceLongToInteger(cnt);
                            using (var progress = new ProgressBar {
                                Count = count
                            })
                            {
                                count = Compare.GenerateRows(type, writer, new TableSchema(tn), null, option, progress);
                            }

                            cout.WriteLine($"{count,10} row(s) generated on {tn.ShortName}");
                        }

                        cout.WriteLine($"completed to generate {type} clauses to \"{SqlFileName}\"");
                    });
                }
            }
            else
            {
                cerr.WriteLine("warning: table or database is not selected");
            }
        }
예제 #8
0
        public string Run(ActionType compareType, TableName[] N1, TableName[] N2, ApplicationCommand cmd)
        {
            string[] exceptColumns = cmd.Columns;

            DatabaseName dname1 = Side1.DatabaseName;
            DatabaseName dname2 = Side2.DatabaseName;

            cout.WriteLine("server1: {0} default database:{1}", Side1.Provider.DataSource, dname1.Name);
            cout.WriteLine("server2: {0} default database:{1}", Side2.Provider.DataSource, dname2.Name);

            if (!Exists(dname1) || !Exists(dname2))
            {
                return(string.Empty);
            }

            StringBuilder builder = new StringBuilder();

            builder.AppendFormat("-- sqlcon:", Side1.Provider.DataSource, dname1.Name).AppendLine();
            builder.AppendFormat("-- compare server={0} db={1}", Side1.Provider.DataSource, dname1.Name).AppendLine();
            builder.AppendFormat("--         server={0} db={1} @ {2}", Side2.Provider.DataSource, dname2.Name, DateTime.Now).AppendLine();

            Wildcard <TableName> match = MatchedDatabase.CreateWildcard(cmd);

            CancelableWork.CanCancel(cts =>
            {
                int i = 0;
                foreach (var tname1 in N1)
                {
                    if (cts.IsCancellationRequested)
                    {
                        return;
                    }

                    TableName tname2 = N2.Where(t => t.Name == tname1.Name).FirstOrDefault();
                    if (tname2 == null)
                    {
                        //when compare tables in the same database, the table name could be different
                        if (i < N2.Length && N2[i].DatabaseName == tname1.DatabaseName)
                        {
                            tname2 = N2[i];
                        }
                        else
                        {
                            tname2 = new TableName(dname2, tname1.SchemaName, tname1.Name);
                        }
                    }

                    if (compareType == ActionType.CompareData && !match.Contains(tname1))
                    {
                        cout.WriteLine("{0} is excluded", tname1);
                        continue;
                    }

                    if (tname2.Exists())
                    {
                        try
                        {
                            builder.Append(CompareTable(compareType, CompareSideType.compare, tname1, tname2, cmd.PK, exceptColumns));
                        }
                        catch (Exception ex)
                        {
                            cerr.WriteLine(ex.Message);
                        }
                    }
                    else
                    {
                        if (compareType == ActionType.CompareSchema)
                        {
                            string sql = tname1.GenerateCreateTableClause(appendGO: false);
                            cout.WriteLine(sql);
                            builder
                            .Append(sql)
                            .AppendLine(SqlScript.GO);
                        }
                        else
                        {
                            cout.WriteLine("{0} doesn't exist", tname2);
                        }
                    }

                    i++;
                }
            });

            return(builder.ToString());
        }