コード例 #1
0
ファイル: Commandee.cs プロジェクト: fjiang2/sqlcon
        public void copy(Command cmd, CompareSideType sideType)
        {
            if (cmd.HasHelp)
            {
                if (sideType == CompareSideType.copy)
                {
                    stdio.WriteLine("copy schema or records from table1 to table2, support table name wildcards");
                    stdio.WriteLine("copy table1 [table2] [/s]");
                }
                else if (sideType == CompareSideType.sync)
                {
                    stdio.WriteLine("synchronize schema or records from table1 to table2");
                    stdio.WriteLine("sync table1 [table2] [/s] : sync table1' records to table2");
                }
                else if (sideType == CompareSideType.compare)
                {
                    stdio.WriteLine("compare schema or records from table1 to table2");
                    stdio.WriteLine("comp table1 [table2] [/s] : sync table1' records to table2");
                }
                stdio.WriteLine("support table name wildcards");
                stdio.WriteLine("[/s]                       : table schema, default table records");
                return;
            }

            CancelableWork.CanCancel(cts =>
            {
                PathBothSide both = new PathBothSide(mgr, cmd);
                var dname2 = mgr.GetPathFrom<DatabaseName>(both.ps2.Node);
                if (both.ps1.MatchedTables == null)
                    return;

                foreach (var tname1 in both.ps1.MatchedTables)
                {
                    if (cts.IsCancellationRequested)
                        return;

                    TableName tname2 = mgr.GetPathFrom<TableName>(both.ps2.Node);
                    if (tname2 == null)
                    {
                        tname2 = new TableName(dname2, tname1.SchemaName, tname1.ShortName);
                    }

                    var adapter = new CompareAdapter(both.ps1.side, both.ps2.side);
                    //stdio.WriteLine("start to {0} from {1} to {2}", sideType, tname1, tname2);
                    var sql = adapter.CompareTable(cmd.IsSchema ? ActionType.CompareSchema : ActionType.CompareData,
                        sideType, tname1, tname2, mgr.Configuration.PK, cmd.Columns);

                    if (sideType == CompareSideType.compare)
                    {
                        if (sql == string.Empty)
                        {
                            stdio.WriteLine("source {0} and destination {1} are identical", tname1, tname2);
                        }
                        continue;
                    }

                    if (sql == string.Empty)
                    {
                        stdio.WriteLine("nothing changes made on destination {0}", tname2);
                    }
                    else
                    {
                        bool exists = tname2.Exists();
                        try
                        {
                            var sqlcmd = new SqlCmd(both.ps2.side.Provider, sql);
                            int count = sqlcmd.ExecuteNonQueryTransaction();
                            if (exists)
                            {
                                if (count >= 0)
                                    stdio.WriteLine("{0} row(s) changed at destination {1}", count, tname2);
                                else
                                    stdio.WriteLine("command(s) completed successfully at destination {1}", count, tname2);
                            }
                            else
                                stdio.WriteLine("table {0} created at destination", tname2);
                        }
                        catch (Exception ex)
                        {
                            stdio.ErrorFormat(ex.Message);
                            return;
                        }
                    }
                } // loop for

                return;
            });
        }
コード例 #2
0
ファイル: SqlShell.cs プロジェクト: fjiang2/sqlcon
        private bool DoSingleLineCommand(string text)
        {
            text = text.Trim();
            if (text == string.Empty)
                return false;

            Command cmd = new Command(text, cfg);
            switch (cmd.Action)
            {
                case "set":
                    commandee.set(cmd);
                    return true;

                case "let":
                    commandee.let(cmd);
                    return true;

                case "md":
                case "mkdir":
                    commandee.mkdir(cmd);
                    return true;

                case "rd":
                case "rmdir":
                    commandee.rmdir(cmd);
                    return true;
            }

            switch (cmd.Action)
            {
                case "ls":
                case "dir":
                    commandee.dir(cmd);
                    return true;

                case "cd":
                case "chdir":
                    if (cmd.arg1 != null || cmd.HasHelp)
                        chdir(cmd);
                    else
                        stdio.WriteLine(mgr.ToString());
                    return true;

                case "type":
                    commandee.type(cmd);
                    return true;

                case "del":
                case "erase":
                    commandee.del(cmd);
                    return true;

                case "ren":
                case "rename":
                    commandee.rename(cmd);
                    return true;

                case "attrib":
                    commandee.attrib(cmd);
                    return true;

                case "echo":
                    stdio.WriteLine(text);
                    return true;

                case "rem":
                    return true;

                case "ver":
                    stdio.WriteLine("sqlcon [Version {0}]", SysExtension.ApplicationVerison);
                    return true;

                case "show":
                    if (cmd.arg1 != null)
                        Show(cmd.arg1.ToLower(), cmd.arg2);
                    else
                        stdio.ErrorFormat("invalid argument");
                    return true;

                case "find":
                    if (cmd.arg1 != null)
                        theSide.FindName(cmd.arg1);
                    else
                        stdio.ErrorFormat("find object undefined");
                    return true;

                case "save":
                    if (cmd.arg1 == "output")
                    {
                        if (!File.Exists(this.cfg.OutputFile))
                        {
                            stdio.ErrorFormat("no output file found : {0}", this.cfg.OutputFile);
                            break;
                        }
                        using (var reader = new StreamReader(this.cfg.OutputFile))
                        {
                            string data = reader.ReadToEnd();
                            System.Windows.Clipboard.SetText(data);
                            stdio.WriteLine("copied to clipboard");
                        }
                    }
                    return true;

                case "execute":
                    commandee.execute(cmd, theSide);
                    return true;

                case "open":
                    commandee.open(cmd, cfg);
                    return true;

                case "compare":
                    {
                        PathBothSide both = new PathBothSide(mgr, cmd);

                        using (var writer = cfg.OutputFile.NewStreamWriter())
                        {
                            ActionType type;
                            if (cmd.IsSchema)
                                type = ActionType.CompareSchema;
                            else
                                type = ActionType.CompareData;

                            if (both.Invalid)
                            {
                                return true;
                            }

                            var adapter = new CompareAdapter(both.ps1.side, both.ps2.side);
                            var sql = adapter.Run(type, both.ps1.MatchedTables, both.ps2.MatchedTables, cfg, cmd.Columns);
                            writer.Write(sql);
                        }
                        stdio.WriteLine("completed");
                        return true;
                    }

                case "copy":
                    commandee.copy(cmd, CompareSideType.copy);
                    return true;

                case "sync":
                    commandee.copy(cmd, CompareSideType.sync);
                    return true;

                case "comp":
                    commandee.copy(cmd, CompareSideType.compare);
                    return true;

                case "xcopy":
                    commandee.xcopy(cmd);
                    return true;

                //example: run func(id=20)
                case "run":
                    {
                        VAL result = Context.Evaluate(cmd.args);
                        if (result.IsNull)
                            stdio.ErrorFormat("undefined query function");
                        else if (result.IsInt)
                        {
                            //show error code
                        }
                        else
                        {
                            if (!result.IsList && result.Size != 2)
                            {
                                stdio.ErrorFormat("invalid format, run query like >run query(id=1)");
                                return true;
                            }

                            try
                            {
                                DataSet ds = new SqlCmd(theSide.Provider, (string)result[0]).ParseParameters(result[1]).FillDataSet();
                                if (ds != null)
                                {
                                    foreach (DataTable dt in ds.Tables)
                                        dt.ToConsole();
                                }
                                else
                                    stdio.ErrorFormat("cannot retrieve data from server");
                            }
                            catch (Exception ex)
                            {
                                stdio.ErrorFormat("{0}", ex.Message);
                                return true;
                            }
                        }
                    }
                    return true;

                case "export":
                    commandee.export(cmd, cfg, this);
                    return true;

                case "clean":
                    commandee.clean(cmd, cfg);
                    return true;

                case "mount":
                    commandee.mount(cmd, cfg);
                    return true;

                case "umount":
                    commandee.umount(cmd, cfg);
                    return true;

                case "edit":
                    commandee.edit(cmd, theSide);
                    return true;

                default:
                    break;
            }

            return false;
        }
