*/
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;
/**
*
* @author Roland Haeder
*/
-public class BaseDatabaseBackend extends BaseFrameworkSystem {
+public abstract class BaseDatabaseBackend extends BaseFrameworkSystem {
/**
* No instances from this class
// 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;
}
* @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;
}
}
}
- // Generate key=value pair
+ // Generate key=value pair
String pair = String.format("key=%s,value=\"%s\";", entry.getKey(), String.valueOf(value)); //NOI18N
// Debug message
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
// 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;
}
* @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;
}
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
}
/**
* 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
/**
* 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