Skip to content

Commit

Permalink
fix optimize_expression
Browse files Browse the repository at this point in the history
* allow non-numeric PK expression optimization
* do not assume PK is the very first field of the list: use
  layer.primaryKeyAttributes() instead
* fixes 3liz#71
* related to 3liz/lizmap-web-client#4534
  • Loading branch information
maxencelaurent committed Jun 23, 2024
1 parent 8cda69e commit 6701a06
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions atlasprint/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,17 +312,19 @@ def optimize_expression(layer, expression):
logger.info("'$id' not found in the expression, returning the input expression.")
return expression

# extract indexes of primary keys
primary_keys = layer.primaryKeyAttributes()
if len(primary_keys) != 1:
logger.info("Primary keys are not defined in the layer '{}'.".format(layer.id()))
return expression

field = layer.fields().at(0)
if not field.isNumeric():
logger.info("The field '{}' is not numeric in layer '{}'.".format(field.name(), layer.id()))
return expression

expression = expression.replace('$id', '"{}"'.format(field.name()))
logger.info('$id has been replaced by "{}" in layer "{}"'.format(field.name(), layer.id()))
# extract primary key from fields list
pk_index = primary_keys[0];
pk_field = layer.fields().at(pk_index)

# replace `$id` with effective PK name
expression = expression.replace('$id', '"{}"'.format(pk_field.name()))
logger.info('$id has been replaced by "{}" in layer "{}"'.format(pk_field.name(), layer.id()))

return expression

0 comments on commit 6701a06

Please sign in to comment.