Skip to content
Permalink
Browse files Browse the repository at this point in the history
request #27166: Naming a field "id" breaks the report
Field names are now prefixed in the SQL query in order to avoid conflict
with the static SQL columns we always retrieve.

Change-Id: I7eaaf95e24f52f6e52647d679b5058bc09139b5f
  • Loading branch information
LeSuisse committed Jun 9, 2022
1 parent 316405f commit b91bcd5
Show file tree
Hide file tree
Showing 28 changed files with 76 additions and 98 deletions.
Expand Up @@ -285,8 +285,9 @@ public function getQueryFrom()
{
}

public function getQuerySelect()
public function getQuerySelect(): string
{
return '';
}

public function getRESTValue(PFUser $user, Tracker_Artifact_Changeset $changeset)
Expand Down
Expand Up @@ -123,7 +123,7 @@ public function getCriteriaWhere($criteria)
return '';
}

public function getQuerySelect()
public function getQuerySelect(): string
{
return '';
}
Expand Down
Expand Up @@ -41,6 +41,8 @@
// phpcs:ignore PSR1.Classes.ClassDeclaration.MissingNamespace, Squiz.Classes.ValidClassName.NotCamelCaps
abstract class Tracker_FormElement_Field extends Tracker_FormElement implements Tracker_Report_Field, Tracker_FormElement_IAcceptFieldVisitor
{
public const PREFIX_NAME_SQL_COLUMN = 'user_defined_';

protected $has_errors = false;

/**
Expand Down Expand Up @@ -203,15 +205,19 @@ public function saveCriteriaValueFromXML(Tracker_Report_Criteria $criteria)
$this->updateCriteriaValue($criteria, $value);
}

final public function getQuerySelectName(): string
{
return \Tuleap\DB\DBFactory::getMainTuleapDBConnection()->getDB()->escapeIdentifier(self::PREFIX_NAME_SQL_COLUMN . $this->name);
}

/**
* Get the "select" statement to retrieve field values
* @return string
* @see getQueryFrom
*/
public function getQuerySelect()
public function getQuerySelect(): string
{
$R = 'R_' . $this->id;
return "$R.value_id AS `" . $this->name . "`";
return "$R.value_id AS " . $this->getQuerySelectName();
}

/**
Expand All @@ -229,15 +235,15 @@ public function getQueryFrom()
/**
* Get the "order by" statement to retrieve field values
*/
public function getQueryOrderby()
public function getQueryOrderby(): string
{
return '`' . $this->name . '`';
return $this->getQuerySelectName();
}

/**
* Get the "group by" statement to retrieve field values
*/
public function getQueryGroupby()
public function getQueryGroupby(): string
{
$R = 'R_' . $this->id;
return "$R.value_id";
Expand Down
Expand Up @@ -70,9 +70,8 @@ public function fetchAdvancedCriteriaValue($criteria)
/**
* Get the "group by" statement to retrieve field values
*/
public function getQueryGroupby()
public function getQueryGroupby(): string
{
$R1 = 'R1_' . $this->id;
$R2 = 'R2_' . $this->id;
return "$R2.value";
}
Expand Down
Expand Up @@ -38,9 +38,9 @@ public function getCriteriaWhere($criteria)
return '';
}

public function getQuerySelect()
public function getQuerySelect(): string
{
return "a.id AS `" . $this->name . "`";
return "a.id AS " . $this->getQuerySelectName();
}

public function getQueryFrom()
Expand All @@ -51,7 +51,7 @@ public function getQueryFrom()
/**
* Get the "group by" statement to retrieve field values
*/
public function getQueryGroupby()
public function getQueryGroupby(): string
{
return "a.id";
}
Expand Down
Expand Up @@ -418,7 +418,7 @@ public function getCriteriaWhere($criteria)
return '';
}

public function getQuerySelect()
public function getQuerySelect(): string
{
return '';
}
Expand Down
Expand Up @@ -447,8 +447,9 @@ public function getCriteriaWhere($criteria)
{
}

