private boolean checkExistenceByQuery( PreparedStatement pstmt, BaseSchema baseSchema, String... params) throws SQLException { int paramIdx = 1; boolean result = false; if (baseSchema.getSchemaName() != null && !baseSchema.getSchemaName().isEmpty()) { pstmt.setString(paramIdx++, baseSchema.getSchemaName().toUpperCase()); } for (; paramIdx <= pstmt.getParameterMetaData().getParameterCount(); paramIdx++) { pstmt.setString(paramIdx, params[paramIdx - 1].toUpperCase()); } ResultSet rs = null; try { rs = pstmt.executeQuery(); while (rs.next()) { if (rs.getString(1).toUpperCase().equals(params[params.length - 1].toUpperCase())) { result = true; break; } } } finally { CatalogUtil.closeQuietly(rs); } return result; }
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."); } 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; }