Search in sources :

Example 1 with ProbeTable

use of org.datanucleus.store.rdbms.table.ProbeTable in project datanucleus-rdbms by datanucleus.

the class RDBMSStoreManager method initialiseSchema.

/**
 * Initialises the schema name for the datastore, and (optionally) the schema table.
 * @param conn A connection to the database
 * @param clr ClassLoader resolver
 */
private void initialiseSchema(Connection conn, ClassLoaderResolver clr) throws Exception {
    if (schemaName == null && catalogName == null) {
        // Initialise the Catalog/Schema names
        if (dba.supportsOption(DatastoreAdapter.CATALOGS_IN_TABLE_DEFINITIONS) || dba.supportsOption(DatastoreAdapter.SCHEMAS_IN_TABLE_DEFINITIONS)) {
            // User didn't provide catalog/schema and the datastore supports one or other so determine the defaults from the datastore
            try {
                try {
                    catalogName = dba.getCatalogName(conn);
                    schemaName = dba.getSchemaName(conn);
                } catch (UnsupportedOperationException e) {
                    if (!getBooleanProperty(PropertyNames.PROPERTY_DATASTORE_READONLY) && getSchemaHandler().isAutoCreateTables()) {
                        // If we aren't a read-only datastore, try to create a table and then
                        // retrieve its details, so as to obtain the catalog, schema.
                        ProbeTable pt = new ProbeTable(this);
                        pt.initialize(clr);
                        pt.create(conn);
                        try {
                            String[] schema_details = pt.findSchemaDetails(conn);
                            if (schema_details != null) {
                                catalogName = schema_details[0];
                                schemaName = schema_details[1];
                            }
                        } finally {
                            pt.drop(conn);
                        }
                    }
                }
            } catch (SQLException e) {
                String msg = Localiser.msg("050005", e.getMessage()) + ' ' + Localiser.msg("050006");
                NucleusLogger.DATASTORE_SCHEMA.warn(msg);
            // This is only logged as a warning since if the JDBC driver has some issue creating the ProbeTable we would be stuck
            // We need to allow SchemaTool "dbinfo" mode to work in all circumstances.
            }
        }
    }
    if (getBooleanProperty(PropertyNames.PROPERTY_DATASTORE_READONLY)) {
        // AutoStarter - Don't allow usage of SchemaTable mechanism if fixed/readonly schema
        String autoStartMechanismName = nucleusContext.getConfiguration().getStringProperty(PropertyNames.PROPERTY_AUTOSTART_MECHANISM);
        if ("SchemaTable".equals(autoStartMechanismName)) {
            // Schema fixed and user requires an auto-starter needing schema content so turn it off
            nucleusContext.getConfiguration().setProperty(PropertyNames.PROPERTY_AUTOSTART_MECHANISM, "None");
        }
    } else {
        // Provide any add-ons for the datastore that may be needed later
        dba.initialiseDatastore(conn);
    }
}
Also used : SQLException(java.sql.SQLException) ProbeTable(org.datanucleus.store.rdbms.table.ProbeTable) MacroString(org.datanucleus.util.MacroString)

Aggregations

SQLException (java.sql.SQLException)1 ProbeTable (org.datanucleus.store.rdbms.table.ProbeTable)1 MacroString (org.datanucleus.util.MacroString)1