Commit 1a3bd2be authored by gmantele's avatar gmantele
Browse files

[ALL] Add SearchTableApi in ADQLLib, and make functions returning ArrayList

and HashMap more generic by returning resp. a List and Map instead.
parent c1422155
Loading
Loading
Loading
Loading
+13 −11
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Stack;

@@ -98,12 +99,12 @@ import adql.search.SimpleSearchHandler;
 * </i></p>
 * 
 * @author Gr&eacute;gory Mantelet (CDS;ARI)
 * @version 1.4 (04/2017)
 * @version 1.4 (09/2017)
 */
public class DBChecker implements QueryChecker {

	/** List of all available tables ({@link DBTable}). */
	protected SearchTableList lstTables;
	protected SearchTableApi lstTables;

	/** <p>List of all allowed geometrical functions (i.e. CONTAINS, REGION, POINT, COORD2, ...).</p>
	 * <p>
@@ -337,8 +338,9 @@ public class DBChecker implements QueryChecker {
	 * <p>Sets the list of all available tables.</p>
	 * 
	 * <p><i><u>Note:</u>
	 * 	Only if the given collection is NOT an instance of {@link SearchTableList},
	 * 	the collection will be copied inside a new {@link SearchTableList}, otherwise it is used as provided.
	 * 	Only if the given collection is NOT an implementation of
	 * 	{@link SearchTableApi}, the collection will be copied inside a new
	 * 	{@link SearchTableList}, otherwise it is used as provided.
	 * </i></p>
	 * 
	 * @param tables	List of {@link DBTable}s.
@@ -346,8 +348,8 @@ public class DBChecker implements QueryChecker {
	public final void setTables(final Collection<? extends DBTable> tables){
		if (tables == null)
			lstTables = new SearchTableList();
		else if (tables instanceof SearchTableList)
			lstTables = (SearchTableList)tables;
		else if (tables instanceof SearchTableApi)
			lstTables = (SearchTableApi)tables;
		else
			lstTables = new SearchTableList(tables);
	}
@@ -546,7 +548,7 @@ public class DBChecker implements QueryChecker {

				// first, try to resolve the table by table alias:
				if (table.getTableName() != null && table.getSchemaName() == null){
					ArrayList<ADQLTable> tables = query.getFrom().getTablesByAlias(table.getTableName(), table.isCaseSensitive(IdentifierField.TABLE));
					List<ADQLTable> tables = query.getFrom().getTablesByAlias(table.getTableName(), table.isCaseSensitive(IdentifierField.TABLE));
					if (tables.size() == 1)
						dbTable = tables.get(0).getDBLink();
				}
@@ -575,7 +577,7 @@ public class DBChecker implements QueryChecker {
	 * @throws ParseException	An {@link UnresolvedTableException} if the given table can't be resolved.
	 */
	protected DBTable resolveTable(final ADQLTable table) throws ParseException{
		ArrayList<DBTable> tables = lstTables.search(table);
		List<DBTable> tables = lstTables.search(table);

		// good if only one table has been found:
		if (tables.size() == 1)
@@ -691,7 +693,7 @@ public class DBChecker implements QueryChecker {
	 * 							or an {@link UnresolvedTableException} if its table reference can't be resolved.
	 */
	protected DBColumn resolveColumn(final ADQLColumn column, final SearchColumnList dbColumns, Stack<SearchColumnList> fathersList) throws ParseException{
		ArrayList<DBColumn> foundColumns = dbColumns.search(column);
		List<DBColumn> foundColumns = dbColumns.search(column);

		// good if only one column has been found:
		if (foundColumns.size() == 1)
@@ -737,7 +739,7 @@ public class DBChecker implements QueryChecker {
		 * So, try resolving the name as an alias.
		 * If it fails, perform the normal column resolution.*/
		if (col.getTableName() == null){
			ArrayList<SelectItem> founds = select.searchByAlias(col.getColumnName(), col.isCaseSensitive(IdentifierField.COLUMN));
			List<SelectItem> founds = select.searchByAlias(col.getColumnName(), col.isCaseSensitive(IdentifierField.COLUMN));
			if (founds.size() == 1)
				return null;
			else if (founds.size() > 1)
@@ -779,7 +781,7 @@ public class DBChecker implements QueryChecker {
			col.setPosition(colRef.getPosition());

			// search among the select_item aliases:
			ArrayList<SelectItem> founds = select.searchByAlias(colRef.getColumnName(), colRef.isCaseSensitive());
			List<SelectItem> founds = select.searchByAlias(colRef.getColumnName(), colRef.isCaseSensitive());
			if (founds.size() == 1)
				return null;
			else if (founds.size() > 1)
+13 −12
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@ package adql.db;
 * You should have received a copy of the GNU Lesser General Public License
 * along with ADQLLibrary.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Copyright 2012-2015 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
 * Copyright 2012-2017 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
 *                       Astronomisches Rechen Institut (ARI)
 */

@@ -24,6 +24,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

@@ -46,7 +47,7 @@ import cds.utils.TextualSearchList;
 * </i></p>
 * 
 * @author Gr&eacute;gory Mantelet (CDS;ARI)
 * @version 1.4 (08/2015)
 * @version 1.4 (09/2017)
 */
public class SearchColumnList extends TextualSearchList<DBColumn> {
	private static final long serialVersionUID = 1L;
@@ -55,10 +56,10 @@ public class SearchColumnList extends TextualSearchList<DBColumn> {
	private boolean distinct = false;

	/** Case-sensitive dictionary of table aliases. (tableAlias <-> TableName) */
	private final HashMap<String,String> tableAliases = new HashMap<String,String>();
	private final Map<String,String> tableAliases = new HashMap<String,String>();

	/** Case-insensitive dictionary of table aliases. (tablealias <-> List&lt;TableName&gt;) */
	private final HashMap<String,ArrayList<String>> mapAliases = new HashMap<String,ArrayList<String>>();
	private final Map<String,List<String>> mapAliases = new HashMap<String,List<String>>();

	/* ************ */
	/* CONSTRUCTORS */
@@ -122,7 +123,7 @@ public class SearchColumnList extends TextualSearchList<DBColumn> {
		if (tableAlias != null && tableName != null){
			tableAliases.put(tableAlias, tableName);

			ArrayList<String> aliases = mapAliases.get(tableAlias.toLowerCase());
			List<String> aliases = mapAliases.get(tableAlias.toLowerCase());
			if (aliases == null){
				aliases = new ArrayList<String>();
				mapAliases.put(tableAlias.toLowerCase(), aliases);
@@ -139,7 +140,7 @@ public class SearchColumnList extends TextualSearchList<DBColumn> {
	public final void removeTableAlias(final String tableAlias){
		tableAliases.remove(tableAlias);

		ArrayList<String> aliases = mapAliases.get(tableAlias.toLowerCase());
		List<String> aliases = mapAliases.get(tableAlias.toLowerCase());
		if (aliases != null){
			aliases.remove(tableAlias);
			if (aliases.isEmpty())
@@ -171,7 +172,7 @@ public class SearchColumnList extends TextualSearchList<DBColumn> {
	 * 
	 * @see TextualSearchList#get(String)
	 */
	public ArrayList<DBColumn> search(final String columnName){
	public List<DBColumn> search(final String columnName){
		return get(columnName);
	}

@@ -187,7 +188,7 @@ public class SearchColumnList extends TextualSearchList<DBColumn> {
	 * 
	 * @see #search(String, String, String, String, byte)
	 */
	public final ArrayList<DBColumn> search(final String catalog, final String schema, final String table, final String column){
	public final List<DBColumn> search(final String catalog, final String schema, final String table, final String column){
		return search(catalog, schema, table, column, (byte)0);
	}

@@ -200,7 +201,7 @@ public class SearchColumnList extends TextualSearchList<DBColumn> {
	 * 
	 * @see #search(String, String, String, String, byte)
	 */
	public ArrayList<DBColumn> search(final ADQLColumn column){
	public List<DBColumn> search(final ADQLColumn column){
		return search(column.getCatalogName(), column.getSchemaName(), column.getTableName(), column.getColumnName(), column.getCaseSensitive());
	}

@@ -217,15 +218,15 @@ public class SearchColumnList extends TextualSearchList<DBColumn> {
	 * 
	 * @see IdentifierField
	 */
	public ArrayList<DBColumn> search(final String catalog, final String schema, final String table, final String column, final byte caseSensitivity){
	public List<DBColumn> search(final String catalog, final String schema, final String table, final String column, final byte caseSensitivity){

		ArrayList<DBColumn> tmpResult = get(column, IdentifierField.COLUMN.isCaseSensitive(caseSensitivity));
		List<DBColumn> tmpResult = get(column, IdentifierField.COLUMN.isCaseSensitive(caseSensitivity));

		/* WITH TABLE PREFIX */
		if (table != null){
			/* 1. Figure out the table alias */
			String tableName = null;
			ArrayList<String> aliasMatches = null;
			List<String> aliasMatches = null;

			// Case sensitive => tableName is set , aliasMatches = null
			if (IdentifierField.TABLE.isCaseSensitive(caseSensitivity)){
+47 −0
Original line number Diff line number Diff line
package adql.db;

/*
 * This file is part of ADQLLibrary.
 * 
 * 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
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ADQLLibrary is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with ADQLLibrary.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Copyright 2017 - Astronomisches Rechen Institut (ARI)
 */

import java.util.List;

import adql.query.from.ADQLTable;

/**
 * Simple interface about a class which allows to search for a specified
 * {@link ADQLTable}.
 * 
 * @author Gr&eacute;gory Mantelet (ARI)
 * @version 1.4 (09/2017)
 * @since 1.4
 * 
 * @see SearchTableList
 */
public interface SearchTableApi {

	/**
	 * Searches all {@link DBTable} elements corresponding to the given {@link ADQLTable} (case insensitive).
	 * 
	 * @param table	An {@link ADQLTable}.
	 * 
	 * @return		The list of all corresponding {@link DBTable} elements.
	 */
	public List<DBTable> search(final ADQLTable table);

}
 No newline at end of file
+11 −9
Original line number Diff line number Diff line
@@ -16,12 +16,13 @@ package adql.db;
 * You should have received a copy of the GNU Lesser General Public License
 * along with ADQLLibrary.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Copyright 2012,2015 - UDS/Centre de Données astronomiques de Strasbourg (CDS)
 * Copyright 2012-2017 - UDS/Centre de Données astronomiques de Strasbourg (CDS)
 *                       Astronomisches Rechen Institut (ARI)
 */

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import adql.query.IdentifierField;
import adql.query.from.ADQLTable;
@@ -36,9 +37,9 @@ import cds.utils.TextualSearchList;
 * </p>
 * 
 * @author Gr&eacute;gory Mantelet (CDS;ARI)
 * @version 1.4 (08/2015)
 * @version 1.4 (09/2017)
 */
public class SearchTableList extends TextualSearchList<DBTable> {
public class SearchTableList extends TextualSearchList<DBTable> implements SearchTableApi {
	private static final long serialVersionUID = 1L;

	/** Indicates whether multiple occurrences are allowed. */
@@ -105,7 +106,7 @@ public class SearchTableList extends TextualSearchList<DBTable> {
	 * 
	 * @see TextualSearchList#get(String)
	 */
	public ArrayList<DBTable> search(final String tableName){
	public List<DBTable> search(final String tableName){
		return get(tableName);
	}

@@ -120,7 +121,7 @@ public class SearchTableList extends TextualSearchList<DBTable> {
	 * 
	 * @see #search(String, String, String, byte)
	 */
	public final ArrayList<DBTable> search(final String catalog, final String schema, final String table){
	public final List<DBTable> search(final String catalog, final String schema, final String table){
		return search(catalog, schema, table, (byte)0);
	}

@@ -133,7 +134,8 @@ public class SearchTableList extends TextualSearchList<DBTable> {
	 * 
	 * @see #search(String, String, String, byte)
	 */
	public ArrayList<DBTable> search(final ADQLTable table){
	@Override
	public List<DBTable> search(final ADQLTable table){
		return search(table.getCatalogName(), table.getSchemaName(), table.getTableName(), table.getCaseSensitive());
	}

@@ -149,11 +151,11 @@ public class SearchTableList extends TextualSearchList<DBTable> {
	 * 
	 * @see IdentifierField
	 */
	public ArrayList<DBTable> search(final String catalog, final String schema, final String table, final byte caseSensitivity){
		ArrayList<DBTable> tmpResult = get(table, IdentifierField.TABLE.isCaseSensitive(caseSensitivity));
	public List<DBTable> search(final String catalog, final String schema, final String table, final byte caseSensitivity){
		List<DBTable> tmpResult = get(table, IdentifierField.TABLE.isCaseSensitive(caseSensitivity));

		if (schema != null){
			ArrayList<DBTable> result = new ArrayList<DBTable>();
			List<DBTable> result = new ArrayList<DBTable>();

			for(DBTable match : tmpResult){
				// No schema name (<=> no schema), then this table can not be a good match:
+7 −5
Original line number Diff line number Diff line
@@ -16,10 +16,12 @@ package adql.query;
 * You should have received a copy of the GNU Lesser General Public License
 * along with ADQLLibrary.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Copyright 2012 - UDS/Centre de Données astronomiques de Strasbourg (CDS)
 * Copyright 2012-2017 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
 *                       Astronomisches Rechen Institut (ARI)
 */

import java.util.ArrayList;
import java.util.List;

import adql.query.operand.ADQLOperand;

@@ -30,8 +32,8 @@ import adql.query.operand.ADQLOperand;
 * <ul><li>The user can specify the maximum number of rows the query must return.</li>
 * <li>He can also ask that all the returned rows are unique according to the first returned column.</li></ul></p>
 * 
 * @author Gr&eacute;gory Mantelet (CDS)
 * @version 06/2011
 * @author Gr&eacute;gory Mantelet (CDS;ARI)
 * @version 1.4 (09/2017)
 */
public class ClauseSelect extends ClauseADQL<SelectItem> {

@@ -218,7 +220,7 @@ public class ClauseSelect extends ClauseADQL<SelectItem> {
	 * @see #searchByAlias(String, boolean)
	 */
	public ADQLOperand searchByAlias(String alias){
		ArrayList<SelectItem> founds = searchByAlias(alias, true);
		List<SelectItem> founds = searchByAlias(alias, true);
		if (founds.isEmpty())
			return null;
		else
@@ -231,7 +233,7 @@ public class ClauseSelect extends ClauseADQL<SelectItem> {
	 * @param alias	Alias of the operand to retrieve.
	 * @return		All the corresponding select items.
	 */
	public ArrayList<SelectItem> searchByAlias(String alias, boolean caseSensitive){
	public List<SelectItem> searchByAlias(String alias, boolean caseSensitive){
		if (alias == null)
			return new ArrayList<SelectItem>(0);

Loading