]> git.mxchange.org Git - jcore.git/commitdiff
Newly created data source backend added.
authorRoland Haeder <roland@mxchange.org>
Thu, 20 Aug 2015 15:18:28 +0000 (17:18 +0200)
committerRoland Haeder <roland@mxchange.org>
Thu, 20 Aug 2015 15:18:28 +0000 (17:18 +0200)
Signed-off-by: Roland Häder <roland@mxchange.org>
src/org/mxchange/jcore/database/backend/datasource/DataSourceDatabaseBackend.java [new file with mode: 0644]
src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.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 (file)
index 0000000..4600b74
--- /dev/null
@@ -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 <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;
+       }
+}
index 81f8c9577033d09f89ece9690c7635e77315d04b..2be82ddd6777628c1ff2f1bbddc8a53246a2de3d 100644 (file)
@@ -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;
        }