From 51a31030c157c2c5dfc4995b624a77e2ac73ff50 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Thu, 13 Aug 2015 14:22:51 +0200 Subject: [PATCH] Some additions to jcore: - renamed isValueEqual() to isFieldValueEqual() as this method does check a class field (aka. attribute) - now search criteria can be limit and skip found matches - added support for string criteria MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by:Roland Häder --- .../mxchange/jcore/BaseFrameworkSystem.java | 2 +- .../mxchange/jcore/FrameworkInterface.java | 2 +- .../mxchange/jcore/contact/BaseContact.java | 2 +- .../mxchange/jcore/criteria/BaseCriteria.java | 6 ++++ src/org/mxchange/jcore/criteria/Criteria.java | 10 ++++++- .../criteria/searchable/SearchCriteria.java | 30 +++++++++++++++++++ .../searchable/SearchableCritera.java | 29 +++++++++++++++++- .../backend/mysql/MySqlDatabaseBackend.java | 12 ++++++++ 8 files changed, 88 insertions(+), 5 deletions(-) diff --git a/src/org/mxchange/jcore/BaseFrameworkSystem.java b/src/org/mxchange/jcore/BaseFrameworkSystem.java index 515981d..8bbf1ea 100644 --- a/src/org/mxchange/jcore/BaseFrameworkSystem.java +++ b/src/org/mxchange/jcore/BaseFrameworkSystem.java @@ -327,7 +327,7 @@ public class BaseFrameworkSystem implements FrameworkInterface { * @throws java.lang.reflect.InvocationTargetException Any other problems? */ @Override - public boolean isValueEqual (final String columnName, final boolean bool) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + public boolean isFieldValueEqual (final String columnName, final boolean bool) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { // Not implemented throw new UnsupportedOperationException(MessageFormat.format("Not implemented. columnName={0},bool={1}", columnName, bool)); //NOI18N } diff --git a/src/org/mxchange/jcore/FrameworkInterface.java b/src/org/mxchange/jcore/FrameworkInterface.java index 0e44d7e..28bcfe1 100644 --- a/src/org/mxchange/jcore/FrameworkInterface.java +++ b/src/org/mxchange/jcore/FrameworkInterface.java @@ -91,7 +91,7 @@ public interface FrameworkInterface { * @throws java.lang.IllegalAccessException If the method cannot be accessed * @throws java.lang.reflect.InvocationTargetException Any other problems? */ - public boolean isValueEqual (final String columnName, final boolean bool) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException; + public boolean isFieldValueEqual (final String columnName, final boolean bool) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException; /** * Some "getter for a value from given column name. This name will be diff --git a/src/org/mxchange/jcore/contact/BaseContact.java b/src/org/mxchange/jcore/contact/BaseContact.java index e00f463..ae2b1fc 100644 --- a/src/org/mxchange/jcore/contact/BaseContact.java +++ b/src/org/mxchange/jcore/contact/BaseContact.java @@ -571,7 +571,7 @@ public class BaseContact extends BaseFrameworkSystem implements Contact { * @return Whether all conditions are met */ @Override - public boolean isValueEqual (final String columnName, final boolean bool) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + public boolean isFieldValueEqual (final String columnName, final boolean bool) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { // Trace message this.getLogger().trace(MessageFormat.format("columnName={0},bool={1} - CALLED!", columnName, bool)); diff --git a/src/org/mxchange/jcore/criteria/BaseCriteria.java b/src/org/mxchange/jcore/criteria/BaseCriteria.java index c95311b..bb3550d 100644 --- a/src/org/mxchange/jcore/criteria/BaseCriteria.java +++ b/src/org/mxchange/jcore/criteria/BaseCriteria.java @@ -46,6 +46,12 @@ public class BaseCriteria extends BaseFrameworkSystem implements Criteria { this.criteria.put(key, value); } + @Override + public void addCriteria (final String key, final String value) { + // Add to map + this.criteria.put(key, value); + } + /** * Gets all entries as a key-value pair * diff --git a/src/org/mxchange/jcore/criteria/Criteria.java b/src/org/mxchange/jcore/criteria/Criteria.java index 74a36b6..bddd6d0 100644 --- a/src/org/mxchange/jcore/criteria/Criteria.java +++ b/src/org/mxchange/jcore/criteria/Criteria.java @@ -27,13 +27,21 @@ import org.mxchange.jcore.FrameworkInterface; */ public interface Criteria extends FrameworkInterface { /** - * Adds a binary criteria + * Adds a boolean criteria * * @param key Key of criteria * @param value Value of criteria */ public void addCriteria (final String key, final boolean value); + /** + * Adds a string criteria + * + * @param key Key of criteria + * @param value Value of criteria + */ + public void addCriteria (final String key, final String value); + /** * Gets all values from underlaying map in an iterator. * diff --git a/src/org/mxchange/jcore/criteria/searchable/SearchCriteria.java b/src/org/mxchange/jcore/criteria/searchable/SearchCriteria.java index ee26956..fbd8894 100644 --- a/src/org/mxchange/jcore/criteria/searchable/SearchCriteria.java +++ b/src/org/mxchange/jcore/criteria/searchable/SearchCriteria.java @@ -27,12 +27,42 @@ import org.mxchange.jcore.database.storage.Storeable; * @author Roland Haeder */ public class SearchCriteria extends BaseCriteria implements SearchableCritera { + /** + * Limit of matches + */ + private int limit; + + /** + * Skipping of matches + */ + private int skip; + /** * Default constructor */ public SearchCriteria () { } + @Override + public int getLimit () { + return this.limit; + } + + @Override + public final void setLimit (final int limit) { + this.limit = limit; + } + + @Override + public int getSkip () { + return this.skip; + } + + @Override + public final void setSkip (final int skip) { + this.skip = skip; + } + @Override public boolean matches (final Storeable storeable) { // Trace message diff --git a/src/org/mxchange/jcore/criteria/searchable/SearchableCritera.java b/src/org/mxchange/jcore/criteria/searchable/SearchableCritera.java index bc8992e..8455ba5 100644 --- a/src/org/mxchange/jcore/criteria/searchable/SearchableCritera.java +++ b/src/org/mxchange/jcore/criteria/searchable/SearchableCritera.java @@ -25,7 +25,6 @@ import org.mxchange.jcore.database.storage.Storeable; * @author Roland Haeder */ public interface SearchableCritera extends Criteria { - /** * Checks if the given instance of a Storeable class matches * @@ -33,4 +32,32 @@ public interface SearchableCritera extends Criteria { * @return Whether the Storeable instance matches */ public boolean matches (final Storeable storeable); + + /** + * Setter for limit of possible matches + * + * @param limit Limit of matches + */ + public void setLimit (final int limit); + + /** + * Getter for limit of possible matches + * + * @return Limit of matches + */ + public int getLimit (); + + /** + * Setter for skipping of possible matches + * + * @param skip Skipping of matches + */ + public void setSkip (final int skip); + + /** + * Getter for skipping of possible matches + * + * @return Skipping of matches + */ + public int getSkip (); } diff --git a/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java b/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java index 7f19012..2d71d90 100644 --- a/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java +++ b/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java @@ -161,6 +161,18 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas } } + // Is limit set? + if (critera.getLimit() > 0) { + // Is skip set? + if (critera.getSkip() > 0) { + // Limit with skip + query.append(String.format(" LIMIT %d,%d", critera.getSkip(), critera.getLimit())); + } else { + // Only limit + query.append(String.format(" LIMIT %d", critera.getLimit())); + } + } + // Full statement is complete here, better log it this.getLogger().debug(MessageFormat.format("query={0} is complete.", query)); -- 2.39.5