Skip to content
Snippets Groups Projects
Commit fd1bc80f authored by gmantele's avatar gmantele
Browse files

[ADQL] Fix table copy bug: the catalog and schema names where not copied or...

[ADQL] Fix table copy bug: the catalog and schema names where not copied or not interpreted when they were prefixing the new table name. WARNING: The copy function of a DBTable has a slightly different behaviour than before.
parent 54674c10
No related branches found
No related tags found
No related merge requests found
......@@ -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:
......
......@@ -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&eacute;gory Mantelet (CDS)
* @version 07/2011
* @author Gr&eacute;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}.
*/
......
......@@ -28,7 +28,7 @@ import java.util.Iterator;
* Default implementation of {@link DBTable}.
*
* @author Gr&eacute;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;
}
}
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