Example #1
0
        private Ast.ImportXlsStmt ParseImportXlsStmt(TokenQueue q)   // or null
        {
            var stmt = new Ast.ImportXlsStmt {
                SourceToken = q.SourceToken
            };
            var start = q.GetLocation();

            if (!q.TakeMaybe("import"))
            {
                q.Jump(start); return(null);
            }
            if (!q.TakeMaybe("xls", "xlsx"))
            {
                q.Jump(start); return(null);
            }
            stmt.FilenameExpr = ParseExpr(q);
            if (q.TakeMaybe("worksheet"))
            {
                stmt.WhichSheetExpr = ParseExpr(q);
            }
            q.Take("into");
            stmt.ImportTable = Check(q, ParseImportTable(q));
            if (q.TakeMaybe("options"))
            {
                stmt.OptionsList = Check(q, ParseOptionsList(q));
            }
            ConsumeSemicolon(q);
            return(stmt);
        }
 private void ExecuteImportXlsStmt(Ast.ImportXlsStmt stmt, ScriptEnv env)
 {
     try {
         ImportXlsStmtRunner.Run(_notebook, env, this, stmt);
     } catch (Exception ex) {
         Throw(env, -1, ex.Message, -1);
     }
 }
        private ImportXlsStmtRunner(INotebook notebook, ScriptEnv env, ScriptRunner runner, Ast.ImportXlsStmt stmt)
        {
            _notebook = notebook;
            _env      = env;
            _runner   = runner;
            _stmt     = stmt;

            _filePath = _runner.EvaluateExpr <string>(_stmt.FilenameExpr, _env);
            if (!File.Exists(_filePath))
            {
                throw new Exception($"The specified XLS/XLSX file was not found: \"{_filePath}\"");
            }

            if (_stmt.WhichSheetExpr != null)
            {
                _whichSheet = _runner.EvaluateExpr(_stmt.WhichSheetExpr, _env);
            }

            int?index;

            foreach (var option in _stmt.OptionsList.GetOptionKeys())
            {
                switch (option)
                {
                case "FIRST_ROW":
                    _firstRowIndex = _stmt.OptionsList.GetOptionInt(option, _runner, _env, 1, minValue: 1) - 1;
                    break;

                case "LAST_ROW":
                    var lastRowNum = _stmt.OptionsList.GetOptionInt(option, _runner, _env, 0, minValue: 0);
                    if (lastRowNum == 0)
                    {
                        _lastRowIndex = null;
                    }
                    else
                    {
                        _lastRowIndex = lastRowNum - 1;
                    }
                    break;

                case "FIRST_COLUMN":
                    index = XlsUtil.ColumnRefToIndex(_stmt.OptionsList.GetOption <object>(option, _runner, _env, null));
                    if (index.HasValue)
                    {
                        _firstColumnIndex = index.Value;
                    }
                    else
                    {
                        throw new Exception($"The {option} option must be a valid column number or string.");
                    }
                    break;

                case "LAST_COLUMN":
                    var lastColumnValue = _stmt.OptionsList.GetOption <object>(option, _runner, _env, null);
                    if (lastColumnValue.Equals(0))
                    {
                        _lastColumnIndex = null;
                        break;
                    }
                    index = XlsUtil.ColumnRefToIndex(lastColumnValue);
                    if (index.HasValue)
                    {
                        _lastColumnIndex = index.Value;
                    }
                    else
                    {
                        throw new Exception($"The {option} option must be a valid column number or string.");
                    }
                    break;

                case "HEADER_ROW":
                    _headerRow = _stmt.OptionsList.GetOptionBool(option, _runner, _env, true);
                    break;

                case "TRUNCATE_EXISTING_TABLE":
                    _truncateExistingTable = _stmt.OptionsList.GetOptionBool(option, _runner, _env, false);
                    break;

                case "TEMPORARY_TABLE":
                    _temporaryTable = _stmt.OptionsList.GetOptionBool(option, _runner, _env, false);
                    break;

                case "IF_CONVERSION_FAILS":
                    _ifConversionFails = (IfConversionFails)_stmt.OptionsList.GetOptionLong(
                        option, _runner, _env, 1, minValue: 1, maxValue: 3);
                    break;

                default:
                    throw new Exception($"\"{option}\" is not a recognized option name.");
                }
            }
        }
        // must be run from the SQLite thread
        public static void Run(INotebook notebook, ScriptEnv env, ScriptRunner runner, Ast.ImportXlsStmt stmt)
        {
            var importer = new ImportXlsStmtRunner(notebook, env, runner, stmt);

            SqlUtil.WithTransaction(notebook, importer.Import);
        }