From e0dbb3c7b440c5f005bb6874a235a93c73188f55 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Fri, 14 Aug 2015 12:00:47 +0200 Subject: [PATCH] =?utf8?q?Continued=20with=20jcore:=20-=20Improved=20handl?= =?utf8?q?ing=20of=20null=20values=20in=20doInsertDataSet()=20-=20Added=20?= =?utf8?q?emptyStringToNull()=20to=20DatabaseFrontend=20interface=20Signed?= =?utf8?q?-off-by:Roland=20H=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../backend/mysql/MySqlDatabaseBackend.java | 20 +++++++++++++++---- .../database/frontend/DatabaseFrontend.java | 9 +++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java b/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java index 8d27643..1b4af28 100644 --- a/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java +++ b/src/org/mxchange/jcore/database/backend/mysql/MySqlDatabaseBackend.java @@ -164,11 +164,23 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas Object value = entry.getValue(); // Debug message - this.getLogger().debug(MessageFormat.format("value={0}", value)); + this.getLogger().debug(MessageFormat.format("value={0} - BEFORE!", value)); - // Add value - valueQuery.append("?,"); - values.add(value); + // Handle possible empty->null convertion + value = this.getFrontend().emptyStringToNull(entry.getKey(), value); + + // Debug message + this.getLogger().debug(MessageFormat.format("value={0} - AFTER!", value)); + + // Is the value null? + if (value == null) { + // Add null + valueQuery.append("NULL,"); + } else { + // Add value + valueQuery.append("?,"); + values.add(value); + } } // Now put all together diff --git a/src/org/mxchange/jcore/database/frontend/DatabaseFrontend.java b/src/org/mxchange/jcore/database/frontend/DatabaseFrontend.java index 5286215..9b050dc 100644 --- a/src/org/mxchange/jcore/database/frontend/DatabaseFrontend.java +++ b/src/org/mxchange/jcore/database/frontend/DatabaseFrontend.java @@ -30,6 +30,15 @@ import org.mxchange.jcore.exceptions.BadTokenException; * @author Roland Haeder */ public interface DatabaseFrontend extends FrameworkInterface { + /** + * Depending on column, an empty value may be converted to null + * + * @param key Key to check + * @param value Value to check + * @return Possible previous value or null + */ + public Object emptyStringToNull (final String key, final Object value); + /** * Gets a Result back from given ResultSet instance * -- 2.39.5