+package de.laktatnebel.libs.dblib;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+import de.laktatnebel.libs.dblib.driver.DBDriver;
+import de.laktatnebel.libs.dblib.exception.DBManagerException;
+import de.laktatnebel.libs.dblib.exception.DBManagerTechnicalException;
+import de.laktatnebel.libs.validatorlib.SQLValidator;
+
+public class DBManager {
+
+ /**
+ *
+ */
+ private Connection connection = null;
+
+ /**
+ *
+ */
+ private String host = null;
+
+ /**
+ *
+ */
+ private String user = null;
+
+ /**
+ *
+ */
+ private String pass = null;
+
+ /**
+ *
+ */
+ private DBDriver driver = null;
+
+ /**
+ *
+ */
+ private String database = null;
+
+ public DBManager() {
+ super();
+ }
+
+ /**
+ * @param connection
+ * @param host
+ * @param user
+ * @param pass
+ * @param driver
+ * @param database
+ */
+ public DBManager(final String host, final String user, final String pass, final DBDriver driver, final String database) {
+ super();
+ this.host = host;
+ this.user = user;
+ this.pass = pass;
+ this.driver = driver;
+ this.database = database;
+ }
+
+ /**
+ * @throws DBManagerException
+ * @throws DBManagerTechnicalException
+ *
+ */
+ public void connect() throws DBManagerException, DBManagerTechnicalException {
+ try {
+ final String driverName = this.driver.getDriverName();
+ Class.forName(driverName);
+
+ final StringBuffer connetionUrl = new StringBuffer();
+ connetionUrl.append(this.driver.getDriverUrl());
+ connetionUrl.append(getHost());
+ connetionUrl.append("/");
+ connetionUrl.append(getDatabase());
+
+ this.connection = DriverManager.getConnection(connetionUrl.toString(), this.user, this.pass);
+ } catch (final ClassNotFoundException cnfe) {
+ throw new DBManagerTechnicalException(cnfe.getMessage(), cnfe);
+ } catch (final SQLException sqle) {
+ throw new DBManagerException(sqle.getMessage(), sqle);
+ }
+ }
+
+ /**
+ * @throws DBManagerTechnicalException
+ *
+ */
+ public void closeConnection() throws DBManagerTechnicalException {
+ try {
+ this.connection.close();
+ } catch (final SQLException sqle) {
+ throw new DBManagerTechnicalException(sqle.getMessage(), sqle);
+ }
+ }
+
+ /**
+ * @return string Host
+ */
+ private String getHost() {
+ return this.host;
+ }
+
+ /**
+ * @return String database name
+ */
+ private String getDatabase() {
+ return this.database;
+ }
+
+ /**
+ * @return boolean isConnected
+ */
+ public boolean isConnected() {
+ if (this.connection == null) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * @param schema
+ * Schema
+ * @param table
+ * Tabelle
+ * @param where
+ * Where-Clause
+ * @param orderBy
+ * OrderBy-Clause
+ * @return List<String> Ergebnisse
+ * @throws DBManagerException
+ * @throws DBManagerTechnicalException
+ */
+ public static String selectWhereOrderBy(final String schema, final String table, final String where, final String orderBy) {
+ final StringBuilder sql = new StringBuilder("SELECT * FROM ");
+ sql.append(schema);
+ sql.append(".");
+ sql.append(table);
+ if (SQLValidator.isValidWhere(where)) {
+ sql.append(" WHERE ");
+ sql.append(where);
+ }
+ if (SQLValidator.isValidOrderBy(orderBy)) {
+ sql.append(" ORDER BY ");
+ sql.append(orderBy);
+ }
+
+ return sql.toString();
+ }
+
+ /**
+ * @param schema
+ * Schema
+ * @param table
+ * Tabelle
+ * @param where
+ * Where-Clause
+ * @param orderBy
+ * OrderBy-Clause
+ * @return List<String> Ergebnisse
+ * @throws DBManagerException
+ * @throws DBManagerTechnicalException
+ */
+ public static String selectColWhereOrderBy(final String schema, final String table, final String columns, final String where, final String orderBy) {
+ final StringBuilder sql = new StringBuilder("SELECT ");
+ sql.append(columns);
+ sql.append(" FROM ");
+ sql.append(schema);
+ sql.append(".");
+ sql.append(table);
+ if (SQLValidator.isValidWhere(where)) {
+ sql.append(" WHERE ");
+ sql.append(where);
+ }
+ if (SQLValidator.isValidOrderBy(orderBy)) {
+ sql.append(" ORDER BY ");
+ sql.append(orderBy);
+ }
+
+ return sql.toString();
+ }
+
+ public Connection getConnection() {
+ return this.connection;
+ }
+
+}