Beispiel #1
0
        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");
            }
        }
Beispiel #2
0
        public static int GenerateRows(SqlScriptType type, StreamWriter writer, ITableSchema schema, Locator where, SqlScriptGenerationOption option, IProgress <int> progress)
        {
            SqlScriptGeneration gen = new SqlScriptGeneration(type, schema)
            {
                Where  = where,
                Option = option,
            };

            return(gen.Generate(writer, progress));
        }