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\""); }
public TableName(String identifierQuote, String tableNamePrefix, String cacheName){ if(identifierQuote == null){ throw new IllegalArgumentException("identifierQuote must not be null"); } if(tableNamePrefix == null){ throw new IllegalArgumentException("tableNamePrefix must not be null"); } if(cacheName == null){ throw new IllegalArgumentException("cacheName must not be null"); } this.identifierQuote = identifierQuote; normalize(tableNamePrefix, cacheName); }
@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 TableName getTableName() { if (tableName == null) { tableName = new TableName(identifierQuoteString, config.tableNamePrefix(), cacheName); } return tableName; }
public String getIndexName(boolean withIdentifier, String indexExt) { TableName table = getTableName(); String tableName = table.toString().replace(identifierQuoteString, ""); String indexName = tableName + "_" + indexExt; if (withIdentifier) { return identifierQuoteString + indexName + identifierQuoteString; } return indexName; }
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; }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullPrefix(){ new TableName(IDENTIFIER_QUOTE, null, ""); }
@Override public String getIndexName(boolean withIdentifier, String indexExt) { if (indexExt.equals(timestampIndexExt)) { // Timestamp for Oracle began with IDX, to keep backwards compatible we have to keep using that indexExt = INDEX_PREFIX; } int maxNameSize = MAX_INDEX_IDENTIFIER_SIZE - indexExt.length() - 1; String tableName = getTableName().toString().replace(identifierQuoteString, ""); String truncatedName = tableName.length() > maxNameSize ? tableName.substring(0, maxNameSize) : tableName; String indexName = indexExt + "_" + truncatedName; if (withIdentifier) { return identifierQuoteString + indexName + identifierQuoteString; } return indexName; }
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 testNullType(){ new TableName(null, "", ""); }
@Override protected boolean indexExists(String indexName, Connection conn) throws PersistenceException { ResultSet rs = null; try { DatabaseMetaData meta = conn.getMetaData(); rs = meta.getIndexInfo(null, null, getTableName().toString(), false, false); while (rs.next()) { String index = rs.getString("INDEX_NAME"); if (indexName.equalsIgnoreCase(index)) { return true; } } } catch (SQLException e) { throw new PersistenceException(e); } finally { JdbcUtil.safeClose(rs); } return false; }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullName(){ new TableName(IDENTIFIER_QUOTE, "", null); }
private void assertTableExistence(Connection connection, String identifierQuote, String secondTable, String firstTable, String tablePrefix) throws Exception { assert !TableManagerTest.existsTable(connection, new TableName(identifierQuote, tablePrefix, "")) : "this table should not exist!"; assert TableManagerTest.existsTable(connection, new TableName(identifierQuote, tablePrefix, firstTable)); assert TableManagerTest.existsTable(connection, new TableName(identifierQuote, tablePrefix, secondTable)); connection.close(); }
@Test(dependsOnMethods = "testCreateTable") public void testExists() throws PersistenceException { assert tableManager.tableExists(connection); assert !tableManager.tableExists(connection, new TableName("\"", "", "does_not_exist")); }