From: Roland Haeder Date: Fri, 14 Aug 2015 21:25:03 +0000 (+0200) Subject: Introduced getTotalRows() to DatabaseBackend implementations + added getIdName()... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=cb5adee6014893291483858d2c33ac42310888d0;p=jcore.git Introduced getTotalRows() to DatabaseBackend implementations + added getIdName() to DatabaseFrontend Signed-off-by:Roland Häder --- diff --git a/src/org/mxchange/jcore/database/backend/DatabaseBackend.java b/src/org/mxchange/jcore/database/backend/DatabaseBackend.java index 34f985d..ea8cb00 100644 --- a/src/org/mxchange/jcore/database/backend/DatabaseBackend.java +++ b/src/org/mxchange/jcore/database/backend/DatabaseBackend.java @@ -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; } diff --git a/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java b/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java index fc10a62..89c8964 100644 --- a/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java +++ b/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java @@ -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; + } } diff --git a/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java b/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java index 1cf298e..a97bd26 100644 --- a/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java +++ b/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java @@ -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; + } } diff --git a/src/org/mxchange/jcore/database/frontend/DatabaseFrontend.java b/src/org/mxchange/jcore/database/frontend/DatabaseFrontend.java index 57d15ba..59e59fb 100644 --- a/src/org/mxchange/jcore/database/frontend/DatabaseFrontend.java +++ b/src/org/mxchange/jcore/database/frontend/DatabaseFrontend.java @@ -76,4 +76,11 @@ public interface DatabaseFrontend extends FrameworkInterface { * @throws java.lang.reflect.InvocationTargetException Any other problems? */ public Storeable toStoreable (final Map map) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException; + + /** + * Some getter for name of id column + * + * @return Name of id column + */ + public String getIdName (); }