liquibase.database.core.H2Database Java Examples

The following examples show how to use liquibase.database.core.H2Database. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CreateSpatialIndexChangeTest.java    From liquibase-spatial with Apache License 2.0 6 votes vote down vote up
/**
 * Generates the test data for
 * {@link #testValidate(String, String, String, String, String, String, String, String, Database, boolean)}
 * .
 *
 * @return the test data.
 */
@DataProvider
public Object[][] validateTestData() {
   final String cat = "mycatalog";
   final String sch = "myschema";
   final String ts = "mytablespace";
   final String tab = "test_table";
   final String col = "GEOM";
   final String ind = "SPATIAL_INDEX";
   final String geom = "POINT";
   final String srid = "4326";
   final H2Database h2 = new H2Database();
   return new Object[][] { new Object[] { cat, sch, ts, tab, col, ind, geom, srid, h2, true },
         new Object[] { cat, sch, ts, tab, col, ind, geom, null, h2, false },
         new Object[] { cat, sch, ts, tab, col, ind, geom, "bad", h2, false },
         new Object[] { cat, sch, ts, tab, col, ind, geom, "4326bad", h2, false },
         new Object[] { cat, sch, ts, tab, col, ind, geom, "bad4326", h2, false },
         new Object[] { cat, sch, ts, tab, col, ind, geom, "b4326ad", h2, false } };
}
 
Example #2
Source File: GeometryColumnsUtilsTest.java    From liquibase-spatial with Apache License 2.0 6 votes vote down vote up
@DataProvider
public Object[][] geometryColumnsExistsTestData() throws SQLException {
   // Create an H2 database instance without geometry_columns table.
   final Database noGeometryColumnsDatabase = new H2Database();
   noGeometryColumnsDatabase.setConnection(new JdbcConnection(DriverManager
         .getConnection("jdbc:h2:mem:target/noGeometryColumns")));

   final Database geometryColumnsDatabase = new H2Database();
   Connection connection = DriverManager
         .getConnection("jdbc:h2:mem:target/geometryColumns");
   DatabaseConnection conn = new JdbcConnection(connection);
   geometryColumnsDatabase.setConnection(conn);

   Statement statement = connection.createStatement();
   statement
         .execute("CREATE TABLE geometry_columns (f_table_schema VARCHAR(128), "
               + "f_table_name VARCHAR(128), f_geometry_column VARCHAR(128), coord_dimension INT, "
               + "srid INT, type VARCHAR(30))");
   statement.close();
   return new Object[][] {
         new Object[] { noGeometryColumnsDatabase, false },
         new Object[] { geometryColumnsDatabase, true } };
}
 
Example #3
Source File: AddGeometryColumnGeneratorGeoDBTest.java    From liquibase-spatial with Apache License 2.0 6 votes vote down vote up
@DataProvider
public Object[][] generateSqlTestData() {
   final Database database = new H2Database();

   final AddColumnStatement notGeometry = new AddColumnStatement(null, null, null, null,
         "BOOLEAN", null);

   final AddColumnStatement nullSchema = new AddColumnStatement(null, null, "TEST", "COLUMN",
         "Geometry(Point, 4327)", null);
   final Sql nullSchemaExpected = new UnparsedSql("CALL AddGeometryColumn('"
         + database.getDefaultSchemaName() + "', 'TEST', 'COLUMN', 4327, 'Point', 2)");

   final AddColumnStatement complete = new AddColumnStatement(null,
         database.getDefaultSchemaName(), "TEST", "COLUMN", "Geometry(Geometry,4326)", null);
   final Sql completeExpected = new UnparsedSql("CALL AddGeometryColumn('"
         + database.getDefaultSchemaName() + "', 'TEST', 'COLUMN', 4326, 'Geometry', 2)");

   return new Object[][] { new Object[] { notGeometry, database, new Sql[0] },
         new Object[] { nullSchema, database, new Sql[] { nullSchemaExpected } },
         new Object[] { complete, database, new Sql[] { completeExpected } }, };
}
 
Example #4
Source File: DropSpatialIndexChange.java    From liquibase-spatial with Apache License 2.0 6 votes vote down vote up
/**
 * Generates a {@link DropSpatialIndexStatement} followed by a {@link DropIndexStatement}, if
 * applicable. The first statement allows extra clean-up when dropping an index. The second
 * statement leverages the normal <code>DROP INDEX</code> logic.
 */
