1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-22 16:56:16 +00:00

Database is now a singleton.

This commit is contained in:
Fabian Schlenz 2017-02-22 06:53:51 +01:00
parent c5921304ed
commit a2224b326d

View File

@ -53,6 +53,7 @@ public class Database {
public UserManager user_manager; public UserManager user_manager;
public TelegramClient client; public TelegramClient client;
private final static Logger logger = LoggerFactory.getLogger(Database.class); private final static Logger logger = LoggerFactory.getLogger(Database.class);
private static Database instance = null;
public Database(UserManager user_manager, TelegramClient client) { public Database(UserManager user_manager, TelegramClient client) {
this(user_manager, client, true); this(user_manager, client, true);
@ -80,6 +81,7 @@ public class Database {
} }
this.init(update_db); this.init(update_db);
instance = this;
System.out.println("Database is ready."); System.out.println("Database is ready.");
} }
@ -89,6 +91,11 @@ public class Database {
updates.doUpdates(); 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) { public void backupDatabase(int currentVersion) {
String filename = String.format(Config.FILE_NAME_DB_BACKUP, currentVersion); String filename = String.format(Config.FILE_NAME_DB_BACKUP, currentVersion);
System.out.println(" Creating a backup of your database as " + filename); System.out.println(" Creating a backup of your database as " + filename);