--- /dev/null
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+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<? extends Storeable> doInsertDataSet (final Map<String, Object> 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<? extends Storeable> result = new DatabaseResult();
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("result={0} - EXIT!", result)); //NOI18N
+
+ // Return it
+ return result;
+ }
+
+ @Override
+ public Result<? extends Storeable> 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<? extends Storeable> 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;
+ }
+}