@Override
public SqlStatement[] generateStatements(final Database database) {
   final Collection<SqlStatement> statements = new ArrayList<SqlStatement>();
   // MySQL and PostgreSQL only need the normal DROP INDEX statement.
   if (!(database instanceof MySQLDatabase) && !(database instanceof PostgresDatabase)) {
      final DropSpatialIndexStatement dropSpatialIndex = new DropSpatialIndexStatement(
            this.indexName, this.catalogName, this.schemaName, this.tableName);
      statements.add(dropSpatialIndex);
   }

   // GeoDB doesn't use a tradition index structure so don't issue the normal DROP INDEX
   // statement.
   if (!(database instanceof DerbyDatabase) && !(database instanceof H2Database)) {
      final DropIndexStatement dropIndex = new DropIndexStatement(this.indexName,
            this.catalogName, this.schemaName, this.tableName, null);
      statements.add(dropIndex);
   }
   return statements.toArray(new SqlStatement[statements.size()]);
}
 
Example #5
Source File: GeometryColumnsUtils.java    From liquibase-spatial with Apache License 2.0 6 votes vote down vote up
/**
 * Indicates if the <code>GEOMETRY_COLUMNS</code> table or view exists.
 * 
 * @param database
 *           the database to check.
 * @return <code>true</code> if the table or view exists.
 */
public static boolean geometryColumnsExists(final Database database) {
   String geometryColumnsName = database.correctObjectName(
         "geometry_columns", Table.class);
   DatabaseObject example = null;
   if (database instanceof DerbyDatabase || database instanceof H2Database) {
      final Table tableExample = new Table();
      tableExample.setName(geometryColumnsName);
      tableExample.setSchema(database.getDefaultCatalogName(),
            database.getDefaultSchemaName());
      example = tableExample;
   } else if (database instanceof PostgresDatabase) {
      final View viewExample = new View();
      viewExample.setName(geometryColumnsName);
      viewExample.setSchema(database.getDefaultCatalogName(), "public");
      example = viewExample;
   }
   try {
      return example != null
            && SnapshotGeneratorFactory.getInstance().has(example, database);
   } catch (final LiquibaseException e) {
      throw new UnexpectedLiquibaseException(
            "Failed to determine if the geometry_columns table or view exists",
            e);
   }
}
 
Example #6
Source File: CustomLockDatabaseChangeLogGenerator.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private Sql generateSelectForUpdate(Database database, int id) {
    String catalog = database.getLiquibaseCatalogName();
    String schema = database.getLiquibaseSchemaName();
    String rawLockTableName = database.getDatabaseChangeLogLockTableName();

    String lockTableName = database.escapeTableName(catalog, schema, rawLockTableName);
    String idColumnName  = database.escapeColumnName(catalog, schema, rawLockTableName, "ID");

    String sqlBase = "SELECT " + idColumnName + " FROM " + lockTableName;
    String sqlWhere = " WHERE " + idColumnName + "=" + id;

    String sql;
    if (database instanceof MySQLDatabase || database instanceof PostgresDatabase || database instanceof H2Database ||
            database instanceof OracleDatabase) {
        sql = sqlBase + sqlWhere + " FOR UPDATE";
    } else if (database instanceof MSSQLDatabase) {
        sql = sqlBase + " WITH (UPDLOCK, ROWLOCK)" + sqlWhere;
    } else if (database instanceof DB2Database) {
        sql = sqlBase + sqlWhere +  " FOR READ ONLY WITH RS USE AND KEEP UPDATE LOCKS";
    } else {
        sql = sqlBase + sqlWhere;
        logger.warnf("No direct support for database %s . Database lock may not work correctly", database.getClass().getName());
    }

    logger.debugf("SQL command for pessimistic lock: %s", sql);
    
    return new UnparsedSql(sql);
}
 
Example #7
Source File: CreateSpatialIndexGeneratorPostgreSQLTest.java    From liquibase-spatial with Apache License 2.0 5 votes vote down vote up
/**
 * Tests
 * {@link CreateSpatialIndexGeneratorPostgreSQL#supports(CreateSpatialIndexStatement, Database)}
 */
