List of usage examples for java.sql DatabaseMetaData getUDTs
ResultSet getUDTs(String catalog, String schemaPattern, String typeNamePattern, int[] types) throws SQLException;
From source file:CreateNewTable.java
private static Vector getDataTypes(Connection con) throws SQLException { String structName = null, distinctName = null, javaName = null; // create a vector of class DataType initialized with // the SQL code, the SQL type name, and two null entries // for the local type name and the creation parameter(s) Vector dataTypes = new Vector(); dataTypes.add(new DataType(java.sql.Types.BIT, "BIT")); dataTypes.add(new DataType(java.sql.Types.TINYINT, "TINYINT")); dataTypes.add(new DataType(java.sql.Types.SMALLINT, "SMALLINT")); dataTypes.add(new DataType(java.sql.Types.INTEGER, "INTEGER")); dataTypes.add(new DataType(java.sql.Types.BIGINT, "BIGINT")); dataTypes.add(new DataType(java.sql.Types.FLOAT, "FLOAT")); dataTypes.add(new DataType(java.sql.Types.REAL, "REAL")); dataTypes.add(new DataType(java.sql.Types.DOUBLE, "DOUBLE")); dataTypes.add(new DataType(java.sql.Types.NUMERIC, "NUMERIC")); dataTypes.add(new DataType(java.sql.Types.DECIMAL, "DECIMAL")); dataTypes.add(new DataType(java.sql.Types.CHAR, "CHAR")); dataTypes.add(new DataType(java.sql.Types.VARCHAR, "VARCHAR")); dataTypes.add(new DataType(java.sql.Types.LONGVARCHAR, "LONGVARCHAR")); dataTypes.add(new DataType(java.sql.Types.DATE, "DATE")); dataTypes.add(new DataType(java.sql.Types.TIME, "TIME")); dataTypes.add(new DataType(java.sql.Types.TIMESTAMP, "TIMESTAMP")); dataTypes.add(new DataType(java.sql.Types.BINARY, "BINARY")); dataTypes.add(new DataType(java.sql.Types.VARBINARY, "VARBINARY")); dataTypes.add(new DataType(java.sql.Types.LONGVARBINARY, "LONGVARBINARY")); dataTypes.add(new DataType(java.sql.Types.NULL, "NULL")); dataTypes.add(new DataType(java.sql.Types.OTHER, "OTHER")); dataTypes.add(new DataType(java.sql.Types.BLOB, "BLOB")); dataTypes.add(new DataType(java.sql.Types.CLOB, "CLOB")); DatabaseMetaData dbmd = con.getMetaData(); ResultSet rs = dbmd.getTypeInfo(); while (rs.next()) { int codeNumber = rs.getInt("DATA_TYPE"); String dbmsName = rs.getString("TYPE_NAME"); String createParams = rs.getString("CREATE_PARAMS"); if (codeNumber == Types.STRUCT && structName == null) structName = dbmsName;// w ww .jav a 2s .c om else if (codeNumber == Types.DISTINCT && distinctName == null) distinctName = dbmsName; else if (codeNumber == Types.JAVA_OBJECT && javaName == null) javaName = dbmsName; else { for (int i = 0; i < dataTypes.size(); i++) { // find entry that matches the SQL code, // and if local type and params are not already set, // set them DataType type = (DataType) dataTypes.get(i); if (type.getCode() == codeNumber) { type.setLocalTypeAndParams(dbmsName, createParams); } } } } int[] types = { Types.STRUCT, Types.DISTINCT, Types.JAVA_OBJECT }; rs = dbmd.getUDTs(null, "%", "%", types); while (rs.next()) { String typeName = null; DataType dataType = null; if (dbmd.isCatalogAtStart()) typeName = rs.getString(1) + dbmd.getCatalogSeparator() + rs.getString(2) + "." + rs.getString(3); else typeName = rs.getString(2) + "." + rs.getString(3) + dbmd.getCatalogSeparator() + rs.getString(1); switch (rs.getInt(5)) { case Types.STRUCT: dataType = new DataType(Types.STRUCT, typeName); dataType.setLocalTypeAndParams(structName, null); break; case Types.DISTINCT: dataType = new DataType(Types.DISTINCT, typeName); dataType.setLocalTypeAndParams(distinctName, null); break; case Types.JAVA_OBJECT: dataType = new DataType(Types.JAVA_OBJECT, typeName); dataType.setLocalTypeAndParams(javaName, null); break; } dataTypes.add(dataType); } return dataTypes; }
From source file:Main.java
private static Vector getDataTypes(Connection con) throws SQLException { String structName = null, distinctName = null, javaName = null; // create a vector of class DataType initialized with // the SQL code, the SQL type name, and two null entries // for the local type name and the creation parameter(s) Vector dataTypes = new Vector(); dataTypes.add(new DataType(java.sql.Types.BIT, "BIT")); dataTypes.add(new DataType(java.sql.Types.TINYINT, "TINYINT")); dataTypes.add(new DataType(java.sql.Types.SMALLINT, "SMALLINT")); dataTypes.add(new DataType(java.sql.Types.INTEGER, "INTEGER")); dataTypes.add(new DataType(java.sql.Types.BIGINT, "BIGINT")); dataTypes.add(new DataType(java.sql.Types.FLOAT, "FLOAT")); dataTypes.add(new DataType(java.sql.Types.REAL, "REAL")); dataTypes.add(new DataType(java.sql.Types.DOUBLE, "DOUBLE")); dataTypes.add(new DataType(java.sql.Types.NUMERIC, "NUMERIC")); dataTypes.add(new DataType(java.sql.Types.DECIMAL, "DECIMAL")); dataTypes.add(new DataType(java.sql.Types.CHAR, "CHAR")); dataTypes.add(new DataType(java.sql.Types.VARCHAR, "VARCHAR")); dataTypes.add(new DataType(java.sql.Types.LONGVARCHAR, "LONGVARCHAR")); dataTypes.add(new DataType(java.sql.Types.DATE, "DATE")); dataTypes.add(new DataType(java.sql.Types.TIME, "TIME")); dataTypes.add(new DataType(java.sql.Types.TIMESTAMP, "TIMESTAMP")); dataTypes.add(new DataType(java.sql.Types.BINARY, "BINARY")); dataTypes.add(new DataType(java.sql.Types.VARBINARY, "VARBINARY")); dataTypes.add(new DataType(java.sql.Types.LONGVARBINARY, "LONGVARBINARY")); dataTypes.add(new DataType(java.sql.Types.NULL, "NULL")); dataTypes.add(new DataType(java.sql.Types.OTHER, "OTHER")); dataTypes.add(new DataType(java.sql.Types.BLOB, "BLOB")); dataTypes.add(new DataType(java.sql.Types.CLOB, "CLOB")); DatabaseMetaData dbmd = con.getMetaData(); ResultSet rs = dbmd.getTypeInfo(); while (rs.next()) { int codeNumber = rs.getInt("DATA_TYPE"); String dbmsName = rs.getString("TYPE_NAME"); String createParams = rs.getString("CREATE_PARAMS"); if (codeNumber == Types.STRUCT && structName == null) structName = dbmsName;//from w ww. j a va 2 s .c o m else if (codeNumber == Types.DISTINCT && distinctName == null) distinctName = dbmsName; else if (codeNumber == Types.JAVA_OBJECT && javaName == null) javaName = dbmsName; else { for (int i = 0; i < dataTypes.size(); i++) { // find entry that matches the SQL code, // and if local type and params are not already set, // set them DataType type = (DataType) dataTypes.get(i); if (type.getCode() == codeNumber) { type.setLocalTypeAndParams(dbmsName, createParams); } } } } int[] types = { Types.STRUCT, Types.DISTINCT, Types.JAVA_OBJECT }; rs = dbmd.getUDTs(null, "%", "%", types); while (rs.next()) { String typeName = null; DataType dataType = null; if (dbmd.isCatalogAtStart()) typeName = rs.getString(1) + dbmd.getCatalogSeparator() + rs.getString(2) + "." + rs.getString(3); else typeName = rs.getString(2) + "." + rs.getString(3) + dbmd.getCatalogSeparator() + rs.getString(1); switch (rs.getInt(5)) { case Types.STRUCT: dataType = new DataType(Types.STRUCT, typeName); dataType.setLocalTypeAndParams(structName, null); break; case Types.DISTINCT: dataType = new DataType(Types.DISTINCT, typeName); dataType.setLocalTypeAndParams(distinctName, null); break; case Types.JAVA_OBJECT: dataType = new DataType(Types.JAVA_OBJECT, typeName); dataType.setLocalTypeAndParams(javaName, null); break; } dataTypes.add(dataType); } return dataTypes; }
From source file:CreateNewType.java
private static Vector getDataTypes(Connection con, String typeToCreate) throws SQLException { String structName = null, distinctName = null, javaName = null; // create a vector of class DataType initialized with // the SQL code, the SQL type name, and two null entries // for the local type name and the creation parameter(s) Vector dataTypes = new Vector(); dataTypes.add(new DataType(java.sql.Types.BIT, "BIT")); dataTypes.add(new DataType(java.sql.Types.TINYINT, "TINYINT")); dataTypes.add(new DataType(java.sql.Types.SMALLINT, "SMALLINT")); dataTypes.add(new DataType(java.sql.Types.INTEGER, "INTEGER")); dataTypes.add(new DataType(java.sql.Types.BIGINT, "BIGINT")); dataTypes.add(new DataType(java.sql.Types.FLOAT, "FLOAT")); dataTypes.add(new DataType(java.sql.Types.REAL, "REAL")); dataTypes.add(new DataType(java.sql.Types.DOUBLE, "DOUBLE")); dataTypes.add(new DataType(java.sql.Types.NUMERIC, "NUMERIC")); dataTypes.add(new DataType(java.sql.Types.DECIMAL, "DECIMAL")); dataTypes.add(new DataType(java.sql.Types.CHAR, "CHAR")); dataTypes.add(new DataType(java.sql.Types.VARCHAR, "VARCHAR")); dataTypes.add(new DataType(java.sql.Types.LONGVARCHAR, "LONGVARCHAR")); dataTypes.add(new DataType(java.sql.Types.DATE, "DATE")); dataTypes.add(new DataType(java.sql.Types.TIME, "TIME")); dataTypes.add(new DataType(java.sql.Types.TIMESTAMP, "TIMESTAMP")); dataTypes.add(new DataType(java.sql.Types.BINARY, "BINARY")); dataTypes.add(new DataType(java.sql.Types.VARBINARY, "VARBINARY")); dataTypes.add(new DataType(java.sql.Types.LONGVARBINARY, "LONGVARBINARY")); dataTypes.add(new DataType(java.sql.Types.NULL, "NULL")); dataTypes.add(new DataType(java.sql.Types.OTHER, "OTHER")); dataTypes.add(new DataType(java.sql.Types.BLOB, "BLOB")); dataTypes.add(new DataType(java.sql.Types.CLOB, "CLOB")); DatabaseMetaData dbmd = con.getMetaData(); ResultSet rs = dbmd.getTypeInfo(); while (rs.next()) { int codeNumber = rs.getInt("DATA_TYPE"); String dbmsName = rs.getString("TYPE_NAME"); String createParams = rs.getString("CREATE_PARAMS"); if (codeNumber == Types.STRUCT && structName == null) structName = dbmsName;/*from www .ja va 2 s. c om*/ else if (codeNumber == Types.DISTINCT && distinctName == null) distinctName = dbmsName; else if (codeNumber == Types.JAVA_OBJECT && javaName == null) javaName = dbmsName; else { for (int i = 0; i < dataTypes.size(); i++) { // find entry that matches the SQL code, // and if local type and params are not already set, // set them DataType type = (DataType) dataTypes.get(i); if (type.getCode() == codeNumber) { type.setLocalTypeAndParams(dbmsName, createParams); } } } } if (typeToCreate.equals("s")) { int[] types = { Types.STRUCT, Types.DISTINCT, Types.JAVA_OBJECT }; rs = dbmd.getUDTs(null, "%", "%", types); while (rs.next()) { String typeName = null; DataType dataType = null; if (dbmd.isCatalogAtStart()) typeName = rs.getString(1) + dbmd.getCatalogSeparator() + rs.getString(2) + "." + rs.getString(3); else typeName = rs.getString(2) + "." + rs.getString(3) + dbmd.getCatalogSeparator() + rs.getString(1); switch (rs.getInt(5)) { case Types.STRUCT: dataType = new DataType(Types.STRUCT, typeName); dataType.setLocalTypeAndParams(structName, null); break; case Types.DISTINCT: dataType = new DataType(Types.DISTINCT, typeName); dataType.setLocalTypeAndParams(distinctName, null); break; case Types.JAVA_OBJECT: dataType = new DataType(Types.JAVA_OBJECT, typeName); dataType.setLocalTypeAndParams(javaName, null); break; } dataTypes.add(dataType); } } return dataTypes; }
From source file:org.apache.bigtop.itest.hive.TestJdbc.java
/** * Test simple DatabaseMetaData calls. getColumns is tested elsewhere, as we need to call * that on a valid table. Same with getFunctions. * * @throws SQLException/*from w ww .j a v a 2 s . c o m*/ */ @Test public void databaseMetaDataCalls() throws SQLException { DatabaseMetaData md = conn.getMetaData(); boolean boolrc = md.allTablesAreSelectable(); LOG.debug("All tables are selectable? " + boolrc); String strrc = md.getCatalogSeparator(); LOG.debug("Catalog separator " + strrc); strrc = md.getCatalogTerm(); LOG.debug("Catalog term " + strrc); ResultSet rs = md.getCatalogs(); while (rs.next()) { strrc = rs.getString(1); LOG.debug("Found catalog " + strrc); } Connection c = md.getConnection(); int intrc = md.getDatabaseMajorVersion(); LOG.debug("DB major version is " + intrc); intrc = md.getDatabaseMinorVersion(); LOG.debug("DB minor version is " + intrc); strrc = md.getDatabaseProductName(); LOG.debug("DB product name is " + strrc); strrc = md.getDatabaseProductVersion(); LOG.debug("DB product version is " + strrc); intrc = md.getDefaultTransactionIsolation(); LOG.debug("Default transaction isolation is " + intrc); intrc = md.getDriverMajorVersion(); LOG.debug("Driver major version is " + intrc); intrc = md.getDriverMinorVersion(); LOG.debug("Driver minor version is " + intrc); strrc = md.getDriverName(); LOG.debug("Driver name is " + strrc); strrc = md.getDriverVersion(); LOG.debug("Driver version is " + strrc); strrc = md.getExtraNameCharacters(); LOG.debug("Extra name characters is " + strrc); strrc = md.getIdentifierQuoteString(); LOG.debug("Identifier quote string is " + strrc); // In Hive 1.2 this always returns an empty RS rs = md.getImportedKeys("a", "b", "d"); // In Hive 1.2 this always returns an empty RS rs = md.getIndexInfo("a", "b", "d", true, true); intrc = md.getJDBCMajorVersion(); LOG.debug("JDBC major version is " + intrc); intrc = md.getJDBCMinorVersion(); LOG.debug("JDBC minor version is " + intrc); intrc = md.getMaxColumnNameLength(); LOG.debug("Maximum column name length is " + intrc); strrc = md.getNumericFunctions(); LOG.debug("Numeric functions are " + strrc); // In Hive 1.2 this always returns an empty RS rs = md.getPrimaryKeys("a", "b", "d"); // In Hive 1.2 this always returns an empty RS rs = md.getProcedureColumns("a", "b", "d", "e"); strrc = md.getProcedureTerm(); LOG.debug("Procedures are called " + strrc); // In Hive 1.2 this always returns an empty RS rs = md.getProcedures("a", "b", "d"); strrc = md.getSchemaTerm(); LOG.debug("Schemas are called " + strrc); rs = md.getSchemas(); while (rs.next()) { strrc = rs.getString(1); LOG.debug("Found schema " + strrc); } strrc = md.getSearchStringEscape(); LOG.debug("Search string escape is " + strrc); strrc = md.getStringFunctions(); LOG.debug("String functions are " + strrc); strrc = md.getSystemFunctions(); LOG.debug("System functions are " + strrc); rs = md.getTableTypes(); while (rs.next()) { strrc = rs.getString(1); LOG.debug("Found table type " + strrc); } strrc = md.getTimeDateFunctions(); LOG.debug("Time/date functions are " + strrc); rs = md.getTypeInfo(); while (rs.next()) { strrc = rs.getString(1); LOG.debug("Found type " + strrc); } // In Hive 1.2 this always returns an empty RS rs = md.getUDTs("a", "b", "d", null); boolrc = md.supportsAlterTableWithAddColumn(); LOG.debug("Supports alter table with add column? " + boolrc); boolrc = md.supportsAlterTableWithDropColumn(); LOG.debug("Supports alter table with drop column? " + boolrc); boolrc = md.supportsBatchUpdates(); LOG.debug("Supports batch updates? " + boolrc); boolrc = md.supportsCatalogsInDataManipulation(); LOG.debug("Supports catalogs in data manipulation? " + boolrc); boolrc = md.supportsCatalogsInIndexDefinitions(); LOG.debug("Supports catalogs in index definition? " + boolrc); boolrc = md.supportsCatalogsInPrivilegeDefinitions(); LOG.debug("Supports catalogs in privilege definition? " + boolrc); boolrc = md.supportsCatalogsInProcedureCalls(); LOG.debug("Supports catalogs in procedure calls? " + boolrc); boolrc = md.supportsCatalogsInTableDefinitions(); LOG.debug("Supports catalogs in table definition? " + boolrc); boolrc = md.supportsColumnAliasing(); LOG.debug("Supports column aliasing? " + boolrc); boolrc = md.supportsFullOuterJoins(); LOG.debug("Supports full outer joins? " + boolrc); boolrc = md.supportsGroupBy(); LOG.debug("Supports group by? " + boolrc); boolrc = md.supportsLimitedOuterJoins(); LOG.debug("Supports limited outer joins? " + boolrc); boolrc = md.supportsMultipleResultSets(); LOG.debug("Supports limited outer joins? " + boolrc); boolrc = md.supportsNonNullableColumns(); LOG.debug("Supports non-nullable columns? " + boolrc); boolrc = md.supportsOuterJoins(); LOG.debug("Supports outer joins? " + boolrc); boolrc = md.supportsPositionedDelete(); LOG.debug("Supports positioned delete? " + boolrc); boolrc = md.supportsPositionedUpdate(); LOG.debug("Supports positioned update? " + boolrc); boolrc = md.supportsResultSetHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT); LOG.debug("Supports result set holdability? " + boolrc); boolrc = md.supportsResultSetType(ResultSet.HOLD_CURSORS_OVER_COMMIT); LOG.debug("Supports result set type? " + boolrc); boolrc = md.supportsSavepoints(); LOG.debug("Supports savepoints? " + boolrc); boolrc = md.supportsSchemasInDataManipulation(); LOG.debug("Supports schemas in data manipulation? " + boolrc); boolrc = md.supportsSchemasInIndexDefinitions(); LOG.debug("Supports schemas in index definitions? " + boolrc); boolrc = md.supportsSchemasInPrivilegeDefinitions(); LOG.debug("Supports schemas in privilege definitions? " + boolrc); boolrc = md.supportsSchemasInProcedureCalls(); LOG.debug("Supports schemas in procedure calls? " + boolrc); boolrc = md.supportsSchemasInTableDefinitions(); LOG.debug("Supports schemas in table definitions? " + boolrc); boolrc = md.supportsSelectForUpdate(); LOG.debug("Supports select for update? " + boolrc); boolrc = md.supportsStoredProcedures(); LOG.debug("Supports stored procedures? " + boolrc); boolrc = md.supportsTransactions(); LOG.debug("Supports transactions? " + boolrc); boolrc = md.supportsUnion(); LOG.debug("Supports union? " + boolrc); boolrc = md.supportsUnionAll(); LOG.debug("Supports union all? " + boolrc); }
From source file:org.apache.tajo.catalog.store.XMLCatalogSchemaManager.java
protected boolean checkExistence(Connection conn, DatabaseObjectType type, String... params) throws SQLException { boolean result = false; DatabaseMetaData metadata = null; PreparedStatement pstmt = null; BaseSchema baseSchema = catalogStore.getSchema(); if (params == null || params.length < 1) { throw new IllegalArgumentException("checkExistence function needs at least one argument."); }/*from ww w .j a v a2 s . c o m*/ switch (type) { case DATA: metadata = conn.getMetaData(); ResultSet data = metadata.getUDTs(null, baseSchema.getSchemaName() != null && !baseSchema.getSchemaName().isEmpty() ? baseSchema.getSchemaName().toUpperCase() : null, params[0].toUpperCase(), null); result = data.next(); CatalogUtil.closeQuietly(data); break; case FUNCTION: metadata = conn.getMetaData(); ResultSet functions = metadata.getFunctions(null, baseSchema.getSchemaName() != null && !baseSchema.getSchemaName().isEmpty() ? baseSchema.getSchemaName().toUpperCase() : null, params[0].toUpperCase()); result = functions.next(); CatalogUtil.closeQuietly(functions); break; case INDEX: if (params.length != 2) { throw new IllegalArgumentException( "Finding index object is needed two strings, table name and index name"); } pstmt = getExistQuery(conn, type); if (pstmt != null) { result = checkExistenceByQuery(pstmt, baseSchema, params); } else { metadata = conn.getMetaData(); ResultSet indexes = metadata.getIndexInfo(null, baseSchema.getSchemaName() != null && !baseSchema.getSchemaName().isEmpty() ? baseSchema.getSchemaName().toUpperCase() : null, params[0].toUpperCase(), false, true); while (indexes.next()) { if (indexes.getString("INDEX_NAME").equals(params[1].toUpperCase())) { result = true; break; } } CatalogUtil.closeQuietly(indexes); } break; case TABLE: pstmt = getExistQuery(conn, type); if (pstmt != null) { result = checkExistenceByQuery(pstmt, baseSchema, params); } else { metadata = conn.getMetaData(); ResultSet tables = metadata.getTables(null, baseSchema.getSchemaName() != null && !baseSchema.getSchemaName().isEmpty() ? baseSchema.getSchemaName().toUpperCase() : null, params[0].toUpperCase(), new String[] { "TABLE" }); result = tables.next(); CatalogUtil.closeQuietly(tables); } break; case DOMAIN: case OPERATOR: case RULE: case SEQUENCE: case TRIGGER: case VIEW: pstmt = getExistQuery(conn, type); if (pstmt == null) { throw new TajoInternalError( "Finding " + type + " type of database object is not supported on this database system."); } result = checkExistenceByQuery(pstmt, baseSchema, params); break; } return result; }