Ejemplo n.º 1
0
    private void SaveBtn_Click(object sender, EventArgs e)
    {
        if (_list.SelectedIndices.Count != 1)
        {
            return;
        }
        var          lvi  = _list.SelectedItems[0];
        var          type = (NotebookItemType)Enum.Parse(typeof(NotebookItemType), lvi.Group.Name);
        NotebookItem item = new(type, lvi.Text);

        using SaveFileDialog saveFileDialog = new() {
                  AddExtension                 = true,
                  AutoUpgradeEnabled           = true,
                  CheckPathExists              = true,
                  DefaultExt                   = ".csv",
                  Filter                       = "CSV files|*.csv",
                  OverwritePrompt              = true,
                  SupportMultiDottedExtensions = true,
                  Title         = "Save CSV As",
                  ValidateNames = true
              };
        if (saveFileDialog.ShowDialog(this) != DialogResult.OK)
        {
            return;
        }
        var filePath = saveFileDialog.FileName;

        var typeKeyword =
            type switch {
            NotebookItemType.Script => "SCRIPT",
            NotebookItemType.Table => "TABLE",
            NotebookItemType.View => "TABLE",
            _ => throw new InvalidOperationException("Unrecognzied notebook item type.")
        };
        var sql =
            $"EXPORT CSV {filePath.SingleQuote()} " +
            $"FROM {typeKeyword} {item.Name.DoubleQuote()} " +
            $"OPTIONS (TRUNCATE_EXISTING_FILE: 1);";

        WaitForm.GoWithCancel(this, "Export", "Exporting to file...", out var success, cancel => {
            SqlUtil.WithCancellableTransaction(_manager.Notebook, () => {
                _manager.ExecuteScriptNoOutput(sql);
            }, cancel);
        });
        _manager.Rescan();
        _manager.SetDirty();
        if (!success)
        {
            return;
        }

        Process.Start(new ProcessStartInfo {
            FileName  = "explorer.exe",
            Arguments = $"/e, /select, \"{filePath}\""
        });
        Close();
    }
Ejemplo n.º 2
0
    private async Task UpdateInputPreview()
    {
        var loadId = Guid.NewGuid();

        _inputPreviewLoadId = loadId;
        _inputPreviewLoadControl.PushLoad();
        try {
            var tempTableName = Guid.NewGuid().ToString();
            var fileEncoding  = _optionsControl.FileEncoding.Value;
            var text          = await Task.Run(() => {
                try {
                    var importSql =
                        @"IMPORT TXT @filePath INTO @tableName (number, line)
                        OPTIONS (TAKE_LINES: 1000, TEMPORARY_TABLE: 1, FILE_ENCODING: @encoding);";
                    _manager.ExecuteScriptNoOutput(importSql, new Dictionary <string, object> {
                        ["@filePath"]  = _filePath,
                        ["@tableName"] = tempTableName,
                        ["@encoding"]  = fileEncoding
                    });

                    using var dt = _manager.ExecuteScript($"SELECT line FROM {tempTableName.DoubleQuote()} ORDER BY number")
                                   .DataTables[0];

                    return(string.Join(Environment.NewLine, dt.Rows.Select(x => x[0].ToString())));
                } finally {
                    _manager.ExecuteScriptNoOutput($"DROP TABLE IF EXISTS {tempTableName.DoubleQuote()}");
                }
            });

            if (_inputPreviewLoadId == loadId)
            {
                _inputPreviewError.Value         = null;
                _inputPreviewControl.PreviewText = text;
            }
        } catch (UncaughtErrorScriptException ex) {
            if (_inputPreviewLoadId == loadId)
            {
                _inputPreviewError.Value = $"Error loading the input file:\r\n{ex.ErrorMessage}";
                _inputPreviewLoadControl.SetError(_inputPreviewError.Value);
            }
        } finally {
            _inputPreviewLoadControl.PopLoad();
        }
    }