DefaultUpdateSqlBuilder extends AbstractTermsFragmentBuilder<UpdateOperatorParameter> implements UpdateSqlBuilder { @Override public SqlRequest build(UpdateOperatorParameter parameter) { if (CollectionUtils.isEmpty(parameter.getColumns())) { throw new UnsupportedOperationException("no columns are updated"); } if (CollectionUtils.isEmpty(parameter.getWhere())) { throw new UnsupportedOperationException("unsupported no conditions update"); } PrepareSqlFragments fragments = PrepareSqlFragments.of(); fragments.addSql("update", table.getFullName(), "set"); int index = 0; for (UpdateColumn column : parameter.getColumns()) { SqlFragments columnFragments = table.getColumn(column.getColumn()) .filter(RDBColumnMetadata::isUpdatable) .<SqlFragments>map(columnMetadata -> { Object value = column.getValue(); if (value == null) { return EmptySqlFragments.INSTANCE; } PrepareSqlFragments sqlFragments = PrepareSqlFragments.of(); sqlFragments.addSql(columnMetadata.getQuoteName(), "="); if (column instanceof NativeSql) { return PrepareSqlFragments.of() .addSql(((NativeSql) column).getSql()) .addParameter(((NativeSql) column).getParameters()); } if (value instanceof NativeSql) { return PrepareSqlFragments.of() .addSql(columnMetadata.getQuoteName(), "=") .addSql(((NativeSql) column.getValue()).getSql()) .addParameter(((NativeSql) column.getValue()).getParameters()); } sqlFragments.addFragments(ofNullable(column.getFunction()) .flatMap(function -> columnMetadata.findFeature(createFeatureId(function))) .map(builder -> builder.create(columnMetadata.getName(), columnMetadata, column.getOpts())) .orElseGet(() -> PrepareSqlFragments.of() .addSql("?") .addParameter(columnMetadata.encode(value)))); return sqlFragments; }).orElse(EmptySqlFragments.INSTANCE); if (columnFragments.isNotEmpty()) { if (index++ != 0) { fragments.addSql(","); } fragments.addFragments(columnFragments); } } if (index == 0) { throw new UnsupportedOperationException("No columns are updated"); } fragments.addSql("where"); SqlFragments where = createTermFragments(parameter, parameter.getWhere()); if (where.isEmpty()) { throw new UnsupportedOperationException("Unsupported No Conditions update"); } fragments.addFragments(where); return fragments.toRequest(); }  @Override SqlRequest build(UpdateOperatorParameter parameter);  }
@Test public void test() { UpdateOperatorParameter parameter = new UpdateOperatorParameter(); { UpdateColumn column = new UpdateColumn(); column.setColumn("name"); column.setValue("admin"); parameter.getColumns().add(column); } { parameter.getWhere().addAll(Query.of().where("id", "1234").and().notNull("name").getParam().getTerms()); } SqlRequest request = builder.build(parameter); System.out.println(request); assertEquals(request.getSql(), "update PUBLIC.test set \"NAME\" = ? where \"ID\" = ? and \"NAME\" is not null"); assertArrayEquals(request.getParameters(), new Object[]{"admin", "1234"}); }
@Test public void testFunction() { UpdateOperatorParameter parameter = new UpdateOperatorParameter(); { UpdateColumn column = new UpdateColumn(); column.setColumn("name"); column.setValue("admin"); parameter.getColumns().add(column); } { UpdateColumn column = new UpdateColumn(); column.setFunction("incr"); column.setOpts(Collections.singletonMap("incr", 1)); column.setColumn("name"); column.setValue("admin"); parameter.getColumns().add(column); } { parameter.getWhere().addAll(Query.of().where("id", "1234").getParam().getTerms()); } builder.getTable() .getColumn("name") .ifPresent(column -> column.addFeature(new FunctionFragmentBuilder() { @Override public String getFunction() { return "incr"; } @Override public SqlFragments create(String columnFullName, RDBColumnMetadata metadata, Map<String, Object> opts) { return PrepareSqlFragments.of().addSql(columnFullName, "+ ?") .addParameter(opts.getOrDefault("incr", 1)); } @Override public String getName() { return "自增"; } })); SqlRequest request = builder.build(parameter); System.out.println(request); assertEquals(request.getSql(), "update PUBLIC.test set \"NAME\" = ? , \"NAME\" = name + ? where \"ID\" = ?"); assertArrayEquals(request.getParameters(), new Object[]{"admin", 1, "1234"}); }