use of org.datanucleus.exceptions.NucleusUserException in project datanucleus-rdbms by datanucleus.
the class SecondaryTable method getPrimaryKey.
/**
* Accessor for the primary key for this table. Overrides the method in TableImpl
* to add on any specification of PK name in the metadata.
* @return The primary key.
*/
public PrimaryKey getPrimaryKey() {
PrimaryKey pk = super.getPrimaryKey();
if (joinMetaData == null) {
// TODO Localise this message
throw new NucleusUserException("A relationship to a secondary table requires a <join> specification. " + "The secondary table is " + this.getDatastoreIdentifierFullyQualified() + " and the primary table is " + this.getPrimaryTable() + ". The fields mapped to this secondary table are: " + memberMappingsMap.keySet().toString());
}
PrimaryKeyMetaData pkmd = joinMetaData.getPrimaryKeyMetaData();
if (pkmd != null && pkmd.getName() != null) {
pk.setName(pkmd.getName());
}
return pk;
}
use of org.datanucleus.exceptions.NucleusUserException in project datanucleus-rdbms by datanucleus.
the class SequenceGenerator method createRepository.
/**
* Method to create the sequence.
* @return Whether it was created successfully.
*/
protected boolean createRepository() {
PreparedStatement ps = null;
RDBMSStoreManager srm = (RDBMSStoreManager) storeMgr;
DatastoreAdapter dba = srm.getDatastoreAdapter();
SQLController sqlControl = srm.getSQLController();
if (!srm.getSchemaHandler().isAutoCreateTables()) {
throw new NucleusUserException(Localiser.msg("040010", sequenceName));
}
Integer min = properties.containsKey(ValueGenerator.PROPERTY_KEY_MIN_VALUE) ? Integer.valueOf(properties.getProperty(ValueGenerator.PROPERTY_KEY_MIN_VALUE)) : null;
Integer max = properties.containsKey(ValueGenerator.PROPERTY_KEY_MAX_VALUE) ? Integer.valueOf(properties.getProperty(ValueGenerator.PROPERTY_KEY_MAX_VALUE)) : null;
Integer start = properties.containsKey(ValueGenerator.PROPERTY_KEY_INITIAL_VALUE) ? Integer.valueOf(properties.getProperty(ValueGenerator.PROPERTY_KEY_INITIAL_VALUE)) : null;
Integer incr = properties.containsKey(ValueGenerator.PROPERTY_KEY_CACHE_SIZE) ? Integer.valueOf(properties.getProperty(ValueGenerator.PROPERTY_KEY_CACHE_SIZE)) : null;
Integer cacheSize = properties.containsKey(ValueGenerator.PROPERTY_KEY_DATABASE_CACHE_SIZE) ? Integer.valueOf(properties.getProperty(ValueGenerator.PROPERTY_KEY_DATABASE_CACHE_SIZE)) : null;
String stmt = dba.getSequenceCreateStmt(sequenceName, min, max, start, incr, cacheSize);
try {
ps = sqlControl.getStatementForUpdate(connection, stmt, false);
sqlControl.executeStatementUpdate(null, connection, stmt, ps, true);
} catch (SQLException e) {
NucleusLogger.DATASTORE.error(e);
throw new ValueGenerationException(Localiser.msg("061000", e.getMessage()) + stmt);
} finally {
try {
if (ps != null) {
sqlControl.closeStatement(connection, ps);
}
} catch (SQLException e) {
// non-recoverable error
}
}
return true;
}
use of org.datanucleus.exceptions.NucleusUserException in project datanucleus-rdbms by datanucleus.
the class RDBMSStoreManager method getTableForStrategy.
private DatastoreClass getTableForStrategy(AbstractClassMetaData cmd, int fieldNumber, ClassLoaderResolver clr) {
DatastoreClass t = getDatastoreClass(cmd.getFullClassName(), clr);
if (t == null && cmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE) {
throw new NucleusUserException(Localiser.msg("032013", cmd.getFullClassName()));
}
if (t == null) {
throw new NucleusUserException("Attempt to find table for class=" + cmd.getFullClassName() + " but no table found! Check the metadata");
}
if (fieldNumber >= 0) {
AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
t = t.getBaseDatastoreClassWithMember(mmd);
} else {
// Go up to overall superclass to find id for that class.
boolean hasSuperclass = true;
while (hasSuperclass) {
DatastoreClass supert = t.getSuperDatastoreClass();
if (supert != null) {
t = supert;
} else {
hasSuperclass = false;
}
}
}
return t;
}
use of org.datanucleus.exceptions.NucleusUserException in project datanucleus-rdbms by datanucleus.
the class RDBMSStoreManager method initialiseIdentifierFactory.
/**
* Method to create the IdentifierFactory to be used by this store.
* Relies on the datastore adapter existing before creation
* @param nucleusContext context
*/
protected void initialiseIdentifierFactory(NucleusContext nucleusContext) {
if (dba == null) {
throw new NucleusException("DatastoreAdapter not yet created so cannot create IdentifierFactory!");
}
String idFactoryName = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_FACTORY);
try {
// Create the control properties for identifier generation
Map props = new HashMap();
if (catalogName != null) {
props.put("DefaultCatalog", catalogName);
}
if (schemaName != null) {
props.put("DefaultSchema", schemaName);
}
String val = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_CASE);
props.put("RequiredCase", val != null ? val : getDefaultIdentifierCase());
val = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_WORD_SEPARATOR);
if (val != null) {
props.put("WordSeparator", val);
}
val = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_TABLE_PREFIX);
if (val != null) {
props.put("TablePrefix", val);
}
val = getStringProperty(PropertyNames.PROPERTY_IDENTIFIER_TABLE_SUFFIX);
if (val != null) {
props.put("TableSuffix", val);
}
props.put("NamingFactory", getNamingFactory());
// Create the IdentifierFactory
ClassLoaderResolver clr = nucleusContext.getClassLoaderResolver(null);
if ("datanucleus2".equalsIgnoreCase(idFactoryName)) {
identifierFactory = new DN2IdentifierFactory(dba, clr, props);
} else if ("jpa".equalsIgnoreCase(idFactoryName)) {
identifierFactory = new JPAIdentifierFactory(dba, clr, props);
} else if ("datanucleus1".equalsIgnoreCase(idFactoryName)) {
identifierFactory = new DNIdentifierFactory(dba, clr, props);
} else if ("jpox".equalsIgnoreCase(idFactoryName)) {
identifierFactory = new JPOXIdentifierFactory(dba, clr, props);
} else {
// Fallback to the plugin mechanism
Class[] argTypes = new Class[] { DatastoreAdapter.class, ClassConstants.CLASS_LOADER_RESOLVER, Map.class };
Object[] args = new Object[] { dba, nucleusContext.getClassLoaderResolver(null), props };
identifierFactory = (IdentifierFactory) nucleusContext.getPluginManager().createExecutableExtension("org.datanucleus.store.rdbms.identifierfactory", "name", idFactoryName, "class-name", argTypes, args);
}
} catch (ClassNotFoundException cnfe) {
throw new NucleusUserException(Localiser.msg("039004", idFactoryName), cnfe).setFatal();
} catch (Exception e) {
NucleusLogger.PERSISTENCE.error("Exception creating IdentifierFactory", e);
throw new NucleusException(Localiser.msg("039005", idFactoryName), e).setFatal();
}
}
use of org.datanucleus.exceptions.NucleusUserException in project datanucleus-rdbms by datanucleus.
the class RDBMSStoreManager method executeScript.
/* (non-Javadoc)
* @see org.datanucleus.store.schema.SchemaScriptAwareStoreManager#executeScript(java.lang.String)
*/
public void executeScript(String script) {
script = StringUtils.replaceAll(script, "\n", " ");
script = StringUtils.replaceAll(script, "\t", " ");
ManagedConnection mc = connectionMgr.getConnection(-1);
try {
// Execute the script on this datastore
// Note that we simply split the script at line delimiter (";")
Connection conn = (Connection) mc.getConnection();
Statement stmt = conn.createStatement();
try {
StringTokenizer tokeniser = new StringTokenizer(script, ";");
while (tokeniser.hasMoreTokens()) {
String token = tokeniser.nextToken().trim();
if (!StringUtils.isWhitespace(token)) {
NucleusLogger.DATASTORE_NATIVE.debug("Executing script statement : " + token);
stmt.execute(token + ";");
}
}
} finally {
stmt.close();
}
} catch (SQLException e) {
NucleusLogger.DATASTORE_NATIVE.error("Exception executing user script", e);
throw new NucleusUserException("Exception executing user script. See nested exception for details", e);
} finally {
mc.release();
}
}
Aggregations