Skip to content

BSL149 — Assigning aliases to selected fields in a query

Summary

Assigning aliases to selected fields in a query

Identifiers

Field Value
Rule code BSL149
Compatible alias AssignAliasFieldsInQuery
Severity INFORMATION
Enabled by default Yes
Implemented Yes
Tags convention, query

Behavior

  • The public identifier BSL149 and alias AssignAliasFieldsInQuery are stable.
  • The rule reports the cases documented on this page.
  • Suppressions and project configuration are applied before publication.
  • The rule requires neither an external analyzer nor network access.

Configuration and suppression

BSL### is the primary stable identifier. The compatible alias is accepted in select, ignore, and compatible block suppression comments.

[tool.onec-hbk-bsl]
select = ["BSL149"]
ignore = ["AssignAliasFieldsInQuery"]

All three suppression families support both a current line and a range. When an opening comment follows code, it affects only that line. Use any one form:

  • noqa:
Value = "example";  // noqa: BSL149
  • bsl-disable:
Value = "example";  // bsl-disable: BSL149
  • compatible BSLLS form:
Value = "example";  // BSLLS:AssignAliasFieldsInQuery-off

When the same opening comment is on a line by itself, it starts a range. Close it with the matching marker from the same family:

// noqa: BSL149
// code without this diagnostic
// noqa-enable: BSL149

// bsl-disable: BSL149
// code without this diagnostic
// bsl-enable: BSL149

// BSLLS:AssignAliasFieldsInQuery-off
// code without this diagnostic
// BSLLS:AssignAliasFieldsInQuery-on

To disable the rule until the end of the file, omit the closing noqa-enable, bsl-enable, or BSLLS:…-on marker.

Opening and closing markers must belong to the same family.

Description

It is recommended to specify optional query constructs, first, to explicitly assign aliases to fields in order to increase the clarity of the query text and the "steadiness" of the code that uses it. For example, if the algorithm uses a query with a field declared as

CashBox.Currency
when changing the name of the attribute, you will also need to change the code that calls the selection from the query result by the name of the Currency property. If the field is declared as

CashBox.Currency As Currency
then changing the attribute name will only change the request text.

You should be especially careful about automatically assigned aliases for fields - attributes of other fields, such as "... CashBox.Currency.Name...". In the above example, the field will be automatically getting aliased CurrencyName, but not Name.

Be sure to include the AS keyword before the alias of the source field.

The aliases of tables and fields from secondary queries from "UNION" are not checked by the diagnostics.

Examples

    Query = New Query;
Query.Text =
"SELECT
|   Currencies.Ref, // Incorrectly
|   Currencies.Ref AS AliasFieldsRef, // Correctly
|   Currencies.Code Code // Incorrectly
|FROM
|   Catalog.Currencies AS Currencies // Ignored
|
|UNION ALL
|
|SELECT
|   Currencies.Ref, // Ignored
|   Currencies.Ref, // Ignored
|   Currencies.Code // Ignored
|FROM
|   Catalog.Currencies AS Currencies // Ignored
|;
|
|////////////////////////////////////////////////////////////////////////////////
|SELECT
|   Currencies.Ref, // Incorrectly
|   Currencies.Ref AS AliasFieldsRef, // Correctly
|   Currencies.Code Code // Incorrectly
|FROM
|   Catalog.Currencies AS Currencies // Ignored
|
|UNION ALL
|
|SELECT
|   Currencies.Ref, // Ignored
|   Currencies.Ref, // Ignored
|   Currencies.Code // Ignored
|FROM
|   Catalog.Currencies AS Currencies"; // Ignored

Query1 = New Query;
Query1.Text =
"SELECT
|   NestedRequest.Ref AS Ref // Correctly
|FROM
|   (SELECT
|       Currencies.Ref // Incorrectly
|   FROM
|       Catalog.Currencies AS Currencies) AS NestedRequest"; // Ignored

Sources

Source: Making query text