@Test
public void testSupports() {
   final CreateSpatialIndexGeneratorPostgreSQL generator = new CreateSpatialIndexGeneratorPostgreSQL();
   final CreateSpatialIndexStatement statement = mock(CreateSpatialIndexStatement.class);
   assertTrue(generator.supports(statement, new PostgresDatabase()));
   assertFalse(generator.supports(statement, new H2Database()));
}
 
Example #8
Source File: DropGeometryColumnGeneratorGeoDBTest.java    From liquibase-spatial with Apache License 2.0 5 votes vote down vote up
/**
 * Provides test data to {@link #testSupports(DropColumnStatement, Database, boolean)}.
 * 
 * @return the test data.
 */
@DataProvider
public Object[][] supportsTestData() {
   final DropColumnStatement statement = new DropColumnStatement(null, null, null, null);
   return new Object[][] { new Object[] { statement, new DerbyDatabase(), true },
         new Object[] { statement, new H2Database(), true },
         new Object[] { statement, new OracleDatabase(), false }, };
}
 
Example #9
Source File: AddGeometryColumnGeneratorGeoDBTest.java    From liquibase-spatial with Apache License 2.0 5 votes vote down vote up
/**
 * Provides test data to {@link #testSupports(AddColumnStatement, Database, boolean)}.
 *
 * @return the test data.
 */
@DataProvider
public Object[][] supportsTestData() {
   final AddColumnStatement statement = new AddColumnStatement((String) null, null, null, null,
         null, null);
   return new Object[][] { new Object[] { statement, new DerbyDatabase(), true },
         new Object[] { statement, new H2Database(), true },
         new Object[] { statement, new OracleDatabase(), false }, };
}
 
Example #10
Source File: CreateSpatialIndexGeneratorOracleTest.java    From liquibase-spatial with Apache License 2.0 5 votes vote down vote up
/**
 * Tests
 * {@link CreateSpatialIndexGeneratorOracle#supports(CreateSpatialIndexStatement, Database)}
 */
@Test
public void testSupports() {
   final CreateSpatialIndexGeneratorOracle generator = new CreateSpatialIndexGeneratorOracle();
   final CreateSpatialIndexStatement statement = mock(CreateSpatialIndexStatement.class);
   assertTrue(generator.supports(statement, new OracleDatabase()));
   assertFalse(generator.supports(statement, new H2Database()));
}
 
Example #11
Source File: DropSpatialTableGeneratorOracleTest.java    From liquibase-spatial with Apache License 2.0 5 votes vote down vote up
/**
 * Tests {@link DropSpatialTableGeneratorOracle#supports(CreateSpatialIndexStatement, Database)}
 */
@Test
public void testSupports() {
   final DropSpatialTableGeneratorOracle generator = new DropSpatialTableGeneratorOracle();
   final DropTableStatement statement = mock(DropTableStatement.class);
   assertTrue(generator.supports(statement, new OracleDatabase()));
   assertFalse(generator.supports(statement, new H2Database()));
}
 
Example #12
Source File: CreateSpatialIndexGeneratorMySQLTest.java    From liquibase-spatial with Apache License 2.0 5 votes vote down vote up
/**
 * Tests {@link CreateSpatialIndexGeneratorMySQL#supports(CreateSpatialIndexStatement, Database)}
 */
@Test
public void testSupports() {
   final CreateSpatialIndexGeneratorMySQL generator = new CreateSpatialIndexGeneratorMySQL();
   final CreateSpatialIndexStatement statement = mock(CreateSpatialIndexStatement.class);
   assertTrue(generator.supports(statement, new MySQLDatabase()));
   assertFalse(generator.supports(statement, new H2Database()));
}
 
Example #13
Source File: SpatialSupportedPrecondition.java    From liquibase-spatial with Apache License 2.0 5 votes vote down vote up
@Override
public ValidationErrors validate(final Database database) {
   final ValidationErrors errors = new ValidationErrors();
   if (!(database instanceof DerbyDatabase || database instanceof H2Database
         || database instanceof MySQLDatabase || database instanceof OracleDatabase || database instanceof PostgresDatabase)) {
      errors.addError(database.getDatabaseProductName() + " is not supported by this extension");
   }
   return errors;
}
 
