protected boolean indexExists(String indexName, Connection conn) throws PersistenceException { ResultSet rs = null; try { TableName table = getTableName(); DatabaseMetaData meta = conn.getMetaData(); rs = meta.getIndexInfo(null, table.getSchema(), table.getName(), false, false); while (rs.next()) { if (indexName.equalsIgnoreCase(rs.getString("INDEX_NAME"))) { return true; } } } catch (SQLException e) { throw new PersistenceException(e); } finally { JdbcUtil.safeClose(rs); } return false; }
@Override public boolean tableExists(Connection connection, TableName tableName) throws PersistenceException { Objects.requireNonNull(tableName, "table name is mandatory"); ResultSet rs = null; try { DatabaseMetaData metaData = connection.getMetaData(); String schemaPattern = tableName.getSchema() == null ? metaData.getUserName() : tableName.getSchema(); rs = metaData.getTables(null, schemaPattern, tableName.getName(), new String[]{"TABLE"}); return rs.next(); } catch (SQLException e) { if (LOG.isTraceEnabled()) LOG.tracef(e, "SQLException occurs while checking the table %s", tableName); return false; } finally { JdbcUtil.safeClose(rs); } }
public boolean tableExists(Connection connection, TableName tableName) throws PersistenceException { Objects.requireNonNull(tableName, "table name is mandatory"); ResultSet rs = null; try { // we need to make sure, that (even if the user has extended permissions) only the tables in current schema are checked // explicit set of the schema to the current user one to make sure only tables of the current users are requested DatabaseMetaData metaData = connection.getMetaData(); String schemaPattern = tableName.getSchema(); rs = metaData.getTables(null, schemaPattern, tableName.getName(), new String[]{"TABLE"}); return rs.next(); } catch (SQLException e) { if (log.isTraceEnabled()) log.tracef(e, "SQLException occurs while checking the table %s", tableName); return false; } finally { JdbcUtil.safeClose(rs); } }
@Test(expectedExceptions = IllegalArgumentException.class) public void testEmptySchema(){ TableName tableName = new TableName(IDENTIFIER_QUOTE, ".ISPN", "FOOBAR"); assertEquals(tableName.getSchema(), ""); }
public void testSchema(){ TableName tableName = new TableName(IDENTIFIER_QUOTE, "TEST.ISPN", "FOOBAR"); assertEquals(tableName.getSchema(), "TEST"); assertEquals(tableName.getName(), "ISPN_FOOBAR"); assertEquals(tableName.toString(), "\"TEST\".\"ISPN_FOOBAR\""); tableName = new TableName(IDENTIFIER_QUOTE, "ISPN", "FOOBAR"); assertEquals(tableName.getSchema(), null); assertEquals(tableName.getName(), "ISPN_FOOBAR"); assertEquals(tableName.toString(), "\"ISPN_FOOBAR\""); }