Exemplo n.º 1
0
        public ShellContext(Configuration cfg)
        {
            this.cfg = cfg;
            this.mgr = new PathManager(cfg);
            this.commandee = new Commandee(mgr);

            string server = cfg.GetValue<string>(Configuration._SERVER0);

            ConnectionProvider pvd = null;
            if (!string.IsNullOrEmpty(server))
                pvd = cfg.GetProvider(server);

            if (pvd != null)
            {
                theSide = new Side(pvd);
                ChangeSide(theSide);
            }
            else if (cfg.Providers.Count() > 0)
            {
                theSide = new Side(cfg.Providers.First());
                ChangeSide(theSide);
            }
            else
            {
                stdio.ErrorFormat("database server not defined");
            }
        }
Exemplo n.º 2
0
        public bool SetSink(string sink)
        {
            if (sink != null)
            {
                return SetSource(sink,"destination");
            }
            else
                node = mgr.current;

            dname = mgr.GetPathFrom<DatabaseName>(node);
            if (dname == null)
            {
                stdio.ErrorFormat("warning: destination database is unavailable");
                return false;
            }

            var tname = mgr.GetPathFrom<TableName>(node);
            if (tname != null)
                T = new TableName[] { tname };
            else
                T = dname.GetTableNames();

            var server = mgr.GetPathFrom<ServerName>(node);
            side = new Side(server.Provider, dname);

            return true;
        }
Exemplo n.º 3
0
        protected void ChangeSide(Side side)
        {
            if (side == null)
            {
                stdio.ErrorFormat("undefined side");
                return;
            }

            this.theSide = side;
            Context.DS.AddHostObject(Context.THESIDE, side);

            commandee.chdir(theSide.Provider.ServerName, theSide.DatabaseName);
        }
Exemplo n.º 4
0
        public void Run(string[] args)
        {
            int i = 0;

            while (i < args.Length)
            {

                switch (args[i++])
                {
                    case "/cfg":
                        i++;
                        break;

                    case "/i":
                        if (i < args.Length && !args[i].StartsWith("/"))
                        {
                            string inputfile = args[i++];
                            string server = cfg.GetValue<string>(Configuration._SERVER0);
                            var pvd = cfg.GetProvider(server);
                            var theSide = new Side(pvd);
                            theSide.ExecuteScript(inputfile);
                            break;
                        }
                        else
                        {
                            stdio.WriteLine("/i undefined sql script file name");
                            return;
                        }

                    case "/o":
                        if (i < args.Length && !args[i].StartsWith("/"))
                        {
                            cfg.OutputFile = args[i++];
                            break;
                        }
                        else
                        {
                            stdio.WriteLine("/o undefined sql script file name");
                            return;
                        }

                    default:
                        Program.Help();
                        return;

                }
            }

            new SqlShell(cfg).DoCommand();
        }
Exemplo n.º 5
0
 public CompareAdapter(Side side1, Side side2)
 {
     this.Side1 = side1;
     this.Side2 = side2;
 }
Exemplo n.º 6
0
 private void chdir(Command cmd)
 {
     if (commandee.chdir(cmd))
     {
         var dname = mgr.GetCurrentPath<DatabaseName>();
         if (dname != null)
         {
             if (theSide == null)
                 theSide = new Side(dname.Provider);
             else
                 theSide.UpdateDatabase(dname.Provider);
         }
         else
         {
             var sname = mgr.GetCurrentPath<ServerName>();
             if (sname != null)
             {
                 if (theSide == null)
                     theSide = new Side(dname.Provider);
                 else
                     theSide.UpdateDatabase(sname.Provider);
             }
         }
     }
 }
