private Ast.ImportTxtStmt ParseImportTxtStmt(TokenQueue q) // or null { var stmt = new Ast.ImportTxtStmt { SourceToken = q.SourceToken }; var start = q.GetLocation(); if (!q.TakeMaybe("import")) { q.Jump(start); return(null); } if (!q.TakeMaybe("txt", "text")) { q.Jump(start); return(null); } stmt.FilenameExpr = ParseExpr(q); q.Take("into"); stmt.TableName = Check(q, ParseIdentifierOrExpr(q)); if (q.Peek() == "(") { q.Take("("); stmt.LineNumberColumnName = Check(q, ParseIdentifierOrExpr(q)); q.Take(","); stmt.TextColumnName = Check(q, ParseIdentifierOrExpr(q)); q.Take(")"); } if (q.TakeMaybe("options")) { stmt.OptionsList = Check(q, ParseOptionsList(q)); } ConsumeSemicolon(q); return(stmt); }
private void ExecuteImportTxtStmt(Ast.ImportTxtStmt stmt, ScriptEnv env) { try { ImportTxtStmtRunner.Run(_notebook, env, this, stmt); } catch (Exception ex) { Throw(env, -1, ex.Message, -1); } }
private ImportTxtStmtRunner(INotebook notebook, ScriptEnv env, ScriptRunner runner, Ast.ImportTxtStmt stmt) { _notebook = notebook; _env = env; _runner = runner; _stmt = stmt; _filePath = GetFilePath(); _tableName = _runner.EvaluateIdentifierOrExpr(_stmt.TableName, _env); _lineNumberColumnName = _stmt.LineNumberColumnName == null ? null : _runner.EvaluateIdentifierOrExpr(_stmt.LineNumberColumnName, _env); _textColumnName = _stmt.TextColumnName == null ? null : _runner.EvaluateIdentifierOrExpr(_stmt.TextColumnName, _env); foreach (var option in _stmt.OptionsList.GetOptionKeys()) { switch (option) { case "SKIP_LINES": _skipLines = _stmt.OptionsList.GetOptionLong(option, _runner, _env, 0, minValue: 0); break; case "TAKE_LINES": _takeLines = _stmt.OptionsList.GetOptionLong(option, _runner, _env, -1, minValue: -1); 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 "FILE_ENCODING": _fileEncoding = _stmt.OptionsList.GetOptionEncoding(option, _runner, _env); break; default: throw new Exception($"\"{option}\" is not a recognized option name."); } } }
private readonly Encoding _fileEncoding; // or null for automatic // must be run from the SQLite thread public static void Run(INotebook notebook, ScriptEnv env, ScriptRunner runner, Ast.ImportTxtStmt stmt) { var importer = new ImportTxtStmtRunner(notebook, env, runner, stmt); SqlUtil.WithTransaction(notebook, importer.Import); }