use of org.datanucleus.store.rdbms.mapping.java.DatastoreIdMapping in project datanucleus-rdbms by datanucleus.
the class AbstractClassTable method addDatastoreId.
/**
* Utility to create the datastore identity column and mapping.
* This is used in 2 modes. The first is where we have a (primary) class table
* and we aren't creating the OID mapping as a FK to another class. The second
* is where we have a (secondary) class table and we are creating the OID mapping
* as a FK to the primary class. In the second case the refTable will be specified.
* @param columnMetaData The column MetaData for the datastore id
* @param refTable Table used as a reference (if any)
* @param cmd The MetaData for the class
*/
void addDatastoreId(ColumnMetaData columnMetaData, DatastoreClass refTable, AbstractClassMetaData cmd) {
// Create the mapping, setting its table
datastoreIdMapping = new DatastoreIdMapping();
datastoreIdMapping.setTable(this);
datastoreIdMapping.initialize(storeMgr, cmd.getFullClassName());
// Create a ColumnMetaData in the container if none is defined
ColumnMetaData colmd = null;
if (columnMetaData == null) {
colmd = new ColumnMetaData();
} else {
colmd = columnMetaData;
}
if (colmd.getName() == null) {
// Provide default column naming if none is defined
if (refTable != null) {
colmd.setName(storeMgr.getIdentifierFactory().newColumnIdentifier(refTable.getIdentifier().getName(), this.storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(DatastoreId.class), FieldRole.ROLE_OWNER, false).getName());
} else {
colmd.setName(storeMgr.getIdentifierFactory().newColumnIdentifier(identifier.getName(), this.storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(DatastoreId.class), FieldRole.ROLE_NONE, false).getName());
}
}
// Add the datastore identity column as the PK
Column idColumn = addColumn(DatastoreId.class.getName(), storeMgr.getIdentifierFactory().newIdentifier(IdentifierType.COLUMN, colmd.getName()), datastoreIdMapping, colmd);
idColumn.setPrimaryKey();
// Set the identity column type based on the IdentityStrategy
String strategyName = cmd.getIdentityMetaData().getValueStrategy().toString();
if (cmd.getIdentityMetaData().getValueStrategy().equals(ValueGenerationStrategy.CUSTOM)) {
strategyName = cmd.getIdentityMetaData().getValueStrategy().getCustomName();
}
if (strategyName != null && ValueGenerationStrategy.NATIVE.toString().equals(strategyName)) {
strategyName = storeMgr.getValueGenerationStrategyForNative(cmd, -1);
}
// Check the value generator type being stored
Class valueGeneratedType = Long.class;
if (strategyName != null && ValueGenerationStrategy.IDENTITY.toString().equals(strategyName)) {
valueGeneratedType = dba.getAutoIncrementJavaTypeForType(valueGeneratedType);
if (valueGeneratedType != Long.class) {
NucleusLogger.DATASTORE_SCHEMA.debug("Class " + cmd.getFullClassName() + " uses IDENTITY strategy and rather than using BIGINT " + " for the column type, using " + valueGeneratedType.getName() + " since the datastore requires that");
}
} else {
// Get the type that will be generated by the chosen ValueGeneration strategy
valueGeneratedType = storeMgr.getValueGenerationManager().getTypeForValueGeneratorForMember(strategyName, storeMgr.getValueGenerationManager().getMemberKey(cmd, -1));
}
storeMgr.getMappingManager().createDatastoreMapping(datastoreIdMapping, idColumn, valueGeneratedType.getName());
logMapping("DATASTORE_ID", datastoreIdMapping);
// Handle any auto-increment requirement
if (isObjectIdDatastoreAttributed()) {
if (this instanceof DatastoreClass && ((DatastoreClass) this).isBaseDatastoreClass()) {
// Only the base class can be autoincremented
idColumn.setIdentity(true);
}
}
// Check if auto-increment and that it is supported by this RDBMS
if (idColumn.isIdentity() && !dba.supportsOption(DatastoreAdapter.IDENTITY_COLUMNS)) {
throw new NucleusException(Localiser.msg("057020", cmd.getFullClassName(), "datastore-identity")).setFatal();
}
}
use of org.datanucleus.store.rdbms.mapping.java.DatastoreIdMapping in project datanucleus-rdbms by datanucleus.
the class QueryToSQLMapper method processCreatorExpression.
/* (non-Javadoc)
* @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processCreatorExpression(org.datanucleus.query.expression.CreatorExpression)
*/
@Override
protected Object processCreatorExpression(CreatorExpression expr) {
String className = expr.getId();
Class cls = null;
try {
cls = clr.classForName(className);
} catch (ClassNotResolvedException cnre) {
if (importsDefinition != null) {
cls = importsDefinition.resolveClassDeclaration(className, clr, null);
}
}
List<SQLExpression> ctrArgExprs = null;
List args = expr.getArguments();
if (args != null) {
Class[] ctrArgTypes = new Class[args.size()];
boolean[] ctrArgTypeCheck = new boolean[args.size()];
ctrArgExprs = new ArrayList<>(args.size());
Iterator iter = args.iterator();
int i = 0;
while (iter.hasNext()) {
Expression argExpr = (Expression) iter.next();
SQLExpression sqlExpr = (SQLExpression) evaluate(argExpr);
// TODO Cater for "SQL_function" mapping on to ANY type of constructor argument at that position since we don't know the precise type
if (argExpr instanceof InvokeExpression && ((InvokeExpression) argExpr).getOperation().equalsIgnoreCase("SQL_function")) {
ctrArgTypeCheck[i] = false;
} else {
ctrArgTypeCheck[i] = true;
}
ctrArgExprs.add(sqlExpr);
if (sqlExpr instanceof NewObjectExpression) {
ctrArgTypes[i] = ((NewObjectExpression) sqlExpr).getNewClass();
} else if (sqlExpr.getJavaTypeMapping() instanceof DatastoreIdMapping || sqlExpr.getJavaTypeMapping() instanceof PersistableMapping) {
ctrArgTypes[i] = clr.classForName(sqlExpr.getJavaTypeMapping().getType());
} else {
ctrArgTypes[i] = sqlExpr.getJavaTypeMapping().getJavaType();
}
i++;
}
// Check that this class has the required constructor
Constructor ctr = ClassUtils.getConstructorWithArguments(cls, ctrArgTypes, ctrArgTypeCheck);
if (ctr == null) {
throw new NucleusUserException(Localiser.msg("021033", className, StringUtils.objectArrayToString(ctrArgTypes)));
}
}
// TODO Retain the selected constructor (above)
NewObjectExpression newExpr = new NewObjectExpression(stmt, cls, ctrArgExprs);
stack.push(newExpr);
return newExpr;
}
Aggregations