From 26cee66cc50be5ad231ae4d47ed96d44bb1b40f8 Mon Sep 17 00:00:00 2001
From: gmantele <gmantele@ari.uni-heidelberg.de>
Date: Thu, 21 Jul 2016 20:42:08 +0200
Subject: [PATCH] [TAP] Add support for schema_index in TAP_SCHEMA.schemas.
 Commit following 6fc7f8fd046a33ededfd161e7b13b8fe3d12a27c.

---
 src/tap/db/JDBCConnection.java    | 34 ++++++++++++++++++++-----------
 src/tap/metadata/TAPMetadata.java |  1 +
 src/tap/metadata/TAPSchema.java   | 27 ++++++++++++++++++++++++
 3 files changed, 50 insertions(+), 12 deletions(-)

diff --git a/src/tap/db/JDBCConnection.java b/src/tap/db/JDBCConnection.java
index be2fd3e..fb27df9 100644
--- a/src/tap/db/JDBCConnection.java
+++ b/src/tap/db/JDBCConnection.java
@@ -885,17 +885,25 @@ public class JDBCConnection implements DBConnection {
 			/* note: if the schema notion is not supported by this DBMS, the column "dbname" is ignored. */
 			boolean hasDBName = supportsSchema && isColumnExisting(tableDef.getDBSchemaName(), tableDef.getDBName(), DB_NAME_COLUMN, connection.getMetaData());
 
+			// Determine whether the schemaIndex column exists:
+			boolean hasSchemaIndex = isColumnExisting(tableDef.getDBSchemaName(), tableDef.getDBName(), "schema_index", connection.getMetaData());
+
 			// Build the SQL query:
 			StringBuffer sqlBuf = new StringBuffer("SELECT ");
 			sqlBuf.append(translator.getColumnName(tableDef.getColumn("schema_name")));
 			sqlBuf.append(", ").append(translator.getColumnName(tableDef.getColumn("description")));
 			sqlBuf.append(", ").append(translator.getColumnName(tableDef.getColumn("utype")));
+			if (hasSchemaIndex)
+				sqlBuf.append(", ").append(translator.getColumnName(tableDef.getColumn("schema_index")));
 			if (hasDBName){
 				sqlBuf.append(", ");
 				translator.appendIdentifier(sqlBuf, DB_NAME_COLUMN, IdentifierField.COLUMN);
 			}
 			sqlBuf.append(" FROM ").append(translator.getTableName(tableDef, supportsSchema));
-			sqlBuf.append(" ORDER BY 1");
+			if (hasSchemaIndex)
+				sqlBuf.append(" ORDER BY 1,4,2");
+			else
+				sqlBuf.append(" ORDER BY 1");
 
 			// Execute the query:
 			rs = stmt.executeQuery(sqlBuf.toString());
@@ -904,12 +912,14 @@ public class JDBCConnection implements DBConnection {
 			while(rs.next()){
 				String schemaName = rs.getString(1),
 						description = rs.getString(2), utype = rs.getString(3),
-						dbName = (hasDBName ? rs.getString(4) : null);
+						dbName = (hasDBName ? (hasSchemaIndex ? rs.getString(5) : rs.getString(4)) : null);
+				int schemaIndex = (hasSchemaIndex ? (rs.getObject(4) == null ? -1 : rs.getInt(4)) : -1);
 
 				// create the new schema:
 				TAPSchema newSchema = new TAPSchema(schemaName, nullifyIfNeeded(description), nullifyIfNeeded(utype));
 				if (dbName != null && dbName.trim().length() > 0)
 					newSchema.setDBName(dbName);
+				newSchema.setIndex(schemaIndex);
 
 				// add the new schema inside the given metadata:
 				metadata.addSchema(newSchema);
@@ -965,15 +975,15 @@ public class JDBCConnection implements DBConnection {
 			sqlBuf.append(", ").append(translator.getColumnName(tableDef.getColumn("table_type")));
 			sqlBuf.append(", ").append(translator.getColumnName(tableDef.getColumn("description")));
 			sqlBuf.append(", ").append(translator.getColumnName(tableDef.getColumn("utype")));
+			if (hasTableIndex)
+				sqlBuf.append(", ").append(translator.getColumnName(tableDef.getColumn("table_index")));
 			if (hasDBName){
 				sqlBuf.append(", ");
 				translator.appendIdentifier(sqlBuf, DB_NAME_COLUMN, IdentifierField.COLUMN);
 			}
-			if (hasTableIndex)
-				sqlBuf.append(", ").append(translator.getColumnName(tableDef.getColumn("table_index")));
 			sqlBuf.append(" FROM ").append(translator.getTableName(tableDef, supportsSchema));
 			if (hasTableIndex)
-				sqlBuf.append(" ORDER BY 1,7,2");
+				sqlBuf.append(" ORDER BY 1,6,2");
 			else
 				sqlBuf.append(" ORDER BY 1,2");
 			sqlBuf.append(';');
@@ -987,8 +997,8 @@ public class JDBCConnection implements DBConnection {
 				String schemaName = rs.getString(1),
 						tableName = rs.getString(2), typeStr = rs.getString(3),
 						description = rs.getString(4), utype = rs.getString(5),
-						dbName = (hasDBName ? rs.getString(6) : null);
-				int tableIndex = (hasTableIndex ? (rs.getObject(7) == null ? -1 : rs.getInt(7)) : -1);
+						dbName = (hasDBName ? (hasTableIndex ? rs.getString(7) : rs.getString(6)) : null);
+				int tableIndex = (hasTableIndex ? (rs.getObject(6) == null ? -1 : rs.getInt(6)) : -1);
 
 				// get the schema:
 				TAPSchema schema = metadata.getSchema(schemaName);
@@ -1086,15 +1096,15 @@ public class JDBCConnection implements DBConnection {
 			sqlBuf.append(", ").append(translator.getColumnName(tableDef.getColumn("principal")));
 			sqlBuf.append(", ").append(translator.getColumnName(tableDef.getColumn("indexed")));
 			sqlBuf.append(", ").append(translator.getColumnName(tableDef.getColumn("std")));
+			if (hasColumnIndex)
+				sqlBuf.append(", ").append(translator.getColumnName(tableDef.getColumn("column_index")));
 			if (hasDBName){
 				sqlBuf.append(", ");
 				translator.appendIdentifier(sqlBuf, DB_NAME_COLUMN, IdentifierField.COLUMN);
 			}
-			if (hasColumnIndex)
-				sqlBuf.append(", ").append(translator.getColumnName(tableDef.getColumn("column_index")));
 			sqlBuf.append(" FROM ").append(translator.getTableName(tableDef, supportsSchema));
 			if (hasColumnIndex)
-				sqlBuf.append(" ORDER BY 1,13,2");
+				sqlBuf.append(" ORDER BY 1,12,2");
 			else
 				sqlBuf.append(" ORDER BY 1,2");
 			sqlBuf.append(';');
@@ -1109,9 +1119,9 @@ public class JDBCConnection implements DBConnection {
 						description = rs.getString(3), unit = rs.getString(4),
 						ucd = rs.getString(5), utype = rs.getString(6),
 						datatype = rs.getString(7),
-						dbName = (hasDBName ? rs.getString(12) : null);
+						dbName = (hasDBName ? (hasColumnIndex ? rs.getString(13) : rs.getString(12)) : null);
 				int size = rs.getInt(8),
-						colIndex = (hasColumnIndex ? (rs.getObject(13) == null ? -1 : rs.getInt(13)) : -1);
+						colIndex = (hasColumnIndex ? (rs.getObject(12) == null ? -1 : rs.getInt(12)) : -1);
 				boolean principal = toBoolean(rs.getObject(9)),
 						indexed = toBoolean(rs.getObject(10)),
 						std = toBoolean(rs.getObject(11));
diff --git a/src/tap/metadata/TAPMetadata.java b/src/tap/metadata/TAPMetadata.java
index 361e0ec..2ea1aaf 100644
--- a/src/tap/metadata/TAPMetadata.java
+++ b/src/tap/metadata/TAPMetadata.java
@@ -828,6 +828,7 @@ public class TAPMetadata implements Iterable<TAPSchema>, VOSIResource, TAPResour
 
 			case SCHEMAS:
 				TAPTable schemas = new TAPTable(STDSchema.TAPSCHEMA + "." + STDTable.SCHEMAS, TableType.table, "List of schemas published in this TAP service.", null);
+				schemas.addColumn("schema_index", new DBType(DBDatatype.INTEGER), "this index is used to recommend schema ordering for clients", null, null, null, false, false, true);
 				schemas.addColumn("schema_name", new DBType(DBDatatype.VARCHAR), "schema name, possibly qualified", null, null, null, true, true, true);
 				schemas.addColumn("description", new DBType(DBDatatype.VARCHAR), "brief description of schema", null, null, null, true, false, true);
 				schemas.addColumn("utype", new DBType(DBDatatype.VARCHAR), "UTYPE if schema corresponds to a data model", null, null, null, false, false, true);
diff --git a/src/tap/metadata/TAPSchema.java b/src/tap/metadata/TAPSchema.java
index cac4049..e225141 100644
--- a/src/tap/metadata/TAPSchema.java
+++ b/src/tap/metadata/TAPSchema.java
@@ -75,6 +75,11 @@ public class TAPSchema implements Iterable<TAPTable> {
 	 * <i>Note: Standard TAP schema field ; MAY be NULL.</i> */
 	private String utype = null;
 
+	/** Ordering index of this schema inside its whole schema set.
+	 * <i>Note: SHOULD be a standard TAP schema field in TAP 1.1, as table_index and column_index are resp. in TAP_SCHEMA.tables and TAP_SCHEMA.columns.</i>
+	 * @since 2.1 */
+	private int index = -1;
+
 	/** Let add some information in addition of the ones of the TAP protocol.
 	 * <i>Note: This object can be anything: an {@link Integer}, a {@link String}, a {@link Map}, a {@link List}, ...
 	 * Its content is totally free and never used or checked.</i> */
@@ -319,6 +324,28 @@ public class TAPSchema implements Iterable<TAPTable> {
 		this.utype = utype;
 	}
 
+	/**
+	 * Get the ordering index of this schema inside its whole schema set.
+	 * 
+	 * @return	Its ordering index.
+	 * 
+	 * @since 2.1
+	 */
+	public final int getIndex(){
+		return index;
+	}
+
+	/**
+	 * Set the ordering index of this schema inside its whole schema set.
+	 * 
+	 * @param schemaIndex	Its new ordering index.
+	 * 
+	 * @since 2.1
+	 */
+	public final void setIndex(int schemaIndex){
+		this.index = schemaIndex;
+	}
+
 	/**
 	 * <p>Get the other (piece of) information associated with this schema.</p>
 	 * 
-- 
GitLab