import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
-import java.sql.PreparedStatement;
+import java.sql.ResultSet;
import java.sql.SQLException;
+import java.sql.Statement;
import java.text.MessageFormat;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
import org.mxchange.jcore.criteria.searchable.SearchableCritera;
import org.mxchange.jcore.database.backend.BaseDatabaseBackend;
import org.mxchange.jcore.database.backend.DatabaseBackend;
*/
private static Connection connection;
- /**
- * Prepared statement for full row count
- */
- private PreparedStatement totalRowCount;
-
/**
* Constructor with table name
*
*/
public MySqlDatabaseBackend (final DatabaseFrontend frontend) throws UnsupportedDatabaseDriverException {
// Trace message
- this.getLogger().trace(MessageFormat.format("frontend={0} - CALLED!", frontend));
+ this.getLogger().trace(MessageFormat.format("frontend={0} - CALLED!", frontend)); //NOI18N
// Validate driver
this.validateDriver("mysql"); //NOI18N
String tableName = frontend.getTableName();
// Debug message
- this.getLogger().debug(MessageFormat.format("tableName={0}", tableName));
+ this.getLogger().debug(MessageFormat.format("tableName={0}", tableName)); //NOI18N
// Now that the driver is there, set the table name
this.setTableName(tableName);
+
+ // Set frontend
+ this.setFrontend(frontend);
}
@Override
this.getLogger().trace("CALLED!"); //NOI18N
// Is the connection already there?
- if (MySqlDatabaseBackend.connection instanceof Connection) {
- // Already connected
+ if (connection instanceof Connection) {
+ // Is the connection really up?
+ if (connection.isClosed()) {
+ // Connection is closed again
+ throw new SQLException("Connection is closed."); //NOI18N
+ }
+
+ // Already connected
this.getLogger().debug("Connection is already established."); //NOI18N
// No need to connect
this.getLogger().debug(MessageFormat.format("Attempting to connect to {0} ...", connect)); //NOI18N
// Now get a connection instance back
- MySqlDatabaseBackend.connection = DriverManager.getConnection(
+ connection = DriverManager.getConnection(
connect,
this.getProperty("database.mysql.login"), //NOI18N
this.getProperty("database.mysql.password") //NOI18N
);
- // Is the connection really up?
- if (MySqlDatabaseBackend.connection.isClosed()) {
- // Connection is closed again
- throw new SQLException("Connection is closed."); //NOI18N
- }
-
// Debug message
this.getLogger().debug("Connection is up, preparing some statements ..."); //NOI18N
- // Here, the connection is established, so prepare some statements
- this.totalRowCount = MySqlDatabaseBackend.connection.prepareStatement(String.format("SELECT COUNT(`id`) AS `cnt` FROM `%s` LIMIT 1", this.getTableName())); //NOI18N
-
// Trace message
this.getLogger().trace("EXIT!"); //NOI18N
}
@Override
- public Result<? extends Storeable> doSelectByCriteria (final SearchableCritera critera) {
- throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: criteria={0}", critera));
+ public Result<? extends Storeable> doSelectByCriteria (final SearchableCritera critera) throws SQLException {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("criteria={0} - CALLED!", critera));
+
+ // Start SELECT query
+ StringBuilder query = new StringBuilder(String.format("SELECT * FROM `%s`", this.getTableName()));
+
+ // Get entry set
+ Set<Map.Entry<String, Object>> set = critera.entrySet();
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("set.isEmpty()={0}", set.isEmpty()));
+
+ // Are there conditions?
+ if (!set.isEmpty()) {
+ // Continue with WHERE
+ query.append(" WHERE ");
+
+ // Get iterator
+ Iterator<Map.Entry<String, Object>> iterator = set.iterator();
+
+ // "Walk through all
+ while (iterator.hasNext()) {
+ // Get element
+ Map.Entry<String, Object> entry = iterator.next();
+
+ // Add key as column name
+ query.append(String.format("`%s`", entry.getKey()));
+
+ // Get value
+ Object value = entry.getValue();
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("value={0}", value));
+
+ // Which type has the value?
+ if (value instanceof Boolean) {
+ // Boolean value
+ query.append(String.format("=%s", value.toString()));
+ } else {
+ // Cannot handle this
+ throw new SQLException(MessageFormat.format("Cannot handle value={0} for key={1} in table {2}", value, entry.getKey(), this.getTableName()));
+ }
+ }
+ }
+
+ // Full statement is complete here, better log it
+ this.getLogger().debug(MessageFormat.format("query={0} is complete.", query));
+
+ // Prepare statement instance
+ Statement statement = connection.createStatement();
+
+ // Run it
+ ResultSet resultSet = statement.executeQuery(query.toString());
+
+ // The result set needs to be transformed into Result, so initialize a result instance here
+ Result<? extends Storeable> result = this.getFrontend().getResultFromSet(resultSet);
+
+ // Return it
+ return result;
}
@Override
public void doShutdown () throws SQLException, IOException {
- // This should not happen:
- assert(MySqlDatabaseBackend.connection instanceof Connection) : MessageFormat.format("connection is not valid: {0}", MySqlDatabaseBackend.connection); //NOI18N
+ // Trace message
+ this.getLogger().trace("CALLED!"); //NOI18N
+
+ // Is the connection still there?
+ if (!connection.isClosed()) {
+ // Close down database connection
+ connection.close();
+ }
- // Close down database connection
- MySqlDatabaseBackend.connection.close();
+ // Trace message
+ this.getLogger().trace("EXIT!"); //NOI18N
}
}