Esempio n. 1
0
    private async Task <(IReadOnlyList <string> Names, IReadOnlyList <string> DetectedTypes)> GetSourceColumns()
    {
        var tempTableName = Guid.NewGuid().ToString();

        try {
            var headerRow    = _optionsControl.HasColumnHeaders.Value;
            var fileEncoding = _optionsControl.FileEncoding.Value;
            var skipLines    = _optionsControl.SkipLines.Value;
            var separator    = _optionsControl.Separator.Value;
            return(await Task.Run(() => {
                var importSql =
                    @$ "IMPORT CSV @filePath INTO @tableName
                    OPTIONS (SKIP_LINES: @skipLines, TAKE_LINES: 1000, HEADER_ROW: @headerRow, TEMPORARY_TABLE: 1, 
                        FILE_ENCODING: @encoding, SEPARATOR: @sep);
                    SELECT * FROM {SqlUtil.DoubleQuote(tempTableName)};";
                var output = _manager.ExecuteScript(importSql, new Dictionary <string, object> {
                    ["@filePath"] = _filePath,
                    ["@tableName"] = tempTableName,
                    ["@sep"] = separator,
                    ["@headerRow"] = headerRow ? 1 : 0,
                    ["@encoding"] = fileEncoding,
                    ["@skipLines"] = skipLines
                });

                IReadOnlyList <string> detectedTypes = null;
                try {
                    detectedTypes = TypeDetection.DetectTypes(output.DataTables[0]);
                } catch {
                    // Don't let this blow up the import.
                    detectedTypes = Array.Empty <string>();
                }

                using var dt = _manager.ExecuteScript($"PRAGMA TABLE_INFO ({tempTableName.DoubleQuote()})")
                               .DataTables[0];
                var nameCol = dt.GetIndex("name");
                return (dt.Rows.Select(x => x[nameCol].ToString()).Where(x => !string.IsNullOrEmpty(x)).ToList(), detectedTypes);
            }));
        } finally {
            await Task.Run(() => {
                _manager.ExecuteScriptNoOutput($"DROP TABLE IF EXISTS {tempTableName.DoubleQuote()}");
            });
        }
    }