diff --git a/solarnet/common/src/main/java/net/solarnetwork/central/common/dao/jdbc/sql/InsertAppSetting.java b/solarnet/common/src/main/java/net/solarnetwork/central/common/dao/jdbc/sql/InsertAppSetting.java index 5987e3a5f..5e22295a5 100644 --- a/solarnet/common/src/main/java/net/solarnetwork/central/common/dao/jdbc/sql/InsertAppSetting.java +++ b/solarnet/common/src/main/java/net/solarnetwork/central/common/dao/jdbc/sql/InsertAppSetting.java @@ -27,6 +27,7 @@ import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Timestamp; +import java.time.Instant; import org.springframework.jdbc.core.PreparedStatementCreator; import org.springframework.jdbc.core.SqlProvider; import net.solarnetwork.central.domain.AppSetting; @@ -35,7 +36,7 @@ * Insert an {@link AppSetting} instance. * * @author matt - * @version 1.0 + * @version 1.1 * @since 2.0 */ public class InsertAppSetting implements PreparedStatementCreator, SqlProvider { @@ -62,22 +63,8 @@ public InsertAppSetting(AppSetting setting, boolean upsert) { @Override public String getSql() { StringBuilder buf = new StringBuilder(); - buf.append("INSERT INTO solarcommon.app_setting ("); - if ( setting.getCreated() != null ) { - buf.append("created, "); - } - if ( setting.getModified() != null ) { - buf.append("modified, "); - } - buf.append("skey, stype, svalue)\n"); - buf.append("VALUES ("); - if ( setting.getCreated() != null ) { - buf.append("?,"); - } - if ( setting.getModified() != null ) { - buf.append("?,"); - } - buf.append("?,?,?)\n"); + buf.append("INSERT INTO solarcommon.app_setting (created, modified, skey, stype, svalue)\n"); + buf.append("VALUES (?,?,?,?,?)\n"); if ( upsert ) { buf.append("ON CONFLICT (skey, stype) DO UPDATE\nSET "); if ( setting.getModified() != null ) { @@ -90,14 +77,15 @@ public String getSql() { @Override public PreparedStatement createPreparedStatement(Connection con) throws SQLException { - PreparedStatement stmt = con.prepareStatement(getSql()); + final PreparedStatement stmt = con.prepareStatement(getSql()); + final Instant now = (setting.getCreated() == null || setting.getModified() == null + ? Instant.now() + : null); int p = 0; - if ( setting.getCreated() != null ) { - stmt.setTimestamp(++p, Timestamp.from(setting.getCreated())); - } - if ( setting.getModified() != null ) { - stmt.setTimestamp(++p, Timestamp.from(setting.getModified())); - } + stmt.setTimestamp(++p, + Timestamp.from(setting.getCreated() != null ? setting.getCreated() : now)); + stmt.setTimestamp(++p, + Timestamp.from(setting.getModified() != null ? setting.getModified() : now)); stmt.setString(++p, setting.getKey()); stmt.setString(++p, setting.getType()); stmt.setString(++p, setting.getValue()); diff --git a/solarnet/common/src/test/resources/net/solarnetwork/central/common/dao/jdbc/sql/test/insert-app-setting-new-upsert-nomod.sql b/solarnet/common/src/test/resources/net/solarnetwork/central/common/dao/jdbc/sql/test/insert-app-setting-new-upsert-nomod.sql index 362db4011..fd96aa56b 100644 --- a/solarnet/common/src/test/resources/net/solarnetwork/central/common/dao/jdbc/sql/test/insert-app-setting-new-upsert-nomod.sql +++ b/solarnet/common/src/test/resources/net/solarnetwork/central/common/dao/jdbc/sql/test/insert-app-setting-new-upsert-nomod.sql @@ -1,4 +1,4 @@ -INSERT INTO solarcommon.app_setting (skey, stype, svalue) -VALUES (?,?,?) +INSERT INTO solarcommon.app_setting (created, modified, skey, stype, svalue) +VALUES (?,?,?,?,?) ON CONFLICT (skey, stype) DO UPDATE SET svalue = EXCLUDED.svalue \ No newline at end of file diff --git a/solarnet/common/src/test/resources/net/solarnetwork/central/common/dao/jdbc/sql/test/insert-app-setting-new-upsert.sql b/solarnet/common/src/test/resources/net/solarnetwork/central/common/dao/jdbc/sql/test/insert-app-setting-new-upsert.sql index 646d27bc3..87678de19 100644 --- a/solarnet/common/src/test/resources/net/solarnetwork/central/common/dao/jdbc/sql/test/insert-app-setting-new-upsert.sql +++ b/solarnet/common/src/test/resources/net/solarnetwork/central/common/dao/jdbc/sql/test/insert-app-setting-new-upsert.sql @@ -1,5 +1,5 @@ -INSERT INTO solarcommon.app_setting (modified, skey, stype, svalue) -VALUES (?,?,?,?) +INSERT INTO solarcommon.app_setting (created, modified, skey, stype, svalue) +VALUES (?,?,?,?,?) ON CONFLICT (skey, stype) DO UPDATE SET modified = EXCLUDED.modified , svalue = EXCLUDED.svalue \ No newline at end of file diff --git a/solarnet/common/src/test/resources/net/solarnetwork/central/common/dao/jdbc/sql/test/insert-app-setting-new.sql b/solarnet/common/src/test/resources/net/solarnetwork/central/common/dao/jdbc/sql/test/insert-app-setting-new.sql index f452902f4..95a92ec26 100644 --- a/solarnet/common/src/test/resources/net/solarnetwork/central/common/dao/jdbc/sql/test/insert-app-setting-new.sql +++ b/solarnet/common/src/test/resources/net/solarnetwork/central/common/dao/jdbc/sql/test/insert-app-setting-new.sql @@ -1,2 +1,2 @@ -INSERT INTO solarcommon.app_setting (modified, skey, stype, svalue) -VALUES (?,?,?,?) \ No newline at end of file +INSERT INTO solarcommon.app_setting (created, modified, skey, stype, svalue) +VALUES (?,?,?,?,?) \ No newline at end of file