Skip to content

Commit

Permalink
Tweak InsertAppSetting to tests can pass consistently with creation/m…
Browse files Browse the repository at this point in the history
…odification dates.
  • Loading branch information
msqr committed Jun 21, 2024
1 parent 22d6e22 commit e071513
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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 ) {
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
INSERT INTO solarcommon.app_setting (modified, skey, stype, svalue)
VALUES (?,?,?,?)
INSERT INTO solarcommon.app_setting (created, modified, skey, stype, svalue)
VALUES (?,?,?,?,?)

0 comments on commit e071513

Please sign in to comment.