/** * Assumes that the DB represented by JuEmUtil is none of the * DbTypes specified. * @param emUtil JuEmUtil * @param types Types we assume the DB is not * @deprecated Use dbIsNot(JuConnUtil, DbType...) instead */ @Deprecated public static void dbIsNot(JuEmUtil emUtil, DbType... types) { DbType actualType = emUtil.getDbType(); for (DbType type : types) { Assume.assumeFalse("Assumed DB was not " + type, type == actualType); } }
@Override public List<String> processMetaData(DatabaseMetaData dbmd) throws SQLException { // TODO: Consider Schema names for other DBs; refactor String schemaName = null; if (getDbType() == DbType.ORACLE) { schemaName = getMetaDataUserName(); } ResultSet rs = dbmd.getTables(schemaName, schemaName, null, new String[]{"TABLE"}); List<String> tableNames = new ArrayList<>(); while (rs.next()) { String tableName = rs.getString("TABLE_NAME"); // We check if the TableName already exists in the list as // Oracle seems to return the same table names multiple times on some // Schemas... if (!tableNames.contains(tableName)) { tableNames.add(tableName); } } rs.close(); Collections.sort(tableNames); return tableNames; } });
@Test public void getDriverName_returnsNotNull() { Assert.assertNotNull(this.emUtil.getDbType()); }
/** * Resets identity generation of all tables or sequences to allow for predictable * and repeatable entity generation. * <p> * The exact behaviour of identity generation may vary from DB to DB. * @param val Value for the next primary key */ public void resetIdentityGenerationOrSequences(int val) { this.getDbType().getDbSpecificHandler(this.asConnUtil()).resetIdentityGenerationOrSequences(val); }
/** * Gets a list of all sequence names of the DB. Sequences names are returned the * way the DB driver returns them, which is usually upper case. * @return List of Sequences names * @throws JuDbException If the list cannot be evaluated */ public List<String> getSequenceNames() throws JuDbException { // There doesn't seem to be a generic way to retrieve sequences using JDBC meta data, so // we have to use native queries. The queries are stored in the DB specific handlers. return this.getDbType().getDbSpecificHandler(this.asConnUtil()).getSequenceNames(); }
public Long getNextValueFromSequence(String sequenceName) throws JuDbException { return this.getDbType().getDbSpecificHandler(this.asConnUtil()).getNextValueFromSequence(sequenceName); }
/** * Gets a list of the primery key columns for the specified table * <p> * Column names are kept the way the driver returns them (may be upper, lower or mixed case) * @param tableName Table name * @return List of all columns that make up the primary key. If no primary key is applied, an empty list is returned. */ public List<String> getPrimaryKeyColumns(final String tableName) { final String actualTableName = this.getDbType().getDbSpecificHandler(this.asConnUtil()).convertTableNameCasing(tableName); List<String> columnNames = this.extractDatabaseMetaData(new DatabaseMetaDataCallback<List<String>>() { @Override public List<String> processMetaData(DatabaseMetaData dbmd) throws SQLException { ResultSet rs = dbmd.getPrimaryKeys(null, null, actualTableName); List<String> columnNames = new ArrayList<>(); while (rs.next()) { columnNames.add(rs.getString("COLUMN_NAME")); } rs.close(); return columnNames; } }); return columnNames; }
@Test public void canRead_allDataTypes_fromDefaultTestData() throws ParseException { new DbSchemaUtil(this.em).prepareDefaultSchemaAndTestData(); DataTypes dt1 = this.em.find(DataTypes.class, -1L); Assert.assertEquals(new Integer(1), dt1.getIntNumber()); Assert.assertEquals(new Long(2), dt1.getBigIntNumber()); Assert.assertEquals("one", dt1.getVarcharText()); Assert.assertEquals("oneClob", dt1.getClobText()); Assert.assertEquals(JuStringUtils.DATE_FORMAT_DAYS.parseObject("03.12.1980"), dt1.getDateField()); // We'll skip TIME checking for Oracle as it isn't supported by this DB if (this.emUtil.getDbType() != DbType.ORACLE) { String hours = JuStringUtils.DATE_FORMAT_SECONDS.format(dt1.getTimeField()); Assert.assertTrue(hours.endsWith("10:11:12")); // hours will be todays date, followed by the time } Assert.assertEquals(JuStringUtils.DATE_FORMAT_SECONDS.parseObject("03.12.1980 10:11:12"), dt1.getTimeStampField()); }
@Test public void canRead_allNullValues_fromDefaultTestData() throws ParseException { new DbSchemaUtil(this.em).prepareDefaultSchemaAndTestData(); DataTypes dt2 = this.em.find(DataTypes.class, -2L); Assert.assertNull(dt2.getIntNumber()); Assert.assertNull(dt2.getBigIntNumber()); Assert.assertNull(dt2.getVarcharText()); Assert.assertNull(dt2.getClobText()); Assert.assertNull(dt2.getDateField()); Assert.assertNull(dt2.getTimeField()); // MySQL sets a TimeStamp field to the current time unless it has been created // explicitly with a NULL attribute which Liquibase doesn't do. Assume.assumeTrue(this.emUtil.getDbType() != DbType.MYSQL); Assert.assertNull(dt2.getTimeStampField()); } }