Skip to content
Snippets Groups Projects
Commit 09fac540 authored by Grégory Mantelet's avatar Grégory Mantelet
Browse files

[ADQL] In the FROM, set a default alias for each table reference

(i.e. not a subquery).

The idea is to avoid ambiguous error messages coming from the database when
raising an error on a table or column name having different DB and ADQL names.

This commit sets by default an alias on each table reference. This default
alias is the ADQL table name as it is used in the ADQL query.

_This commit fixes the GitHub issue #108 ._
parent e136017d
No related branches found
No related tags found
No related merge requests found
...@@ -2,21 +2,21 @@ package adql.query.from; ...@@ -2,21 +2,21 @@ package adql.query.from;
/* /*
* This file is part of ADQLLibrary. * This file is part of ADQLLibrary.
* *
* ADQLLibrary is free software: you can redistribute it and/or modify * ADQLLibrary is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by * it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* ADQLLibrary is distributed in the hope that it will be useful, * ADQLLibrary is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details. * GNU Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with ADQLLibrary. If not, see <http://www.gnu.org/licenses/>. * along with ADQLLibrary. If not, see <http://www.gnu.org/licenses/>.
* *
* Copyright 2012-2017 - UDS/Centre de Données astronomiques de Strasbourg (CDS), * Copyright 2012-2019 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
* Astronomisches Rechen Institut (ARI) * Astronomisches Rechen Institut (ARI)
*/ */
...@@ -38,9 +38,9 @@ import adql.query.TextPosition; ...@@ -38,9 +38,9 @@ import adql.query.TextPosition;
/** /**
* It represents any item of the clause FROM: a table name or a sub-query.<br /> * It represents any item of the clause FROM: a table name or a sub-query.<br />
* A table reference may have an alias (MUST if it is a sub-query). * A table reference may have an alias (MUST if it is a sub-query).
* *
* @author Gr&eacute;gory Mantelet (CDS;ARI) * @author Gr&eacute;gory Mantelet (CDS;ARI)
* @version 1.4 (09/2017) * @version 1.5 (09/2019)
*/ */
public class ADQLTable implements ADQLObject, FromContent { public class ADQLTable implements ADQLObject, FromContent {
...@@ -56,7 +56,8 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -56,7 +56,8 @@ public class ADQLTable implements ADQLObject, FromContent {
/** A sub-query whose the result will be used as a table. */ /** A sub-query whose the result will be used as a table. */
private ADQLQuery subQuery; private ADQLQuery subQuery;
/** Label of the table reference. */ /** Label of the table reference.
* If not a sub-query, this attribute is set by default to the table name. */
private String alias = null; private String alias = null;
/** Lets specify the case sensitivity of the catalog, schema, table and alias parts. */ /** Lets specify the case sensitivity of the catalog, schema, table and alias parts. */
...@@ -70,7 +71,7 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -70,7 +71,7 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Builds a reference to a table with its name (simple or full ({catalog}.{schema}.{table})). * Builds a reference to a table with its name (simple or full ({catalog}.{schema}.{table})).
* *
* @param table Name of the table (simple or full ({catalog}.{schema}.{table})). * @param table Name of the table (simple or full ({catalog}.{schema}.{table})).
*/ */
public ADQLTable(String table){ public ADQLTable(String table){
...@@ -80,7 +81,7 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -80,7 +81,7 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Builds a reference to a table with its name and the name of its schema. * Builds a reference to a table with its name and the name of its schema.
* *
* @param schema Name of its schema. * @param schema Name of its schema.
* @param table Name of the table. * @param table Name of the table.
*/ */
...@@ -91,7 +92,7 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -91,7 +92,7 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Builds a reference to a table with its name, the name of its schema and the name of its catalog. * Builds a reference to a table with its name, the name of its schema and the name of its catalog.
* *
* @param catalog Name of its catalog. * @param catalog Name of its catalog.
* @param schema Name of its schema. * @param schema Name of its schema.
* @param table Name of the table. * @param table Name of the table.
...@@ -103,9 +104,9 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -103,9 +104,9 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Builds a reference to a sub-query. * Builds a reference to a sub-query.
* *
* @param query Sub-query. * @param query Sub-query.
* *
* @see #setSubQuery(ADQLQuery) * @see #setSubQuery(ADQLQuery)
*/ */
public ADQLTable(ADQLQuery query){ public ADQLTable(ADQLQuery query){
...@@ -114,9 +115,9 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -114,9 +115,9 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Builds an ADQL table by copying the given one. * Builds an ADQL table by copying the given one.
* *
* @param toCopy The ADQLTable to copy. * @param toCopy The ADQLTable to copy.
* *
* @throws Exception If there is an error during the copy. * @throws Exception If there is an error during the copy.
*/ */
public ADQLTable(ADQLTable toCopy) throws Exception{ public ADQLTable(ADQLTable toCopy) throws Exception{
...@@ -133,10 +134,10 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -133,10 +134,10 @@ public class ADQLTable implements ADQLObject, FromContent {
* <p>Lets normalizing any catalog/schema/table name or alias.</p> * <p>Lets normalizing any catalog/schema/table name or alias.</p>
* <p>If the name is surrounded by double-quotes, they are removed * <p>If the name is surrounded by double-quotes, they are removed
* and the corresponding field will be declared as case sensitive.</p> * and the corresponding field will be declared as case sensitive.</p>
* *
* @param name Name to normalize. * @param name Name to normalize.
* @param field The name part to normalize and to get (if normalized, the case sensitivity of the given field will be set). * @param field The name part to normalize and to get (if normalized, the case sensitivity of the given field will be set).
* *
* @return The normalized name. * @return The normalized name.
*/ */
protected String normalizeName(final String name, IdentifierField field){ protected String normalizeName(final String name, IdentifierField field){
...@@ -163,7 +164,7 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -163,7 +164,7 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Gets the position in the original ADQL query string. * Gets the position in the original ADQL query string.
* *
* @return The position of this {@link ADQLTable}. * @return The position of this {@link ADQLTable}.
*/ */
@Override @Override
...@@ -173,7 +174,7 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -173,7 +174,7 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Sets the position at which this {@link ADQLTable} has been found in the original ADQL query string. * Sets the position at which this {@link ADQLTable} has been found in the original ADQL query string.
* *
* @param pos Position of this {@link ADQLTable}. * @param pos Position of this {@link ADQLTable}.
*/ */
@Override @Override
...@@ -183,7 +184,7 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -183,7 +184,7 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Gets the name of the catalog which contains this table. * Gets the name of the catalog which contains this table.
* *
* @return Catalog name. * @return Catalog name.
*/ */
public final String getCatalogName(){ public final String getCatalogName(){
...@@ -192,7 +193,7 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -192,7 +193,7 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Sets the name of the catalog which contains this table. * Sets the name of the catalog which contains this table.
* *
* @param catalog The new name of its catalog. * @param catalog The new name of its catalog.
*/ */
public final void setCatalogName(String catalog){ public final void setCatalogName(String catalog){
...@@ -204,7 +205,7 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -204,7 +205,7 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Gets the name of the schema which contains this table. * Gets the name of the schema which contains this table.
* *
* @return Schema name. * @return Schema name.
*/ */
public final String getSchemaName(){ public final String getSchemaName(){
...@@ -213,7 +214,7 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -213,7 +214,7 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Sets the name of the schema which contains this table. * Sets the name of the schema which contains this table.
* *
* @param schema The new name of its schema. * @param schema The new name of its schema.
*/ */
public final void setSchemaName(String schema){ public final void setSchemaName(String schema){
...@@ -225,7 +226,7 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -225,7 +226,7 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Gets the name of the table. * Gets the name of the table.
* *
* @return Table name. * @return Table name.
*/ */
public final String getTableName(){ public final String getTableName(){
...@@ -235,7 +236,7 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -235,7 +236,7 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Gets the full name of this table (catalogName . schemaName . tableName) * Gets the full name of this table (catalogName . schemaName . tableName)
* by respecting the case sensitivity of each field (if case sensitive double-quotes will surround the concerned fields name). * by respecting the case sensitivity of each field (if case sensitive double-quotes will surround the concerned fields name).
* *
* @return Its full name. * @return Its full name.
*/ */
public final String getFullTableName(){ public final String getFullTableName(){
...@@ -269,21 +270,35 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -269,21 +270,35 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Sets the name of the table. * Sets the name of the table.
* *
* @param newTableName The new name of the table. * @param newTableName The new name of the table.
*/ */
public void setTableName(String newTableName){ public void setTableName(String newTableName){
// Normalise the table name:
final String temp = normalizeName(newTableName, IdentifierField.TABLE); final String temp = normalizeName(newTableName, IdentifierField.TABLE);
// Remove the DB metadata ONLY if the table name is different:
if ((this.table == null && temp != null) || (this.table != null && !this.table.equalsIgnoreCase(temp))) if ((this.table == null && temp != null) || (this.table != null && !this.table.equalsIgnoreCase(temp)))
dbLink = null; dbLink = null;
// Set the table name:
this.table = temp; this.table = temp;
// Ensure no sub-query is set:
if (table != null) if (table != null)
subQuery = null; subQuery = null;
// Finally set this table name as default table alias:
/* Note: this aims to avoid ambiguous error messages coming from the
* database when the DB and the ADQL names of a table are
* different. */
setAlias(this.table);
setCaseSensitive(IdentifierField.ALIAS, isCaseSensitive(IdentifierField.TABLE));
} }
/** /**
* Updates the whole Table according to the given table reference ({catalog}.{schema}.{table}). * Updates the whole Table according to the given table reference ({catalog}.{schema}.{table}).
* *
* @param tableRef The complete table reference ({catalog}.{schema}.{table}). * @param tableRef The complete table reference ({catalog}.{schema}.{table}).
*/ */
public final void setTable(String tableRef){ public final void setTable(String tableRef){
...@@ -302,7 +317,7 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -302,7 +317,7 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Gets the sub-query used as table. * Gets the sub-query used as table.
* *
* @return Sub-query. * @return Sub-query.
*/ */
public final ADQLQuery getSubQuery(){ public final ADQLQuery getSubQuery(){
...@@ -311,9 +326,9 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -311,9 +326,9 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Sets the sub-query to use as table. * Sets the sub-query to use as table.
* *
* @param query Sub-query (MUST NOT BE NULL). * @param query Sub-query (MUST NOT BE NULL).
* *
* @see #refreshDBLink() * @see #refreshDBLink()
*/ */
public final void setSubQuery(final ADQLQuery query){ public final void setSubQuery(final ADQLQuery query){
...@@ -334,7 +349,7 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -334,7 +349,7 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* (Re-)Builds a default description of this ADQL table <u>ONLY IF it is a sub-query AND there is an alias</u>. * (Re-)Builds a default description of this ADQL table <u>ONLY IF it is a sub-query AND there is an alias</u>.
* This method has no effect if this table is not a sub-query or has no alias. * This method has no effect if this table is not a sub-query or has no alias.
* *
* @see DefaultDBTable * @see DefaultDBTable
* @see ADQLQuery#getResultingColumns() * @see ADQLQuery#getResultingColumns()
* @see DBColumn#copy(String, String, DBTable) * @see DBColumn#copy(String, String, DBTable)
...@@ -351,7 +366,7 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -351,7 +366,7 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Tells whether this table reference is a sub-query or a table name/alias. * Tells whether this table reference is a sub-query or a table name/alias.
* *
* @return <i>true</i> if this table is a sub-query, <i>false</i> else. * @return <i>true</i> if this table is a sub-query, <i>false</i> else.
*/ */
public final boolean isSubQuery(){ public final boolean isSubQuery(){
...@@ -360,7 +375,7 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -360,7 +375,7 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Gets the label of this table. * Gets the label of this table.
* *
* @return Table label. * @return Table label.
*/ */
public final String getAlias(){ public final String getAlias(){
...@@ -369,7 +384,7 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -369,7 +384,7 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Tells whether this table has an alias or not. * Tells whether this table has an alias or not.
* *
* @return <i>true</i> if this table has an alias, <i>false</i> otherwise. * @return <i>true</i> if this table has an alias, <i>false</i> otherwise.
*/ */
public final boolean hasAlias(){ public final boolean hasAlias(){
...@@ -378,7 +393,7 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -378,7 +393,7 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Sets the label of this table. * Sets the label of this table.
* *
* @param alias Label to put on this table. * @param alias Label to put on this table.
*/ */
public void setAlias(String alias){ public void setAlias(String alias){
...@@ -387,11 +402,11 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -387,11 +402,11 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Indicates whether the specified field (catalog, schema or table) is case sensitive or not. * Indicates whether the specified field (catalog, schema or table) is case sensitive or not.
* *
* @param field A field (catalog, schema or table). * @param field A field (catalog, schema or table).
* *
* @return <i>true</i> if the specified field is case sensitive, <i>false</i> otherwise. * @return <i>true</i> if the specified field is case sensitive, <i>false</i> otherwise.
* *
* @see IdentifierField * @see IdentifierField
* @see IdentifierField#isCaseSensitive(byte) * @see IdentifierField#isCaseSensitive(byte)
*/ */
...@@ -401,11 +416,11 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -401,11 +416,11 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Sets the case sensitivity of the specified field (catalog, schema or table). * Sets the case sensitivity of the specified field (catalog, schema or table).
* *
* @param field The field for which the case sensitivity must be updated. * @param field The field for which the case sensitivity must be updated.
* *
* @param sensitive <i>true</i> if the specified field must be case sensitive, <i>false</i> otherwise. * @param sensitive <i>true</i> if the specified field must be case sensitive, <i>false</i> otherwise.
* *
* @see IdentifierField * @see IdentifierField
* @see IdentifierField#setCaseSensitive(byte, boolean) * @see IdentifierField#setCaseSensitive(byte, boolean)
*/ */
...@@ -415,9 +430,9 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -415,9 +430,9 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Indicates whether all fields (catalog, schema and table) are case sensitive. * Indicates whether all fields (catalog, schema and table) are case sensitive.
* *
* @return <i>true</i> if all fields are case sensitive, <i>false</i> otherwise. * @return <i>true</i> if all fields are case sensitive, <i>false</i> otherwise.
* *
* @see IdentifierField#isFullCaseSensitive(byte) * @see IdentifierField#isFullCaseSensitive(byte)
*/ */
public final boolean isCaseSensitive(){ public final boolean isCaseSensitive(){
...@@ -426,9 +441,9 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -426,9 +441,9 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Sets the case sensitivity of all fields (catalog, schema and table). * Sets the case sensitivity of all fields (catalog, schema and table).
* *
* @param sensitive <i>true</i> if all fields must be case sensitive, <i>false</i> otherwise. * @param sensitive <i>true</i> if all fields must be case sensitive, <i>false</i> otherwise.
* *
* @see IdentifierField#getFullCaseSensitive(boolean) * @see IdentifierField#getFullCaseSensitive(boolean)
*/ */
public final void setCaseSensitive(boolean sensitive){ public final void setCaseSensitive(boolean sensitive){
...@@ -437,9 +452,9 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -437,9 +452,9 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Gets the whole case sensitivity of this ADQL table. * Gets the whole case sensitivity of this ADQL table.
* *
* @return Its new case sensitivity (one bit per fields). * @return Its new case sensitivity (one bit per fields).
* *
* @see IdentifierField * @see IdentifierField
*/ */
public final byte getCaseSensitive(){ public final byte getCaseSensitive(){
...@@ -448,9 +463,9 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -448,9 +463,9 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Sets the whole case sensitivity of this ADQL table. * Sets the whole case sensitivity of this ADQL table.
* *
* @param sensitivity Its new case sensitivity (one bit per fields). * @param sensitivity Its new case sensitivity (one bit per fields).
* *
* @see IdentifierField * @see IdentifierField
*/ */
public final void setCaseSensitive(final byte sensitivity){ public final void setCaseSensitive(final byte sensitivity){
...@@ -460,7 +475,7 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -460,7 +475,7 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* <p>Gets the corresponding {@link DBTable}.</p> * <p>Gets the corresponding {@link DBTable}.</p>
* <p><i><u>Note:</u> This information is added automatically by {@link DBChecker} when {@link DBChecker#check(adql.query.ADQLQuery)} is called.</i></p> * <p><i><u>Note:</u> This information is added automatically by {@link DBChecker} when {@link DBChecker#check(adql.query.ADQLQuery)} is called.</i></p>
* *
* @return The corresponding {@link DBTable}. * @return The corresponding {@link DBTable}.
*/ */
public final DBTable getDBLink(){ public final DBTable getDBLink(){
...@@ -469,7 +484,7 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -469,7 +484,7 @@ public class ADQLTable implements ADQLObject, FromContent {
/** /**
* Sets the {@link DBTable} corresponding to this {@link ADQLTable}. * Sets the {@link DBTable} corresponding to this {@link ADQLTable}.
* *
* @param dbLink Its corresponding {@link DBTable}. * @param dbLink Its corresponding {@link DBTable}.
*/ */
public final void setDBLink(DBTable dbLink){ public final void setDBLink(DBTable dbLink){
...@@ -529,7 +544,7 @@ public class ADQLTable implements ADQLObject, FromContent { ...@@ -529,7 +544,7 @@ public class ADQLTable implements ADQLObject, FromContent {
@Override @Override
public ADQLIterator adqlIterator(){ public ADQLIterator adqlIterator(){
return new ADQLIterator(){ return new ADQLIterator() {
private boolean subQueryGot = !isSubQuery(); private boolean subQueryGot = !isSubQuery();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment