]> git.mxchange.org Git - jcore.git/commitdiff
Continued:
authorRoland Haeder <roland@mxchange.org>
Thu, 27 Aug 2015 21:26:47 +0000 (23:26 +0200)
committerRoland Haeder <roland@mxchange.org>
Thu, 27 Aug 2015 21:26:47 +0000 (23:26 +0200)
- Added method numRows() (and implemented it generically)
- made doSelectByCrirtia() in BaseDatabaseBackend abstract
Signed-off-by:Roland Häder <roland@mxchange.org>

src/org/mxchange/jcore/database/backend/BaseDatabaseBackend.java
src/org/mxchange/jcore/database/backend/DatabaseBackend.java
src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java
src/org/mxchange/jcore/database/backend/datasource/DataSourceDatabaseBackend.java
src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java

index 73df2bfad97f9465cb181000e9e29c523f1d1e85..6a38586a69094bd976aad6917676b24997a8ca66 100644 (file)
  */
 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<? extends Storeable> 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<? extends Storeable> doSelectByCriteria (SearchableCriteria criteria) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException;
 }
index ef014e7287c10adb823fda3642595c1f14d4f681..d6ce3525a14cf4dafbe9cc184a99ed2b44b8f407 100644 (file)
@@ -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;
 }
index f1186a81f78b8a196dcd7b731dce899e3206c7e3..99f2ba66c1e75500f7e4cd3c7abb8417be0a991e 100644 (file)
@@ -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<String, String> 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<String, String> 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
                }
index 221ad86bdad7098fd495a1f8f5bbccaca4b92d44..b905676dc4c367a3bc742929dd34e7d6d0469c7d 100644 (file)
@@ -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
index 86f487e900284564cc0cd54f401f60a063593731..2a1498d32bc0c751cc507b841ca66e1ebe2d4657 100644 (file)
@@ -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