private Ast.ImportColumn ParseImportColumn(TokenQueue q) { var n = new Ast.ImportColumn { SourceToken = q.SourceToken }; Check(q, n.ColumnName = ParseIdentifierOrExpr(q)); if (q.Peek() == "as") { q.Take("as"); n.AsName = Check(q, ParseIdentifierOrExpr(q)); } switch (q.Peek()) { case "text": q.Take(); n.TypeConversion = Ast.TypeConversion.Text; break; case "integer": q.Take(); n.TypeConversion = Ast.TypeConversion.Integer; break; case "real": q.Take(); n.TypeConversion = Ast.TypeConversion.Real; break; case "date": q.Take(); n.TypeConversion = Ast.TypeConversion.Date; break; case "datetime": q.Take(); n.TypeConversion = Ast.TypeConversion.DateTime; break; case "datetimeoffset": q.Take(); n.TypeConversion = Ast.TypeConversion.DateTimeOffset; break; } return(n); }
public static void GetDestinationColumns(IEnumerable <Ast.ImportColumn> importColumns, ScriptRunner runner, ScriptEnv env, IReadOnlyList <string> srcColNames, out Ast.ImportColumn[] dstColNodes, out string[] dstColNames) { // if there is no column list specified, then all columns are imported with default settings. if (!importColumns.Any()) { importColumns = srcColNames.Select(x => new Ast.ImportColumn { ColumnName = new Ast.IdentifierOrExpr { Identifier = x }, TypeConversion = Ast.TypeConversion.Text }).ToList(); } var lowercaseSrcColNames = srcColNames.Select(x => x.ToLower()).ToArray(); var lowercaseDstColNames = new HashSet <string>(); // to ensure we don't have duplicate column names dstColNodes = new Ast.ImportColumn[srcColNames.Count]; dstColNames = new string[srcColNames.Count]; foreach (var importCol in importColumns) { var name = runner.EvaluateIdentifierOrExpr(importCol.ColumnName, env); var colIndex = lowercaseSrcColNames.IndexOf(name.ToLower()); if (colIndex.HasValue) { if (dstColNodes[colIndex.Value] == null) { dstColNodes[colIndex.Value] = importCol; } else { throw new Exception($"The input column \"{name}\" was specified more than once in the column list."); } } else { // the user specified a column name that does not exist in the CSV file throw new Exception($"The column \"{name}\" does not exist in the CSV file. " + $"The columns that were found are: {string.Join(", ", srcColNames.Select(DoubleQuote))}"); } // apply the user's rename if specified string dstName; if (importCol.AsName != null) { dstName = runner.EvaluateIdentifierOrExpr(importCol.AsName, env); } else { dstName = name; } dstColNames[colIndex.Value] = dstName; // ensure this isn't a duplicate destination column name if (lowercaseDstColNames.Contains(dstName.ToLower())) { throw new Exception($"The column \"{dstName}\" was specified more than once as a destination column name."); } else { lowercaseDstColNames.Add(dstName.ToLower()); } } }