]> git.mxchange.org Git - jcore.git/commitdiff
A lot changes on jcore:
authorRoland Haeder <roland@mxchange.org>
Thu, 13 Aug 2015 10:07:33 +0000 (12:07 +0200)
committerRoland Haeder <roland@mxchange.org>
Thu, 13 Aug 2015 13:33:53 +0000 (15:33 +0200)
- Implemented doSelectByCriteria() in MySQL backend
- Close only connection in same backend if it is still open
- Added method size() to Result/DatabaseResult
- Moved member entrySet() in BaseCriteria to proper location
- Fixed comments of interface Criteria and SearchableCriteria
- Some trace/debug messages added
- Now database frontends have a new method getResultFromSet() which transforms a ResultSet (SQL) to a Result (jcore)
- Sorted class/interface imports
Signed-off-by:Roland Häder <roland@mxchange.org>

src/org/mxchange/jcore/BaseFrameworkSystem.java
src/org/mxchange/jcore/criteria/BaseCriteria.java
src/org/mxchange/jcore/criteria/Criteria.java
src/org/mxchange/jcore/criteria/searchable/SearchCriteria.java
src/org/mxchange/jcore/criteria/searchable/SearchableCritera.java
src/org/mxchange/jcore/database/backend/DatabaseBackend.java
src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java
src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java
src/org/mxchange/jcore/database/frontend/DatabaseFrontend.java
src/org/mxchange/jcore/database/result/DatabaseResult.java
src/org/mxchange/jcore/database/result/Result.java

