示例#1
0
        private void WriteEntries(IEnumerable <Entry> entries, SqliteConnection connection, int maxId, ref DateTime min, ref DateTime max)
        {
            using (var transaction = connection.BeginTransaction())
            {
                foreach (var e in entries)
                {
                    StoryTable.Write(e, connection, transaction);

                    if (e.Date > max)
                    {
                        max = e.Date;
                    }

                    if (e.Date < min)
                    {
                        min = e.Date;
                    }
                }

                DateRangeTable.Write(min, max, connection, transaction);
                LastWriteTable.Write(maxId, connection, transaction);

                transaction.Commit();
            }
        }
示例#2
0
        /// <summary>
        /// Handles the one-time migration of data from the binary file format to SQLite.
        /// </summary>
        public static void Main(string[] args)
        {
            const string databaseLocation = @"C:\git\csharp\hn-reader\data";
            const string dataLocation     = @"C:\git\csharp\hn-reader";

            var dbName = Path.Combine(databaseLocation, "hn-data.sqlite");

            if (File.Exists(dbName))
            {
                throw new InvalidOperationException("Database already exists! " + dbName);
            }

            using (var connection = Connector.ConnectToFile(dbName))
            {
                var command = new SQLiteCommand(Schema.Create, connection);

                command.ExecuteNonQuery();

                VersionTable.Write(1, connection);

                var backfilledFiles = Directory.GetFiles(dataLocation, "*-complete.bin");

                var trackers = new DataTrackers();

                using (var transaction = connection.BeginTransaction())
                {
                    foreach (var backfilledFile in backfilledFiles)
                    {
                        WriteFileIntoDatabase(backfilledFile, connection, trackers);
                        Console.WriteLine("Completed file: " + Path.GetFileName(backfilledFile));
                    }

                    transaction.Commit();
                }

                using (var transaction = connection.BeginTransaction())
                {
                    WriteFileIntoDatabase(Path.Combine(dataLocation, "hn.bin"), connection, trackers);
                    transaction.Commit();
                }

                LastWriteTable.Write(trackers.MaxId, connection);
                DateRangeTable.Write(trackers.MinDate, trackers.MaxDate, connection);
            }
        }