mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-09-30 07:37:34 +02:00
Fixed issues around storing Longs in the database.
This commit is contained in:
parent
8ee876eac4
commit
ec5dacfc3a
@ -37,6 +37,7 @@ import us.tastybento.bskyblock.util.Util;
|
|||||||
public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
|
public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
|
||||||
|
|
||||||
private static final String DATABASE_FOLDER_NAME = "database";
|
private static final String DATABASE_FOLDER_NAME = "database";
|
||||||
|
private static final boolean DEBUG = false;
|
||||||
public FlatFileDatabaseHandler(BSkyBlock plugin, Class<T> type, DatabaseConnecter databaseConnecter) {
|
public FlatFileDatabaseHandler(BSkyBlock plugin, Class<T> type, DatabaseConnecter databaseConnecter) {
|
||||||
super(plugin, type, databaseConnecter);
|
super(plugin, type, databaseConnecter);
|
||||||
}
|
}
|
||||||
@ -135,66 +136,81 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
|
|||||||
|
|
||||||
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(field.getName(), type);
|
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(field.getName(), type);
|
||||||
Method method = propertyDescriptor.getWriteMethod();
|
Method method = propertyDescriptor.getWriteMethod();
|
||||||
//plugin.getLogger().info("DEBUG: " + field.getName() + ": " + propertyDescriptor.getPropertyType().getTypeName());
|
if (DEBUG)
|
||||||
if (propertyDescriptor.getPropertyType().equals(HashMap.class)) {
|
plugin.getLogger().info("DEBUG: " + field.getName() + ": " + propertyDescriptor.getPropertyType().getTypeName());
|
||||||
|
if (config.contains(field.getName())) {
|
||||||
|
if (propertyDescriptor.getPropertyType().equals(HashMap.class)) {
|
||||||
|
|
||||||
|
// Note that we have no idea what type this is
|
||||||
|
List<Type> collectionTypes = Util.getCollectionParameterTypes(method);
|
||||||
|
// collectionTypes should be 2 long
|
||||||
|
Type keyType = collectionTypes.get(0);
|
||||||
|
Type valueType = collectionTypes.get(1);
|
||||||
|
if (DEBUG)
|
||||||
|
plugin.getLogger().info("DEBUG: is HashMap<" + keyType.getTypeName() + ", " + valueType.getTypeName() + ">");
|
||||||
|
// TODO: this may not work with all keys. Further serialization may be required.
|
||||||
|
HashMap<Object,Object> value = new HashMap<Object, Object>();
|
||||||
|
for (String key : config.getConfigurationSection(field.getName()).getKeys(false)) {
|
||||||
|
Object mapKey = deserialize(key,Class.forName(keyType.getTypeName()));
|
||||||
|
Object mapValue = deserialize(config.get(field.getName() + "." + key), Class.forName(valueType.getTypeName()));
|
||||||
|
if (DEBUG) {
|
||||||
|
plugin.getLogger().info("DEBUG: mapKey = " + mapKey + " (" + mapKey.getClass().getCanonicalName() + ")");
|
||||||
|
plugin.getLogger().info("DEBUG: mapValue = " + mapValue + " (" + mapValue.getClass().getCanonicalName() + ")");
|
||||||
|
}
|
||||||
|
value.put(mapKey, mapValue);
|
||||||
|
}
|
||||||
|
method.invoke(instance, value);
|
||||||
|
} else if (propertyDescriptor.getPropertyType().equals(Set.class)) {
|
||||||
|
if (DEBUG) {
|
||||||
|
plugin.getLogger().info("DEBUG: is Set " + propertyDescriptor.getReadMethod().getGenericReturnType().getTypeName());
|
||||||
|
plugin.getLogger().info("DEBUG: adding a set");
|
||||||
|
}
|
||||||
|
// Loop through the collection resultset
|
||||||
|
// Note that we have no idea what type this is
|
||||||
|
List<Type> collectionTypes = Util.getCollectionParameterTypes(method);
|
||||||
|
// collectionTypes should be only 1 long
|
||||||
|
Type setType = collectionTypes.get(0);
|
||||||
|
if (DEBUG)
|
||||||
|
plugin.getLogger().info("DEBUG: is HashSet<" + setType.getTypeName() + ">");
|
||||||
|
Set<Object> value = new HashSet<Object>();
|
||||||
|
if (DEBUG) {
|
||||||
|
plugin.getLogger().info("DEBUG: collection type argument = " + collectionTypes);
|
||||||
|
plugin.getLogger().info("DEBUG: setType = " + setType.getTypeName());
|
||||||
|
}
|
||||||
|
for (Object listValue: config.getList(field.getName())) {
|
||||||
|
//plugin.getLogger().info("DEBUG: collectionResultSet size = " + collectionResultSet.getFetchSize());
|
||||||
|
((Set<Object>) value).add(deserialize(listValue,Class.forName(setType.getTypeName())));
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: this may not work with all keys. Further serialization may be required.
|
||||||
|
//Set<Object> value = new HashSet((List<Object>) config.getList(field.getName()));
|
||||||
|
method.invoke(instance, value);
|
||||||
|
} else if (propertyDescriptor.getPropertyType().equals(ArrayList.class)) {
|
||||||
|
//plugin.getLogger().info("DEBUG: is Set " + propertyDescriptor.getReadMethod().getGenericReturnType().getTypeName());
|
||||||
|
if (DEBUG)
|
||||||
|
plugin.getLogger().info("DEBUG: adding a set");
|
||||||
|
// Loop through the collection resultset
|
||||||
|
// Note that we have no idea what type this is
|
||||||
|
List<Type> collectionTypes = Util.getCollectionParameterTypes(method);
|
||||||
|
// collectionTypes should be only 1 long
|
||||||
|
Type setType = collectionTypes.get(0);
|
||||||
|
List<Object> value = new ArrayList<Object>();
|
||||||
|
//plugin.getLogger().info("DEBUG: collection type argument = " + collectionTypes);
|
||||||
|
//plugin.getLogger().info("DEBUG: setType = " + setType.getTypeName());
|
||||||
|
for (Object listValue: config.getList(field.getName())) {
|
||||||
|
//plugin.getLogger().info("DEBUG: collectionResultSet size = " + collectionResultSet.getFetchSize());
|
||||||
|
((List<Object>) value).add(deserialize(listValue,Class.forName(setType.getTypeName())));
|
||||||
|
}
|
||||||
|
// TODO: this may not work with all keys. Further serialization may be required.
|
||||||
|
//Set<Object> value = new HashSet((List<Object>) config.getList(field.getName()));
|
||||||
|
method.invoke(instance, value);
|
||||||
|
} else {
|
||||||
|
// Not a collection
|
||||||
|
Object value = config.get(field.getName());
|
||||||
|
method.invoke(instance, deserialize(value,propertyDescriptor.getPropertyType()));
|
||||||
|
|
||||||
// Note that we have no idea what type this is
|
|
||||||
List<Type> collectionTypes = Util.getCollectionParameterTypes(method);
|
|
||||||
// collectionTypes should be 2 long
|
|
||||||
Type keyType = collectionTypes.get(0);
|
|
||||||
Type valueType = collectionTypes.get(1);
|
|
||||||
//plugin.getLogger().info("DEBUG: is HashMap<" + keyType.getTypeName() + ", " + valueType.getTypeName() + ">");
|
|
||||||
// TODO: this may not work with all keys. Further serialization may be required.
|
|
||||||
HashMap<Object,Object> value = new HashMap<Object, Object>();
|
|
||||||
for (String key : config.getConfigurationSection(field.getName()).getKeys(false)) {
|
|
||||||
Object mapKey = deserialize(key,Class.forName(keyType.getTypeName()));
|
|
||||||
Object mapValue = deserialize(config.get(field.getName() + "." + key), Class.forName(valueType.getTypeName()));
|
|
||||||
value.put(mapKey, mapValue);
|
|
||||||
}
|
}
|
||||||
method.invoke(instance, value);
|
|
||||||
} else if (propertyDescriptor.getPropertyType().equals(Set.class)) {
|
|
||||||
//plugin.getLogger().info("DEBUG: is Set " + propertyDescriptor.getReadMethod().getGenericReturnType().getTypeName());
|
|
||||||
//plugin.getLogger().info("DEBUG: adding a set");
|
|
||||||
// Loop through the collection resultset
|
|
||||||
// Note that we have no idea what type this is
|
|
||||||
List<Type> collectionTypes = Util.getCollectionParameterTypes(method);
|
|
||||||
// collectionTypes should be only 1 long
|
|
||||||
Type setType = collectionTypes.get(0);
|
|
||||||
//plugin.getLogger().info("DEBUG: is HashSet<" + setType.getTypeName() + ">");
|
|
||||||
Set<Object> value = new HashSet<Object>();
|
|
||||||
//plugin.getLogger().info("DEBUG: collection type argument = " + collectionTypes);
|
|
||||||
//plugin.getLogger().info("DEBUG: setType = " + setType.getTypeName());
|
|
||||||
for (Object listValue: config.getList(field.getName())) {
|
|
||||||
//plugin.getLogger().info("DEBUG: collectionResultSet size = " + collectionResultSet.getFetchSize());
|
|
||||||
((Set<Object>) value).add(deserialize(listValue,Class.forName(setType.getTypeName())));
|
|
||||||
}
|
|
||||||
// TODO: this may not work with all keys. Further serialization may be required.
|
|
||||||
//Set<Object> value = new HashSet((List<Object>) config.getList(field.getName()));
|
|
||||||
method.invoke(instance, value);
|
|
||||||
} else if (propertyDescriptor.getPropertyType().equals(ArrayList.class)) {
|
|
||||||
//plugin.getLogger().info("DEBUG: is Set " + propertyDescriptor.getReadMethod().getGenericReturnType().getTypeName());
|
|
||||||
//plugin.getLogger().info("DEBUG: adding a set");
|
|
||||||
// Loop through the collection resultset
|
|
||||||
// Note that we have no idea what type this is
|
|
||||||
List<Type> collectionTypes = Util.getCollectionParameterTypes(method);
|
|
||||||
// collectionTypes should be only 1 long
|
|
||||||
Type setType = collectionTypes.get(0);
|
|
||||||
List<Object> value = new ArrayList<Object>();
|
|
||||||
//plugin.getLogger().info("DEBUG: collection type argument = " + collectionTypes);
|
|
||||||
//plugin.getLogger().info("DEBUG: setType = " + setType.getTypeName());
|
|
||||||
for (Object listValue: config.getList(field.getName())) {
|
|
||||||
//plugin.getLogger().info("DEBUG: collectionResultSet size = " + collectionResultSet.getFetchSize());
|
|
||||||
((List<Object>) value).add(deserialize(listValue,Class.forName(setType.getTypeName())));
|
|
||||||
}
|
|
||||||
// TODO: this may not work with all keys. Further serialization may be required.
|
|
||||||
//Set<Object> value = new HashSet((List<Object>) config.getList(field.getName()));
|
|
||||||
method.invoke(instance, value);
|
|
||||||
} else {
|
|
||||||
// Not a collection
|
|
||||||
Object value = config.get(field.getName());
|
|
||||||
method.invoke(instance, deserialize(value,propertyDescriptor.getPropertyType()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return instance;
|
return instance;
|
||||||
@ -301,7 +317,12 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Object deserialize(Object value, Class<? extends Object> clazz) {
|
private Object deserialize(Object value, Class<? extends Object> clazz) {
|
||||||
//plugin.getLogger().info("DEBUG: deserialize - class is " + clazz.getCanonicalName());
|
if (DEBUG) {
|
||||||
|
plugin.getLogger().info("DEBUG: deserialize - class is " + clazz.getCanonicalName());
|
||||||
|
plugin.getLogger().info("DEBUG: value is " + value);
|
||||||
|
if (value != null)
|
||||||
|
plugin.getLogger().info("DEBUG: value class is " + value.getClass().getCanonicalName());
|
||||||
|
}
|
||||||
if (value instanceof String && value.equals("null")) {
|
if (value instanceof String && value.equals("null")) {
|
||||||
// If the value is null as a string, return null
|
// If the value is null as a string, return null
|
||||||
return null;
|
return null;
|
||||||
@ -311,6 +332,9 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
// Types that need to be deserialized
|
// Types that need to be deserialized
|
||||||
|
if (clazz.equals(Long.class) && value.getClass().equals(Integer.class)) {
|
||||||
|
return new Long((Integer)value);
|
||||||
|
}
|
||||||
if (clazz.equals(UUID.class)) {
|
if (clazz.equals(UUID.class)) {
|
||||||
value = UUID.fromString((String)value);
|
value = UUID.fromString((String)value);
|
||||||
}
|
}
|
||||||
|
@ -77,7 +77,7 @@ public abstract class AbstractDatabaseHandler<T> {
|
|||||||
* instead of the names of the variables
|
* instead of the names of the variables
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
protected String getColumns(boolean usePlaceHolders) {
|
public String getColumns(boolean usePlaceHolders) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
boolean first = true;
|
boolean first = true;
|
||||||
@ -109,7 +109,7 @@ public abstract class AbstractDatabaseHandler<T> {
|
|||||||
* @throws SQLException
|
* @throws SQLException
|
||||||
* @throws ClassNotFoundException
|
* @throws ClassNotFoundException
|
||||||
*/
|
*/
|
||||||
protected abstract List<T> loadObjects() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, SQLException, SecurityException, ClassNotFoundException;
|
public abstract List<T> loadObjects() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, SQLException, SecurityException, ClassNotFoundException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a <T> filled with values from the corresponding
|
* Creates a <T> filled with values from the corresponding
|
||||||
@ -125,7 +125,7 @@ public abstract class AbstractDatabaseHandler<T> {
|
|||||||
* @throws ClassNotFoundException
|
* @throws ClassNotFoundException
|
||||||
* @throws SecurityException
|
* @throws SecurityException
|
||||||
*/
|
*/
|
||||||
protected abstract T loadObject(String uniqueId) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, SQLException, SecurityException, ClassNotFoundException;
|
public abstract T loadObject(String uniqueId) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, SQLException, SecurityException, ClassNotFoundException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save T into the corresponding database
|
* Save T into the corresponding database
|
||||||
@ -140,7 +140,7 @@ public abstract class AbstractDatabaseHandler<T> {
|
|||||||
* @throws SQLException
|
* @throws SQLException
|
||||||
* @throws NoSuchMethodException
|
* @throws NoSuchMethodException
|
||||||
*/
|
*/
|
||||||
protected abstract void saveObject(T instance) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, SQLException, SecurityException, InstantiationException, NoSuchMethodException;
|
public abstract void saveObject(T instance) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, SQLException, SecurityException, InstantiationException, NoSuchMethodException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes the object with the unique id from the database
|
* Deletes the object with the unique id from the database
|
||||||
|
@ -540,7 +540,7 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
|
|||||||
* @see us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler#selectObject(java.lang.String)
|
* @see us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler#selectObject(java.lang.String)
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected T loadObject(String uniqueId) throws InstantiationException,
|
public T loadObject(String uniqueId) throws InstantiationException,
|
||||||
IllegalAccessException, IllegalArgumentException,
|
IllegalAccessException, IllegalArgumentException,
|
||||||
InvocationTargetException, IntrospectionException, SQLException, SecurityException, ClassNotFoundException {
|
InvocationTargetException, IntrospectionException, SQLException, SecurityException, ClassNotFoundException {
|
||||||
Connection connection = null;
|
Connection connection = null;
|
||||||
|
Loading…
Reference in New Issue
Block a user