mirror of
https://github.com/1C-Company/v8-code-style.git
synced 2025-02-13 08:23:51 +02:00
#1090 Проверка использования констант в правой и левой части в бинарных операциях в запросах (#1098)
This commit is contained in:
parent
58e921623a
commit
06b0651cdf
@ -60,7 +60,7 @@
|
||||
#### Запросы
|
||||
|
||||
- В качестве правого операнда операции сравнения "ПОДОБНО" указано поле таблицы
|
||||
|
||||
- В запросе в бинарной операции используются константные значения
|
||||
|
||||
#### Права ролей
|
||||
|
||||
|
@ -0,0 +1,58 @@
|
||||
# Using binary operations with constants or parameters in queries.
|
||||
|
||||
Do not generate a template string using calculation or use string concatenation with the query language.
|
||||
|
||||
This requirement is based on some specifics of migrating applications to various database management systems.
|
||||
|
||||
## Noncompliant Code Example
|
||||
|
||||
```bsl
|
||||
SELECT
|
||||
Products.Name AS Name,
|
||||
"My" + "Goods" AS Code
|
||||
FROM
|
||||
Catalogs.Products AS Products;
|
||||
|
||||
SELECT
|
||||
Products.Name AS Name,
|
||||
"My" + &Parameter AS Code
|
||||
FROM
|
||||
Catalogs.Products AS Products;
|
||||
|
||||
SELECT
|
||||
Products.Name AS Name,
|
||||
FieldName AS Code
|
||||
FROM
|
||||
Catalogs.Products AS Products
|
||||
WHERE
|
||||
FieldName LIKE "123" + "%";
|
||||
```
|
||||
|
||||
## Compliant Solution
|
||||
|
||||
```bsl
|
||||
SELECT
|
||||
Products.Name AS Name,
|
||||
"MyGoods" AS Code
|
||||
FROM
|
||||
Catalogs.Products AS Products;
|
||||
|
||||
SELECT
|
||||
Products.Name AS Name,
|
||||
&Parameter AS Code
|
||||
FROM
|
||||
Catalogs.Products AS Products;
|
||||
|
||||
SELECT
|
||||
Products.Name AS Name,
|
||||
FieldName AS Code
|
||||
FROM
|
||||
Catalogs.Products AS Products
|
||||
WHERE
|
||||
FieldName LIKE "123%";
|
||||
```
|
||||
|
||||
## See
|
||||
|
||||
- [Effective query conditions](https://support.1ci.com/hc/en-us/articles/360011121019-Effective-query-conditions)
|
||||
- [Specifics of using LIKE operator in queries](https://support.1ci.com/hc/en-us/articles/360011001500-Specifics-of-using-LIKE-operator-in-queries)
|
@ -0,0 +1,58 @@
|
||||
# В запросе в бинарной операции используются константные значения или параметры.
|
||||
|
||||
Запрещается формировать строку шаблона при помощи вычислений, использовать конкатенацию строк средствами языка запросов.
|
||||
|
||||
Данное требование продиктовано необходимостью переносимости прикладных решений на различные СУБД.
|
||||
|
||||
## Неправильно
|
||||
|
||||
```bsl
|
||||
ВЫБРАТЬ
|
||||
Номенклатура.Наименование КАК Наименование,
|
||||
"Мой" + "Товар" КАК КодАртикул
|
||||
ИЗ
|
||||
Справочник.Номенклатура КАК Номенклатура;
|
||||
|
||||
ВЫБРАТЬ
|
||||
Номенклатура.Наименование КАК Наименование,
|
||||
"Мой" + &Параметр КАК КодАртикул
|
||||
ИЗ
|
||||
Справочник.Номенклатура КАК Номенклатура
|
||||
|
||||
ВЫБРАТЬ
|
||||
Номенклатура.Наименование КАК Наименование,
|
||||
ИмяПоля КАК КодАртикул
|
||||
ИЗ
|
||||
Справочник.Номенклатура КАК Номенклатура
|
||||
ГДЕ
|
||||
ИмяПоля ПОДОБНО "123" + "%";
|
||||
```
|
||||
|
||||
## Правильно
|
||||
|
||||
```bsl
|
||||
ВЫБРАТЬ
|
||||
Номенклатура.Наименование КАК Наименование,
|
||||
"МойТовар" КАК КодАртикул
|
||||
ИЗ
|
||||
Справочник.Номенклатура КАК Номенклатура;
|
||||
|
||||
ВЫБРАТЬ
|
||||
Номенклатура.Наименование КАК Наименование,
|
||||
&Параметр КАК КодАртикул
|
||||
ИЗ
|
||||
Справочник.Номенклатура КАК Номенклатура
|
||||
|
||||
ВЫБРАТЬ
|
||||
Номенклатура.Наименование КАК Наименование,
|
||||
ИмяПоля КАК КодАртикул
|
||||
ИЗ
|
||||
Справочник.Номенклатура КАК Номенклатура
|
||||
ГДЕ
|
||||
ИмяПоля ПОДОБНО "123%";
|
||||
```
|
||||
|
||||
## См.
|
||||
- [Эффективные условия запросов](https://its.1c.ru/db/v8std#content:658:hdoc)
|
||||
- [Особенности использования в запросах оператора ПОДОБНО](https://its.1c.ru/db/v8std#content:726:hdoc)
|
||||
|
@ -50,6 +50,10 @@
|
||||
category="com.e1c.v8codestyle.ql"
|
||||
class="com.e1c.v8codestyle.ql.check.LikeExpressionWithFieldCheck">
|
||||
</check>
|
||||
<check
|
||||
category="com.e1c.v8codestyle.ql"
|
||||
class="com.e1c.v8codestyle.ql.check.ConstantsInBinaryOperationCheck">
|
||||
</check>
|
||||
</extension>
|
||||
|
||||
</plugin>
|
||||
|
@ -0,0 +1,78 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (C) 2022, 1C-Soft LLC and others.
|
||||
*
|
||||
* This program and the accompanying materials are made
|
||||
* available under the terms of the Eclipse Public License 2.0
|
||||
* which is available at https://www.eclipse.org/legal/epl-2.0/
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*
|
||||
* Contributors:
|
||||
* 1C-Soft LLC - initial API and implementation
|
||||
* Denis Maslennikov - issue #1090
|
||||
*******************************************************************************/
|
||||
package com.e1c.v8codestyle.ql.check;
|
||||
|
||||
import static com._1c.g5.v8.dt.ql.model.QlPackage.Literals.ABINARY_OPERATORS_EXPRESSION__RIGHT;
|
||||
|
||||
import org.eclipse.core.runtime.IProgressMonitor;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
|
||||
import com._1c.g5.v8.dt.ql.model.ABinaryOperatorsExpression;
|
||||
import com._1c.g5.v8.dt.ql.model.ALiteralsExpression;
|
||||
import com.e1c.g5.v8.dt.check.CheckComplexity;
|
||||
import com.e1c.g5.v8.dt.check.ICheckParameters;
|
||||
import com.e1c.g5.v8.dt.check.settings.IssueSeverity;
|
||||
import com.e1c.g5.v8.dt.check.settings.IssueType;
|
||||
import com.e1c.g5.v8.dt.ql.check.QlBasicDelegateCheck;
|
||||
import com.e1c.v8codestyle.check.StandardCheckExtension;
|
||||
import com.e1c.v8codestyle.internal.ql.CorePlugin;
|
||||
|
||||
/**
|
||||
* This class checks using two constants in binary operations in queries.
|
||||
*
|
||||
* @author Denis Maslennikov
|
||||
*/
|
||||
public class ConstantsInBinaryOperationCheck
|
||||
extends QlBasicDelegateCheck
|
||||
{
|
||||
|
||||
private static final String CHECK_ID = "ql-constants-in-binary-operation"; //$NON-NLS-1$
|
||||
|
||||
@Override
|
||||
public String getCheckId()
|
||||
{
|
||||
return CHECK_ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configureCheck(CheckConfigurer builder)
|
||||
{
|
||||
builder.title(Messages.ConstantsInBinaryOperationCheck_title)
|
||||
.description(Messages.ConstantsInBinaryOperationCheck_description)
|
||||
.complexity(CheckComplexity.NORMAL)
|
||||
.severity(IssueSeverity.MINOR)
|
||||
.issueType(IssueType.PORTABILITY)
|
||||
.extension(new StandardCheckExtension(getCheckId(), CorePlugin.PLUGIN_ID))
|
||||
.delegate(ABinaryOperatorsExpression.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void checkQlObject(EObject object, QueryOwner owner, IQlResultAcceptor resultAceptor,
|
||||
ICheckParameters parameters, IProgressMonitor monitor)
|
||||
{
|
||||
ABinaryOperatorsExpression binaryExpression = (ABinaryOperatorsExpression)object;
|
||||
if (monitor.isCanceled())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if ((binaryExpression.getLeft() instanceof ALiteralsExpression)
|
||||
&& (binaryExpression.getRight() instanceof ALiteralsExpression))
|
||||
{
|
||||
String message =
|
||||
Messages.ConstantsInBinaryOperationCheck_Using_binary_operations_with_constants_in_queries_is_forbidden;
|
||||
resultAceptor.addIssue(message, binaryExpression, ABINARY_OPERATORS_EXPRESSION__RIGHT);
|
||||
}
|
||||
}
|
||||
}
|
@ -32,6 +32,9 @@ final class Messages
|
||||
public static String CastToMaxNumber_Query_cast_to_number_with_lenth__0__and_max_allowed__1;
|
||||
public static String CastToMaxNumber_Query_cast_to_number_with_precision__0__and_max_allowed__1;
|
||||
public static String CastToMaxNumber_title;
|
||||
public static String ConstantsInBinaryOperationCheck_description;
|
||||
public static String ConstantsInBinaryOperationCheck_title;
|
||||
public static String ConstantsInBinaryOperationCheck_Using_binary_operations_with_constants_in_queries_is_forbidden;
|
||||
public static String IncorrectLikeRightOperandCheck_description;
|
||||
public static String IncorrectLikeRightOperandCheck_The_right_operand_of_the_LIKE_operation_is_table_field;
|
||||
public static String IncorrectLikeRightOperandCheck_title;
|
||||
|
@ -32,6 +32,12 @@ CastToMaxNumber_description = Query cast to max number
|
||||
|
||||
CastToMaxNumber_title = Query cast to max number
|
||||
|
||||
ConstantsInBinaryOperationCheck_Using_binary_operations_with_constants_in_queries_is_forbidden = Using binary operations with constants in queries is forbidden
|
||||
|
||||
ConstantsInBinaryOperationCheck_description = Using binary operations with constants in queries is forbidden
|
||||
|
||||
ConstantsInBinaryOperationCheck_title = Using binary operations with constants in queries is forbidden
|
||||
|
||||
IncorrectLikeRightOperandCheck_The_right_operand_of_the_LIKE_operation_is_table_field = The right operand of the LIKE operation is a table field
|
||||
|
||||
IncorrectLikeRightOperandCheck_description = The right operand of the LIKE comparison operation is a table field
|
||||
|
@ -33,6 +33,12 @@ CastToMaxNumber_description = Выражение к максимальному
|
||||
|
||||
CastToMaxNumber_title = Выражение к максимальному числу в запросе
|
||||
|
||||
ConstantsInBinaryOperationCheck_Using_binary_operations_with_constants_in_queries_is_forbidden = Не допускается использование бинарных операций с константами в запросах
|
||||
|
||||
ConstantsInBinaryOperationCheck_description = Не допускается использование бинарных операций с константами в запросах
|
||||
|
||||
ConstantsInBinaryOperationCheck_title = Не допускается использование бинарных операций с константами в запросах
|
||||
|
||||
IncorrectLikeRightOperandCheck_The_right_operand_of_the_LIKE_comparison_operation_is_query_field = В качестве правого операнда операции сравнения "ПОДОБНО" указано поле таблицы
|
||||
|
||||
IncorrectLikeRightOperandCheck_description = В качестве правого операнда операции сравнения "ПОДОБНО" указано поле таблицы
|
||||
|
@ -0,0 +1,20 @@
|
||||
SELECT
|
||||
Products.Name AS Name,
|
||||
"MyGoods" AS Code
|
||||
FROM
|
||||
Catalogs.Products AS Products;
|
||||
|
||||
SELECT
|
||||
Products.Name AS Name,
|
||||
&Parameter AS Code
|
||||
FROM
|
||||
Catalogs.Products AS Products;
|
||||
|
||||
SELECT
|
||||
Products.Name AS Name,
|
||||
FieldName AS Code
|
||||
FROM
|
||||
Catalogs.Products AS Products
|
||||
WHERE
|
||||
FieldName LIKE "123%";
|
||||
|
@ -0,0 +1,20 @@
|
||||
SELECT
|
||||
Products.Name AS Name,
|
||||
"My" + "Goods" AS Code
|
||||
FROM
|
||||
Catalogs.Products AS Products;
|
||||
|
||||
SELECT
|
||||
Products.Name AS Name,
|
||||
"My" + &Parameter AS Code
|
||||
FROM
|
||||
Catalogs.Products AS Products;
|
||||
|
||||
SELECT
|
||||
Products.Name AS Name,
|
||||
FieldName AS Code
|
||||
FROM
|
||||
Catalogs.Products AS Products
|
||||
WHERE
|
||||
FieldName LIKE "123" + "%";
|
||||
|
@ -0,0 +1,88 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (C) 2022, 1C-Soft LLC and others.
|
||||
*
|
||||
* This program and the accompanying materials are made
|
||||
* available under the terms of the Eclipse Public License 2.0
|
||||
* which is available at https://www.eclipse.org/legal/epl-2.0/
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*
|
||||
* Contributors:
|
||||
* 1C-Soft LLC - initial API and implementation
|
||||
* Denis Maslennikov - issue #1090
|
||||
*******************************************************************************/
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.e1c.v8codestyle.ql.check.itests;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.e1c.v8codestyle.ql.check.ConstantsInBinaryOperationCheck;
|
||||
import com.e1c.v8codestyle.ql.check.itests.TestingQlResultAcceptor.QueryMarker;
|
||||
|
||||
/**
|
||||
* The test for class {@link ConstantsInBinaryOperationCheck}.
|
||||
*
|
||||
* @author Denis Maslennikov
|
||||
*/
|
||||
public class ConstantsInBinaryOperationTest
|
||||
extends AbstractQueryTestBase
|
||||
{
|
||||
private static final String PROJECT_NAME = "QlFullDemo";
|
||||
private static final String FOLDER = FOLDER_RESOURCE + "ql-constants-in-binary-operation/";
|
||||
|
||||
public ConstantsInBinaryOperationTest()
|
||||
{
|
||||
super(ConstantsInBinaryOperationCheck.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTestConfigurationName()
|
||||
{
|
||||
return PROJECT_NAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test correct using binary operation in query.
|
||||
*
|
||||
* @throws Exception the exception
|
||||
*/
|
||||
@Test
|
||||
public void testConstantsInBinaryOperationCompliant() throws Exception
|
||||
{
|
||||
loadQueryAndValidate(FOLDER + "compliant.ql");
|
||||
List<QueryMarker> markers = getQueryMarkers();
|
||||
assertEquals(0, markers.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test using binary operation in query with two constants.
|
||||
*
|
||||
* @throws Exception the exception
|
||||
*/
|
||||
@Test
|
||||
public void testConstantsInBinaryOperationNonCompliant() throws Exception
|
||||
{
|
||||
loadQueryAndValidate(FOLDER + "non-compliant.ql");
|
||||
List<QueryMarker> markers = getQueryMarkers();
|
||||
assertEquals(3, markers.size());
|
||||
|
||||
QueryMarker marker = markers.get(0);
|
||||
assertNotNull(marker.getTarget());
|
||||
assertEquals(3, marker.getLineNumber());
|
||||
|
||||
marker = markers.get(1);
|
||||
assertNotNull(marker.getTarget());
|
||||
assertEquals(9, marker.getLineNumber());
|
||||
|
||||
marker = markers.get(2);
|
||||
assertNotNull(marker.getTarget());
|
||||
assertEquals(19, marker.getLineNumber());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user