コード例 #3
0
ファイル: Commandee.cs プロジェクト: fjiang2/sqlcon
        public void xcopy(Command cmd)
        {
            if (cmd.HasHelp)
            {
                stdio.WriteLine("xcopy large size records, support table/database name wildcards");
                stdio.WriteLine("   table must have same structure");
                stdio.WriteLine("xcopy database1 [database2]");
                stdio.WriteLine("xcopy table1 [table2]");
                stdio.WriteLine("       /col:c1[=d1],c2[=d2],...         copy selected columns (mapping)");
                stdio.WriteLine("       /s                               compare table schema");
                stdio.WriteLine("note that: to xcopy selected records of table, mkdir locator first, example:");
                stdio.WriteLine(@"  \local\NorthWind\Products> md ProductId<2000");
                stdio.WriteLine(@"  \local\NorthWind\Products> xcopy 1 \local\db");
                return;
            }

            CancelableWork.CanCancel(cts =>
            {
                PathBothSide both = new PathBothSide(mgr, cmd);
                var dname2 = mgr.GetPathFrom<DatabaseName>(both.ps2.Node);
                if (both.ps1.MatchedTables == null)
                    return;

                foreach (var tname1 in both.ps1.MatchedTables)
                {
                    if (cts.IsCancellationRequested)
                        return;

                    TableName tname2 = mgr.GetPathFrom<TableName>(both.ps2.Node);
                    if (tname2 == null)
                        tname2 = new TableName(dname2, tname1.SchemaName, tname1.ShortName);

                    if (cmd.IsSchema)
                    {
                        string result = Compare.TableSchemaDifference(CompareSideType.compare, tname1, tname2);
                        if (!string.IsNullOrEmpty(result))
                        {
                            stdio.ErrorFormat("destination table is not compatible or doesn't exist");
                            continue;
                        }
                    }

                    List<SqlBulkCopyColumnMapping> maps = new List<SqlBulkCopyColumnMapping>();
                    if (cmd.Columns.Length > 0)
                    {
                        SqlBulkCopyColumnMapping mapping;
                        foreach (var column in cmd.Columns)
                        {
                            string[] items = column.Split('=');
                            if (items.Length == 2)
                                mapping = new SqlBulkCopyColumnMapping(items[0], items[1]);
                            else
                                mapping = new SqlBulkCopyColumnMapping(column, column);

                            maps.Add(mapping);
                        }
                    }

                    TableReader tableReader;
                    if (both.ps1.Node.Item is Locator)
                    {
                        Locator locator = mgr.GetCombinedLocator(both.ps1.Node);
                        string where = locator.Path;
                        tableReader = new TableReader(tname1, where.Inject());
                    }
                    else
                        tableReader = new TableReader(tname1);

                    int count = tableReader.Count;

                    stdio.Write("copying {0}", tname1.Name);
                    using (var progress = new ProgressBar { Count = count })
                    {
                        TableBulkCopy bulkCopy = new TableBulkCopy(tableReader);
                        bulkCopy.CopyTo(tname2, maps.ToArray(), cts, progress);

                        if (cts.IsCancellationRequested)
                            progress.Report(count);
                    }

                    if (!cts.IsCancellationRequested)
                        stdio.WriteLine(", Done.");
                }
            });
        }