public string GetCsvImportReadySqlScript(string fileContent, string currentFolder) { if (fileContent.Contains(Constants.DefaultPlaceholders.DbUpgradeOperationScriptsContent)) { var startIndexOfOperation = fileContent.IndexOf(Constants.ToolOperations.CsvImport, StringComparison.Ordinal); if (startIndexOfOperation > 0) { var endIndexOfOperationDefinition = fileContent.Substring(startIndexOfOperation) .IndexOf(">", StringComparison.Ordinal); if (endIndexOfOperationDefinition > 0) { var operationDefinitionString = fileContent.Substring(startIndexOfOperation, endIndexOfOperationDefinition + 1); var tableIndex = operationDefinitionString.IndexOf(Constants.ToolOperations.Attributes.Table, StringComparison.Ordinal); var columnsIndex = operationDefinitionString.IndexOf( Constants.ToolOperations.Attributes.Columns, StringComparison.Ordinal); var csvFileIndex = operationDefinitionString.IndexOf( Constants.ToolOperations.Attributes.CsvFile, StringComparison.Ordinal); if (tableIndex < 0 || columnsIndex < 0 || csvFileIndex < 0) { throw new ArgumentException( "The CSV import tool operation is not defined properly: There should be TABLE,COLUMNS and CSV_FILE attributes."); } var csvImport = new CsvImport { Table = GetAttributeValue(operationDefinitionString, Constants.ToolOperations.Attributes.Table), Columns = GetAttributeValue(operationDefinitionString, Constants.ToolOperations.Attributes.Columns).Split(',').ToList(), CsvFile = GetAttributeValue(operationDefinitionString, Constants.ToolOperations.Attributes.CsvFile) }; fileContent = fileContent.Replace( Constants.DefaultPlaceholders.DbUpgradeOperationScriptsContent, string.Concat("\n", GetCsvImportSqlScript(currentFolder, csvImport))); } } } return fileContent; }
private static string GetCsvImportSqlScript(string currentFolder, CsvImport csvImport) { var retVal = new StringBuilder(); var csvContent = File.ReadAllLines(string.Concat(currentFolder, @"\", csvImport.CsvFile)); var insertFormat = string.Format("INSERT INTO {0} ({1})", csvImport.Table, string.Join(",", csvImport.Columns.ToArray())); var i = 0; foreach (var csvLine in csvContent) { // skip the header line. if (i++ <= 0) continue; var values = CsvRowToStringArray(csvLine); retVal.AppendLine(string.Concat(insertFormat, string.Format(" VALUES({0});", string.Join(",", values)))); } return retVal.ToString(); }