Search code examples
javajdbcfirebirdjaybird

Jaybird 3 and database-file properties (page size, SQL Dialect, ODS major/minor)


In version of jaybird 2 I can get the database page size via gds isc_info_page_size. But now I can't find out - is it possible in jaybird 3.0 to do that? I have host:port, database file name/alias and login/password - and I do not want to use MON$-tables.

I see documentation and only find the FBManager can return int getPageSize() but docs says: The page size to be used when creating a database, or -1 if the database default is used.

But I have an existing database, and want to now its page size. Can it be done without getting statistic and parsing header info?

PS. Also would be helpful if you point me how to know database SQL dialect, and ODS versions


Solution

  • The class FBManager is for creating (or dropping) a database, the page size there is for the database creation.

    The following methods are available in the FirebirdDatabaseMetaData interface:

    /**
     * Get the major version of the ODS (On-Disk Structure) of the database.
     * 
     * @return The major version number of the database itself
     * @exception SQLException if a database access error occurs
     */
    int getOdsMajorVersion() throws SQLException;
    
    /**
     * Get the minor version of the ODS (On-Disk Structure) of the database.
     * 
     * @return The minor version number of the database itself
     * @exception SQLException if a database access error occurs
     */
    int getOdsMinorVersion() throws SQLException;
    
    /**
     * Get the dialect of the database.
     *
     * @return The dialect of the database
     * @throws SQLException if a database access error occurs
     * @see #getConnectionDialect()
     */
    int getDatabaseDialect() throws SQLException;
    
    /**
     * Get the dialect of the connection.
     * <p>
     * The connection dialect may be different from the database dialect.
     * </p>
     *
     * @return The dialect of the connection
     * @throws SQLException if a database access error occurs
     * @see #getDatabaseDialect()
     */
    int getConnectionDialect() throws SQLException;
    

    To access these you need to unwrap the database metadata object:

    Connection connection = ...
    FirebirdDatabaseMetaData fbmd = connection.getMetaData().unwrap(FirebirdDatabaseMetaData.class);
    
    int odsMajorVersion = fbmd.getOdsMajorVersion();
    // etc
    

    As far as I can recall, the page size isn't exposed anywhere. Please file an improvement request on http://tracker.firebirdsql.org/browse/JDBC

    And if you really want to fiddle with the internals, you could always use the new internal API, but it should be considered unstable and might change at any moment (and it might become inaccessible or harder to access in Java 9 in future versions depending on how we will implement module support).

    Warning: I typed this without compiling or testing:

    Connection connection = ...
    FbDatabase db = connection.unwrap(FirebirdConnection.class).getFbDatabase();
    byte[] info = db.getDatabaseInfo(new byte[] { ISCConstants.isc_info_page_size }, 20);
    if (info[0] == ISCConstants.isc_info_page_size) {
        int valueLength = VaxEncoding.iscVaxInteger2(info, 1);
        int pageSize = VaxEncoding.iscVaxInteger(info, 3, valueLength);
        // ...
    }
    

    Or alternatively using its sibling

    /**
     * Request database info.
     *
     * @param requestItems
     *         Array of info items to request
     * @param bufferLength
     *         Response buffer length to use
     * @param infoProcessor
     *         Implementation of {@link InfoProcessor} to transform
     *         the info response
     * @return Transformed info response of type T
     * @throws SQLException
     *         For errors retrieving or transforming the response.
     */
    <T> T getDatabaseInfo(byte[] requestItems, int bufferLength, InfoProcessor<T> infoProcessor) throws SQLException;
    

    See an example at work in FBStatisticsManager

    Disclaimer: I maintain Jaybird.