Пример #1
0
        public static void AlterDB(string fileName)
        {
            using (IDBHelpers dbHelpers = new DBHelpers(fileName))
            {
                Console.WriteLine("Creating Tables...");
                CreateTables(dbHelpers);
                Console.WriteLine("Altering DB...");

                using (DataTable dt = dbHelpers.GetDataTable("pragma table_info(File_Battles)"))
                {
                    CheckAndCreateColumn("File_Battles", "bpMileage", "INTEGER null", dbHelpers, dt);
                    CheckAndCreateColumn("File_Battles", "bpRatingEff", "INTEGER null", dbHelpers, dt);
                    CheckAndCreateColumn("File_Battles", "bpRatingEffWeight", "INTEGER null", dbHelpers, dt);
                    CheckAndCreateColumn("File_Battles", "bpRatingBR", "INTEGER null", dbHelpers, dt);
                    CheckAndCreateColumn("File_Battles", "bpRatingBRWeight", "INTEGER null", dbHelpers, dt);
                    CheckAndCreateColumn("File_Battles", "bpRatingWN7", "INTEGER null", dbHelpers, dt);
                    CheckAndCreateColumn("File_Battles", "bpRatingWN7Weight", "INTEGER null", dbHelpers, dt);
                    CheckAndCreateColumn("File_Battles", "bpRatingWN8", "INTEGER null", dbHelpers, dt);
                    CheckAndCreateColumn("File_Battles", "bpRatingWN8Weight", "INTEGER null", dbHelpers, dt);
                    CheckAndCreateColumn("File_Battles", "bpRatingVersion", "INTEGER default (0) null", dbHelpers, dt);

                    //CheckAndCreateColumn("File_Battles", "bpRatingExpDamage", "real default (0) null", dbHelpers, dt);
                    //CheckAndCreateColumn("File_Battles", "bpRatingExpFrag", "real default (0) null", dbHelpers, dt);
                    //CheckAndCreateColumn("File_Battles", "bpRatingExpSpot", "real default (0) null", dbHelpers, dt);
                    //CheckAndCreateColumn("File_Battles", "bpRatingExpDef", "real default (0) null", dbHelpers, dt);
                    //CheckAndCreateColumn("File_Battles", "bpRatingExpWin", "real default (0) null", dbHelpers, dt);
                }

                using (DataTable dt = dbHelpers.GetDataTable("pragma table_info(RecentBattles)"))
                {
                    CheckAndCreateColumn("RecentBattles", "rbMileage", "real default(0)", dbHelpers, dt);
                    CheckAndCreateColumn("RecentBattles", "rbRatingEff", "real default(0)", dbHelpers, dt);
                    CheckAndCreateColumn("RecentBattles", "rbRatingBR", "real default(0)", dbHelpers, dt);
                    CheckAndCreateColumn("RecentBattles", "rbRatingWN7", "real default(0)", dbHelpers, dt);
                    CheckAndCreateColumn("RecentBattles", "rbRatingWN8", "real default(0)", dbHelpers, dt);
                    CheckAndCreateColumn("RecentBattles", "rbDamageAssistedRadio", "real default(0)", dbHelpers, dt);
                    CheckAndCreateColumn("RecentBattles", "rbDamageAssistedTracks", "real default(0)", dbHelpers, dt);
                    CheckAndCreateColumn("RecentBattles", "rbGlobalAvgDefPoints", "REAL default (0)  NULL", dbHelpers, dt);
                    CheckAndCreateColumn("RecentBattles", "rbBattleMode", "INTEGER default (15)  NULL", dbHelpers, dt);
                }

                using (DataTable dt = dbHelpers.GetDataTable("pragma table_info(RecentBattles_Session)"))
                {
                    var fieldCheck = (from x in dt.AsEnumerable()
                                      where x.Field <string>("name") == "rsUEDateFrom"
                                      select x).DefaultIfEmpty(null).FirstOrDefault();

                    if (fieldCheck == null)
                    {
                        string sql = "Alter table RecentBattles_Session Add rsUEDateFrom real NULL";
                        dbHelpers.ExecuteNonQuery(sql);
                        sql = "Alter table RecentBattles_Session Add rsUEDateTo real NULL";
                        dbHelpers.ExecuteNonQuery(sql);
                        sql = @"UPDATE RecentBattles_Session
                                SET
                                      rsUEDateFrom = (SELECT ifnull(min(RecentBattles.rbBattleTime), datetime('now', 'unixepoch'))
                                                            FROM RecentBattles
                                                            WHERE RecentBattles.rbSessionID = RecentBattles_Session.rsKey )
                                    , rsUEDateTo = (SELECT ifnull(Max(RecentBattles.rbBattleTime), datetime('now', 'unixepoch'))
                                                            FROM RecentBattles
                                                            WHERE RecentBattles.rbSessionID = RecentBattles_Session.rsKey )
                                WHERE
                                    EXISTS (
                                        SELECT *
                                        FROM RecentBattles
                                        WHERE RecentBattles.rbSessionID = RecentBattles_Session.rsKey
                                    )";
                        dbHelpers.ExecuteNonQuery(sql);
                        sql = @"UPDATE RecentBattles_Session
                                SET
                                      rsUEDateFrom =  strftime('%s', '2000-01-01 00:00:00')
                                    , rsUEDateTo = strftime( '%s', '2000-01-01 00:00:01')
                                WHERE
                                  rsUEDateFrom is null";
                        dbHelpers.ExecuteNonQuery(sql);
                        dbHelpers.ExecuteNonQuery(sql);
                        sql = @"update RecentBattles_Session
                                set rsUEDateTo =  strftime('%s', '2037-01-01 00:00:00')
                                where rsID = (select max(rsID) from RecentBattles_Session)";
                        dbHelpers.ExecuteNonQuery(sql);
                    }
                }



                Console.WriteLine("Altering DB done");
            }
        }