package com.tesora.dve.dbc; /* * #%L * Tesora Inc. * Database Virtualization Engine * %% * Copyright (C) 2011 - 2014 Tesora Inc. * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License, version 3, * as published by the Free Software Foundation. * * This program 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 Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * #L% */ import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; import com.tesora.dve.db.mysql.MysqlNativeTypeUtils; import com.tesora.dve.resultset.ColumnMetadata; import com.tesora.dve.resultset.ColumnSet; public class DBCResultSetMetaData implements ResultSetMetaData { ServerDBConnection conn; private ColumnSet columnSet; public DBCResultSetMetaData(ServerDBConnection conn, ColumnSet columnSet) { this.conn = conn; this.columnSet = columnSet; } protected ColumnMetadata getColumnMetadata(int index) { return columnSet.getColumn(index); } @Override public int getColumnCount() throws SQLException { return columnSet.size(); } @Override public boolean isAutoIncrement(int column) throws SQLException { return columnSet.getColumn(column).isAutoGenerated(); } @Override public boolean isCaseSensitive(int column) throws SQLException { return conn.findType(getColumnTypeName(column)).isCaseSensitive(); } @Override public boolean isCurrency(int column) throws SQLException { unsupportedMethod(); return false; } /* * the nullability status of the given column; one of columnNoNulls, * columnNullable or columnNullableUnknown currently the underlying * implementation of ColumnInfo doesn't support columnNullableUnknown */ @Override public int isNullable(int index) throws SQLException { if (columnSet.getColumn(index).isNullable()) return ResultSetMetaData.columnNullable; return ResultSetMetaData.columnNoNulls; } @Override public int getColumnDisplaySize(int column) throws SQLException { return columnSet.getColumn(column).getSize(); } @Override public String getColumnLabel(int column) throws SQLException { return columnSet.getColumn(column).getAliasName(); } @Override public String getColumnName(int column) throws SQLException { return columnSet.getColumn(column).getName(); } @Override public String getSchemaName(int column) throws SQLException { return ""; } @Override public int getPrecision(int column) throws SQLException { return columnSet.getColumn(column).getPrecision(); } @Override public int getScale(int column) throws SQLException { return columnSet.getColumn(column).getScale(); } @Override public String getTableName(int column) throws SQLException { if ( columnSet.getColumn(column).getTableName() == null ) return ""; return columnSet.getColumn(column).getTableName(); } @Override public String getCatalogName(int column) throws SQLException { if ( columnSet.getColumn(column).getDbName() == null ) return ""; return columnSet.getColumn(column).getDbName(); } @Override public int getColumnType(int column) throws SQLException { return columnSet.getColumn(column).getDataType(); } @Override public String getColumnTypeName(int column) throws SQLException { return columnSet.getColumn(column).getTypeName(); } @Override public boolean isReadOnly(int arg0) throws SQLException { return false; } @Override public boolean isSearchable(int index) throws SQLException { // TODO getSearchable returns an int which needs to be interpreted and // converted to bool // return conn.findType(getColumnTypeName(index)).getSearchable(); return true; } @Override public boolean isSigned(int index) throws SQLException { if ( !conn.findType(columnSet.getColumn(index).getTypeName()).isUnsignedAttribute() ) return false; // TODO this is Mysql specific.... return ! MysqlNativeTypeUtils.isUnsigned(columnSet.getColumn(index)); } @Override public boolean isWritable(int arg0) throws SQLException { return true; } @Override public boolean isDefinitelyWritable(int column) throws SQLException { return true; } /** * Currently unsupported methods */ void unsupportedMethod() throws SQLException { throw new SQLFeatureNotSupportedException("Unsupported ResultSet method call"); } @Override public <T> T unwrap(Class<T> iface) throws SQLException { unsupportedMethod(); return null; } @Override public boolean isWrapperFor(Class<?> iface) throws SQLException { unsupportedMethod(); return false; } @Override public String getColumnClassName(int column) throws SQLException { unsupportedMethod(); return null; } }