From a7fa49a83add125234498963af3e103a40cfa0d8 Mon Sep 17 00:00:00 2001 From: Antoine Aflalo Date: Mon, 13 Feb 2012 11:12:10 +0100 Subject: [PATCH 1/4] Added a database factory and a database configuration file to set easily a Database. --- lib/PatPeter/SQLibrary/DatabaseConfig.java | 145 ++++++++++++++++++ lib/PatPeter/SQLibrary/DatabaseFactory.java | 46 ++++++ .../SQLibrary/InvalidConfiguration.java | 74 +++++++++ 3 files changed, 265 insertions(+) create mode 100644 lib/PatPeter/SQLibrary/DatabaseConfig.java create mode 100644 lib/PatPeter/SQLibrary/DatabaseFactory.java create mode 100644 lib/PatPeter/SQLibrary/InvalidConfiguration.java diff --git a/lib/PatPeter/SQLibrary/DatabaseConfig.java b/lib/PatPeter/SQLibrary/DatabaseConfig.java new file mode 100644 index 0000000..92844aa --- /dev/null +++ b/lib/PatPeter/SQLibrary/DatabaseConfig.java @@ -0,0 +1,145 @@ +/************************************************************************ + * This file is part of SQLibrary. + * + * SQLibrary is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * SQLibrary is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with SQLibrary. If not, see . + ************************************************************************/ +package lib.PatPeter.SQLibrary; + +import java.util.EnumMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.logging.Logger; + +/** + * @author Balor (aka Antoine Aflalo) + * + */ +public class DatabaseConfig { + private final Map config = new EnumMap(Parameter.class); + private DatabaseType type; + private Logger log; + + public enum DatabaseType { + MYSQL, + SQLITE, + ALL; + } + + public enum Parameter { + HOSTNAME(DatabaseType.MYSQL), + USER(DatabaseType.MYSQL), + PASSWORD(DatabaseType.MYSQL), + PORT_NUMBER(DatabaseType.MYSQL), + DATABASE(DatabaseType.MYSQL), + DB_PREFIX(DatabaseType.ALL), + DB_LOCATION(DatabaseType.SQLITE), + DB_NAME(DatabaseType.SQLITE); + private Set dbTypes = new HashSet(); + private final static Map count = new EnumMap( + DatabaseType.class); + + static { + for (Parameter param : values()) + for (DatabaseType type : param.getDbTypes()) { + Integer nb = count.get(type); + nb++; + count.put(type, nb); + } + } + + /** + * + */ + private Parameter(DatabaseType... type) { + for (int i = 0; i < type.length; i++) + dbTypes.add(type[i]); + + } + + public boolean validParam(DatabaseType toCheck) { + if (dbTypes.contains(DatabaseType.ALL)) + return true; + if (dbTypes.contains(toCheck)) + return true; + return false; + + } + + /** + * @return the dbTypes + */ + private Set getDbTypes() { + return dbTypes; + } + + public static int getCount(DatabaseType type) { + int nb = count.get(DatabaseType.ALL) + count.get(type); + return nb; + } + } + + /** + * @param type + * the type to set + */ + public void setType(DatabaseType type) throws IllegalArgumentException { + if (type == DatabaseType.ALL) + throw new IllegalArgumentException("You can't set your database type to ALL"); + this.type = type; + } + + /** + * @param log + * the log to set + */ + public void setLog(Logger log) { + this.log = log; + } + + /** + * @return the type + */ + public DatabaseType getType() { + return type; + } + + /** + * @return the log + */ + public Logger getLog() { + return log; + } + + public void setParameter(Parameter param, String value) throws NullPointerException, + InvalidConfiguration { + if (this.type == null) + throw new NullPointerException("You must set the type of the database first"); + if (!param.validParam(type)) + throw new InvalidConfiguration(param.toString() + + " is invalid for a database type of : " + type.toString()); + config.put(param, value); + + } + + public String getParameter(Parameter param) { + return config.get(param); + } + + public boolean isValid() throws InvalidConfiguration { + if (log == null) + throw new InvalidConfiguration("You need to set the logger."); + return config.size() == Parameter.getCount(type); + } +} diff --git a/lib/PatPeter/SQLibrary/DatabaseFactory.java b/lib/PatPeter/SQLibrary/DatabaseFactory.java new file mode 100644 index 0000000..f9963ac --- /dev/null +++ b/lib/PatPeter/SQLibrary/DatabaseFactory.java @@ -0,0 +1,46 @@ +/************************************************************************ + * This file is part of SQLibrary. + * + * SQLibrary is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * SQLibrary is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with SQLibrary. If not, see . + ************************************************************************/ +package lib.PatPeter.SQLibrary; + +import lib.PatPeter.SQLibrary.DatabaseConfig.Parameter; + +/** + * @author Balor (aka Antoine Aflalo) + * + */ +public class DatabaseFactory { + public static DatabaseHandler createDatabase(DatabaseConfig config) throws InvalidConfiguration { + if (!config.isValid()) + throw new InvalidConfiguration( + "The configuration is invalid, you don't have enought parameter for that DB : " + + config.getType()); + switch (config.getType()) { + case MYSQL: + return new MySQL(config.getLog(), config.getParameter(Parameter.DB_PREFIX), + config.getParameter(Parameter.HOSTNAME), + config.getParameter(Parameter.PORT_NUMBER), + config.getParameter(Parameter.DATABASE), config.getParameter(Parameter.USER), + config.getParameter(Parameter.PASSWORD)); + case SQLITE: + return new SQLite(config.getLog(), config.getParameter(Parameter.DB_PREFIX), + config.getParameter(Parameter.DB_NAME), + config.getParameter(Parameter.DB_LOCATION)); + default: + return null; + } + } +} diff --git a/lib/PatPeter/SQLibrary/InvalidConfiguration.java b/lib/PatPeter/SQLibrary/InvalidConfiguration.java new file mode 100644 index 0000000..bea973b --- /dev/null +++ b/lib/PatPeter/SQLibrary/InvalidConfiguration.java @@ -0,0 +1,74 @@ +/************************************************************************ + * This file is part of SQLibrary. + * + * SQLibrary is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * SQLibrary is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with SQLibrary. If not, see . + ************************************************************************/ +package lib.PatPeter.SQLibrary; + +/** + * @author Balor (aka Antoine Aflalo) + * + */ +public class InvalidConfiguration extends Exception { + + /** + * + */ + private static final long serialVersionUID = 7939781253235805155L; + + /** + * + */ + public InvalidConfiguration() { + // TODO Auto-generated constructor stub + } + + /** + * @param message + */ + public InvalidConfiguration(String message) { + super(message); + // TODO Auto-generated constructor stub + } + + /** + * @param cause + */ + public InvalidConfiguration(Throwable cause) { + super(cause); + // TODO Auto-generated constructor stub + } + + /** + * @param message + * @param cause + */ + public InvalidConfiguration(String message, Throwable cause) { + super(message, cause); + // TODO Auto-generated constructor stub + } + + /** + * @param message + * @param cause + * @param enableSuppression + * @param writableStackTrace + */ + public InvalidConfiguration(String message, Throwable cause, boolean enableSuppression, + boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + // TODO Auto-generated constructor stub + } + +} From 12049a0eefc71f191ed9afd49c88ef3701d3a41a Mon Sep 17 00:00:00 2001 From: Antoine Aflalo Date: Mon, 13 Feb 2012 11:14:58 +0100 Subject: [PATCH 2/4] Minor optimization to add the possbility to do : setParameter().setParameter() etc ... --- lib/PatPeter/SQLibrary/DatabaseConfig.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/PatPeter/SQLibrary/DatabaseConfig.java b/lib/PatPeter/SQLibrary/DatabaseConfig.java index 92844aa..80ed415 100644 --- a/lib/PatPeter/SQLibrary/DatabaseConfig.java +++ b/lib/PatPeter/SQLibrary/DatabaseConfig.java @@ -122,7 +122,7 @@ public Logger getLog() { return log; } - public void setParameter(Parameter param, String value) throws NullPointerException, + public DatabaseConfig setParameter(Parameter param, String value) throws NullPointerException, InvalidConfiguration { if (this.type == null) throw new NullPointerException("You must set the type of the database first"); @@ -130,6 +130,7 @@ public void setParameter(Parameter param, String value) throws NullPointerExcept throw new InvalidConfiguration(param.toString() + " is invalid for a database type of : " + type.toString()); config.put(param, value); + return this; } From a84e94cc197bd197c6c758e53443e14277c12c35 Mon Sep 17 00:00:00 2001 From: Antoine Aflalo Date: Mon, 13 Feb 2012 11:20:17 +0100 Subject: [PATCH 3/4] Bug Fix. --- lib/PatPeter/SQLibrary/DatabaseConfig.java | 24 ++++++++-------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/lib/PatPeter/SQLibrary/DatabaseConfig.java b/lib/PatPeter/SQLibrary/DatabaseConfig.java index 80ed415..68edf4a 100644 --- a/lib/PatPeter/SQLibrary/DatabaseConfig.java +++ b/lib/PatPeter/SQLibrary/DatabaseConfig.java @@ -50,21 +50,14 @@ public enum Parameter { private final static Map count = new EnumMap( DatabaseType.class); - static { - for (Parameter param : values()) - for (DatabaseType type : param.getDbTypes()) { - Integer nb = count.get(type); - nb++; - count.put(type, nb); - } - } - /** * */ private Parameter(DatabaseType... type) { - for (int i = 0; i < type.length; i++) + for (int i = 0; i < type.length; i++) { dbTypes.add(type[i]); + updateCount(type[i]); + } } @@ -77,13 +70,12 @@ public boolean validParam(DatabaseType toCheck) { } - /** - * @return the dbTypes - */ - private Set getDbTypes() { - return dbTypes; + private void updateCount(DatabaseType type) { + Integer nb = count.get(type); + nb++; + count.put(type, nb); } - + public static int getCount(DatabaseType type) { int nb = count.get(DatabaseType.ALL) + count.get(type); return nb; From ed9b57e7c92ca4d409259281441684d01823003e Mon Sep 17 00:00:00 2001 From: Antoine Aflalo Date: Mon, 13 Feb 2012 11:23:55 +0100 Subject: [PATCH 4/4] Forgot to initialize. --- lib/PatPeter/SQLibrary/DatabaseConfig.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/PatPeter/SQLibrary/DatabaseConfig.java b/lib/PatPeter/SQLibrary/DatabaseConfig.java index 68edf4a..831a894 100644 --- a/lib/PatPeter/SQLibrary/DatabaseConfig.java +++ b/lib/PatPeter/SQLibrary/DatabaseConfig.java @@ -47,8 +47,7 @@ public enum Parameter { DB_LOCATION(DatabaseType.SQLITE), DB_NAME(DatabaseType.SQLITE); private Set dbTypes = new HashSet(); - private final static Map count = new EnumMap( - DatabaseType.class); + private static Map count; /** * @@ -70,12 +69,17 @@ public boolean validParam(DatabaseType toCheck) { } - private void updateCount(DatabaseType type) { + private static void updateCount(DatabaseType type) { + if (count == null) + count = new EnumMap(DatabaseType.class); Integer nb = count.get(type); - nb++; + if (nb == null) + nb = 1; + else + nb++; count.put(type, nb); } - + public static int getCount(DatabaseType type) { int nb = count.get(DatabaseType.ALL) + count.get(type); return nb;