示例#1
0
        /// <summary>
        ///   Reads a single SQL query, or returns null if a parsing error was
        ///   encountered.
        /// </summary>
        private static string ReadQuery()
        {
            var lexer        = new SqlLexer();
            var continuation = false;
            var buffer       = new StringBuilder();

            do
            {
                if (continuation)
                {
                    Console.Write(">>>> ");
                }
                else
                {
                    Console.Write("sql> ");
                    continuation = true;
                }

                var line = Console.ReadLine() + "\n";
                buffer.Append(line);
                lexer.Feed(line);
            } while (lexer.State != LexerState.COMPLETE &&
                     lexer.State != LexerState.ERROR);

            if (lexer.State == LexerState.COMPLETE)
            {
                return(buffer.ToString());
            }
            else
            {
                return(null);
            }
        }
示例#2
0
        public static void Main(string[] Args)
        {
            var config = ParseArguments(Args);

            RpcWrapper rpc = new RpcWrapper(new Uri(config.Url));

            if (!config.VerifyCertificate)
            {
                ServicePointManager.ServerCertificateValidationCallback +=
                    (sender, cert, chain, errors) => true;
            }

            var done = false;

            while (!done)
            {
                var sql = ReadQuery()?.Trim();
                if (sql == null)
                {
                    Console.Error.WriteLine("Could not parse SQL");
                    continue;
                }

                try
                {
                    switch (sql)
                    {
                    case "quit;":
                        rpc.Quit();
                        done = true;
                        break;

                    case "tables;":
                        var tables        = rpc.RetrieveTables();
                        var tablePageInfo = TableListingToPage(tables);
                        DisplayPage(tablePageInfo.Item1, tablePageInfo.Item2, false);
                        break;

                    case "views;":
                        var views        = rpc.RetrieveViews();
                        var viewPageInfo = TableListingToPage(views);
                        DisplayPage(viewPageInfo.Item1, viewPageInfo.Item2, false);
                        break;

                    case "columns;":
                        Console.Write("Enter the table name (catalog and schema are optional): ");
                        Console.Out.Flush();

                        var dottedName  = Console.ReadLine();
                        var dottedParts = SqlLexer.ParseDottedName(dottedName);

                        string catalog = "", schema = "", table = null;

                        switch (dottedParts.Count)
                        {
                        case 1:
                            table = dottedParts[0];
                            break;

                        case 2:
                            schema = dottedParts[0];
                            table  = dottedParts[1];
                            break;

                        case 3:
                            catalog = dottedParts[0];
                            schema  = dottedParts[1];
                            table   = dottedParts[2];
                            break;
                        }

                        if (table == null)
                        {
                            Console.WriteLine("Invalid table name provided");
                        }
                        else
                        {
                            var cols         = rpc.RetrieveColumns(catalog, schema, table);
                            var colsPageInfo = ColumnListingToPage(cols);
                            DisplayPage(colsPageInfo.Item1, colsPageInfo.Item2, false);
                        }
                        break;

                    default:
                        try
                        {
                            rpc.ExecuteSql(sql);

                            var resultColumns = rpc.RetrieveQueryColumns();
                            if (resultColumns.Count == 0)
                            {
                                Console.WriteLine("Records affected: {0}", rpc.RetrieveResultCount());
                            }
                            else if (config.Paginate)
                            {
                                var page        = new List <Dictionary <string, string> >();
                                var pagePrinted = false;
                                do
                                {
                                    page = rpc.RetrievePage();

                                    var canDisplay = page.Count > 0 || !pagePrinted;
                                    if (canDisplay && !DisplayPage(resultColumns, page, true))
                                    {
                                        break;
                                    }

                                    pagePrinted = true;
                                } while (page.Count != 0);
                            }
                            else
                            {
                                var results = new List <Dictionary <string, string> >();
                                var page    = new List <Dictionary <string, string> >();
                                do
                                {
                                    page = rpc.RetrievePage();
                                    results.AddRange(page);
                                } while (page.Count != 0);

                                DisplayPage(resultColumns, results, false);
                            }
                        }
                        finally
                        {
                            try
                            {
                                rpc.FinishQuery();
                            }
                            catch (RpcException)
                            {
                            }
                        }
                        break;
                    }
                }
                catch (RpcException error)
                {
                    Console.Error.WriteLine("Received error from server: {0}", error);
                }
                catch (Exception error)
                {
                    Console.Error.WriteLine("Received error in client: {0}", error.Message);
                    break;
                }
            }
        }