private void WriteTable(string sql)
    {
        using var status = WaitStatus.StartRows(Path.GetFileName(_filePath));
        var fileMode = _truncateExistingFile ? FileMode.Create : FileMode.Append;

        using var stream = File.Open(_filePath, fileMode, FileAccess.Write, FileShare.None);
        using var writer = new StreamWriter(stream, _fileEncoding);

        using var statement = _notebook.Prepare(sql);
        var args = statement.GetArgs(_env.Vars);

        statement.ExecuteStream(args, OnHeader, OnRow, CancellationToken.None);

        void OnHeader(List <string> columnNames)
        {
            if (_headerRow)
            {
                writer.WriteLine(string.Join(_separator, columnNames.Select(c => CsvUtil.QuoteCsv(c, _separator))));
            }
        }

        void OnRow(object[] row)
        {
            _cancel.ThrowIfCancellationRequested();
            CsvUtil.WriteCsvLine(writer, _separator, row);
            status.IncrementRows();
        }
    }
Example #2
0
    private void Execute()
    {
        var sql = _inputText.SqlText.Trim();

        if (string.IsNullOrWhiteSpace(sql))
        {
            return;
        }

        using var output = WaitForm.GoWithCancel(TopLevelControl, "Console", "Executing...", out var success, cancel => {
            return(SqlUtil.WithCancellation(_manager.Notebook, () => {
                using var status = WaitStatus.StartRows("Script output");
                return _manager.ExecuteScript(sql, onRow: status.IncrementRows);
            }, cancel));
        });
        _manager.SetDirty();
        _manager.Rescan();
        if (!success)
        {
            return;
        }

        _inputText.SqlText = "";
        Log(sql, output);
        TakeFocus();
    }
Example #3
0
 public void SaveAs(string filePath, CancellationToken cancel)
 {
     NotebookItemsSaveRequest?.Invoke(this, EventArgs.Empty);
     using var status = WaitStatus.StartCustom(Path.GetFileName(filePath));
     Notebook.SaveAs(filePath,
                     c => status.SetProgress($"{c}% complete"),
                     cancel);
 }
    private void WriteTable(SimpleDataTable sdt)
    {
        using var status = WaitStatus.StartRows(Path.GetFileName(_filePath));
        var fileMode = _truncateExistingFile ? FileMode.Create : FileMode.Append;

        using var stream = File.Open(_filePath, fileMode, FileAccess.Write, FileShare.None);
        using var writer = new StreamWriter(stream, _fileEncoding);
        if (_headerRow)
        {
            writer.WriteLine(string.Join(_separator, sdt.Columns.Select(c => CsvUtil.QuoteCsv(c, _separator))));
        }
        CsvUtil.WriteCsv(sdt.Rows, writer, status.IncrementRows, _separator, _cancel);
    }
 private void ImportLink()
 {
     using var status = WaitStatus.StartStatic(GetTruncatedDstTableName());
     if (_vendor == "mssql")
     {
         _notebook.Execute(
             $"CREATE VIRTUAL TABLE {_dstTableName.DoubleQuote()} USING mssql " +
             $"('{_connectionString.Replace("'", "''")}', '{_srcTableName.Replace("'", "''")}', " +
             $"'{_srcSchemaName.Replace("'", "''")}')");
     }
     else
     {
         _notebook.Execute(
             $"CREATE VIRTUAL TABLE {_dstTableName.DoubleQuote()} USING {_vendor} " +
             $"('{_connectionString.Replace("'", "''")}', '{_srcTableName.Replace("'", "''")}')");
     }
 }
