Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions server/api-service/openblocks-plugins/mssqlPlugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.openblocks.plugin.mssql.gui;

public final class GuiConstants {

public static final String MSSQL_COLUMN_DELIMITER_FRONT = "[";
public static final String MSSQL_COLUMN_DELIMITER_BACK = "]";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.openblocks.plugin.mssql.gui;

import static com.openblocks.plugin.mssql.gui.GuiConstants.MSSQL_COLUMN_DELIMITER_BACK;
import static com.openblocks.plugin.mssql.gui.GuiConstants.MSSQL_COLUMN_DELIMITER_FRONT;
import static com.openblocks.sdk.plugin.sqlcommand.changeset.BulkObjectChangeSet.parseBulkRecords;

import java.util.Map;

import com.openblocks.sdk.plugin.sqlcommand.GuiSqlCommand;
import com.openblocks.sdk.plugin.sqlcommand.changeset.BulkObjectChangeSet;
import com.openblocks.sdk.plugin.sqlcommand.command.BulkInsertCommand;

public class MssqlBulkInsertCommand extends BulkInsertCommand {
protected MssqlBulkInsertCommand(String table, BulkObjectChangeSet bulkObjectChangeSet) {
super(table, bulkObjectChangeSet, MSSQL_COLUMN_DELIMITER_FRONT, MSSQL_COLUMN_DELIMITER_BACK);
}

public static BulkInsertCommand from(Map<String, Object> commandDetail) {
String table = GuiSqlCommand.parseTable(commandDetail);
String recordStr = parseBulkRecords(commandDetail);
BulkObjectChangeSet bulkObjectChangeSet = new BulkObjectChangeSet(recordStr);
return new MssqlBulkInsertCommand(table, bulkObjectChangeSet);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.openblocks.plugin.mssql.gui;

import static com.openblocks.plugin.mssql.gui.GuiConstants.MSSQL_COLUMN_DELIMITER_BACK;
import static com.openblocks.plugin.mssql.gui.GuiConstants.MSSQL_COLUMN_DELIMITER_FRONT;
import static com.openblocks.sdk.plugin.sqlcommand.GuiSqlCommand.parseTable;
import static com.openblocks.sdk.plugin.sqlcommand.changeset.BulkObjectChangeSet.parseBulkRecords;
import static com.openblocks.sdk.plugin.sqlcommand.changeset.BulkObjectChangeSet.parsePrimaryKey;

import java.util.Map;

import com.openblocks.sdk.plugin.sqlcommand.changeset.BulkObjectChangeSet;
import com.openblocks.sdk.plugin.sqlcommand.command.BulkUpdateCommand;

public class MssqlBulkUpdateCommand extends BulkUpdateCommand {

protected MssqlBulkUpdateCommand(String table, BulkObjectChangeSet bulkObjectChangeSet, String primaryKey) {
super(table, bulkObjectChangeSet, primaryKey, MSSQL_COLUMN_DELIMITER_FRONT, MSSQL_COLUMN_DELIMITER_BACK);
}

public static MssqlBulkUpdateCommand from(Map<String, Object> commandDetail) {
String table = parseTable(commandDetail);
String recordStr = parseBulkRecords(commandDetail);
BulkObjectChangeSet bulkObjectChangeSet = new BulkObjectChangeSet(recordStr);
return new MssqlBulkUpdateCommand(table, bulkObjectChangeSet, parsePrimaryKey(commandDetail));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.openblocks.plugin.mssql.gui;

import static com.openblocks.plugin.mssql.gui.GuiConstants.MSSQL_COLUMN_DELIMITER_BACK;
import static com.openblocks.plugin.mssql.gui.GuiConstants.MSSQL_COLUMN_DELIMITER_FRONT;
import static com.openblocks.sdk.plugin.sqlcommand.filter.FilterSet.parseFilterSet;

import java.util.Map;

import com.openblocks.sdk.plugin.sqlcommand.GuiSqlCommand;
import com.openblocks.sdk.plugin.sqlcommand.command.DeleteCommand;
import com.openblocks.sdk.plugin.sqlcommand.filter.FilterSet;

public class MssqlDeleteCommand extends DeleteCommand {

protected MssqlDeleteCommand(String table, FilterSet filterSet, boolean allowMultiModify) {
super(table, filterSet, allowMultiModify, MSSQL_COLUMN_DELIMITER_FRONT, MSSQL_COLUMN_DELIMITER_BACK);
}

public static DeleteCommand from(Map<String, Object> commandDetail) {
String table = GuiSqlCommand.parseTable(commandDetail);
FilterSet filterSet = parseFilterSet(commandDetail);
boolean allowMultiModify = GuiSqlCommand.parseAllowMultiModify(commandDetail);
return new MssqlDeleteCommand(table, filterSet, allowMultiModify);
}

@Override
public GuiSqlCommandRenderResult render(Map<String, Object> requestMap) {
return super.render(requestMap);
}

@Override
protected void renderTable(String renderedTable, StringBuilder sb) {
sb.append("delete ");
if (!allowMultiModify) {
sb.append("top (1) ");
}
sb.append("from ").append(renderedTable);
}

@Override
protected void renderLimit(StringBuilder sb) {
// do nothing
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package com.openblocks.sdk.plugin.sqlcommand.command.mssql;
package com.openblocks.plugin.mssql.gui;

import static com.openblocks.plugin.mssql.gui.GuiConstants.MSSQL_COLUMN_DELIMITER_BACK;
import static com.openblocks.plugin.mssql.gui.GuiConstants.MSSQL_COLUMN_DELIMITER_FRONT;

import java.util.Map;

Expand All @@ -8,9 +11,6 @@

public class MssqlInsertCommand extends InsertCommand {

private static final String MSSQL_COLUMN_DELIMITER_FRONT = "[";
private static final String MSSQL_COLUMN_DELIMITER_BACK = "]";

private MssqlInsertCommand(Map<String, Object> commandDetail) {
super(commandDetail, MSSQL_COLUMN_DELIMITER_FRONT, MSSQL_COLUMN_DELIMITER_BACK);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.openblocks.plugin.mssql.gui;

import static com.openblocks.plugin.mssql.gui.GuiConstants.MSSQL_COLUMN_DELIMITER_BACK;
import static com.openblocks.plugin.mssql.gui.GuiConstants.MSSQL_COLUMN_DELIMITER_FRONT;

import java.util.Map;

import com.google.common.annotations.VisibleForTesting;
import com.openblocks.sdk.plugin.sqlcommand.changeset.ChangeSet;
import com.openblocks.sdk.plugin.sqlcommand.command.UpdateCommand;
import com.openblocks.sdk.plugin.sqlcommand.filter.FilterSet;

public class MssqlUpdateCommand extends UpdateCommand {

private MssqlUpdateCommand(Map<String, Object> commandDetail) {
super(commandDetail, MSSQL_COLUMN_DELIMITER_FRONT, MSSQL_COLUMN_DELIMITER_BACK);
}

@VisibleForTesting
protected MssqlUpdateCommand(String table, ChangeSet changeSet, FilterSet filterSet, boolean allowMultiModify) {
super(table, changeSet, filterSet, allowMultiModify, MSSQL_COLUMN_DELIMITER_FRONT, MSSQL_COLUMN_DELIMITER_BACK);
}

@Override
protected void appendTable(String renderedTable, StringBuilder sb) {
sb.append("update ");
if (!allowMultiModify) {
sb.append(" top (1) ");
}
sb.append(renderedTable);
}

@Override
protected void appendLimit(StringBuilder sb) {
// do nothing
}

public static MssqlUpdateCommand from(Map<String, Object> commandDetail) {
return new MssqlUpdateCommand(commandDetail);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.apache.commons.collections4.MapUtils;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.openblocks.sdk.exception.PluginException;

import lombok.Getter;
Expand All @@ -17,11 +18,20 @@
public class MssqlQueryConfig {
private final String sql;
private final boolean disablePreparedStatement;
private final String mode;
private final String guiStatementType;
private final Map<String, Object> guiStatementDetail;

@JsonCreator
private MssqlQueryConfig(String sql, boolean disablePreparedStatement) {
private MssqlQueryConfig(String sql, boolean disablePreparedStatement,
String mode,
@JsonProperty("commandType") String guiStatementType,
@JsonProperty("command") Map<String, Object> guiStatementDetail) {
this.sql = sql;
this.disablePreparedStatement = disablePreparedStatement;
this.mode = mode;
this.guiStatementType = guiStatementType;
this.guiStatementDetail = guiStatementDetail;
}

public static MssqlQueryConfig from(Map<String, Object> queryConfigs) {
Expand All @@ -41,4 +51,8 @@ public String getSql() {
return sql.trim();
}

public boolean isGuiMode() {
return "GUI".equalsIgnoreCase(mode);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static com.openblocks.sdk.exception.PluginCommonError.QUERY_EXECUTION_ERROR;
import static com.openblocks.sdk.plugin.common.QueryExecutionUtils.getIdenticalColumns;
import static com.openblocks.sdk.plugin.common.SqlQueryUtils.isInsertQuery;
import static com.openblocks.sdk.util.ExceptionUtils.wrapException;
import static com.openblocks.sdk.util.JsonUtils.toJson;
import static com.openblocks.sdk.util.MustacheHelper.doPrepareStatement;
import static com.openblocks.sdk.util.MustacheHelper.extractMustacheKeysInOrder;
Expand All @@ -36,6 +37,7 @@

import javax.annotation.Nonnull;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.pf4j.Extension;
Expand Down Expand Up @@ -243,10 +245,7 @@ private void bindPreparedStatementForGuiMode(PreparedStatement preparedQuery, Li
bindParam(index + 1, value, preparedQuery, "");
}
} catch (Exception e) {
if (e instanceof PluginException pluginException) {
throw pluginException;
}
throw new PluginException(PREPARED_STATEMENT_BIND_PARAMETERS_ERROR, "PREPARED_STATEMENT_BIND_PARAMETERS_ERROR", e.getMessage());
throw wrapException(PREPARED_STATEMENT_BIND_PARAMETERS_ERROR, "PREPARED_STATEMENT_BIND_PARAMETERS_ERROR", e);
}
}

Expand Down Expand Up @@ -298,7 +297,7 @@ private List<Long> getGeneratedIds(ResultSet generatedKeys) throws SQLException
private List<LocaleMessage> populateHintMessages(List<String> columnNames) {
List<LocaleMessage> messages = new ArrayList<>();
List<String> identicalColumns = getIdenticalColumns(columnNames);
if (!org.springframework.util.CollectionUtils.isEmpty(identicalColumns)) {
if (!CollectionUtils.isEmpty(identicalColumns)) {
messages.add(new LocaleMessage("DUPLICATE_COLUMN", String.join("/", identicalColumns)));
}
return messages;
Expand All @@ -316,10 +315,7 @@ private void bindPreparedStatementParamsWithMustacheKeyValues(PreparedStatement
bindParam(index + 1, value, preparedStatement, mustacheKey);
}
} catch (Exception e) {
if (e instanceof PluginException pluginException) {
throw pluginException;
}
throw new PluginException(PREPARED_STATEMENT_BIND_PARAMETERS_ERROR, "PREPARED_STATEMENT_BIND_PARAMETERS_ERROR", e.getMessage());
throw wrapException(PREPARED_STATEMENT_BIND_PARAMETERS_ERROR, "PREPARED_STATEMENT_BIND_PARAMETERS_ERROR", e);
}
}

Expand Down Expand Up @@ -353,7 +349,8 @@ private void bindParam(int bindIndex, Object value, PreparedStatement preparedSt
return;
}
throw new PluginException(PREPARED_STATEMENT_BIND_PARAMETERS_ERROR, "PS_BIND_ERROR",
bindKeyName, value.getClass().getSimpleName());
StringUtils.isBlank(bindKeyName) ? String.valueOf(value) : bindKeyName,
value.getClass().getSimpleName());
}

private void releaseResources(AutoCloseable... autoCloseables) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static com.openblocks.sdk.plugin.common.QueryExecutionUtils.getIdenticalColumns;
import static com.openblocks.sdk.util.JsonUtils.fromJsonList;
import static com.openblocks.sdk.util.JsonUtils.toJson;
import static com.openblocks.sdk.util.MustacheHelper.doPrepareStatement;
import static com.openblocks.sdk.util.MustacheHelper.renderMustacheString;

import java.io.IOException;
Expand Down Expand Up @@ -147,7 +148,7 @@ private QueryExecutionResult executeCommon(HikariDataSource hikariDataSource,
bindPreparedStatementForGuiMode(preparedQuery, renderResult.bindParams());
} else {
List<String> mustacheValuesInOrder = MustacheHelper.extractMustacheKeysInOrder(rawQuery);
String updatedQuery = MustacheHelper.replaceMustacheWithQuestionMark(rawQuery, mustacheValuesInOrder);
var updatedQuery = doPrepareStatement(rawQuery, mustacheValuesInOrder, requestParams);
List<DataType> explicitCastDataTypes = PostgresDataTypeUtils.extractExplicitCasting(updatedQuery);

preparedQuery = connection.prepareStatement(updatedQuery);
Expand Down Expand Up @@ -297,7 +298,8 @@ private void bindParam(int bindIndex, Object value, PreparedStatement preparedSt
return;
}
throw new PluginException(PREPARED_STATEMENT_BIND_PARAMETERS_ERROR, "PS_BIND_ERROR",
bindKeyName, value.getClass().getSimpleName());
StringUtils.isBlank(bindKeyName) ? String.valueOf(value) : bindKeyName,
value.getClass().getSimpleName());
}

private void releaseResources(Connection connectionFromPool, Statement statement,
Expand Down Expand Up @@ -339,7 +341,7 @@ private void releaseResources(Connection connectionFromPool, Statement statement
private List<LocaleMessage> populateHintMessages(List<String> columnNames) {
List<LocaleMessage> messages = new ArrayList<>();
List<String> identicalColumns = getIdenticalColumns(columnNames);
if (!org.springframework.util.CollectionUtils.isEmpty(identicalColumns)) {
if (!CollectionUtils.isEmpty(identicalColumns)) {
messages.add(new LocaleMessage("DUPLICATE_COLUMN", String.join("/", identicalColumns)));
}
return messages;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.util.CollectionUtils;
import org.apache.commons.collections4.MapUtils;

import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;
Expand Down Expand Up @@ -57,7 +57,7 @@ public static String getStringValueSafelyFromFormData(Map<String, Object> formDa

@SuppressWarnings("unchecked")
public static Object getValueSafelyFromFormData(Map<String, Object> formData, String field) {
if (CollectionUtils.isEmpty(formData)) {
if (MapUtils.isEmpty(formData)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,27 @@ public GuiSqlCommandRenderResult render(Map<String, Object> requestMap) {
String renderedTable = MustacheHelper.renderMustacheString(table, requestMap);

StringBuilder sb = new StringBuilder();
sb.append("delete from ").append(renderedTable);
renderTable(renderedTable, sb);
if (filterSet.isEmpty()) {
if (!allowMultiModify) {
sb.append(" limit 1");
}
renderLimit(sb);
return new GuiSqlCommandRenderResult(sb.toString(), Collections.emptyList());
}

GuiSqlCommandRenderResult render = filterSet.render(requestMap, columnFrontDelimiter, columnBackDelimiter, isRenderWithRawSql(),
escapeStrFunc());
sb.append(render.sql());
renderLimit(sb);
return new GuiSqlCommandRenderResult(sb.toString(), render.bindParams());
}

protected void renderTable(String renderedTable, StringBuilder sb) {
sb.append("delete from ").append(renderedTable);
}

protected void renderLimit(StringBuilder sb) {
if (!allowMultiModify) {
sb.append(" limit 1");
}
return new GuiSqlCommandRenderResult(sb.toString(), render.bindParams());
}

@Override
Expand Down
Loading