/** * Method to create the repository for ids to be stored. * * @return Whether it was created successfully. */ protected boolean createRepository() { RDBMSStoreManager srm = (RDBMSStoreManager) storeMgr; if (!srm.isAutoCreateTables()) { throw new NucleusUserException(LOCALISER.msg("040011", sequenceTable)); } try { if (sequenceTable == null) { initialiseSequenceTable(); } sequenceTable.exists((Connection) connection.getConnection(), true); repositoryExists = true; return true; } catch (SQLException sqle) { throw new ValueGenerationException( "Exception thrown calling table.exists() for " + sequenceTable, sqle); } }
/** Method to initialise the sequence table used for storing the sequence values. */ protected void initialiseSequenceTable() { // Set catalog/schema name (using properties, and if not specified using the values for the // table) String catalogName = properties.getProperty("sequence-catalog-name"); if (catalogName == null) { catalogName = properties.getProperty("catalog-name"); } String schemaName = properties.getProperty("sequence-schema-name"); if (schemaName == null) { schemaName = properties.getProperty("schema-name"); } String tableName = (properties.getProperty("sequence-table-name") == null ? DEFAULT_TABLE_NAME : properties.getProperty("sequence-table-name")); RDBMSStoreManager storeMgr = (RDBMSStoreManager) this.storeMgr; DatastoreAdapter dba = storeMgr.getDatastoreAdapter(); DatastoreIdentifier identifier = storeMgr.getIdentifierFactory().newTableIdentifier(tableName); if (dba.supportsOption(DatastoreAdapter.CATALOGS_IN_TABLE_DEFINITIONS) && catalogName != null) { identifier.setCatalogName(catalogName); } if (dba.supportsOption(DatastoreAdapter.SCHEMAS_IN_TABLE_DEFINITIONS) && schemaName != null) { identifier.setSchemaName(schemaName); } DatastoreClass table = storeMgr.getDatastoreClass(identifier); if (table != null) { sequenceTable = (SequenceTable) table; } else { String sequenceNameColumnName = DEFAULT_SEQUENCE_COLUMN_NAME; String nextValColumnName = DEFAULT_NEXTVALUE_COLUMN_NAME; if (properties.getProperty("sequence-name-column-name") != null) { sequenceNameColumnName = properties.getProperty("sequence-name-column-name"); } if (properties.getProperty("sequence-nextval-column-name") != null) { nextValColumnName = properties.getProperty("sequence-nextval-column-name"); } sequenceTable = new SequenceTable(identifier, storeMgr, sequenceNameColumnName, nextValColumnName); sequenceTable.initialize(storeMgr.getNucleusContext().getClassLoaderResolver(null)); } }
/** * Method to reserve a block of "size" identities. * * @param size Block size * @return The reserved block */ public ValueGenerationBlock reserveBlock(long size) { if (size < 1) { return null; } // search for an ID in the database List oid = new ArrayList(); try { if (sequenceTable == null) { initialiseSequenceTable(); } DatastoreIdentifier sourceTableIdentifier = null; if (properties.getProperty("table-name") != null) { sourceTableIdentifier = ((RDBMSStoreManager) storeMgr) .getIdentifierFactory() .newTableIdentifier(properties.getProperty("table-name")); // TODO Apply passed in catalog/schema to this identifier rather than the default for the // factory } Long nextId = sequenceTable.getNextVal( sequenceName, connection, (int) size, sourceTableIdentifier, properties.getProperty("column-name"), initialValue); for (int i = 0; i < size; i++) { oid.add(nextId); nextId = Long.valueOf(nextId.longValue() + 1); } if (NucleusLogger.VALUEGENERATION.isDebugEnabled()) { NucleusLogger.VALUEGENERATION.debug(LOCALISER.msg("040004", "" + size)); } return new ValueGenerationBlock(oid); } catch (SQLException e) { throw new ValueGenerationException(LOCALISER_RDBMS.msg("061001", e.getMessage())); } }
/** * Method to return if the repository already exists. * * @return Whether the repository exists */ protected boolean repositoryExists() { if (repositoryExists) { return repositoryExists; } else if (storeMgr.getBooleanProperty( RDBMSPropertyNames.PROPERTY_RDBMS_OMIT_DATABASEMETADATA_GETCOLUMNS)) { // Assumed to exist if ignoring DMD.getColumns() repositoryExists = true; return repositoryExists; } try { if (sequenceTable == null) { initialiseSequenceTable(); } sequenceTable.exists((Connection) connection.getConnection(), true); repositoryExists = true; return true; } catch (SQLException sqle) { throw new ValueGenerationException( "Exception thrown calling table.exists() for " + sequenceTable, sqle); } }