]> git.mxchange.org Git - jcore.git/commitdiff
Introduced getTotalRows() to DatabaseBackend implementations + added getIdName()...
authorRoland Haeder <roland@mxchange.org>
Fri, 14 Aug 2015 21:25:03 +0000 (23:25 +0200)
committerRoland Haeder <roland@mxchange.org>
Fri, 14 Aug 2015 21:30:17 +0000 (23:30 +0200)
Signed-off-by:Roland Häder <roland@mxchange.org>

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

index 34f985dc70e65cb2e23343740b3f6951caf18ddc..ea8cb00b2f8c8ea5793f6486d062954ecf36d3a1 100644 (file)
@@ -75,4 +75,13 @@ public interface DatabaseBackend extends FrameworkInterface {
         * @throws java.io.IOException If any IO error occurs
         */
        public void doShutdown () throws SQLException, IOException;
+
+       /**
+        * Some getter for total rows
+        *
+        * @return Total rows
+        * @throws java.io.IOException If an IO error occurs
+        * @throws java.sql.SQLException If any SQL error occurs
+        */
+       public Long getTotalRows () throws IOException, SQLException;
 }
index fc10a6220b4440f11664c42e580ee0c78671ee14..89c8964b779d1f757392002b4f6be0ef9838fd93 100644 (file)
@@ -128,6 +128,9 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat
                // Full output string
                StringBuilder output = new StringBuilder(dataset.size() * 20);
 
+               // Add index column
+               output.append(String.format("key=%s,value=\"%s\";", this.getFrontend().getIdName(), this.getTotalRows() + 1));
+
                // "Walk" over all entries
                while (iterator.hasNext()) {
                        // Get next entry
@@ -142,7 +145,7 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat
                                String str = (String) value;
 
                                // Does it contain a " ?
-                               if (str.contains("")) {
+                               if (str.contains("\"")) {
                                        // Don't accept here
                                        throw new IllegalArgumentException("value " + value + " with double-quote not supported yet.");
                                }
@@ -151,6 +154,9 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat
                                        // Generate key=value pair
                        String pair = String.format("key=%s,value=\"%s\";", entry.getKey(), String.valueOf(value));
 
+                       // Debug message
+                       this.getLogger().debug("pair=" + pair);
+
                        // Append to output
                        output.append(pair);
                }
@@ -424,4 +430,31 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat
                // Return finished map
                return map;
        }
+
+       @Override
+       public final Long getTotalRows () throws IOException {
+               // Trace message
+               this.getLogger().trace("CALLED!");
+
+               // Init count
+               Long count = 0L;
+
+               // First rewind
+               this.rewind();
+
+               // Walk through all rows
+               while (!this.isEndOfFile()) {
+                       // Get next line
+                       String line = this.readLine();
+
+                       // Count one up
+                       count++;
+               }
+
+               // Trace message
+               this.getLogger().trace("count=" + count + " - EXIT!");
+
+               // Return it
+               return count;
+       }
 }
index 1cf298e3e14de5a703c8732b0c12139139146091..a97bd262bd1fb8bb56256f38dea611212ae8f760 100644 (file)
@@ -47,6 +47,11 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas
         */
        private static Connection connection;
 
+       /**
+        * Prepared statement for total row count
+        */
+       private PreparedStatement totalRows;
+
        /**
         * Constructor with table name
         * 
@@ -86,7 +91,7 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas
                                throw new SQLException("Connection is closed."); //NOI18N
                        }
 
-               // Already connected
+                       // Already connected
                        this.getLogger().debug("Connection is already established."); //NOI18N
 
                        // No need to connect
@@ -112,6 +117,13 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas
                // Debug message
                this.getLogger().debug("Connection is up, preparing some statements ..."); //NOI18N
 
+               // Set prepared statement
+               this.totalRows = connection.prepareStatement("SELECT COUNT(`?`) AS `cnt` FROM `?` LIMIT 1");
+
+               // Set values in prepared statement
+               this.totalRows.setObject(1, this.getFrontend().getIdName());
+               this.totalRows.setObject(2, this.getTableName());
+
                // Trace message
                this.getLogger().trace("EXIT!"); //NOI18N
        }
@@ -378,4 +390,25 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas
                // Return it
                return statement;
        }
+
+       @Override
+       public Long getTotalRows () throws IOException, SQLException {
+               // Trace message
+               this.getLogger().trace("CALLED!");
+
+               // Execute query
+               ResultSet set = this.totalRows.executeQuery();
+
+               // Rewind to beginning
+               set.beforeFirst();
+
+               // Get long
+               Long count = set.getLong("cnt");
+
+               // Trace message
+               this.getLogger().trace("count=" + count + " - EXIT!");
+
+               // Return it
+               return count;
+       }
 }
index 57d15bab6924dc7c329aa1a0e0e728a35770651e..59e59fbf5289055be60a53061e53d41424998af1 100644 (file)
@@ -76,4 +76,11 @@ public interface DatabaseFrontend extends FrameworkInterface {
         * @throws java.lang.reflect.InvocationTargetException Any other problems?
         */
        public Storeable toStoreable (final Map<String, String> map) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException;
+
+       /**
+        * Some getter for name of id column
+        *
+        * @return Name of id column
+        */
+       public String getIdName ();
 }