public function getQuerySelect()
public function getQuerySelect(): string
{
return '';
}

public function getQueryFrom()
Expand Down
Expand Up @@ -627,7 +627,7 @@ public function getCriteriaWhere($criteria)
return '';
}

public function getQuerySelect()
public function getQuerySelect(): string
{
return '';
}
Expand Down
Expand Up @@ -139,7 +139,7 @@ public function getCriteriaWhere($criteria)
return '';
}

public function getQuerySelect()
public function getQuerySelect(): string
{
return '';
}
Expand Down
Expand Up @@ -413,11 +413,10 @@ public function getCriteriaWhere($criteria)
return '';
}

public function getQuerySelect()
public function getQuerySelect(): string
{
$R1 = 'R1_' . $this->id;
$R2 = 'R2_' . $this->id;
return "$R2.value AS `" . $this->name . "`";
return "$R2.value AS " . $this->getQuerySelectName();
}

public function getQueryFrom()
Expand All @@ -432,9 +431,8 @@ public function getQueryFrom()
/**
* Get the "group by" statement to retrieve field values
*/
public function getQueryGroupby()
public function getQueryGroupby(): string
{
$R1 = 'R1_' . $this->id;
$R2 = 'R2_' . $this->id;
return "$R2.value";
}
Expand Down
Expand Up @@ -69,7 +69,7 @@ public function getCriteriaWhere($criteria)
return '';
}

public function getQuerySelect()
public function getQuerySelect(): string
{
return '';
}
Expand Down
Expand Up @@ -67,9 +67,9 @@ public function getCriteriaWhere($criteria)
return '';
}

public function getQuerySelect()
public function getQuerySelect(): string
{
return "c.submitted_by AS `" . $this->name . "`";
return "c.submitted_by AS " . $this->getQuerySelectName();
}

public function getQueryFrom()
Expand All @@ -84,16 +84,11 @@ public function getQueryFromAggregate()
return " LEFT JOIN user AS $R2 ON ($R2.user_id = c.submitted_by ) ";
}

public function getQueryGroupby()
public function getQueryGroupby(): string
{
return '';
}

public function getQueryOrderby()
{
return $this->name;
}

public static function getFactoryLabel()
{
return dgettext('tuleap-tracker', 'Last Updated By');
Expand Down
Expand Up @@ -68,10 +68,10 @@ public function getCriteriaWhere($criteria)
return '';
}

public function getQuerySelect()
public function getQuerySelect(): string
{
//Last update date is stored in the changeset (the date of the changeset)
return "c.submitted_on AS `" . $this->name . "`";
return "c.submitted_on AS " . $this->getQuerySelectName();
}

