Java Code Examples for java.sql.DatabaseMetaData#supportsSchemasInDataManipulation()

The following examples show how to use java.sql.DatabaseMetaData#supportsSchemasInDataManipulation() . 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: ScriptRunner.java    From sis with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new runner which will execute the statements using the given connection.
 *
 * <p>Some {@code maxRowsPerInsert} parameter values of interest:</p>
 * <ul>
 *   <li>A value of 0 means to create only the schemas without inserting any data in them.</li>
 *   <li>A value of 1 means to use one separated {@code INSERT INTO} statement for each row, which may be slow.</li>
 *   <li>A value of 100 is a value which have been found empirically as giving good results.</li>
 *   <li>A value of {@link Integer#MAX_VALUE} means to not perform any attempt to limit the number of rows in an
 *       {@code INSERT INTO} statement. Note that this causes {@link StackOverflowError} in some JDBC driver.</li>
 * </ul>
 *
 * @param  connection        the connection to the database.
 * @param  maxRowsPerInsert  maximum number of rows per {@code "INSERT INTO"} statement.
 * @throws SQLException if an error occurred while creating a SQL statement.
 */
public ScriptRunner(final Connection connection, final int maxRowsPerInsert) throws SQLException {
    ArgumentChecks.ensureNonNull("connection", connection);
    ArgumentChecks.ensurePositive("maxRowsPerInsert", maxRowsPerInsert);
    final DatabaseMetaData metadata = connection.getMetaData();
    this.maxRowsPerInsert   = maxRowsPerInsert;
    this.dialect            = Dialect.guess(metadata);
    this.identifierQuote    = metadata.getIdentifierQuoteString();
    this.isSchemaSupported  = metadata.supportsSchemasInTableDefinitions() &&
                              metadata.supportsSchemasInDataManipulation();
    this.isCatalogSupported = metadata.supportsCatalogsInTableDefinitions() &&
                              metadata.supportsCatalogsInDataManipulation();
    this.statement          = connection.createStatement();
    switch (dialect) {
        default: {
            isEnumTypeSupported      = false;
            isGrantOnSchemaSupported = false;
            isGrantOnTableSupported  = false;
            isCreateLanguageRequired = false;
            isCommentSupported       = false;
            break;
        }
        case POSTGRESQL: {
            final int version = metadata.getDatabaseMajorVersion();
            isEnumTypeSupported      = (version == 8) ? metadata.getDatabaseMinorVersion() >= 4 : version >= 8;
            isGrantOnSchemaSupported = true;
            isGrantOnTableSupported  = true;
            isCreateLanguageRequired = (version < 9);
            isCommentSupported       = true;
            break;
        }
        case HSQL: {
            isEnumTypeSupported      = false;
            isGrantOnSchemaSupported = false;
            isGrantOnTableSupported  = false;
            isCreateLanguageRequired = false;
            isCommentSupported       = false;
            /*
             * HSQLDB stores tables in memory by default. For storing the tables on files, we have to
             * use "CREATE CACHED TABLE" statement, which is HSQL-specific. For avoiding SQL dialect,
             * the following statement change the default setting on current connection.
             *
             * Reference: http://hsqldb.org/doc/guide/dbproperties-chapt.html#dpc_db_props_url
             */
            statement.execute("SET DATABASE DEFAULT TABLE TYPE CACHED");
            break;
        }
    }
    /*
     * Now build the list of statements to skip, depending of which features are supported by the database.
     * WARNING: do not use capturing group here, because some subclasses (e.g. EPSGInstaller) will use their
     * own capturing groups. A non-capturing group is declared by "(?:A|B)" instead than a plain "(A|B)".
     */
    if (!isEnumTypeSupported) {
        addStatementToSkip("CREATE\\s+(?:TYPE|CAST)\\s+.*");
    }
    if (!isGrantOnSchemaSupported || !isGrantOnTableSupported) {
        addStatementToSkip("GRANT\\s+\\w+\\s+ON\\s+");
        if (isGrantOnSchemaSupported) {
            regexOfStmtToSkip.append("TABLE");
        } else if (isGrantOnTableSupported) {
            regexOfStmtToSkip.append("SCHEMA");
        } else {
            regexOfStmtToSkip.append("(?:TABLE|SCHEMA)");
        }
        regexOfStmtToSkip.append("\\s+.*");
    }
    if (!isCommentSupported) {
        addStatementToSkip("COMMENT\\s+ON\\s+.*");
    }
}