Example #6
0
    private static void InitHelpNotebook(Notebook notebook, string sqlnbFilePath)
    {
        var exeDir    = Path.GetDirectoryName(Application.ExecutablePath);
        var docDir    = Path.Combine(exeDir, "doc");
        var htmlFiles = (
            from htmlFilePath in Directory.GetFiles(docDir, "*.html", SearchOption.AllDirectories)
            let content = File.ReadAllText(htmlFilePath)
                          select(FilePath: htmlFilePath, Content: content)
            ).ToList();

        notebook.Execute("BEGIN");

        notebook.Execute(
            @"CREATE TABLE docs (
                id INTEGER PRIMARY KEY,
                path TEXT NOT NULL,
                book TEXT NOT NULL, 
                title TEXT NOT NULL,
                html TEXT NOT NULL
            )");
        notebook.Execute(
            @"CREATE TABLE books_txt (number INTEGER PRIMARY KEY, line TEXT NOT NULL)");
        notebook.Execute(
            @"CREATE TABLE art (file_path TEXT PRIMARY KEY, content BLOB)");
        notebook.Execute("CREATE VIRTUAL TABLE docs_fts USING fts5 (id, title, text)");

        using var status = WaitStatus.StartCustom("Documentation index");
        for (var i = 0; i < htmlFiles.Count; i++)
        {
            status.SetProgress($"{i * 100 / htmlFiles.Count}% complete");
            var(filePath, content) = htmlFiles[i];
            var filename = Path.GetFileName(filePath);
            if (filename == "doc.html" || filename == "index.html")
            {
                continue;
            }

            // Parse out the title.
            var title = "(no title)";
            {
                var startIndex = content.IndexOf("<title>");
                var endIndex   = content.IndexOf("</title>");
                if (startIndex >= 0 && endIndex > startIndex)
                {
                    title   = WebUtility.HtmlDecode(content[(startIndex + "<title>".Length)..endIndex]).Trim();
        internal ConfirmationResponse Wait(int millisecondsTimeout)
        {
            try
            {
                var response = true;
                if (_confirmationMessage == null)
                {
                    _waitStatus = WaitStatus.Waiting;
                    response    = _confirmEvent.WaitOne(millisecondsTimeout);
                }

                return(new ConfirmationResponse(response, _confirmationMessage, _isSuccess));
            }
            finally
            {
                _waitStatus = WaitStatus.DoneWaiting;
            }
        }
Example #8
0
 public void SetFuncBtn(WaitStatus status)
 {
     nowWaitStatus = status;
     if (status == WaitStatus.beReady)
     {
         funcText.text = "准备";
     }
     else if (status == WaitStatus.cancelReady)
     {
         funcText.text = "取消准备";
     }
     else if (status == WaitStatus.startMatch)
     {
         funcText.text = "开始匹配";
     }
     else if (status == WaitStatus.matching)
     {
         funcText.text = "匹配中...";
     }
 }
    public void OnOpen()
    {
        using var simpleDataTable = WaitForm.GoWithCancel(TopLevelControl, "Table", "Reading table...", out var success, cancel => {
            using var status = WaitStatus.StartStatic(_tableName);
            cancel.Register(() => _manager.Notebook.BeginUserCancel());
            try {
                return(_manager.Notebook.Query($"SELECT * FROM {_tableName.DoubleQuote()} LIMIT 1000"));
            } finally {
                _manager.Notebook.EndUserCancel();
            }
        });
        if (!success)
        {
            throw new OperationCanceledException();
        }

        _grid.DataSource = simpleDataTable.ToDataTable();
        _grid.AutoSizeColumns(this.Scaled(500));

        _query = $"SELECT\r\n{string.Join(",\r\n", simpleDataTable.Columns.Select(x => "    " + x.DoubleQuote()))}\r\nFROM {_tableName.DoubleQuote()}\r\nLIMIT 1000;\r\n";
    }
Example #10
0
 private void OnWaitStatus(EventArgs e)
 {
     try { WaitStatus?.Invoke(this, e); } catch { }
 }
Example #11
0
 public static long ExitStatus(this WaitStatus w)
 {
     return(0L);
 }
Example #12
0
 public static Signal StopSignal(this WaitStatus w)
 {
     return(0L);
 }
Example #13
0
 public static long TrapCause(this WaitStatus w)
 {
     return(0L);
 }
Example #14
0
 public static bool Continued(this WaitStatus w)
 {
     return(false);
 }
Example #15
0
 public static bool Stopped(this WaitStatus w)
 {
     return(false);
 }
Example #16
0
 public static bool CoreDump(this WaitStatus w)
 {
     return(false);
 }
Example #17
0
 public static bool Signaled(this WaitStatus w)
 {
     return(false);
 }
Example #18
0
 public static bool Exited(this WaitStatus w)
 {
     return(false);
 }