Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Picking Terminal 1: search by Barcode #10798

Merged
merged 23 commits into from
Mar 11, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import javax.annotation.Nullable;

import de.metas.dao.sql.SqlParamsInliner;
import org.adempiere.ad.dao.IQueryBL;
import org.adempiere.ad.dao.IQueryFilter;
import org.adempiere.ad.dao.IQueryInsertExecutor.QueryInsertExecutorResult;
Expand Down Expand Up @@ -102,6 +103,10 @@ public class TypedSqlQuery<T> extends AbstractTypedQuery<T>
{
private static final Logger log = LogManager.getLogger(TypedSqlQuery.class);

private static final SqlParamsInliner sqlParamsInliner = SqlParamsInliner.builder()
.failOnError(false)
.build();

private final Properties ctx;
private final String tableName;
private String sqlFrom = null;
Expand Down Expand Up @@ -129,7 +134,7 @@ protected TypedSqlQuery(
final Class<T> modelClass,
final String tableName,
final String whereClause,
final String trxName)
@Nullable final String trxName)
{
this.modelClass = modelClass;
this.tableName = InterfaceWrapperHelper.getTableName(modelClass, tableName);
Expand All @@ -144,7 +149,7 @@ public TypedSqlQuery(
final Properties ctx,
final Class<T> modelClass,
final String whereClause,
final String trxName)
@Nullable final String trxName)
{
this(ctx,
modelClass,
Expand Down Expand Up @@ -1383,69 +1388,7 @@ public String toString()
@VisibleForTesting
static String inlineSqlParams(final String sql, final List<Object> params)
{
final int paramsCount = params != null ? params.size() : 0;

final int sqlLength = sql.length();
final StringBuilder sqlFinal = new StringBuilder(sqlLength);

boolean insideQuotes = false;
int nextParamIndex = 0;
for (int i = 0; i < sqlLength; i++)
{
final char ch = sql.charAt(i);

if (ch == '?')
{
if (insideQuotes)
{
sqlFinal.append(ch);
}
else
{
if (nextParamIndex < paramsCount)
{
sqlFinal.append(DB.TO_SQL(params.get(nextParamIndex)));
}
else
{
// error: parameter index is invalid
sqlFinal.append("?missing?");
}

nextParamIndex++;
}
}
else if (ch == '\'')
{
sqlFinal.append(ch);
insideQuotes = !insideQuotes;
}
else
{
sqlFinal.append(ch);
}
}

if (nextParamIndex < paramsCount)
{
sqlFinal.append(" -- Exceeding params: ");
boolean firstExceedingParam = true;
for (int i = nextParamIndex; i < paramsCount; i++)
{
if (firstExceedingParam)
{
firstExceedingParam = false;
}
else
{
sqlFinal.append(", ");
}

sqlFinal.append(DB.TO_SQL(params.get(i)));
}
}

return sqlFinal.toString();
return sqlParamsInliner.inline(sql, params);
}

// metas
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1832,7 +1832,7 @@ public boolean isRemoteProcess()
* @param param
* @return parameter as SQL code
*/
public String TO_SQL(final Object param)
public String TO_SQL(@Nullable final Object param)
{
// TODO: check and refactor together with buildSqlList(...)
if (param == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* #%L
* de.metas.adempiere.adempiere.base
* %%
* Copyright (C) 2021 metas GmbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/

package de.metas.dao.sql;

import lombok.Builder;
import lombok.NonNull;
import org.adempiere.exceptions.AdempiereException;
import org.compiere.util.DB;

import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.List;

public class SqlParamsInliner
{
private final boolean failOnError;

@Builder
private SqlParamsInliner(
final boolean failOnError)
{
this.failOnError = failOnError;
}

public String inline(@NonNull final String sql, @Nullable final Object... params)
{
final List<Object> paramsList = params != null && params.length > 0 ? Arrays.asList(params) : null;
return inline(sql, paramsList);
}

public String inline(@NonNull final String sql, @Nullable final List<Object> params)
{
try
{
return inline0(sql, params);
}
catch (final Exception ex)
{
//noinspection ConstantConditions
throw AdempiereException.wrapIfNeeded(ex)
.setParameter("sql", sql)
.setParameter("sqlParams", params)
.appendParametersToMessage();
}
}

private String inline0(@NonNull final String sql, @Nullable final List<Object> params)
{
final int paramsCount = params != null ? params.size() : 0;

final int sqlLength = sql.length();
final StringBuilder sqlFinal = new StringBuilder(sqlLength);

boolean insideQuotes = false;
int nextParamIndex = 0;
for (int i = 0; i < sqlLength; i++)
{
final char ch = sql.charAt(i);

if (ch == '?')
{
if (insideQuotes)
{
sqlFinal.append(ch);
}
else
{
if (params != null && nextParamIndex < paramsCount)
{
sqlFinal.append(DB.TO_SQL(params.get(nextParamIndex)));
}
else
{
// error: parameter index is invalid
appendMissingParam(sqlFinal, nextParamIndex);
}

nextParamIndex++;
}
}
else if (ch == '\'')
{
sqlFinal.append(ch);
insideQuotes = !insideQuotes;
}
else
{
sqlFinal.append(ch);
}
}

if (params != null && nextParamIndex < paramsCount)
{
appendExceedingParams(sqlFinal, nextParamIndex, paramsCount, params);
}

return sqlFinal.toString();
}

private void appendMissingParam(
final StringBuilder sql,
final int missingParamIndexZeroBased)
{
final int missingParamIndexOneBased = missingParamIndexZeroBased + 1;

if (failOnError)
{
throw new AdempiereException("Missing SQL parameter with index=" + missingParamIndexOneBased);
}

sql.append("?missing").append(missingParamIndexOneBased).append("?");
}

private void appendExceedingParams(
final StringBuilder sql,
final int firstExceedingParamIndex,
final int paramsCount,
final List<Object> allParams)
{
if (failOnError)
{
throw new AdempiereException("Got more SQL params than needed: " + allParams.subList(firstExceedingParamIndex, allParams.size()));
}

sql.append(" -- Exceeding params: ");
boolean firstExceedingParam = true;
for (int i = firstExceedingParamIndex; i < paramsCount; i++)
{
if (firstExceedingParam)
{
firstExceedingParam = false;
}
else
{
sql.append(", ");
}

sql.append(DB.TO_SQL(allParams.get(i)));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ public static class WebuiViewToOpen
@JsonCreator
private WebuiViewToOpen(
@JsonProperty("viewId") @NonNull final String viewId,
@JsonProperty("profileId") final String profileId,
@JsonProperty("profileId") @Nullable final String profileId,
@JsonProperty("target") @NonNull final ViewOpenTarget target)
{
this.viewId = viewId;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* #%L
* de.metas.adempiere.adempiere.base
* %%
* Copyright (C) 2021 metas GmbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/

package de.metas.dao.sql;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.*;

class SqlParamsInlinerTest
{
@Test
void standardCase()
{
final SqlParamsInliner sqlParamsInliner = SqlParamsInliner.builder().failOnError(false).build();

assertThat(sqlParamsInliner.inline("SELECT * FROM MD_Stock WHERE ((M_Product_ID = ?) AND (M_Warehouse_ID=?) AND (1=1))", 1000111, 540008))
.isEqualTo("SELECT * FROM MD_Stock WHERE ((M_Product_ID = 1000111) AND (M_Warehouse_ID=540008) AND (1=1))");
}

@Nested
class missingParams
{
@Test
void doNotFailOnError()
{
final SqlParamsInliner sqlParamsInliner = SqlParamsInliner.builder().failOnError(false).build();

assertThat(sqlParamsInliner.inline("SELECT * FROM Table where a=? and b=? and c=?", 1, "str"))
.isEqualTo("SELECT * FROM Table where a=1 and b='str' and c=?missing3?");
}

@Test
void failOnError()
{
final SqlParamsInliner sqlParamsInliner = SqlParamsInliner.builder().failOnError(true).build();

assertThatThrownBy(() -> sqlParamsInliner.inline("SELECT * FROM Table where a=? and b=? and c=?", 1, "str"))
.hasMessageStartingWith("Missing SQL parameter with index=3");
}
}

@Nested
class exceeedingParams
{
@Test
void doNotFailOnError()
{
final SqlParamsInliner sqlParamsInliner = SqlParamsInliner.builder().failOnError(false).build();

assertThat(sqlParamsInliner.inline("SELECT * FROM Table where c=?", 1, "str", 3))
.isEqualTo("SELECT * FROM Table where c=1 -- Exceeding params: 'str', 3");
}

@Test
void failOnError()
{
final SqlParamsInliner sqlParamsInliner = SqlParamsInliner.builder().failOnError(true).build();

assertThatThrownBy(() -> sqlParamsInliner.inline("SELECT * FROM Table where c=?", 1, "str", 3))
.hasMessageStartingWith("Got more SQL params than needed: [str, 3]");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void standardCase()
public void missingParams()
{
assertThatInliningSqlParams("SELECT * FROM Table where a=? and b=? and c=?", 1, "str")
.isEqualTo("SELECT * FROM Table where a=1 and b='str' and c=?missing?");
.isEqualTo("SELECT * FROM Table where a=1 and b='str' and c=?missing3?");
}

@Test
Expand Down