public void CsvTableWriter_BlankRow() { string path = Path.GetTempFileName(); try { using (var writer = new CsvTableWriter(new string[] { "Col0", "Col1", "Col2" }, new FileStream(path, FileMode.Create), Encoding.UTF8)) { writer.Set("Col0", "(0,0)"); writer.Set("Col1", "(1,0)"); writer.Set("Col2", "(2,0)"); writer.WriteRow(); writer.WriteRow(); writer.Set("Col0", "(0,2)"); writer.Set("Col1", "(1,2)"); writer.Set("Col2", "(2,2)"); writer.WriteRow(); } using (var reader = new CsvTableReader(new FileStream(path, FileMode.Open), Encoding.UTF8)) { Assert.True(reader.ColumnMap.ContainsKey("Col0")); Assert.True(reader.ColumnMap.ContainsKey("Col1")); Assert.True(reader.ColumnMap.ContainsKey("Col2")); Assert.NotNull(reader.ReadRow()); Assert.Equal("(0,0)", reader["Col0"]); Assert.Equal("(1,0)", reader["Col1"]); Assert.Equal("(2,0)", reader["Col2"]); Assert.NotNull(reader.ReadRow()); Assert.Equal("", reader["Col0"]); Assert.Equal("", reader["Col1"]); Assert.Equal("", reader["Col2"]); Assert.NotNull(reader.ReadRow()); Assert.Equal("(0,2)", reader["Col0"]); Assert.Equal("(1,2)", reader["Col1"]); Assert.Equal("(2,2)", reader["Col2"]); Assert.Null(reader.ReadRow()); } } finally { File.Delete(path); } }
public void CsvTableWriter_Basic() { string path = Path.GetTempFileName(); try { using (var writer = new CsvTableWriter(new string[] { "Col0", "Col1", "Col2" }, new FileStream(path, FileMode.Create), Encoding.UTF8)) { Assert.AreEqual(0, writer.GetColumnIndex("Col0")); Assert.AreEqual(1, writer.GetColumnIndex("Col1")); Assert.AreEqual(2, writer.GetColumnIndex("Col2")); Assert.AreEqual(-1, writer.GetColumnIndex("Col3")); writer.Set("Col0", "(0,0)"); writer.Set("Col1", "(1,0)"); writer.Set("Col2", "(2,0)"); writer.WriteRow(); writer.Set("Col0", "(0,1)"); writer.Set("Col1", "(1,1)"); writer.Set("Col2", "(2,1)"); writer.WriteRow(); } using (var reader = new CsvTableReader(new FileStream(path, FileMode.Open), Encoding.UTF8)) { Assert.IsTrue(reader.ColumnMap.ContainsKey("Col0")); Assert.IsTrue(reader.ColumnMap.ContainsKey("Col1")); Assert.IsTrue(reader.ColumnMap.ContainsKey("Col2")); Assert.IsNotNull(reader.ReadRow()); Assert.AreEqual("(0,0)", reader.Parse("Col0", (string)null)); Assert.AreEqual("(1,0)", reader.Parse("Col1", (string)null)); Assert.AreEqual("(2,0)", reader.Parse("Col2", (string)null)); Assert.IsNotNull(reader.ReadRow()); Assert.AreEqual("(0,1)", reader.Parse("Col0", (string)null)); Assert.AreEqual("(1,1)", reader.Parse("Col1", (string)null)); Assert.AreEqual("(2,1)", reader.Parse("Col2", (string)null)); Assert.IsNull(reader.ReadRow()); } } finally { Helper.DeleteFile(path); } }
public void CsvTableWriter_Basic() { using (var tempFolder = new TempFolder()) { string path = Path.Combine(tempFolder.Path, "test.csv"); using (var writer = new CsvTableWriter(new string[] { "Col0", "Col1", "Col2" }, new FileStream(path, FileMode.Create), Encoding.UTF8)) { Assert.Equal(0, writer.GetColumnIndex("Col0")); Assert.Equal(1, writer.GetColumnIndex("Col1")); Assert.Equal(2, writer.GetColumnIndex("Col2")); Assert.Equal(-1, writer.GetColumnIndex("Col3")); writer.Set("Col0", "(0,0)"); writer.Set("Col1", "(1,0)"); writer.Set("Col2", "(2,0)"); writer.WriteRow(); writer.Set("Col0", "(0,1)"); writer.Set("Col1", "(1,1)"); writer.Set("Col2", "(2,1)"); writer.WriteRow(); } using (var reader = new CsvTableReader(new FileStream(path, FileMode.Open), Encoding.UTF8)) { Assert.True(reader.ColumnMap.ContainsKey("Col0")); Assert.True(reader.ColumnMap.ContainsKey("Col1")); Assert.True(reader.ColumnMap.ContainsKey("Col2")); Assert.NotNull(reader.ReadRow()); Assert.Equal("(0,0)", reader["Col0"]); Assert.Equal("(1,0)", reader["Col1"]); Assert.Equal("(2,0)", reader["Col2"]); Assert.NotNull(reader.ReadRow()); Assert.Equal("(0,1)", reader["Col0"]); Assert.Equal("(1,1)", reader["Col1"]); Assert.Equal("(2,1)", reader["Col2"]); Assert.Null(reader.ReadRow()); } } }
private static int ListZones(string[] args) { bool db = args.Length >= 2 && String.Compare(args[1], "-db", true) == 0; bool dbUpgrade = args.Length >= 2 && String.Compare(args[1], "-dbupgrade", true) == 0; // Note that one obscure timezone has an ID and display name with a single // quote (') that causes problems in the TSQL script. I'm going to correct // this by replacing the quote with a dash. if (db) { foreach (var zone in TimeZoneInfo.GetSystemTimeZones()) { Console.WriteLine("insert into PortalTimeZones(ID,DisplayName,TimeZoneInfo) values('{0}','{1}','{2}')", zone.Id.Replace("'", "-"), zone.DisplayName.Replace("'", "-"), zone.ToSerializedString().Replace("'", "-")); } } else if (dbUpgrade) { // This is a one-time hack to deal with the fact that I mistakenly used // the time zone DisplayName as the unique identifier in the Paraworks // time zone table. I need to add an ID column instead and use this. // The problem is that existing organizations will have references to // this table (probably only to the PST zone) and I'd like to maintain // these references. // // The script generated below performs the following steps: // // 1. Adds the ID and Upgraded columns to the PortalTimeZone // table, initializing ID=' ' and Upgraded=0. // // 2. For each Windows time zone, code will be generated that // looks for an exact match in the database based on // the display name. If a match is found, the ID column // will be set to the time zone ID and Upgraded will // be set to 1. If no match is found, then a new // time zone row will be added. // // 3. Any rows with Upgraded=0 will be deleted. // // 4. The Upgraded column will be removed. // // 5. Create an index on the ID column. Console.WriteLine("alter table PortalTimeZones"); Console.WriteLine(" add ID $(ObjectName) not null default ' '"); Console.WriteLine("alter table PortalTimeZones"); Console.WriteLine(" add Upgraded bit"); Console.WriteLine("go"); Console.WriteLine(); Console.WriteLine("update PortalTimeZones set Upgraded=0"); Console.WriteLine("go"); Console.WriteLine(); foreach (var zone in TimeZoneInfo.GetSystemTimeZones()) { string id = "'" + zone.Id.Replace("'", "-") + "'"; string displayName = "'" + zone.DisplayName.Replace("'", "-") + "'"; string data = "'" + zone.ToSerializedString().Replace("'", "-") + "'"; string script = @"if exists(select 1 from PortalTimeZones where DisplayName={1}) update PortalTimeZones set ID={0}, DisplayName={1}, Upgraded=1 where DisplayName={1} else insert into PortalTimeZones(ID,DisplayName,TimeZoneInfo,Upgraded) values({0},{1},{2},1) "; Console.WriteLine(script, id, displayName, data); } Console.WriteLine("go"); Console.WriteLine(); Console.WriteLine("delete from PortalTimeZones where Upgraded=0"); Console.WriteLine("go"); Console.WriteLine(); Console.WriteLine("alter table PortalTimeZones"); Console.WriteLine(" drop column Upgraded"); Console.WriteLine("go"); Console.WriteLine(); Console.WriteLine("create unique index ID on PortalTimeZones(ID)"); Console.WriteLine("go"); } else { using (var writer = new CsvTableWriter(new string[] { "ID", "DisplayName", "Data" }, Console.OpenStandardOutput(), Helper.AnsiEncoding)) { foreach (var zone in TimeZoneInfo.GetSystemTimeZones()) { writer.Set("ID", zone.Id); writer.Set("DisplayName", zone.DisplayName.Replace("'", "-")); writer.Set("Data", zone.ToSerializedString().Replace("'", "-")); writer.WriteRow(); } } } return(0); }