Example #14
Source File: SpatialSupportedPrecondition.java    From liquibase-spatial with Apache License 2.0 5 votes vote down vote up
@Override
public Warnings warn(final Database database) {
   final Warnings warnings = new Warnings();
   if (!(database instanceof DerbyDatabase || database instanceof H2Database
         || database instanceof MySQLDatabase || database instanceof OracleDatabase || database instanceof PostgresDatabase)) {
      warnings.addWarning(database.getDatabaseProductName()
            + " is not supported by this extension");
   }
   return warnings;
}
 
Example #15
Source File: SpatialIndexExistsPrecondition.java    From liquibase-spatial with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an example of the database object for which to check.
 *
 * @param database
 *           the database instance.
 * @param tableName
 *           the table name of the index.
 * @return the database object example.
 */
public DatabaseObject getExample(final Database database, final String tableName) {
   final Schema schema = new Schema(getCatalogName(), getSchemaName());
   final DatabaseObject example;

   // For GeoDB, the index is another table.
   if (database instanceof DerbyDatabase || database instanceof H2Database) {
      final String correctedTableName = database.correctObjectName(getHatboxTableName(),
            Table.class);
      example = new Table().setName(correctedTableName).setSchema(schema);
   } else {
      example = getIndexExample(database, schema, tableName);
   }
   return example;
}
 
Example #16
Source File: AddGeometryColumnGeneratorGeoDB.java    From liquibase-spatial with Apache License 2.0 5 votes vote down vote up
/**
 * @see liquibase.sqlgenerator.core.AbstractSqlGenerator#supports(liquibase.statement.SqlStatement,
 *      liquibase.database.Database)
 */
@Override
public boolean supports(final AddColumnStatement statement,
      final Database database) {
   return database instanceof DerbyDatabase
         || database instanceof H2Database;
}
 
Example #17
Source File: DropSpatialTableGeneratorGeoDB.java    From liquibase-spatial with Apache License 2.0 4 votes vote down vote up
/**
 * @see AbstractSqlGenerator#supports(SqlStatement, Database)
 */
@Override
public boolean supports(final DropTableStatement statement, final Database database) {
   return database instanceof DerbyDatabase || database instanceof H2Database;
}
 
Example #18
Source File: DropGeometryColumnGeneratorGeoDB.java    From liquibase-spatial with Apache License 2.0 4 votes vote down vote up
/**
 * @see liquibase.sqlgenerator.core.AbstractSqlGenerator#supports(liquibase.statement.SqlStatement,
 *      liquibase.database.Database)
 */
@Override
public boolean supports(final DropColumnStatement statement, final Database database) {
   return database instanceof DerbyDatabase || database instanceof H2Database;
}
 
Example #19
Source File: SpatialUpdateGeneratorGeoDB.java    From liquibase-spatial with Apache License 2.0 4 votes vote down vote up
@Override
public boolean supports(final UpdateStatement statement, final Database database) {
   return database instanceof DerbyDatabase || database instanceof H2Database;
}
 
Example #20
Source File: CreateSpatialIndexGeneratorGeoDB.java    From liquibase-spatial with Apache License 2.0 4 votes vote down vote up
@Override
public boolean supports(final CreateSpatialIndexStatement statement, final Database database) {
   return database instanceof DerbyDatabase || database instanceof H2Database;
}
 
Example #21
Source File: SpatialInsertGeneratorGeoDB.java    From liquibase-spatial with Apache License 2.0 4 votes vote down vote up
@Override
public boolean supports(final InsertStatement statement, final Database database) {
   return database instanceof DerbyDatabase || database instanceof H2Database;
}
 
Example #22
Source File: CreateSpatialTableGeneratorGeoDB.java    From liquibase-spatial with Apache License 2.0 4 votes vote down vote up
/**
 * @see liquibase.sqlgenerator.core.AbstractSqlGenerator#supports(liquibase.statement.SqlStatement,
 *      liquibase.database.Database)
 */
@Override
public boolean supports(final CreateTableStatement statement, final Database database) {
   return database instanceof DerbyDatabase || database instanceof H2Database;
}
 
Example #23
Source File: DropSpatialIndexGeneratorGeoDB.java    From liquibase-spatial with Apache License 2.0 4 votes vote down vote up
/**
 * @see liquibase.sqlgenerator.core.AbstractSqlGenerator#supports(liquibase.statement.SqlStatement,
 *      liquibase.database.Database)
 */
@Override
public boolean supports(final DropSpatialIndexStatement statement,
      final Database database) {
   return database instanceof DerbyDatabase || database instanceof H2Database;
}