Exemplo n.º 7
0
        public void execute(Command cmd, Side theSide)
        {
            if (cmd.HasHelp)
            {
                stdio.WriteLine("execute sql script files");
                stdio.WriteLine("execute variable_name /s");
                stdio.WriteLine("execute file");
                stdio.WriteLine("       /s                     : execute multiple sql script files defined on the user.cfg");
                stdio.WriteLine("examples:");
                stdio.WriteLine("  execute northwind.sql       : execute single sql script file");
                stdio.WriteLine("  execute db_install /s       : variable db_install = {file1, file2, ...};");
                stdio.WriteLine("                                defined on the user.cfg");
                return;
            }

            string inputfile;
            if (cmd.arg1 != null)
                inputfile = cmd.arg1;
            else
            {
                stdio.ErrorFormat("input undefined");
                return;
            }

            if (cmd.IsSchema)
            {
                string tag = inputfile;
                string[] files = mgr.Configuration.GetValue<string[]>(tag);
                if (files == null)
                {
                    stdio.ErrorFormat("no varible string[] {0} found on config file: {0}", tag);
                    return;
                }

                foreach (var file in files)
                {
                    if (!theSide.ExecuteScript(file))
                    {
                        if (!stdio.YesOrNo("are you sure to continue to run next file(y/n)?"))
                        {
                            stdio.ErrorFormat("interupted on {0}", file);
                            return;
                        }
                    }
                }
            }
            else
                theSide.ExecuteScript(inputfile);
        }
Exemplo n.º 8
0
        public void edit(Command cmd, Side theSide)
        {
            if (cmd.HasHelp)
            {
                stdio.WriteLine("edit, view and execute sql script");
                stdio.WriteLine("edit                          : create new file and edit");
                stdio.WriteLine("edit [file]                   : edit file, it is read-only if file is hyperlink");
                stdio.WriteLine("options:");
                stdio.WriteLine("   /usr                       : FTP user name");
                stdio.WriteLine("   /pwd                       : FTP password");
                stdio.WriteLine("examples:");
                stdio.WriteLine("  edit c:\\db\\northwind.sql");
                stdio.WriteLine("  edit file://datconn/northwind.sql");
                stdio.WriteLine("  edit http://www.datconn.com/demos/northwind.sql");
                stdio.WriteLine("  edit ftp://www.datconn.com/demos/northwind.sql /usr:user /pwd:password");
                return;
            }

            FileLink fileLink = null;
            if (cmd.arg1 != null)
            {
                string inputfile = cmd.arg1;

                if (inputfile.IndexOf("://") < 0)
                {
                    if (Path.GetDirectoryName(inputfile) == string.Empty)
                    {
                        string path = cmd.Configuration.GetValue<string>("MyDocuments", Directory.GetCurrentDirectory());
                        inputfile = $"{path}\\{inputfile}";
                    }
                }

                fileLink = FileLink.CreateLink(inputfile, cmd.GetValue("usr"), cmd.GetValue("pwd"));

                try
                {
                    if (!fileLink.Exists)
                    {
                        if (!fileLink.IsLocalLink)
                        {
                            stdio.ErrorFormat("file {0} doesn't exist", fileLink);
                            return;
                        }
                        else
                        {
                            File.WriteAllText(inputfile, string.Empty);
                            fileLink = FileLink.CreateLink(inputfile);
                        }
                    }
                }
                catch (Exception ex)
                {
                    stdio.Error(ex.Message);
                    return;
                }

            }

            try
            {
                var editor = new Windows.SqlEditor(cmd.Configuration, theSide.Provider, fileLink);
                editor.ShowDialog();
            }
            catch (Exception ex)
            {
                stdio.Error(ex.Message);
                return;
            }
        }
Exemplo n.º 9
0
        private bool SetSource(string source,  string sourceText)
        {
            if (source == null)
            {
                stdio.ErrorFormat("invalid argument");
                return false;
            }

            var path = new PathName(source);

            node = mgr.Navigate(path);
            if (node == null)
            {
                stdio.ErrorFormat("invalid path:" + path);
                return false;
            }

            dname = mgr.GetPathFrom<DatabaseName>(node);
            if (dname == null)
            {
                stdio.ErrorFormat("warning: {0} database is unavailable", sourceText);
                return false;
            }

            var server = mgr.GetPathFrom<ServerName>(node);
            side = new Side(server.Provider, dname);

            T = new TableName[] { };

            if (path.wildcard != null)
            {
                var m1 = new MatchedDatabase(dname, path.wildcard, mgr.Configuration.compareExcludedTables);
                T = m1.MatchedTableNames;
            }
            else
            {
                TableName tname = mgr.GetPathFrom<TableName>(node);
                if (tname == null)
                {
                    T = dname.GetTableNames();
                }
                else
                {
                    T = new TableName[] { tname };
                }
            }

            return true;
        }