/// <summary> /// Reads one db file /// </summary> /// <param name="db">The rtDatabase to fill from the file</param> /// <param name="fName">The qualified filename</param> /// <returns>The result string, either empty or error</returns> private static string ReadDbFile(ref rtDatabase db, string fName) { var icaoPre = Path.GetFileNameWithoutExtension(fName); string ret = ""; using (var sr = new StreamReader(fName)) { string buffer = sr.ReadLine( ); buffer = sr.ReadLine( ); // header line while (!sr.EndOfStream) { if (buffer.Contains(".")) { buffer = sr.ReadLine( ); continue; } ; // invalidate if (buffer.Contains("/")) { buffer = sr.ReadLine( ); continue; } ; // invalidate var rec = FromNative(buffer); if (rec.IsValid) { ret += db.Add(rec); // collect adding information } buffer = sr.ReadLine( ); } // } return(ret); }
/// <summary> /// Write complete route db into the supplied database as one transaction /// </summary> /// <param name="db">The route db to dump</param> /// <param name="sqConnection">The db connection</param> /// <returns>The result string, either empty or error</returns> public static string WriteSqDB(rtDatabase db, SQLiteConnection sqConnection) { string ret = ""; using (SQLiteCommand sqlite_cmd = sqConnection.CreateCommand( )) { sqlite_cmd.CommandText = "BEGIN TRANSACTION;"; sqlite_cmd.ExecuteNonQuery( ); try { foreach (var c in PREFIXES) { ret = WriteFile(sqConnection, db.GetSubtable(c.ToString( ))); } sqlite_cmd.CommandText = "COMMIT;"; sqlite_cmd.ExecuteNonQuery( ); } catch { sqlite_cmd.CommandText = "ROLLBACK;"; sqlite_cmd.ExecuteNonQuery( ); } finally { if (!string.IsNullOrEmpty(ret)) { ret = $"ERROR - inserting rows failed: {ret}\n"; } } } return(ret); }
/// <summary> /// Reads all data from the given file /// </summary> /// <param name="db">The rtDatabase to fill from the file</param> /// <param name="tsvFile">The file to read</param> /// <returns>The result string, either empty or error</returns> public static string ReadDb(ref rtDatabase db, string tsvFile) { if (!File.Exists(tsvFile)) { return($"File does not exist\n"); } return(ReadDbFile(ref db, tsvFile)); }
/// <summary> /// Write the route db as CSV formatted file /// </summary> /// <param name="db">The database to dump</param> /// <param name="csvOutStream">The stream to write to</param> /// <returns>True for success</returns> public static bool WriteCsv(rtDatabase db, Stream csvOutStream) { using (var sw = new StreamWriter(csvOutStream, Encoding.UTF8)) { sw.WriteLine(rtRec.CsvHeader); foreach (var c in PREFIXES) { WriteFile(sw, db.GetSubtable(c.ToString( ))); } } return(true); }
/// <summary> /// Write the route db as FA formatted Json files into the given folder /// </summary> /// <param name="db">The database to dump</param> /// <param name="dbFolder">The folder to write to</param> /// <returns>True for success</returns> public static bool WriteDb(rtDatabase db, string dbFolder) { if (!Directory.Exists(dbFolder)) { return(false); } Dictionary <string, int> prefixes = new Dictionary <string, int>( ); foreach (var c in PREFIXES) { DecomposeTable(dbFolder, db.GetSubtable(c.ToString( ))); // level one get always decomposed } return(true); }