public function getQueryFrom()
Expand All @@ -83,7 +83,7 @@ public function getQueryFrom()
/**
* Get the "group by" statement to retrieve field values
*/
public function getQueryGroupby()
public function getQueryGroupby(): string
{
//Last update date is stored in the changeset (the date of the changeset)
return 'c.submitted_on';
Expand Down
Expand Up @@ -162,9 +162,8 @@ public function getCriteriaWhere($criteria)
*
* @see getQueryFrom
*
* @return string
*/
public function getQuerySelect()
public function getQuerySelect(): string
{
return $this->getBind()->getQuerySelect();
}
Expand Down Expand Up @@ -205,15 +204,15 @@ public function getQueryFromWithDecorator()
/**
* Get the "order by" statement to retrieve field values
*/
public function getQueryOrderby()
public function getQueryOrderby(): string
{
return $this->getBind()->getQueryOrderby();
}

/**
* Get the "group by" statement to retrieve field values
*/
public function getQueryGroupby()
public function getQueryGroupby(): string
{
return $this->getBind()->getQueryGroupby();
}
Expand Down
Expand Up @@ -326,10 +326,9 @@ protected function getIdsToSearch($criteria_value)

/**
* Get the "select" statement to retrieve field values
* @return string
* @see getQueryFrom
*/
abstract public function getQuerySelect();
abstract public function getQuerySelect(): string;

/**
* Get the "select" statement to retrieve field values with their decorator if they exist
Expand Down Expand Up @@ -393,12 +392,12 @@ abstract public function getBindtableSqlFragment();
/**
* Get the "order by" statement to retrieve field values
*/
abstract public function getQueryOrderby();
abstract public function getQueryOrderby(): string;

/**
* Get the "group by" statement to retrieve field values
*/
abstract public function getQueryGroupby();
abstract public function getQueryGroupby(): string;

public function getSelectOptionStyles($value_id)
{
Expand Down
Expand Up @@ -150,10 +150,9 @@ public function getCriteriaWhere($criteria)

/**
* Get the "select" statement to retrieve field values
* @return string
* @see getQueryFrom
*/
public function getQuerySelect()
public function getQuerySelect(): string
{
return '';
}
Expand Down Expand Up @@ -205,15 +204,15 @@ public function getBindtableSqlFragment()
/**
* Get the "order by" statement to retrieve field values
*/
public function getQueryOrderby()
public function getQueryOrderby(): string
{
return '';
}

/**
* Get the "group by" statement to retrieve field values
*/
public function getQueryGroupby()
public function getQueryGroupby(): string
{
return '';
}
Expand Down
Expand Up @@ -274,14 +274,12 @@ protected function getIdsToSearch($criteria_value)

/**
* Get the "select" statement to retrieve field values
* @return string
* @see getQueryFrom
*/
public function getQuerySelect()
public function getQuerySelect(): string
{
$R1 = 'R1_' . $this->field->id;
$R2 = 'R2_' . $this->field->id;
return "$R2.id AS `" . $this->field->name . "`";
return "$R2.id AS " . $this->field->getQuerySelectName();
}

/**
Expand Down Expand Up @@ -334,19 +332,17 @@ public function getQueryFromWithDecorator($changesetvalue_table = 'tracker_chang
/**
* Get the "order by" statement to retrieve field values
*/
public function getQueryOrderby()
public function getQueryOrderby(): string
{
$R1 = 'R1_' . $this->field->id;
$R2 = 'R2_' . $this->field->id;
return $this->is_rank_alpha ? "$R2.label" : "$R2.rank";
}

/**
* Get the "group by" statement to retrieve field values
*/
public function getQueryGroupby()
public function getQueryGroupby(): string
{
$R1 = 'R1_' . $this->field->id;
$R2 = 'R2_' . $this->field->id;
return "$R2.id";
}
Expand Down
Expand Up @@ -225,15 +225,12 @@ public function getFieldData($submitted_value, $is_multiple)

/**
* Get the "select" statement to retrieve field values
* @return string
* @see getQueryFrom
*/
public function getQuerySelect()
public function getQuerySelect(): string
{
$R1 = 'R1_' . $this->field->id;
$R2 = 'R2_' . $this->field->id;
$R3 = 'R3_' . $this->field->id;
return "$R2.id AS `" . $this->field->name . "`";
return "$R2.id AS " . $this->field->getQuerySelectName();
}

/**
Expand Down Expand Up @@ -265,20 +262,17 @@ public function getQueryFrom($changesetvalue_table = 'tracker_changeset_value_li
/**
* Get the "order by" statement to retrieve field values
*/
public function getQueryOrderby()
public function getQueryOrderby(): string
{
$uh = UserHelper::instance();
$R1 = 'R1_' . $this->field->id;
$R2 = 'R2_' . $this->field->id;
return "$R2.ugroup_id";
}

/**
* Get the "group by" statement to retrieve field values
*/
public function getQueryGroupby()
public function getQueryGroupby(): string
{
$R1 = 'R1_' . $this->field->id;
$R2 = 'R2_' . $this->field->id;
return "$R2.id";
}
Expand Down

0 comments on commit b91bcd5

Please sign in to comment.