private static bool CloseHandler(CtrlType sig) { if (_instance != null) { _instance.Dispose(); } switch (sig) { case CtrlType.CTRL_C_EVENT: case CtrlType.CTRL_LOGOFF_EVENT: case CtrlType.CTRL_SHUTDOWN_EVENT: case CtrlType.CTRL_CLOSE_EVENT: default: return(false); } }
public static void Initialize(string databasePath) { // we need to safely dispose of the database when the application closes // this is a console app, so we need to hook into the console ctrl signal _closeHandler += CloseHandler; SetConsoleCtrlHandler(_closeHandler, true); _instance = new UsersDatabase(); databasePath = Path.GetFullPath(databasePath); if (!File.Exists(databasePath)) { SQLiteConnection.CreateFile(databasePath); } if (File.Exists(databasePath)) { SQLiteConnectionStringBuilder connBuilder = new SQLiteConnectionStringBuilder() { DataSource = databasePath, Version = 3, PageSize = 4096, CacheSize = 10000, JournalMode = SQLiteJournalModeEnum.Wal, LegacyFormat = false, DefaultTimeout = 500 }; _instance._db = new SQLiteConnection(connBuilder.ToString()); _instance._db.Open(); if (_instance._db.State == ConnectionState.Open) { bool read = false; using (SQLiteCommand queryTables = new SQLiteCommand("SELECT * FROM sqlite_master WHERE type='table' AND name='users'", _instance._db)) { using (SQLiteDataReader reader = queryTables.ExecuteReader()) { while (reader.Read()) { read = true; break; } } } if (!read) { L.Log(Category, "No database found, creating now"); using (SQLiteCommand createTables = new SQLiteCommand(@"CREATE TABLE users ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, password TEXT NOT NULL, email TEXT NOT NULL, country TEXT NOT NULL, lastip TEXT NOT NULL, lasttime INTEGER NULL DEFAULT '0', session INTEGER NULL DEFAULT '0', score1v1 INTEGER NULL DEFAULT 1000, score2v2 INTEGER NULL DEFAULT 1000, score3v3 INTEGER NULL DEFAULT 1000, disconnects INTEGER NULL DEFAULT '0', allingameticks INTEGER NULL DEFAULT '0', best1v1winstreak INTEGER NULL DEFAULT '0', current1v1winstreak INTEGER NULL DEFAULT '0', modified INTEGER NULL DEFAULT '0', smgamescount INTEGER NULL DEFAULT '0', csmgamescount INTEGER NULL DEFAULT '0', orkgamescount INTEGER NULL DEFAULT '0', eldargamescount INTEGER NULL DEFAULT '0', iggamescount INTEGER NULL DEFAULT '0', necrgamescount INTEGER NULL DEFAULT '0', taugamescount INTEGER NULL DEFAULT '0', degamescount INTEGER NULL DEFAULT '0', sobgamescount INTEGER NULL DEFAULT '0', smwincount INTEGER NULL DEFAULT '0', csmwincount INTEGER NULL DEFAULT '0', orkwincount INTEGER NULL DEFAULT '0', eldarwincount INTEGER NULL DEFAULT '0', igwincount INTEGER NULL DEFAULT '0', necrwincount INTEGER NULL DEFAULT '0', tauwincount INTEGER NULL DEFAULT '0', dewincount INTEGER NULL DEFAULT '0', sobwincount INTEGER NULL DEFAULT '0' )", _instance._db)) { createTables.ExecuteNonQuery(); } L.Log(Category, "Using " + databasePath); _instance.PrepareStatements(); return; } else { L.Log(Category, "Using " + databasePath); _instance.PrepareStatements(); return; } } } L.LogError(Category, "Error creating database"); _instance.Dispose(); _instance = null; }
protected virtual void Dispose(bool disposing) { try { if (disposing) { if (_getUsersByName != null) { _getUsersByName.Dispose(); _getUsersByName = null; } if (_getUsersByEmail != null) { _getUsersByEmail.Dispose(); _getUsersByEmail = null; } if (_updateUser != null) { _updateUser.Dispose(); _updateUser = null; } if (_createUser != null) { _createUser.Dispose(); _createUser = null; } if (_countUsers != null) { _countUsers.Dispose(); _countUsers = null; } if (_logUser != null) { _logUser.Dispose(); _logUser = null; } if (_logUserUpdateCountry != null) { _logUserUpdateCountry.Dispose(); _logUserUpdateCountry = null; } if (_updateUserStats != null) { _updateUserStats.Dispose(); _updateUserStats = null; } if (_getUserStatsByProfileId != null) { _getUserStatsByProfileId.Dispose(); _getUserStatsByProfileId = null; } if (_getUserStatsByNick != null) { _getUserStatsByNick.Dispose(); _getUserStatsByNick = null; } if (_db != null) { _db.Close(); _db.Dispose(); _db = null; } _instance = null; if (_instance != null) { _instance.Dispose(); _instance = null; } } } catch (Exception) { } }