if (monitor_changes == null || monitor_interval == null)
return false;
monitor = monitor_changes;
this.monitor_interval = monitor_interval.getMilliSeconds();
final Database db;
try {
final Object o = type.initialise(this, n);
if (o == null)
return false;
this.db = db = (Database) o;
} catch (final RuntimeException e) {
if (e instanceof DatabaseException) {// not in a catch clause to not produce a ClassNotFoundException when this class is loaded and SQLibrary is not present
Skript.error(e.getMessage());
return false;
}
throw e;
}
SkriptLogger.setNode(null);
if (!db.open()) {
Skript.error("Cannot connect to the database '" + databaseName + "'!");
return false;
}
// periodically executes queries to keep the collection alive
final Task keepAlive = new Task(Skript.getInstance(), 20, 20, true) {
@Override
public void run() {
synchronized (dbLock) {
try {
final Database db = (Database) DatabaseStorage.this.db;
if (db != null)
db.query("SELECT * FROM " + TABLE_NAME + " LIMIT 1");
} catch (final SQLException e) {}
}
}
};
Skript.closeOnDisable(keepAlive);
try {
final boolean hasOldTable = db.isTable(OLD_TABLE_NAME);
final boolean hadNewTable = db.isTable(TABLE_NAME);
try {
db.query(type.createQuery);
} catch (final SQLException e) {
Skript.error("Could not create the variables table in the database '" + databaseName + "': " + e.getLocalizedMessage() + ". "
+ "Please create the table yourself using the following query: " + type.createQuery.replace(",", ", ").replaceAll("\\s+", " "));
return false;
}
if (!prepareQueries()) {
return false;
}
// old
if (hasOldTable) {
final ResultSet r1 = db.query("SELECT " + SELECT_ORDER + " FROM " + OLD_TABLE_NAME);
assert r1 != null;
try {
oldLoadVariables(r1);
} finally {
r1.close();
}
}
// new
final ResultSet r2 = db.query("SELECT " + SELECT_ORDER + " FROM " + TABLE_NAME);
assert r2 != null;
try {
loadVariables(r2);
} finally {
r2.close();
}
// store old variables in new table and delete the old table
if (hasOldTable) {
if (!hadNewTable) {
Skript.info("[2.1] Updating the database '" + databaseName + "' to the new format...");
try {
Variables.getReadLock().lock();
for (final Entry<String, Object> v : Variables.getVariablesHashMap().entrySet()) {
if (accept(v.getKey())) {// only one database was possible, so only checking this database is correct
@SuppressWarnings("null")
final SerializedVariable var = Variables.serialize(v.getKey(), v.getValue());
final SerializedVariable.Value d = var.value;
save(var.name, d == null ? null : d.type, d == null ? null : d.data);
}
}
Skript.info("Updated and transferred " + Variables.getVariablesHashMap().size() + " variables to the new table.");
} finally {
Variables.getReadLock().unlock();
}
}
db.query("DELETE FROM " + OLD_TABLE_NAME + " WHERE value IS NULL");
db.query("DELETE FROM old USING " + OLD_TABLE_NAME + " AS old, " + TABLE_NAME + " AS new WHERE old.name = new.name");
final ResultSet r = db.query("SELECT * FROM " + OLD_TABLE_NAME + " LIMIT 1");
try {
if (r.next()) {// i.e. the old table is not empty
Skript.error("Could not successfully convert & transfer all variables to the new table in the database '" + databaseName + "'. "
+ "Variables that could not be transferred are left in the old table and Skript will reattempt to transfer them whenever it starts until the old table is empty or is manually deleted. "
+ "Please note that variables recreated by scripts will count as converted and will be removed from the old table on the next restart.");
} else {
boolean error = false;
try {
disconnect(); // prevents SQLITE_LOCKED error
connect();
db.query("DROP TABLE " + OLD_TABLE_NAME);
} catch (final SQLException e) {
Skript.error("There was an error deleting the old variables table from the database '" + databaseName + "', please delete it yourself: " + e.getLocalizedMessage());
error = true;
}
if (!error)