| Summary: | Query wizard confuses columns with the same name in different tables | ||
|---|---|---|---|
| Product: | LibreOffice | Reporter: | Hossein <hossein> |
| Component: | Base | Assignee: | Not Assigned <libreoffice-bugs> |
| Status: | NEW --- | ||
| Severity: | normal | CC: | mentoring |
| Priority: | medium | Keywords: | difficultyMedium, easyHack, skillJava, skillSql |
| Version: | 24.8.0.0 alpha0+ | ||
| Hardware: | All | ||
| OS: | All | ||
| See Also: | https://bugs.documentfoundation.org/show_bug.cgi?id=67664 | ||
| Whiteboard: | |||
| Crash report or crash signature: | Regression By: | ||
|
Description
Hossein
2024-05-17 08:48:41 UTC
Here is the suggested path to fix the issues: If you open the created query using "Edit in SQL View...", you will see this: SELECT "Table1"."a" "a", "Table2"."a" "a" FROM "Table1", "Table2" ORDER BY "a" ASC, "a" ASC If you choose a and b for the alias of the two fields, it becomes: SELECT "Table1"."a" "a", "Table2"."a" "b" FROM "Table1" "Table1", "Table2" "Table2" ORDER BY "a", "a" which is wrong. If you change it to the below SQL query, it should work correctly: SELECT "Table1"."a", "Table2"."a" FROM "Table1", "Table2" ORDER BY "Table1"."a", "Table2"."a" Even in this case, the title of the both columns are "a", and does not show anything about the table name. This is an issue. Also, this one should work: SELECT "Table1"."a" "a", "Table2"."a" "b" FROM "Table1" "Table1", "Table2" "Table2" ORDER BY "a", "b" An implementation path could be making sure that the correct alias is used, and also to avoid using duplicate aliases by prefixing table names when needed. In this way, the column display would be also fixed. Code pointers:
The relevant source code is inside wizards/com/sun/star/wizards/db/ and wizards/com/sun/star/wizards/query/ folders, and specifically the Java file wizards/com/sun/star/wizards/db/SQLQueryComposer.java contains most of relevant code.
One should start from getSelectClause() method, and go step by step to see how query is being built:
public String getSelectClause(boolean _baddAliasFieldNames) throws SQLException
{
....
}
There are methods for Sortingcriteria, GroupByColumns, FromClause and AliasDisplayName. To fix the alias used in sorting, look into appendSortingcriteria().
The query summary is created in wizards/com/sun/star/wizards/query/QuerySummary.java, and it gives you hints where to look. For example, when getting this in summary:
Sorting order: a (ASC), a (ASC)
The code can be found by searching for "Sorting order:" which leads to RID_QUERY_51, that is found in wizards/com/sun/star/wizards/query/QuerySummary.java:60. The relevant code is directly inside setSummaryString() method:
sSortingFraction = combinePartString("RID_QUERY_51", getSortFieldNames(), "RID_QUERY_52", "RID_QUERY_93", new String[]
{
"<FIELDNAME>", "<SORTMODE>"
}) + sReturnChar;
On the other hand, combineFieldNameFraction() creates this line, which is more complete:
"Fields in the Query: a (Table1.a), a (Table2.a)"
One can look into the above method, and create a similar method for sorting.
|