index 88f598a58bfdf9e0ce1850c88d7d8730daf56ead..515981d72ae2a63dd8725ecbc7d49ee48e1f81f9 100644 (file)
@@ -735,6 +735,9 @@ public class BaseFrameworkSystem implements FrameworkInterface {
         * Initializes i18n bundles
         */
        protected void initBundle () {
+               // Trace message
+               this.getLogger().trace("CALLED!");
+
                // Is the bundle set?
                if (bundle instanceof ResourceBundle) {
                        // Is already set
@@ -743,6 +746,9 @@ public class BaseFrameworkSystem implements FrameworkInterface {
 
                // Set instance
                bundle = ResourceBundle.getBundle(FrameworkInterface.I18N_BUNDLE_FILE); // NOI18N
+
+               // Trace message
+               this.getLogger().trace("EXIT!");
        }
 
        /**
index 395fabae61030e6529559f6dc70ddb585d85f48d..c95311b380d3edf1e09c807ce3b63a0f36a8236e 100644 (file)
@@ -47,24 +47,24 @@ public class BaseCriteria extends BaseFrameworkSystem implements Criteria {
        }
 
        /**
-        * Gets all values from underlaying map in an iterator.
+        * Gets all entries as a key-value pair
         *
-        * @return Values iteratable
+        * @return Key-value paira of all entries
         */
        @Override
-       public Iterable<Object> values () {
-               // Call map's method
-               return this.criteria.values();
-               
+       public Set<Map.Entry<String, Object>> entrySet () {
+               return this.criteria.entrySet();
        }
 
        /**
-        * Gets all entries as a key-value pair
+        * Gets all values from underlaying map in an iterator.
         *
-        * @return Key-value paira of all entries
+        * @return Values iteratable
         */
        @Override
-       public Set<Map.Entry<String, Object>> entrySet () {
-               return this.criteria.entrySet();
+       public Iterable<Object> values () {
+               // Call map's method
+               return this.criteria.values();
+               
        }
 }
index 0016a5057858e8eb00c3199607175d1c2564619b..74a36b66ddd195ac70a7c8857472e9c1149e9a99 100644 (file)
@@ -1,5 +1,3 @@
-package org.mxchange.jcore.criteria;
-
 /*
  * Copyright (C) 2015 Roland Haeder
  *
@@ -16,7 +14,7 @@ package org.mxchange.jcore.criteria;
  * 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.criteria;
 
 import java.util.Map;
 import java.util.Set;
index 96a637fa3d323cad6f97bb30a4c0da7a885c320a..ee26956700659d93b47d54fe547da3058614bd66 100644 (file)
@@ -34,7 +34,7 @@ public class SearchCriteria extends BaseCriteria implements SearchableCritera {
        }
 
        @Override
-       public boolean matchesAnd (final Storeable storeable) {
+       public boolean matches (final Storeable storeable) {
                // Trace message
                this.getLogger().trace(MessageFormat.format("storeable={0} - CALLED!", storeable));
 
index 2447e2b27e5ae88dbaa8c210610c5bad554e1b49..bc8992e5927b636c9bfd5281520a360c30da8c68 100644 (file)
@@ -1,8 +1,3 @@
-package org.mxchange.jcore.criteria.searchable;
-
-import org.mxchange.jcore.criteria.Criteria;
-import org.mxchange.jcore.database.storage.Storeable;
-
 /*
  * Copyright (C) 2015 Roland Haeder
  *
@@ -19,7 +14,10 @@ import org.mxchange.jcore.database.storage.Storeable;
  * 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.criteria.searchable;
 
+import org.mxchange.jcore.criteria.Criteria;
+import org.mxchange.jcore.database.storage.Storeable;
 
 /**
  * A searchable criteria
@@ -29,10 +27,10 @@ import org.mxchange.jcore.database.storage.Storeable;
 public interface SearchableCritera extends Criteria {
 
        /**
-        * Checks if the given instance of a Storeable class matchesAnd all criteria
+        * Checks if the given instance of a Storeable class matches
         *
         * @param storeable A Storeable instance to check
-        * @return Whether the Storeable instance matchesAnd all criteria
+        * @return Whether the Storeable instance matches
         */
-       public boolean matchesAnd (final Storeable storeable);
+       public boolean matches (final Storeable storeable);
 }
index 0dd7dac728e13fe1a1d46b9fc482683ccaa94214..1391a679d05f0e3e24b45b050c87d28326c58e25 100644 (file)
@@ -47,8 +47,9 @@ public interface DatabaseBackend extends FrameworkInterface {
         * @return A result instance
         * @throws java.io.IOException If any IO error occurs
         * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found
+        * @throws java.sql.SQLException If any SQL error occurs
         */
-       public Result<? extends Storeable> doSelectByCriteria (final SearchableCritera critera) throws IOException, BadTokenException;
+       public Result<? extends Storeable> doSelectByCriteria (final SearchableCritera critera) throws IOException, BadTokenException, SQLException;
 
        /**
         * Shuts down this backend
index 1d3bdc72ce505f877f38ec8e81ecce9cc671d470..0913d9ce7beadd7405ba541b770c7d8c1f0593ce 100644 (file)
@@ -128,8 +128,8 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat
                        // Debug message
                        this.getLogger().debug(MessageFormat.format("storeable={0}", storeable));
 
-                       // Now matchesAnd the found instance
-                       if (critera.matchesAnd(storeable)) {
+                       // Now matches the found instance
+                       if (critera.matches(storeable)) {
                                // Then add it to result
                                result.add(storeable);
                        }
index c87f46f21846c1d465d7d3080f6b4fe7e6b71c4b..7f190126c51638ed8b21b5c5afbe7b06df7efbc1 100644 (file)
@@ -19,9 +19,13 @@ package org.mxchange.jcore.database.backend.mysql;
 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.sql.Statement;
 import java.text.MessageFormat;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
 import org.mxchange.jcore.criteria.searchable.SearchableCritera;
 import org.mxchange.jcore.database.backend.BaseDatabaseBackend;
 import org.mxchange.jcore.database.backend.DatabaseBackend;
@@ -41,11 +45,6 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas
         */
        private static Connection connection;
 
-       /**
-        * Prepared statement for full row count
-        */
-       private PreparedStatement totalRowCount;
-
        /**
         * Constructor with table name
         * 
@@ -54,7 +53,7 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas
         */
        public MySqlDatabaseBackend (final DatabaseFrontend frontend) throws UnsupportedDatabaseDriverException {
                // Trace message
-               this.getLogger().trace(MessageFormat.format("frontend={0} - CALLED!", frontend));
+               this.getLogger().trace(MessageFormat.format("frontend={0} - CALLED!", frontend)); //NOI18N
 
                // Validate driver
                this.validateDriver("mysql"); //NOI18N
@@ -63,10 +62,13 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas
                String tableName = frontend.getTableName();
 
                // Debug message
-               this.getLogger().debug(MessageFormat.format("tableName={0}", tableName));
+               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
@@ -75,8 +77,14 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas
                this.getLogger().trace("CALLED!"); //NOI18N
 
                // Is the connection already there?
-               if (MySqlDatabaseBackend.connection instanceof Connection) {
-                       // Already connected
+               if (connection instanceof Connection) {
+                       // Is the connection really up?
+                       if (connection.isClosed()) {
+                               // Connection is closed again
+                               throw new SQLException("Connection is closed."); //NOI18N
+                       }
+
+               // Already connected
                        this.getLogger().debug("Connection is already established."); //NOI18N
 
                        // No need to connect
@@ -93,39 +101,94 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas
                this.getLogger().debug(MessageFormat.format("Attempting to connect to {0} ...", connect)); //NOI18N
 
                // Now get a connection instance back
-               MySqlDatabaseBackend.connection = DriverManager.getConnection(
+               connection = DriverManager.getConnection(
                                connect,
                                this.getProperty("database.mysql.login"), //NOI18N
                                this.getProperty("database.mysql.password") //NOI18N
                );
 
-               // Is the connection really up?
-               if (MySqlDatabaseBackend.connection.isClosed()) {
-                       // Connection is closed again
-                       throw new SQLException("Connection is closed."); //NOI18N
-               }
-
                // Debug message
                this.getLogger().debug("Connection is up, preparing some statements ..."); //NOI18N
 
-               // Here, the connection is established, so prepare some statements
-               this.totalRowCount = MySqlDatabaseBackend.connection.prepareStatement(String.format("SELECT COUNT(`id`) AS `cnt` FROM `%s` LIMIT 1", this.getTableName())); //NOI18N
-
                // Trace message
                this.getLogger().trace("EXIT!"); //NOI18N
        }
 
        @Override
-       public Result<? extends Storeable> doSelectByCriteria (final SearchableCritera critera) {
-               throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: criteria={0}", critera));
+       public Result<? extends Storeable> doSelectByCriteria (final SearchableCritera critera) throws SQLException {
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("criteria={0} - CALLED!", critera));
+
+               // Start SELECT query
+               StringBuilder query = new StringBuilder(String.format("SELECT * FROM `%s`", this.getTableName()));
+
+               // Get entry set
+               Set<Map.Entry<String, Object>> set = critera.entrySet();
+
+               // Debug message
+               this.getLogger().debug(MessageFormat.format("set.isEmpty()={0}", set.isEmpty()));
+
+               // Are there conditions?
+               if (!set.isEmpty()) {
+                       // Continue with WHERE
+                       query.append(" WHERE ");
+
+                       // Get iterator
+                       Iterator<Map.Entry<String, Object>> iterator = set.iterator();
+
+                       // "Walk through all
+                       while (iterator.hasNext()) {
+                               // Get element
+                               Map.Entry<String, Object> entry = iterator.next();
+
+                               // Add key as column name
+                               query.append(String.format("`%s`", entry.getKey()));
+
+                               // Get value
+                               Object value = entry.getValue();
+
+                               // Debug message
+                               this.getLogger().debug(MessageFormat.format("value={0}", value));
+
+                               // Which type has the value?
+                               if (value instanceof Boolean) {
+                                       // Boolean value
+                                       query.append(String.format("=%s", value.toString()));
+                               } else {
+                                       // Cannot handle this
+                                       throw new SQLException(MessageFormat.format("Cannot handle value={0} for key={1} in table {2}", value, entry.getKey(), this.getTableName()));
+                               }
+                       }
+               }
+
+               // Full statement is complete here, better log it
+               this.getLogger().debug(MessageFormat.format("query={0} is complete.", query));
+
+               // Prepare statement instance
+               Statement statement = connection.createStatement();
+
+               // Run it
+               ResultSet resultSet = statement.executeQuery(query.toString());
+
+               // The result set needs to be transformed into Result, so initialize a result instance here
+               Result<? extends Storeable> result = this.getFrontend().getResultFromSet(resultSet);
+
+               // Return it
+               return result;
        }
 
        @Override
        public void doShutdown () throws SQLException, IOException {
-               // This should not happen:
-               assert(MySqlDatabaseBackend.connection instanceof Connection) : MessageFormat.format("connection is not valid: {0}", MySqlDatabaseBackend.connection); //NOI18N
+               // Trace message
+               this.getLogger().trace("CALLED!"); //NOI18N
+
+               // Is the connection still there?
+               if (!connection.isClosed()) {
+                       // Close down database connection
+                       connection.close();
+               }
 
-               // Close down database connection
-               MySqlDatabaseBackend.connection.close();
+               // Trace message
+               this.getLogger().trace("EXIT!"); //NOI18N
        }
 }
index 2d2e645831767bb563771145324ba126f38f8cef..528621596b26d535bef519bbddb544d5249a2ef8 100644 (file)
 package org.mxchange.jcore.database.frontend;
 
 import java.io.IOException;
+import java.sql.ResultSet;
 import java.sql.SQLException;
 import org.mxchange.jcore.FrameworkInterface;
+import org.mxchange.jcore.database.result.Result;
 import org.mxchange.jcore.database.storage.Storeable;
 import org.mxchange.jcore.exceptions.BadTokenException;
 
@@ -28,6 +30,15 @@ import org.mxchange.jcore.exceptions.BadTokenException;
  * @author Roland Haeder
  */
 public interface DatabaseFrontend extends FrameworkInterface {
+       /**
+        * Gets a Result back from given ResultSet instance
+        *
+        * @param resultSet ResultSet instance from SQL driver
+        * @return A typorized Result instance
+        * @throws java.sql.SQLException If an SQL error occurs
+        */
+       public Result<? extends Storeable> getResultFromSet (final ResultSet resultSet) throws SQLException;
+
        /**
         * Parses given line from database backend into a Storeable instance. Please
         * note that not all backends need this.
index abddc78ff7e672d32e30008ead62475c97df6cea..c8dca70e474a939e6229bcac66579151d2ca0a15 100644 (file)
@@ -36,6 +36,9 @@ public class DatabaseResult extends BaseFrameworkSystem implements Result<Storea
         * Default constructor
         */
        public DatabaseResult () {
+               // Trace message
+               this.getLogger().trace("CALLED!");
+
                // Init set here
                this.result = new TreeSet<>();
        }
@@ -51,6 +54,11 @@ public class DatabaseResult extends BaseFrameworkSystem implements Result<Storea
                this.result.add(storeable);
        }
 
+       @Override
+       public int size () {
+               return this.result.size();
+       }
+
        @Override
        public boolean hasNext () {
                // Call iterator's method
index 0e5d9dae8221c0c4e7fac1844f22eb018ea64ff2..96056ccda0e1d51e63fdf581e24c7198306e969d 100644 (file)
@@ -34,4 +34,11 @@ public interface Result<T extends Storeable> extends FrameworkInterface, Iterato
         * @param storeable An instance of a Storeable class
         */
        public void add (final Storeable storeable);
+
+       /**
+        * Returns size of result
+        *
+        * @return Size of result
+        */
+       public int size ();
 }