Commit 3d96c9d9 authored by gmantele's avatar gmantele
Browse files

[ADQL] Put column aliases in lower case while translating into SQL

if the alias is not delimited in ADQL.

This commit fixes the GitHub issue #56
parent 0be32298
Loading
Loading
Loading
Loading
+44 −35
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@ import adql.search.ISearchHandler;
 * <p>The resulting object of the {@link ADQLParser} is an object of this class.</p>
 *
 * @author Gr&eacute;gory Mantelet (CDS;ARI)
 * @version 1.4 (04/2017)
 * @version 1.4 (11/2017)
 */
public class ADQLQuery implements ADQLObject {

@@ -324,11 +324,20 @@ public class ADQLQuery implements ADQLObject {
				DBColumn col = null;
				// ...whose the name will be set with the SELECT item's alias:
				if (item.hasAlias()){
					// put the alias in lower case if not written between "":
					/* Note: This aims to avoid unexpected behavior at execution
					 *       time in the DBMS (i.e. the case sensitivity is
					 *       forced for every references to this column alias). */
					String alias = item.getAlias();
					if (!item.isCaseSensitive())
						alias = alias.toLowerCase();

					// create the DBColumn:
					if (operand instanceof ADQLColumn && ((ADQLColumn)operand).getDBLink() != null){
						col = ((ADQLColumn)operand).getDBLink();
						col = col.copy(col.getDBName(), item.getAlias(), col.getTable());
						col = col.copy(col.getDBName(), alias, col.getTable());
					}else
						col = new DefaultDBColumn(item.getAlias(), null);
						col = new DefaultDBColumn(alias, null);
				}
				// ...or whose the name will be the name of the SELECT item:
				else{
+4 −1
Original line number Diff line number Diff line
@@ -443,7 +443,10 @@ public abstract class JDBCTranslator implements ADQLTranslator {
		StringBuffer translation = new StringBuffer(translate(item.getOperand()));
		if (item.hasAlias()){
			translation.append(" AS ");
			appendIdentifier(translation, item.getAlias(), item.isCaseSensitive());
			if (item.isCaseSensitive())
				appendIdentifier(translation, item.getAlias(), true);
			else
				appendIdentifier(translation, item.getAlias().toLowerCase(), true);
		}else{
			translation.append(" AS ");
			appendIdentifier(translation, item.getName(), true);
+20 −1
Original line number Diff line number Diff line
@@ -55,7 +55,6 @@ public class TestSubQueries {
			ADQLParser adqlParser = new ADQLParser(new DBChecker(esaTables));

			ADQLQuery query = adqlParser.parseQuery("SELECT oid FROM table1 as MyAlias WHERE oid IN (SELECT oid2 FROM table2 WHERE oid2 = myAlias.oid)");
			System.out.println((new PostgreSQLTranslator()).translate(query));
			assertEquals("SELECT \"myalias\".\"oid\" AS \"oid\"\nFROM \"public\".\"table1\" AS \"myalias\"\nWHERE \"myalias\".\"oid\" IN (SELECT \"public\".\"table2\".\"oid2\" AS \"oid2\"\nFROM \"public\".\"table2\"\nWHERE \"public\".\"table2\".\"oid2\" = \"myalias\".\"oid\")", (new PostgreSQLTranslator()).translate(query));
		}catch(Exception ex){
			ex.printStackTrace(System.err);
@@ -63,4 +62,24 @@ public class TestSubQueries {
		}
	}

	@Test
	public void testParentRefToMixedCaseColumnAliasInsideSubQueries(){
		try{
			TableSetParser tsParser = new TableSetParser();
			TAPMetadata esaMetaData = tsParser.parse(new File("test/adql/db/subquery_test_tables.xml"));
			ArrayList<DBTable> esaTables = new ArrayList<DBTable>(esaMetaData.getNbTables());
			Iterator<TAPTable> itTables = esaMetaData.getTables();
			while(itTables.hasNext())
				esaTables.add(itTables.next());

			ADQLParser adqlParser = new ADQLParser(new DBChecker(esaTables));

			ADQLQuery query = adqlParser.parseQuery("SELECT t.* FROM (SELECT (ra+ra_error) AS x, (dec+dec_error) AS Y, pmra AS \"ProperMotion\" FROM table2) AS t");
			assertEquals("SELECT \"t\".\"x\" AS \"x\",\"t\".\"y\" AS \"y\",\"t\".\"ProperMotion\" AS \"ProperMotion\"\nFROM (SELECT (\"public\".\"table2\".\"ra\"+\"public\".\"table2\".\"ra_error\") AS \"x\" , (\"public\".\"table2\".\"dec\"+\"public\".\"table2\".\"dec_error\") AS \"y\" , \"public\".\"table2\".\"pmra\" AS \"ProperMotion\"\nFROM \"public\".\"table2\") AS \"t\"", (new PostgreSQLTranslator()).translate(query));
		}catch(Exception ex){
			ex.printStackTrace(System.err);
			fail("No error expected! (see console for more details)");
		}
	}

}