From 7b0ba7cc35d550930815eb976ac0064e1fe021a7 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Thu, 20 Aug 2015 17:18:28 +0200 Subject: [PATCH] Newly created data source backend added. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../datasource/DataSourceDatabaseBackend.java | 154 ++++++++++++++++++ .../backend/mysql/MySqlDatabaseBackend.java | 16 +- 2 files changed, 165 insertions(+), 5 deletions(-) create mode 100644 src/org/mxchange/jcore/database/backend/datasource/DataSourceDatabaseBackend.java diff --git a/src/org/mxchange/jcore/database/backend/datasource/DataSourceDatabaseBackend.java b/src/org/mxchange/jcore/database/backend/datasource/DataSourceDatabaseBackend.java new file mode 100644 index 0000000..4600b74 --- /dev/null +++ b/src/org/mxchange/jcore/database/backend/datasource/DataSourceDatabaseBackend.java @@ -0,0 +1,154 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program 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. + * + * This program 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 this program. If not, see . + */ +package org.mxchange.jcore.database.backend.datasource; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.text.MessageFormat; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; +import org.mxchange.jcore.criteria.searchable.SearchableCriteria; +import org.mxchange.jcore.database.backend.BaseDatabaseBackend; +import org.mxchange.jcore.database.backend.DatabaseBackend; +import org.mxchange.jcore.database.frontend.DatabaseFrontend; +import org.mxchange.jcore.database.result.DatabaseResult; +import org.mxchange.jcore.database.result.Result; +import org.mxchange.jcore.database.storage.Storeable; +import org.mxchange.jcore.exceptions.UnsupportedDatabaseDriverException; + +/** + * A backend class for MySQL connections + * + * @author Roland Haeder + */ +public class DataSourceDatabaseBackend extends BaseDatabaseBackend implements DatabaseBackend { + /** + * Constructor with table name + * + * @param frontend An instance of the frontend + * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseDriverException If the requested driver is not supported + */ + public DataSourceDatabaseBackend (final DatabaseFrontend frontend) throws UnsupportedDatabaseDriverException { + // Trace message + this.getLogger().trace(MessageFormat.format("frontend={0} - CALLED!", frontend)); //NOI18N + + // Get table name + String tableName = frontend.getTableName(); + + // Debug message + this.getLogger().debug(MessageFormat.format("tableName={0}", tableName)); //NOI18N + + // Now that the driver is there, set the table name + this.setTableName(tableName); + + // Set frontend + this.setFrontend(frontend); + } + + @Override + public void connectToDatabase () throws SQLException { + // Trace message + this.getLogger().trace("CALLED!"); //NOI18N + + // Trace message + this.getLogger().trace("EXIT!"); //NOI18N + } + + /** + * Inserts given dataset instance and returns a Result instance on success. + * Please note that this method can insert only a single record into + * database. Multiple inserts are not yet supported. + * + * @param dataset A dataset instance + * @return An instance of Result + * @todo Support more than one record being inserted in a separate method + */ + @Override + public Result doInsertDataSet (final Map dataset) throws SQLException { + // Trace message + this.getLogger().trace(MessageFormat.format("dataset={0} - CALLED!", dataset)); //NOI18N + + // dataset should not be null and not empty + if (dataset == null) { + // It is null, so abort here + throw new NullPointerException("dataset is null"); //NOI18N + } else if (dataset.isEmpty()) { + // It is empty, also abort here + throw new IllegalArgumentException("dataset is empty"); //NOI18N + } + + // Init result + Result result = new DatabaseResult(); + + // Trace message + this.getLogger().trace(MessageFormat.format("result={0} - EXIT!", result)); //NOI18N + + // Return it + return result; + } + + @Override + public Result doSelectByCriteria (final SearchableCriteria critera) throws SQLException { + // Trace message + this.getLogger().trace(MessageFormat.format("criteria={0} - CALLED!", critera)); //NOI18N + + // criteria must not be null + if (critera == null) { + // Abort here + throw new NullPointerException("criteria is null"); //NOI18N + } + + // Init result + Result result = new DatabaseResult(); + + // Trace message + this.getLogger().trace(MessageFormat.format("result={0} - EXIT!", result)); //NOI18N + + // Return it + return result; + } + + @Override + public void doShutdown () throws SQLException, IOException { + // Trace message + this.getLogger().trace("CALLED!"); //NOI18N + + // Trace message + this.getLogger().trace("EXIT!"); //NOI18N + } + + @Override + public Long getTotalRows () throws IOException, SQLException { + // Trace message + this.getLogger().trace("CALLED!"); //NOI18N + + // Get long + Long count = null; + + // Trace message + this.getLogger().trace(MessageFormat.format("count={0} - EXIT!", count)); //NOI18N + + // Return it + return count; + } +} diff --git a/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java b/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java index 81f8c95..2be82dd 100644 --- a/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java +++ b/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java @@ -226,6 +226,12 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas // Trace message this.getLogger().trace(MessageFormat.format("criteria={0} - CALLED!", critera)); //NOI18N + // criteria must not be null + if (critera == null) { + // Abort here + throw new NullPointerException("criteria is null"); //NOI18N + } + // Start SELECT query StringBuilder query = new StringBuilder(String.format("SELECT * FROM `%s`", this.getTableName())); //NOI18N @@ -328,19 +334,19 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas public Long getTotalRows () throws IOException, SQLException { // Trace message this.getLogger().trace("CALLED!"); //NOI18N - + // Execute query ResultSet set = this.totalRows.executeQuery(); - + // Rewind to beginning set.beforeFirst(); - + // Get long Long count = set.getLong("cnt"); //NOI18N - + // Trace message this.getLogger().trace(MessageFormat.format("count={0} - EXIT!", count)); //NOI18N - + // Return it return count; } -- 2.39.5