Esempio n. 1
0
 /// <summary>
 /// Writes one file from the given sub table
 /// </summary>
 /// <param name="sw">Stream to write to</param>
 /// <param name="subTable">The subtable to write out</param>
 private static void WriteFile(StreamWriter sw, rtTable subTable)
 {
     foreach (var rec in subTable)
     {
         sw.WriteLine(rec.Value.AsCsv( ));
     }
 }
Esempio n. 2
0
 /// <summary>
 /// cTor: init the database
 /// </summary>
 public rtDatabase()
 {
     m_db = new rtTable[m_PREFIX.Length];
     for (int i = 0; i < m_PREFIX.Length; i++)
     {
         m_db[i] = new rtTable(m_PREFIX[i].ToString( ));
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Gets a table with prefix to either write or decompose again
        /// </summary>
        /// <param name="dbFolder">The database folder</param>
        /// <param name="table">The table containing the prefixed records</param>
        /// <param name="prefix">The prefix limiting the choice</param>
        /// <returns></returns>
        private static void DecomposeTable(string dbFolder, rtTable table)
        {
            int cnt = table.Count; // how many with that prefix

            if (cnt <= NREC)
            {
                WriteFile(dbFolder, table, "");
                return;
            }
            else
            {
                // decompose and analyze
                SortedDictionary <string, rtTable> tmpParts = new SortedDictionary <string, rtTable>( );
                // make a split into all child tables
                var writeTable = new rtTable(table.DbPrefix); // the main table carries the submitted prefix
                int total      = 0;
                foreach (var c in PREFIXES)
                {
                    // scan all prefixes
                    string qualifier = table.DbPrefix + c.ToString( ); // one child level deeper
                    var    subTable  = table.GetSubtable(qualifier);
                    if (subTable != null)
                    {
                        if ((subTable != null) && (total + subTable.Count) < NREC)
                        {
                            // collect
                            total += subTable.Count;
                            writeTable.AddSubtable(table.GetSubtable(qualifier));
                        }
                        else
                        {
                            tmpParts.Add(qualifier, table.GetSubtable(qualifier)); // collect all tables
                        }
                    }
                }
                // first write the ones that fit
                // "{children":["39","3C"]}
                // compose extension:
                string extension = "";
                foreach (var tab in tmpParts)
                {
                    extension += $"\"{tab.Key}\",";
                }
                extension = extension.Substring(0, extension.Length - 1); // remove last comma
                extension = $"\"children\":[{extension}]";
                WriteFile(dbFolder, writeTable, extension);
                writeTable = null; // free

                // process the ones that do not fit
                foreach (var tab in tmpParts)
                {
                    DecomposeTable(dbFolder, tab.Value); // recursive
                }
                return;
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Exec INSERT statements for an airport subtable
 /// </summary>
 /// <param name="sqConnection">The db connection</param>
 /// <param name="subTable">The subtable to dump</param>
 /// <returns>The result string, either empty or error</returns>
 private static string WriteFile(SQLiteConnection sqConnection, rtTable subTable)
 {
     using (SQLiteCommand sqlite_cmd = sqConnection.CreateCommand( )) {
         foreach (var rec in subTable)
         {
             try {
                 sqlite_cmd.CommandText = "INSERT INTO routes (flight_code, from_apt_icao, to_apt_icao)"
                                          + $" VALUES ('{rec.Value.flight_code}','{rec.Value.from_apt_icao}','{rec.Value.to_apt_icao}');";
                 sqlite_cmd.ExecuteNonQuery( );
             }
             catch (SQLiteException sqex) {
                 return($"ERROR - writing route: {sqex.Message}\n");
             }
         }
     }
     return("");
 }
Esempio n. 5
0
        /// <summary>
        /// Writes one file from the given sub table
        /// </summary>
        /// <param name="dbFolder">The folder to write to</param>
        /// <param name="subTable">The subtable to write out</param>
        /// <param name="extension">The extension string to add for the FA record keeping</param>
        private static void WriteFile(string dbFolder, rtTable subTable, string extension)
        {
            string fName = Path.Combine(dbFolder, subTable.DbPrefix + ".json");

            using (var sw = new StreamWriter(fName)) {
                string buffer = "";
                foreach (var rec in subTable)
                {
                    buffer += rec.Value.AsJson(subTable.DbPrefix) + ","; // delete prefix from the record
                }
                if (!string.IsNullOrEmpty(extension))
                {
                    buffer += extension;
                }
                if (buffer.EndsWith(","))
                {
                    buffer = buffer.Substring(0, buffer.Length - 1); // cut last comma
                }
                sw.Write($"{{{buffer}}}");                           //  { buffer }
            }
        }