From 1df9464bed5a2f85af16170d8d2955b298dc6ea1 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Thu, 27 Aug 2015 23:26:47 +0200 Subject: [PATCH] =?utf8?q?Continued:=20-=20Added=20method=20numRows()=20(a?= =?utf8?q?nd=20implemented=20it=20generically)=20-=20made=20doSelectByCrir?= =?utf8?q?tia()=20in=20BaseDatabaseBackend=20abstract=20Signed-off-by:Rola?= =?utf8?q?nd=20H=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../database/backend/BaseDatabaseBackend.java | 48 ++++++++++++- .../database/backend/DatabaseBackend.java | 15 ++++ .../base64/Base64CsvDatabaseBackend.java | 69 ++++++++++--------- .../datasource/DataSourceDatabaseBackend.java | 5 +- .../backend/mysql/MySqlDatabaseBackend.java | 5 +- 5 files changed, 105 insertions(+), 37 deletions(-) diff --git a/src/org/mxchange/jcore/database/backend/BaseDatabaseBackend.java b/src/org/mxchange/jcore/database/backend/BaseDatabaseBackend.java index 73df2bf..6a38586 100644 --- a/src/org/mxchange/jcore/database/backend/BaseDatabaseBackend.java +++ b/src/org/mxchange/jcore/database/backend/BaseDatabaseBackend.java @@ -16,11 +16,19 @@ */ package org.mxchange.jcore.database.backend; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.sql.Driver; import java.sql.DriverManager; +import java.sql.SQLException; import java.text.MessageFormat; import java.util.Enumeration; import org.mxchange.jcore.BaseFrameworkSystem; +import org.mxchange.jcore.criteria.searchable.SearchableCriteria; +import org.mxchange.jcore.database.result.Result; +import org.mxchange.jcore.database.storage.Storeable; +import org.mxchange.jcore.exceptions.BadTokenException; +import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException; import org.mxchange.jcore.exceptions.UnsupportedDatabaseDriverException; /** @@ -28,7 +36,7 @@ import org.mxchange.jcore.exceptions.UnsupportedDatabaseDriverException; * * @author Roland Haeder */ -public class BaseDatabaseBackend extends BaseFrameworkSystem { +public abstract class BaseDatabaseBackend extends BaseFrameworkSystem { /** * No instances from this class @@ -95,4 +103,42 @@ public class BaseDatabaseBackend extends BaseFrameworkSystem { // Trace message this.getLogger().trace("EXIT!"); //NOI18N } + + public int numRows (final SearchableCriteria criteria) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { + // Trace message + this.getLogger().trace("criteria=" + criteria + " - CALLED!"); + + // Criteria should not be null + if (null == criteria) { + // Abort here + throw new NullPointerException("criteria is null"); + } + + // Run "SELECT" + Result result = this.doSelectByCriteria(criteria); + + // Get size + int numRows = result.size(); + + // Trace message + this.getLogger().trace("numRows=" + numRows + " - EXIT!"); + + // Return it + return numRows; + } + + /** + * Runs a SELECT "query" on database backend + * + * @param criteria Criteria instance + * @return Result instance + * @throws java.io.IOException If any IO error occurs + * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found + * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the file is badly damaged + * @throws java.sql.SQLException If any SQL error occurs + * @throws java.lang.NoSuchMethodException If a method was not found + * @throws java.lang.IllegalAccessException If the method cannot be accessed + * @throws java.lang.reflect.InvocationTargetException Any other problems? + */ + public abstract Result doSelectByCriteria (SearchableCriteria criteria) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException; } diff --git a/src/org/mxchange/jcore/database/backend/DatabaseBackend.java b/src/org/mxchange/jcore/database/backend/DatabaseBackend.java index ef014e7..d6ce352 100644 --- a/src/org/mxchange/jcore/database/backend/DatabaseBackend.java +++ b/src/org/mxchange/jcore/database/backend/DatabaseBackend.java @@ -89,4 +89,19 @@ public interface DatabaseBackend extends FrameworkInterface { * @throws java.sql.SQLException If any SQL error occurs */ public int getTotalRows () throws IOException, SQLException; + + /** + * Number of total found rows. + * + * @param criteria Search criteria instance + * @return Number of found rows + * @throws java.io.IOException If any IO error occurs + * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found + * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the file is badly damaged + * @throws java.sql.SQLException If any SQL error occurs + * @throws java.lang.NoSuchMethodException If a method was not found + * @throws java.lang.IllegalAccessException If the method cannot be accessed + * @throws java.lang.reflect.InvocationTargetException Any other problems? + */ + public int numRows (final SearchableCriteria criteria) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException; } diff --git a/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java b/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java index f1186a8..99f2ba6 100644 --- a/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java +++ b/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java @@ -149,7 +149,7 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat } } - // Generate key=value pair + // Generate key=value pair String pair = String.format("key=%s,value=\"%s\";", entry.getKey(), String.valueOf(value)); //NOI18N // Debug message @@ -230,44 +230,47 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat public final int getTotalRows () throws IOException { // Trace message this.getLogger().trace("CALLED!"); //NOI18N - + // Init count int count = 0; - + // First rewind this.rewind(); - + // Walk through all rows while (!this.isEndOfFile()) { // Get next line String line = this.readLine(); - + // Debug message this.getLogger().debug(MessageFormat.format("line={0}", line)); //NOI18N - + // Count one up count++; } - + // Trace message this.getLogger().trace(MessageFormat.format("count={0} - EXIT!", count)); //NOI18N - + // Return it return count; } /** - * Tries to interpret the given decoded line and puts its key/value pairs into a map. + * Tries to interpret the given decoded line and puts its key/value pairs + * into a map. * * @param line Decoded line from database file * @return A Map with keys and values from line - * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the file is believed damaged - * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found + * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If + * the file is believed damaged + * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token + * was found */ private Map getMapFromLine (final String line) throws CorruptedDatabaseFileException, BadTokenException { // Trace message this.getLogger().debug(MessageFormat.format("line={0} - CALLED!", line)); //NOI18N - + // "line" must not be null or empty if (null == line) { // Is null @@ -285,55 +288,55 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat // Bad line found throw new CorruptedDatabaseFileException(this.fileName, "No \"value=bla\" found."); //NOI18N } - + Pattern pattern = Pattern.compile("(key=([a-z0-9_]{1,}),value=\"([^\"]*)\";){1,}"); //NOI18N Matcher matcher = pattern.matcher(line); - + // Debug message this.getLogger().debug(MessageFormat.format("matches={0}", matcher.matches())); //NOI18N - + // Matches? if (!matcher.matches()) { // Corrupted file found throw new CorruptedDatabaseFileException(this.fileName, MessageFormat.format("line {0} doesn't match regular expression.", line)); //NOI18N } - + // Instance map Map map = new HashMap<>(line.length() / 40); - + pattern = Pattern.compile("(key=([a-z0-9_]{1,}),value=\"([^\"]*)\";)"); //NOI18N matcher = pattern.matcher(line); - + // Init group count int init = 0; - + // Then get all while (matcher.find(init)) { // Get group match String match = matcher.group(1); String key = matcher.group(2); String value = matcher.group(3); - + // key must noch be empty - assert((key != null) && (!key.isEmpty())) : MessageFormat.format("key={0} is not valid", key); //NOI18N - + assert ((key != null) && (!key.isEmpty())) : MessageFormat.format("key={0} is not valid", key); //NOI18N + // Get start and end int start = matcher.start(); int end = matcher.end(); - + // Debug message this.getLogger().debug(MessageFormat.format("init={0},start={1},end={2},match={3},key={4},value={5}", init, start, end, match, key, value)); //NOI18N - + // Add key/value to map map.put(key, value); - + // Hop to next match init = end; } - + // Trace message this.getLogger().trace(MessageFormat.format("map()={0} - EXIT!", map.size())); //NOI18N - + // Return finished map return map; } @@ -374,14 +377,16 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat * @return Length of underlaying file */ private long length () throws IOException { - long length = 0; + // Trace message + this.getLogger().trace("CALLED!"); // Try to get length from file - length = this.getStorageFile().length(); - this.getLogger().debug(MessageFormat.format("length={0}", length)); //NOI18N + long length = this.getStorageFile().length(); - // Return result + // Trace message this.getLogger().trace(MessageFormat.format("length={0} : EXIT!", length)); //NOI18N + + // Return it return length; } @@ -450,7 +455,7 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat if (null == output) { // Is null throw new NullPointerException("output is null"); //NOI18N - } else if (output.length() == 0) { + } else if (output.length() == 0) { // Is empty throw new IllegalArgumentException("output is empty"); //NOI18N } diff --git a/src/org/mxchange/jcore/database/backend/datasource/DataSourceDatabaseBackend.java b/src/org/mxchange/jcore/database/backend/datasource/DataSourceDatabaseBackend.java index 221ad86..b905676 100644 --- a/src/org/mxchange/jcore/database/backend/datasource/DataSourceDatabaseBackend.java +++ b/src/org/mxchange/jcore/database/backend/datasource/DataSourceDatabaseBackend.java @@ -55,9 +55,10 @@ public class DataSourceDatabaseBackend extends BaseDatabaseBackend implements Da /** * Constructor with table name - * + * * @param frontend An instance of the frontend - * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseDriverException If the requested driver is not supported + * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseDriverException + * If the requested driver is not supported */ public DataSourceDatabaseBackend (final DatabaseFrontend frontend) throws UnsupportedDatabaseDriverException { // Trace message diff --git a/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java b/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java index 86f487e..2a1498d 100644 --- a/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java +++ b/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java @@ -54,9 +54,10 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas /** * Constructor with table name - * + * * @param frontend An instance of the frontend - * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseDriverException If the requested driver is not supported + * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseDriverException + * If the requested driver is not supported */ public MySqlDatabaseBackend (final DatabaseFrontend frontend) throws UnsupportedDatabaseDriverException { // Trace message -- 2.39.5