Database is now a singleton.

This commit is contained in:
Fabian Schlenz 2017-02-22 06:53:51 +01:00
parent c5921304ed
commit a2224b326d
1 changed files with 7 additions and 0 deletions

View File

@ -53,6 +53,7 @@ public class Database {
public UserManager user_manager;
public TelegramClient client;
private final static Logger logger = LoggerFactory.getLogger(Database.class);
private static Database instance = null;
public Database(UserManager user_manager, TelegramClient client) {
this(user_manager, client, true);
@ -80,6 +81,7 @@ public class Database {
}
this.init(update_db);
instance = this;
System.out.println("Database is ready.");
}
@ -89,6 +91,11 @@ public class Database {
updates.doUpdates();
}
public static Database getInstance() {
if (instance == null) throw new RuntimeException("Database is not initialized but getInstance() was called.");
return instance;
}
public void backupDatabase(int currentVersion) {
String filename = String.format(Config.FILE_NAME_DB_BACKUP, currentVersion);
System.out.println(" Creating a backup of your database as " + filename);