예제 #1
0
        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 (_db != null)
                    {
                        _db.Close();
                        _db.Dispose();
                        _db = null;
                    }
                    _instance = null;

                    if (_instance != null)
                    {
                        _instance.Dispose();
                        _instance = null;
                    }
                }
            } catch (Exception) {
            }
        }
예제 #2
0
        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);
            }
        }
예제 #3
0
        public static void Initialize(string databasePath, Action <string, string> log, Action <string, string> logError)
        {
            // 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);

            Log      = log;
            LogError = logError;

            _instance = new LoginDatabase();

            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)
                    {
                        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' )", _instance._db)) {
                            createTables.ExecuteNonQuery();
                        }
                        Log(Category, "Using " + databasePath);
                        _instance.PrepareStatements();
                        return;
                    }
                    else
                    {
                        Log(Category, "Using " + databasePath);
                        _instance.PrepareStatements();
                        return;
                    }
                }
            }

            LogError(Category, "Error creating database");
            _instance.Dispose();
            _instance = null;
        }
예제 #4
0
        public static void Initialize(string databasePath, Action<string, string> log, Action<string, string> logError)
        {
            // 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);

            Log = log;
            LogError = logError;

            _instance = new LoginDatabase();

            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) {
                        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' )", _instance._db)) {
                            createTables.ExecuteNonQuery();
                        }
                        Log(Category, "Using " + databasePath);
                        _instance.PrepareStatements();
                        return;
                    } else {
                        Log(Category, "Using " + databasePath);
                        _instance.PrepareStatements();
                        return;
                    }
                }
            }

            LogError(Category, "Error creating database");
            _instance.Dispose();
            _instance = null;
        }
예제 #5
0
        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 (_db != null) {
                        _db.Close();
                        _db.Dispose();
                        _db = null;
                    }
                    _instance = null;

                    if (_instance != null) {
                        _instance.Dispose();
                        _instance = null;
                    }
                }
            } catch (Exception) {
            }
        }