Beispiel #1
0
        public void del(Command cmd)
        {
            if (cmd.HasHelp)
            {
                stdio.WriteLine("command del or erase: drop tables or delete data rows");
                stdio.WriteLine("del tablename               : drop table");
                stdio.WriteLine("del [sql where clause]      : delete current table filtered rows");
                stdio.WriteLine("example:");
                stdio.WriteLine(@"local> del Northwind\Products       : drop table [Products]");
                stdio.WriteLine(@"local\Northwind\Products> del       : delete all rows of table [Products]");
                stdio.WriteLine(@"local\Northwind\Products> del col1=1 and col2='match' : del rows matched on columns:c1 or c2");
                return;
            }

            var pt = mgr.current;
            if (!(pt.Item is Locator) && !(pt.Item is TableName))
            {
                TableName[] T = null;
                if (cmd.arg1 != null)
                {
                    PathName path = new PathName(cmd.arg1);
                    var node = mgr.Navigate(path);
                    if (node != null)
                    {
                        var dname = mgr.GetPathFrom<DatabaseName>(node);
                        if (dname != null)
                        {
                            if (cmd.wildcard != null)
                            {
                                var m = new MatchedDatabase(dname, cmd.wildcard, new string[] { });
                                T = m.MatchedTableNames;
                            }
                            else
                            {
                                var _tname = mgr.GetPathFrom<TableName>(node);
                                if (_tname != null)
                                    T = new TableName[] { _tname };
                                else
                                {
                                    stdio.ErrorFormat("invalid path");
                                    return;
                                }
                            }
                        }
                        else
                        {
                            stdio.ErrorFormat("database is unavailable");
                            return;
                        }
                    }
                    else
                    {
                        stdio.ErrorFormat("invalid path");
                        return;
                    }
                }

                if (T != null && T.Length > 0)
                {
                    if (!stdio.YesOrNo("are you sure to drop {0} tables (y/n)?", T.Length))
                        return;

                    try
                    {
                        var sqlcmd = new SqlCmd(T[0].Provider, string.Empty);
                        sqlcmd.ExecuteNonQueryTransaction(T.Select(row => string.Format("DROP TABLE {0}", row)));
                        stdio.ErrorFormat("completed to drop table(s):\n{0}", string.Join<TableName>("\n", T));
                    }
                    catch (Exception ex)
                    {
                        stdio.ErrorFormat(ex.Message);
                    }
                }
                else
                    stdio.ErrorFormat("table is not selected");

                return;
            }

            TableName tname = null;
            Locator locator = null;
            if (pt.Item is Locator)
            {
                locator = mgr.GetCombinedLocator(pt);
                tname = mgr.GetCurrentPath<TableName>();
                if (!string.IsNullOrEmpty(cmd.args))
                    locator.And(new Locator(cmd.args));
            }

            if (pt.Item is TableName)
            {
                tname = (TableName)pt.Item;
                if (!string.IsNullOrEmpty(cmd.args))
                    locator = new Locator(cmd.args);
            }

            if (locator == null)
                stdio.Write("are you sure to delete all rows (y/n)?");
            else
                stdio.Write("are you sure to delete (y/n)?");

            if (stdio.ReadKey() != ConsoleKey.Y)
                return;

            stdio.WriteLine();

            try
            {
                int count;
                if (locator == null)
                    count = new SqlBuilder().DELETE(tname).SqlCmd.ExecuteNonQuery();
                else
                    count = new SqlBuilder().DELETE(tname).WHERE(locator).SqlCmd.ExecuteNonQuery();

                stdio.WriteLine("{0} of row(s) affected", count);
            }
            catch (Exception ex)
            {
                stdio.ErrorFormat(ex.Message);
            }
        }
Beispiel #2
0
        private bool Navigate(PathName path)
        {
            this.pt = mgr.current;

            if (path != null)
            {
                pt = mgr.Navigate(path);
                if (pt == null)
                {
                    stdio.ErrorFormat("invalid path");
                    return false;
                }
            }

            return true;
        }
Beispiel #3
0
 public void chdir(ServerName serverName, DatabaseName databaseName)
 {
     string path = string.Format("\\{0}\\{1}\\", serverName.Path, databaseName.Path);
     PathName pathName = new PathName(path);
     var node = mgr.Navigate(pathName);
     if (node != null)
     {
         mgr.current = node;
     }
     else
         stdio.ErrorFormat("invalid path:" + path);
 }
Beispiel #4
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;
        }