diff --git a/src/adql/db/DBChecker.java b/src/adql/db/DBChecker.java index cdc6dc59997d22f9e0873fed85edd45f37e1fc1c..7dcbc62b104f70b3aa75c4d8a0eeb7d38b57004d 100644 --- a/src/adql/db/DBChecker.java +++ b/src/adql/db/DBChecker.java @@ -207,7 +207,7 @@ public class DBChecker implements QueryChecker { }else{ dbTable = resolveTable(table); if (table.hasAlias()) - dbTable = dbTable.copy(dbTable.getDBName(), table.getAlias()); + dbTable = dbTable.copy(null, table.getAlias()); } // link with the matched DBTable: diff --git a/src/adql/db/DBTable.java b/src/adql/db/DBTable.java index 8e5d52b8dddb5979f874c97c3bdd2caa00e215dd..f72389f4c7b39cc88bd88155ae2673f91a3dd995 100644 --- a/src/adql/db/DBTable.java +++ b/src/adql/db/DBTable.java @@ -16,7 +16,8 @@ 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 - UDS/Centre de DonnĂ©es astronomiques de Strasbourg (CDS) + * Copyright 2012,2014 - UDS/Centre de DonnĂ©es astronomiques de Strasbourg (CDS), + * Astronomisches Rechen Institut (ARI) */ /** @@ -27,8 +28,8 @@ package adql.db; * and corresponds to a real table in the "database" with its DB name ({@link #getDBName()}). * </p> * - * @author Grégory Mantelet (CDS) - * @version 07/2011 + * @author Grégory Mantelet (CDS;ARI) + * @version 1.3 (09/2014) */ public interface DBTable extends Iterable<DBColumn> { @@ -86,10 +87,25 @@ public interface DBTable extends Iterable<DBColumn> { public DBColumn getColumn(String colName, boolean adqlName); /** - * Makes a copy of this instance of {@link DBTable}, with the possibility to change the DB and ADQL names. + * <p>Makes a copy of this instance of {@link DBTable}, with the possibility to change the DB and ADQL names.</p> + * + * <p><b>IMPORTANT:</b> + * <b>The given DB and ADQL name may be NULL.</b> If NULL, the copy will contain exactly the same full name (DB and/or ADQL).<br/> + * <b>And they may be qualified</b> (that's to say: prefixed by the schema name or by the catalog and schema name). It means that it is possible to + * change the catalog, schema and table name in the copy.<br/> + * For instance: + * </p> + * <ul> + * <li><i>.copy(null, "foo") =></i> a copy with the same full DB name, but with no ADQL catalog and schema name and with an ADQL table name equals to "foo"</li> + * <li><i>.copy("schema.table", ) =></i> a copy with the same full ADQL name, but with no DB catalog name, with a DB schema name equals to "schema" and with a DB table name equals to "table"</li> + * </ul> * * @param dbName Its new DB name. + * It may be qualified. + * It may also be NULL ; if so, the full DB name won't be different in the copy. * @param adqlName Its new ADQL name. + * It may be qualified. + * It may also be NULL ; if so, the full DB name won't be different in the copy. * * @return A modified copy of this {@link DBTable}. */ diff --git a/src/adql/db/DefaultDBTable.java b/src/adql/db/DefaultDBTable.java index 35fcfc5caa73500e151735faac3bab3d5279314e..7093a1b9c59216385b0dd45ecdf957f5459192af 100644 --- a/src/adql/db/DefaultDBTable.java +++ b/src/adql/db/DefaultDBTable.java @@ -28,7 +28,7 @@ import java.util.Iterator; * Default implementation of {@link DBTable}. * * @author Grégory Mantelet (CDS;ARI) - * @version 1.2 (11/2013) + * @version 1.3 (09/2014) */ public class DefaultDBTable implements DBTable { @@ -247,8 +247,52 @@ public class DefaultDBTable implements DBTable { return splitRes; } + /** + * <p>Join the last 3 items of the given string array with a dot ('.'). + * These three parts should be: [0]=catalog name, [1]=schema name, [2]=table name.</p> + * + * <p> + * If the array contains less than 3 items, all the given items will be though joined. + * However, if it contains more than 3 items, only the three last items will be. + * </p> + * + * <p>A null item will be written as an empty string (string of length 0 ; "").</p> + * + * <p> + * In the case the first and the third items are not null, but the second is null, the final string will contain in the middle two dots. + * Example: if the array is {"cat", NULL, "table"}, then the joined string will be: "cat..table". + * </p> + * + * @param nameParts String items to join. + * + * @return A string joining the 3 last string items of the given array, + * or an empty string if the given array is NULL. + * + * @since 1.3 + */ + public static final String joinTableName(final String[] nameParts){ + if (nameParts == null) + return ""; + + StringBuffer str = new StringBuffer(); + boolean empty = true; + for(int i = (nameParts.length <= 3) ? 0 : (nameParts.length - 3); i < nameParts.length; i++){ + if (!empty) + str.append('.'); + + String part = (nameParts[i] == null) ? null : nameParts[i].trim(); + if (part != null && part.length() > 0){ + str.append(part); + empty = false; + } + } + return str.toString(); + } + @Override - public DBTable copy(final String dbName, final String adqlName){ + public DBTable copy(String dbName, String adqlName){ + dbName = (dbName == null) ? joinTableName(new String[]{dbCatalogName,dbSchemaName,this.dbName}) : dbName; + adqlName = (adqlName == null) ? joinTableName(new String[]{adqlCatalogName,adqlSchemaName,this.adqlName}) : adqlName; DefaultDBTable copy = new DefaultDBTable(dbName, adqlName); for(DBColumn col : this){ if (col instanceof DBCommonColumn) @@ -258,5 +302,4 @@ public class DefaultDBTable implements DBTable { } return copy; } - }